{-# LINE 1 "src/LibBF/Opts.hsc" #-}
{-# Language PatternSynonyms, CApiFFI, ViewPatterns #-}
module LibBF.Opts
(
BFOpts(..)
, allowSubnormal
, float16
, float32
, float64
, float128
, float256
, precBits
, precBitsMin
, precBitsMax
, infPrec
, expBits
, expBitsMin
, expBitsMax
, rnd
, RoundMode(..)
, pattern NearEven
, pattern ToZero
, pattern ToNegInf
, pattern ToPosInf
, pattern NearAway
, pattern Away
, pattern Faithful
, ShowFmt(..)
, showRnd
, showFixed
, showFrac
, showFree
, showFreeMin
, addPrefix
, forceExp
, radixMax
, Status(..)
, pattern Ok
, pattern InvalidOp
, pattern DivideByZero
, pattern Overflow
, pattern Underflow
, pattern Inexact
, pattern MemError
, LimbT
, SLimbT
, FlagsT
)
where
import Data.Word
import Data.Int
import Foreign.C.Types
import Data.Bits
import Data.List
type LimbT = Word64
{-# LINE 75 "src/LibBF/Opts.hsc" #-}
type SLimbT = Int64
{-# LINE 78 "src/LibBF/Opts.hsc" #-}
type FlagsT = Word32
{-# LINE 81 "src/LibBF/Opts.hsc" #-}
data BFOpts = BFOpts !LimbT !FlagsT
instance Semigroup BFOpts where
BFOpts l f <> BFOpts l1 f1 = BFOpts (max l l1) (f .|. f1)
infPrec :: BFOpts
infPrec = BFOpts 4611686018427387903 0
{-# LINE 94 "src/LibBF/Opts.hsc" #-}
precBits :: Int -> BFOpts
precBits n = BFOpts (fromIntegral n) 0
rnd :: RoundMode -> BFOpts
rnd (RoundMode r) = BFOpts 0 r
foreign import capi "libbf.h value BF_PREC_MIN"
precBitsMin :: Int
foreign import capi "libbf.h value BF_PREC_MAX"
precBitsMax :: Int
allowSubnormal :: BFOpts
allowSubnormal = BFOpts 0 8
{-# LINE 117 "src/LibBF/Opts.hsc" #-}
foreign import capi "libbf.h bf_set_exp_bits"
bf_set_exp_bits :: CInt -> FlagsT
expBits :: Int -> BFOpts
expBits n = BFOpts 0 (bf_set_exp_bits (fromIntegral n))
foreign import capi "libbf.h value BF_EXP_BITS_MIN"
expBitsMin :: Int
foreign import capi "libbf.h value BF_EXP_BITS_MAX"
expBitsMax :: Int
float16:: RoundMode -> BFOpts
float16 r = rnd r <> precBits 11 <> expBits 5
float32 :: RoundMode -> BFOpts
float32 r = rnd r <> precBits 24 <> expBits 8
float64 :: RoundMode -> BFOpts
float64 r = rnd r <> precBits 53 <> expBits 11
float128 :: RoundMode -> BFOpts
float128 r = rnd r <> precBits 113 <> expBits 15
float256 :: RoundMode -> BFOpts
float256 r = rnd r <> precBits 237 <> expBits 19
data ShowFmt = ShowFmt !LimbT !FlagsT
showRnd :: RoundMode -> ShowFmt
showRnd (RoundMode r) = ShowFmt 1 r
instance Semigroup ShowFmt where
ShowFmt a x <> ShowFmt b y = ShowFmt (max a b) (x .|. y)
showFixed :: Word64 -> ShowFmt
showFixed n = ShowFmt n 0
{-# LINE 175 "src/LibBF/Opts.hsc" #-}
showFrac :: Word64 -> ShowFmt
showFrac n = ShowFmt n 65536
{-# LINE 179 "src/LibBF/Opts.hsc" #-}
showFree :: Maybe Word64 -> ShowFmt
showFree mb = ShowFmt prec 131072
{-# LINE 189 "src/LibBF/Opts.hsc" #-}
where prec = case mb of
Nothing -> 4611686018427387903
{-# LINE 191 "src/LibBF/Opts.hsc" #-}
Just n -> n
showFreeMin :: Maybe Word64 -> ShowFmt
showFreeMin mb = ShowFmt prec 196608
{-# LINE 198 "src/LibBF/Opts.hsc" #-}
where prec = case mb of
Nothing -> 4611686018427387903
{-# LINE 200 "src/LibBF/Opts.hsc" #-}
Just n -> n
addPrefix :: ShowFmt
addPrefix = ShowFmt 0 2097152
{-# LINE 208 "src/LibBF/Opts.hsc" #-}
forceExp :: ShowFmt
forceExp = ShowFmt 0 1048576
{-# LINE 212 "src/LibBF/Opts.hsc" #-}
foreign import capi "libbf.h value BF_RADIX_MAX"
radixMax :: Int
newtype RoundMode = RoundMode FlagsT
deriving Show
pattern NearEven :: RoundMode
pattern NearEven = RoundMode 0
{-# LINE 230 "src/LibBF/Opts.hsc" #-}
pattern ToZero :: RoundMode
pattern ToZero = RoundMode 1
{-# LINE 234 "src/LibBF/Opts.hsc" #-}
pattern ToNegInf :: RoundMode
pattern ToNegInf = RoundMode 2
{-# LINE 238 "src/LibBF/Opts.hsc" #-}
pattern ToPosInf :: RoundMode
pattern ToPosInf = RoundMode 3
{-# LINE 242 "src/LibBF/Opts.hsc" #-}
pattern NearAway :: RoundMode
pattern NearAway = RoundMode 4
{-# LINE 246 "src/LibBF/Opts.hsc" #-}
pattern Away :: RoundMode
pattern Away = RoundMode 5
{-# LINE 250 "src/LibBF/Opts.hsc" #-}
pattern Faithful :: RoundMode
pattern Faithful = RoundMode 6
{-# LINE 255 "src/LibBF/Opts.hsc" #-}
newtype Status = Status CInt deriving (Eq,Ord)
checkStatus :: CInt -> Status -> Bool
checkStatus n (Status x) = (x .&. n) > 0
pattern Ok :: Status
pattern Ok = Status 0
pattern InvalidOp :: Status
pattern InvalidOp <- (checkStatus 1 -> True)
{-# LINE 272 "src/LibBF/Opts.hsc" #-}
where InvalidOp = Status 1
{-# LINE 273 "src/LibBF/Opts.hsc" #-}
pattern DivideByZero :: Status
pattern DivideByZero <- (checkStatus 2 -> True)
{-# LINE 277 "src/LibBF/Opts.hsc" #-}
where DivideByZero = Status 2
{-# LINE 278 "src/LibBF/Opts.hsc" #-}
pattern Overflow :: Status
pattern Overflow <- (checkStatus 4 -> True)
{-# LINE 282 "src/LibBF/Opts.hsc" #-}
where Overflow = Status 4
{-# LINE 283 "src/LibBF/Opts.hsc" #-}
pattern Underflow :: Status
pattern Underflow <- (checkStatus 8 -> True)
{-# LINE 287 "src/LibBF/Opts.hsc" #-}
where Underflow = Status 8
{-# LINE 288 "src/LibBF/Opts.hsc" #-}
pattern Inexact :: Status
pattern Inexact <- (checkStatus 16 -> True)
{-# LINE 292 "src/LibBF/Opts.hsc" #-}
where Inexact = Status 16
{-# LINE 293 "src/LibBF/Opts.hsc" #-}
pattern MemError :: Status
pattern MemError <- (checkStatus 32 -> True)
{-# LINE 297 "src/LibBF/Opts.hsc" #-}
where MemError = Status 32
{-# LINE 298 "src/LibBF/Opts.hsc" #-}
instance Show Status where
show x@(Status i) = case x of
Ok -> "Ok"
_ -> case checkInv of
[] -> "(Status " ++ show i ++ ")"
xs -> "[" ++ intercalate "," xs ++ "]"
where
checkInv = case x of
InvalidOp -> "InvalidOp" : checkZ
_ -> checkZ
checkZ = case x of
DivideByZero -> "DivideByZero" : checkO
_ -> checkO
checkO = case x of
Overflow -> "Overflow" : checkU
_ -> checkU
checkU = case x of
Underflow -> "Underflow" : checkI
_ -> checkI
checkI = case x of
Inexact -> "Inexact" : checkM
_ -> checkM
checkM = case x of
MemError -> ["MemError"]
_ -> []