FixedPoint-simple 0.5 → 0.5.1
raw patch · 3 files changed
+35/−12 lines, 3 files
Files
- Data/FixedPoint.lhs +15/−2
- Data/FixedPoint/TH.hs +19/−9
- FixedPoint-simple.cabal +1/−1
Data/FixedPoint.lhs view
@@ -161,6 +161,13 @@ > Read (GenericFixedPoint a b c) where > readsPrec n s = [ (realToFrac (r::Double), s) | (r,s) <- readsPrec n s] +> instance (Bits b, Bits c, Bits a, Integral a, Integral b) =>+> RealFrac (GenericFixedPoint a b c) where+> properFraction f@(FixedPoint a) =+> let nr = fracBits f+> m = 2^nr - 1+> in (fromIntegral $ a `shiftR` nr, FixedPoint $ a .&. m)+ Now we need some advanced functions (beyond +,-,*,/) on our fixed point type. Specifically, we want 'exp' (exponentiation with a base of 'e' ~ 2.71), erf (the "error function"), and square root. All of these will be implemented by some@@ -197,7 +204,7 @@ e^x = (e^(x/2))^2 to break the problem down (if the table hasn't already). > expTable :: [(Double,Double)]-> expTable = let ys = [b*2**x | b <- [-1,1], x <- [8,7..(-10)]] in zip ys (map exp ys)+> expTable = let ys = [b*2**x | b <- [-1,1], x <- [64,63..(-10)]] in zip ys (map exp ys) > > exp' :: (Show a, Ord a, Fractional a, Eq a) => Int -> a -> a > exp' n a =@@ -246,6 +253,9 @@ > > instance (Ord a, Bits a, Bits b, Integral a, Integral b, Bits c) => > Bits (GenericFixedPoint a b c) where+> testBit a = testBit (toFlat a)+> bit i = FixedPoint (bit i)+> popCount = popCount . toFlat > setBit = flat1 setBit > (.|.) = flat2 (.|.) > xor = flat2 xor@@ -294,6 +304,7 @@ > a == b = EQ == compare a b > > instance Bits Word128 where+> popCount (W128 h l) = popCount h + popCount l > bit i | i >= 64 = W128 (bit $ i - 64) 0 > | otherwise = W128 0 (bit i) > complement = pointwise complement@@ -372,7 +383,7 @@ > type Word512 = BigWord Word256 Word256 > -- |A 576 bit unsigned word-> type Word576 = BigWord Word512 Word64+> type Word576 = BigWord Word64 Word512 > > -- |A 584 bit unsigned word > type Word584 = BigWord Word72 Word512@@ -453,6 +464,7 @@ > {-# SPECIALIZE instance Bits Word2048 #-} > {-# SPECIALIZE instance Bits Word4096 #-} > {-# SPECIALIZE instance Bits Word8192 #-}+> popCount (BigWord a b) = popCount a + popCount b > bit i | i >= bitSize b = r1 > | otherwise = r2 > where r1@(BigWord _ b) = BigWord (bit $ i - bitSize b) 0@@ -617,6 +629,7 @@ > else BigInt (fromInteger i) > > instance (Bits a, Num a, Ord a) => Bits (BigInt a) where+> popCount (BigInt a) = popCount a > (.&.) a b = BigInt (unBI a .&. unBI b) > (.|.) a b = BigInt (unBI a .|. unBI b) > xor a b = BigInt (unBI a `xor` unBI b)
Data/FixedPoint/TH.hs view
@@ -19,10 +19,15 @@ let b = isNothing info if b then do let (h,l) = getParts i- hD <- mkWord h- lD <- mkWord l- a <- tySynD (mkW i) [] (appT (appT (conT $ mkName "BigWord") (conT $ mkW h)) (conT $ mkW l))- return $ a:(hD++lD)+ if h == 0+ then do let l' = l`div`2+ lD <- mkWord l'+ a <- tySynD (mkW i) [] (appT (appT (conT $ mkName "BigWord") (conT $ mkW l')) (conT $ mkW l'))+ return $ a:lD+ else do hD <- mkWord h+ lD <- mkWord l+ a <- tySynD (mkW i) [] (appT (appT (conT $ mkName "BigWord") (conT $ mkW h)) (conT $ mkW l))+ return $ a:(hD++lD) else return [] mkS :: Int -> String@@ -42,12 +47,17 @@ -- See the requirements under 'mkWord' for additional information. mkInt :: Int -> DecsQ mkInt i = do- d <- mkWord i- e <- tySynD (mkName . ("Int" ++) . show $ i) [] (appT (conT $ mkName "BigInt") (conT $ mkW i))- return (e:d)+ info <- lookupTypeName (mkS i)+ if isNothing info+ then do+ d <- mkWord i+ e <- tySynD (mkName . ("Int" ++) . show $ i) [] (appT (conT $ mkName "BigInt") (conT $ mkW i))+ return (e:d)+ else return [] --- @mkFixedPoint X Y@ Builds a fixed point alias named @FixedPointX_Y@. See--- the requirements under 'mkWord' for additional information.+-- @mkFixedPoint X Y@ Builds a fixed point alias named @FixedPointX_Y@+-- where X is the integral size in bits and Y is the fractional size in+-- bits. See the requirements under 'mkWord' for additional information. mkFixedPoint :: Int -> Int -> DecsQ mkFixedPoint int frac | (int + frac) `rem` 8 /= 0 = error "For fixed points, The sum of the integral and fractional bits must be a multiple of 8."
FixedPoint-simple.cabal view
@@ -1,5 +1,5 @@ Name: FixedPoint-simple-Version: 0.5+Version: 0.5.1 Synopsis: Fixed point, large word, and large int numerical representations (types and common class instances) Description: This library uses elementary techniques to implement fixed point types in terms of basic integrals such as Word64. All mathematical operations are implemented