ieee-utils 0.2 → 0.3
raw patch · 4 files changed
+105/−30 lines, 4 files
Files
- cbits/fe_round.c +0/−5
- cbits/ieee_utils.c +15/−0
- ieee-utils.cabal +25/−25
- src/Numeric/IEEE/FloatExceptions.hs +65/−0
− cbits/fe_round.c
@@ -1,5 +0,0 @@-#include <fenv.h>-int fe_tonearest = FE_TONEAREST;-int fe_upward = FE_UPWARD;-int fe_downward = FE_DOWNWARD;-int fe_towardzero = FE_TOWARDZERO;
+ cbits/ieee_utils.c view
@@ -0,0 +1,15 @@+#include <fenv.h>++/* Rounding Mode */+int fe_tonearest = FE_TONEAREST;+int fe_upward = FE_UPWARD;+int fe_downward = FE_DOWNWARD;+int fe_towardzero = FE_TOWARDZERO;++/* Floating Point Exceptions */+int fe_divbyzero = FE_DIVBYZERO;+int fe_inexact = FE_INEXACT;+int fe_invalid = FE_INVALID;+int fe_overflow = FE_OVERFLOW;+int fe_underflow = FE_UNDERFLOW;+int fe_all_except = FE_ALL_EXCEPT;
ieee-utils.cabal view
@@ -1,25 +1,25 @@-name: ieee-utils -version: 0.2 -cabal-version: >= 1.3 -build-type: Simple -license: BSD3 -license-file: LICENSE -category: Numerical -author: Matt Morrow -copyright: (c) Matt Morrow -maintainer: Matt Morrow <mjm2002@gmail.com> -stability: provisional -synopsis: ieee-utils -description: IEEE 754 (Standard for Binary Floating-Point Arithmetic) Utilities. - Haddock docs are at <http://code.haskell.org/~morrow/code/haskell/ieee-utils/haddock/>. - -library - build-tools: - build-depends: base - ghc-options: -O2 -Wall - extensions: ForeignFunctionInterface - hs-source-dirs: src - includes: fenv.h - c-sources: cbits/fe_round.c - extra-libraries: - exposed-modules: Numeric.IEEE.RoundMode +name: ieee-utils+version: 0.3+cabal-version: >= 1.3+build-type: Simple+license: BSD3+license-file: LICENSE+category: Numerical+author: Matt Morrow, Sterling Clover+copyright: (c) Matt Morrow+maintainer: Matt Morrow <mjm2002@gmail.com>+stability: provisional+synopsis: ieee-utils+description: IEEE 754 (Standard for Binary Floating-Point Arithmetic) Utilities.++library+ build-tools:+ build-depends: base+ ghc-options: -O2 -Wall+ extensions: ForeignFunctionInterface+ hs-source-dirs: src+ includes: fenv.h+ c-sources: cbits/ieee_utils.c+ extra-libraries:+ exposed-modules: Numeric.IEEE.RoundMode,+ Numeric.IEEE.FloatExceptions
+ src/Numeric/IEEE/FloatExceptions.hs view
@@ -0,0 +1,65 @@+{-# LANGUAGE ForeignFunctionInterface #-}++{- |+ Module : Numeric.IEEE.FloatExceptions+ Copyright : (c) Sterling Clover 2008 <s.clover@gmail.com>+ License : BSD3+ Maintainer : Matt Morrow <mjm2002@gmail.com>+ Stability : provisional+ Portability : portable (FFI)+-}++module Numeric.IEEE.FloatExceptions (+ getFloatExcepts+ , clearFloatExcepts+ , ArithException (..)+) where++import Foreign.C(CInt)+import Foreign.Ptr(Ptr)+import Foreign.Storable(peek)+import System.IO.Unsafe(unsafePerformIO)+import Control.Exception(ArithException(..))+import Data.Bits((.&.),(.|.))++foreign import ccall unsafe "fenv.h fetestexcept" c_fetestexcept :: CInt -> IO CInt+foreign import ccall unsafe "fenv.h feclearexcept" c_feclearexcept :: CInt -> IO CInt++-- | Clears the specified exceptions from the fpu's exception register.+clearFloatExcepts :: [ArithException] -> IO Bool+clearFloatExcepts xs = (==0) `fmap` (c_feclearexcept . foldr (.|.) 0 . map (fromIntegral . fromEnum) $ xs)++-- | Returns all exceptions set in the fpu's exception register.+getFloatExcepts :: IO [ArithException]+getFloatExcepts = do+ exs <- fromIntegral `fmap` c_fetestexcept (fromIntegral feAllExcept)+ return . map toEnum . filter (/=0) . map (.&. exs) $+ [feDivByZero, feInexact, feInvalid, feOverflow, feUnderflow]++instance Enum ArithException where+ toEnum n+ | n==feDivByZero = DivideByZero+ | n==feInexact = LossOfPrecision+ | n==feInvalid = Denormal+ | n==feOverflow = Overflow+ | n==feUnderflow = Underflow+ fromEnum DivideByZero = feDivByZero+ fromEnum LossOfPrecision = feInexact+ fromEnum Denormal = feInvalid+ fromEnum Overflow = feOverflow+ fromEnum Underflow = feUnderflow++feDivByZero, feInexact, feInvalid, feOverflow, feUnderflow, feAllExcept :: Int+feDivByZero = fromIntegral . unsafePerformIO . peek $ fe_divbyzero+feInexact = fromIntegral . unsafePerformIO . peek $ fe_inexact+feInvalid = fromIntegral . unsafePerformIO . peek $ fe_invalid+feOverflow = fromIntegral . unsafePerformIO . peek $ fe_overflow+feUnderflow = fromIntegral . unsafePerformIO . peek $ fe_underflow+feAllExcept = fromIntegral . unsafePerformIO . peek $ fe_all_except++foreign import ccall unsafe "&" fe_divbyzero :: Ptr CInt+foreign import ccall unsafe "&" fe_inexact :: Ptr CInt+foreign import ccall unsafe "&" fe_invalid :: Ptr CInt+foreign import ccall unsafe "&" fe_overflow :: Ptr CInt+foreign import ccall unsafe "&" fe_underflow :: Ptr CInt+foreign import ccall unsafe "&" fe_all_except :: Ptr CInt