ieee-utils (empty) → 0.1
raw patch · 5 files changed
+121/−0 lines, 5 filesdep +basesetup-changed
Dependencies added: base
Files
- LICENSE +26/−0
- Setup.lhs +3/−0
- cbits/fe_round.c +5/−0
- ieee-utils.cabal +24/−0
- src/Numeric/IEEE/RoundMode.hs +63/−0
+ LICENSE view
@@ -0,0 +1,26 @@+Copyright (c) Matt Morrow.+All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions+are met:+1. Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.+2. Redistributions in binary form must reproduce the above copyright+ notice, this list of conditions and the following disclaimer in the+ documentation and/or other materials provided with the distribution.+3. The names of the author may not be used to endorse or promote+ products derived from this software without specific prior written+ permission.++THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND+ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE+IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE+ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE+FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL+DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS+OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)+HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT+LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY+OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF+SUCH DAMAGE.
+ Setup.lhs view
@@ -0,0 +1,3 @@+> import Distribution.Simple +> main :: IO () +> main = defaultMain
+ cbits/fe_round.c view
@@ -0,0 +1,5 @@+#include <fenv.h>+int fe_tonearest = FE_TONEAREST;+int fe_upward = FE_UPWARD;+int fe_downward = FE_DOWNWARD;+int fe_towardzero = FE_TOWARDZERO;
+ ieee-utils.cabal view
@@ -0,0 +1,24 @@+name: ieee-utils +version: 0.1 +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: unstable +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/fe_round.c + extra-libraries: + exposed-modules: Numeric.IEEE.RoundMode
+ src/Numeric/IEEE/RoundMode.hs view
@@ -0,0 +1,63 @@+{-# LANGUAGE ForeignFunctionInterface #-}++{- |+ Module : Numeric.IEEE.RoundMode+ Copyright : (c) Matt Morrow 2008+ License : BSD3+ Maintainer : Matt Morrow <mjm2002@gmail.com>+ Stability : unstable+ Portability : portable (FFI)+-}++module Numeric.IEEE.RoundMode (+ RoundMode(..)+ , getRound+ , setRound+) where++import Foreign.C(CInt)+import Foreign.Ptr(Ptr)+import Foreign.Storable(peek)+import System.IO.Unsafe(unsafePerformIO)++data RoundMode+ = ToNearest+ | Upward+ | Downward+ | TowardZero+ deriving (Eq,Ord,Show,Read)++foreign import ccall unsafe "fenv.h fegetround" c_fegetround :: IO CInt+foreign import ccall unsafe "fenv.h fesetround" c_fesetround :: CInt -> IO CInt++-- | Get the fpu's current rounding mode.+getRound :: IO RoundMode+getRound = (toEnum . fromIntegral) `fmap` c_fegetround++-- | Set the fpu's rounding mode. Returns @True@ if successful.+setRound :: RoundMode -> IO Bool+setRound m = (==0) `fmap` c_fesetround (fromIntegral . fromEnum $ m)++instance Enum RoundMode where+ toEnum n+ | n==feToNearest = ToNearest+ | n==feUpward = Upward+ | n==feDownward = Downward+ | n==feTowardZero = TowardZero+ fromEnum ToNearest = feToNearest+ fromEnum Upward = feUpward+ fromEnum Downward = feDownward+ fromEnum TowardZero = feTowardZero++feToNearest :: Int+feUpward :: Int+feDownward :: Int+feTowardZero :: Int+feToNearest = fromIntegral (unsafePerformIO (peek fe_tonearest))+feUpward = fromIntegral (unsafePerformIO (peek fe_upward))+feDownward = fromIntegral (unsafePerformIO (peek fe_downward))+feTowardZero = fromIntegral (unsafePerformIO (peek fe_towardzero))+foreign import ccall unsafe "&" fe_tonearest :: Ptr CInt+foreign import ccall unsafe "&" fe_upward :: Ptr CInt+foreign import ccall unsafe "&" fe_downward :: Ptr CInt+foreign import ccall unsafe "&" fe_towardzero :: Ptr CInt