packages feed

hw-bits 0.7.1.4 → 0.7.1.5

raw patch · 9 files changed

+200/−5 lines, 9 files

Files

hw-bits.cabal view
@@ -1,7 +1,7 @@ cabal-version:  2.2  name:                   hw-bits-version:                0.7.1.4+version:                0.7.1.5 synopsis:               Bit manipulation description:            Please see README.md category:               Data, Bit@@ -107,6 +107,10 @@   main-is:              Spec.hs   other-modules:        HaskellWorks.Data.Bits.BitReadSpec                         HaskellWorks.Data.Bits.BitWiseSpec+                        HaskellWorks.Data.Bits.Broadword.Word8Spec+                        HaskellWorks.Data.Bits.Broadword.Word16Spec+                        HaskellWorks.Data.Bits.Broadword.Word32Spec+                        HaskellWorks.Data.Bits.Broadword.Word64Spec                         HaskellWorks.Data.Bits.FromBitTextByteStringSpec                         HaskellWorks.Data.Bits.Log2Spec                         HaskellWorks.Data.Bits.Writer.StorableSpec
src/HaskellWorks/Data/Bits/Broadword/Word16.hs view
@@ -77,7 +77,13 @@ -- >>> showHex (kBitDiffPos 8 0x20ff 0x10ff) "" -- "1000" kBitDiffPos :: Int -> Word16 -> Word16 -> Word16-kBitDiffPos k x y = let d = kBitDiff k x y in d .&. kBitDiff k 0 ((comp d .&. h 8) .>. fromIntegral (k - 1))+kBitDiffPos k x y =+                                                                        -- let !_ = trace (">> x = " <> bitShow x) x in+                                                                        -- let !_ = trace (">> y = " <> bitShow y) y in+  let d = kBitDiff k x y                                            in  -- let !_ = trace (">> d = " <> bitShow d) d in+  let s = kBitDiff k 0 ((comp d .&. h k) .>. fromIntegral (k - 1))  in  -- let !_ = trace (">> s = " <> bitShow s) s in+  let r = d .&. s                                                   in  -- let !_ = trace (">> r = " <> bitShow r) r in+  r {-# INLINE kBitDiffPos #-}  -- | Broadword subtraction of sub-words of size 'k' where 'k' ∈ { 2, 4, 8, 16, 32, 64 } where all the sub-words of 'x' and 'y' must
src/HaskellWorks/Data/Bits/Broadword/Word32.hs view
@@ -83,7 +83,13 @@ -- >>> showHex (kBitDiffPos 8 0x200000ff 0x100000ff) "" -- produces nonsense in the last sub-word -- "10000000" kBitDiffPos :: Int -> Word32 -> Word32 -> Word32-kBitDiffPos k x y = let d = kBitDiff k x y in d .&. kBitDiff k 0 ((comp d .&. h 8) .>. fromIntegral (k - 1))+kBitDiffPos k x y =+                                                                        -- let !_ = trace (">> x = " <> bitShow x) x in+                                                                        -- let !_ = trace (">> y = " <> bitShow y) y in+  let d = kBitDiff k x y                                            in  -- let !_ = trace (">> d = " <> bitShow d) d in+  let s = kBitDiff k 0 ((comp d .&. h k) .>. fromIntegral (k - 1))  in  -- let !_ = trace (">> s = " <> bitShow s) s in+  let r = d .&. s                                                   in  -- let !_ = trace (">> r = " <> bitShow r) r in+  r {-# INLINE kBitDiffPos #-}  -- | Broadword subtraction of sub-words of size 'k' where 'k' ∈ { 2, 4, 8, 16, 32 } where all the sub-words of 'x' and 'y' must
src/HaskellWorks/Data/Bits/Broadword/Word64.hs view
@@ -89,7 +89,13 @@ -- >>> showHex (kBitDiffPos 8 0x20000000000000ff 0x10000000000000ff) "" -- "1000000000000000" kBitDiffPos :: Int -> Word64 -> Word64 -> Word64-kBitDiffPos k x y = let d = kBitDiff k x y in d .&. kBitDiff k 0 ((comp d .&. h 8) .>. fromIntegral (k - 1))+kBitDiffPos k x y =+                                                                        -- let !_ = trace (">> x = " <> bitShow x) x in+                                                                        -- let !_ = trace (">> y = " <> bitShow y) y in+  let d = kBitDiff k x y                                            in  -- let !_ = trace (">> d = " <> bitShow d) d in+  let s = kBitDiff k 0 ((comp d .&. h k) .>. fromIntegral (k - 1))  in  -- let !_ = trace (">> s = " <> bitShow s) s in+  let r = d .&. s                                                   in  -- let !_ = trace (">> r = " <> bitShow r) r in+  r {-# INLINE kBitDiffPos #-}  -- | Broadword subtraction of sub-words of size 'k' where 'k' ∈ { 2, 4, 8, 16, 32, 64 } where all the sub-words of 'x' and 'y' must
src/HaskellWorks/Data/Bits/Broadword/Word8.hs view
@@ -71,7 +71,13 @@ -- >>> showHex (kBitDiff 8 0xff 0xff) "" -- "0" kBitDiffPos :: Int -> Word8 -> Word8 -> Word8-kBitDiffPos k x y = let d = kBitDiff k x y in d .&. kBitDiff k 0 ((comp d .&. h 8) .>. fromIntegral (k - 1))+kBitDiffPos k x y =+                                                                        -- let !_ = trace (">> x = " <> bitShow x) x in+                                                                        -- let !_ = trace (">> y = " <> bitShow y) y in+  let d = kBitDiff k x y                                            in  -- let !_ = trace (">> d = " <> bitShow d) d in+  let s = kBitDiff k 0 ((comp d .&. h k) .>. fromIntegral (k - 1))  in  -- let !_ = trace (">> s = " <> bitShow s) s in+  let r = d .&. s                                                   in  -- let !_ = trace (">> r = " <> bitShow r) r in+  r {-# INLINE kBitDiffPos #-}  -- | Broadword subtraction of sub-words of size 'k' where 'k' ∈ { 2, 4, 8 } where all the sub-words of 'x' and 'y' must
+ test/HaskellWorks/Data/Bits/Broadword/Word16Spec.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE BinaryLiterals      #-}+{-# LANGUAGE ScopedTypeVariables #-}++module HaskellWorks.Data.Bits.Broadword.Word16Spec where++import HaskellWorks.Hspec.Hedgehog+import Hedgehog+import Test.Hspec++import qualified HaskellWorks.Data.Bits.Broadword.Word16 as W8+import qualified Hedgehog.Gen                            as G+import qualified Hedgehog.Range                          as R++{-# ANN module ("HLint: ignore Redundant do" :: String) #-}++spec :: Spec+spec = describe "HaskellWorks.Data.Bits.Broadword.Word16" $ do+  it "kBitDiff" $ requireProperty $ do+    a <- forAll $ G.word16 (R.linear 0 0xffff)+    b <- forAll $ G.word16 (R.linear 0 0xffff)++    W8.kBitDiff 16 a b === a - b+  it "kBitDiffPos" $ requireProperty $ do+    a <- forAll $ G.word16 (R.linear 0 0x7fff)+    b <- forAll $ G.word16 (R.linear 0 0x7fff)++    if a > b+      then W8.kBitDiffPos 16 a b === a - b+      else W8.kBitDiffPos 16 a b === 0
+ test/HaskellWorks/Data/Bits/Broadword/Word32Spec.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE BinaryLiterals      #-}+{-# LANGUAGE ScopedTypeVariables #-}++module HaskellWorks.Data.Bits.Broadword.Word32Spec where++import HaskellWorks.Hspec.Hedgehog+import Hedgehog+import Test.Hspec++import qualified HaskellWorks.Data.Bits.Broadword.Word32 as W8+import qualified Hedgehog.Gen                            as G+import qualified Hedgehog.Range                          as R++{-# ANN module ("HLint: ignore Redundant do" :: String) #-}++spec :: Spec+spec = describe "HaskellWorks.Data.Bits.Broadword.Word32" $ do+  it "kBitDiff" $ requireProperty $ do+    a <- forAll $ G.word32 (R.linear 0 0xffffffff)+    b <- forAll $ G.word32 (R.linear 0 0xffffffff)++    W8.kBitDiff 32 a b === a - b+  it "kBitDiffPos" $ requireProperty $ do+    a <- forAll $ G.word32 (R.linear 0 0x7fffffff)+    b <- forAll $ G.word32 (R.linear 0 0x7fffffff)++    if a > b+      then W8.kBitDiffPos 32 a b === a - b+      else W8.kBitDiffPos 32 a b === 0
+ test/HaskellWorks/Data/Bits/Broadword/Word64Spec.hs view
@@ -0,0 +1,29 @@+{-# LANGUAGE BinaryLiterals      #-}+{-# LANGUAGE ScopedTypeVariables #-}++module HaskellWorks.Data.Bits.Broadword.Word64Spec where++import HaskellWorks.Hspec.Hedgehog+import Hedgehog+import Test.Hspec++import qualified HaskellWorks.Data.Bits.Broadword.Word64 as W8+import qualified Hedgehog.Gen                            as G+import qualified Hedgehog.Range                          as R++{-# ANN module ("HLint: ignore Redundant do" :: String) #-}++spec :: Spec+spec = describe "HaskellWorks.Data.Bits.Broadword.Word64" $ do+  it "kBitDiff" $ requireProperty $ do+    a <- forAll $ G.word64 (R.linear 0 0xffffffffffffffff)+    b <- forAll $ G.word64 (R.linear 0 0xffffffffffffffff)++    W8.kBitDiff 64 a b === a - b+  it "kBitDiffPos" $ requireProperty $ do+    a <- forAll $ G.word64 (R.linear 0 0x7fffffffffffffff)+    b <- forAll $ G.word64 (R.linear 0 0x7fffffffffffffff)++    if a > b+      then W8.kBitDiffPos 64 a b === a - b+      else W8.kBitDiffPos 64 a b === 0
+ test/HaskellWorks/Data/Bits/Broadword/Word8Spec.hs view
@@ -0,0 +1,80 @@+{-# LANGUAGE BinaryLiterals      #-}+{-# LANGUAGE ScopedTypeVariables #-}+{-# LANGUAGE TypeApplications    #-}++module HaskellWorks.Data.Bits.Broadword.Word8Spec where++import Data.Semigroup                 ((<>))+import Data.Word+import HaskellWorks.Data.Bits.BitShow+import HaskellWorks.Data.Bits.BitWise+import HaskellWorks.Hspec.Hedgehog+import Hedgehog+import Test.Hspec++import qualified HaskellWorks.Data.Bits.Broadword.Word8 as W8+import qualified Hedgehog.Gen                           as G+import qualified Hedgehog.Range                         as R++{-# ANN module ("HLint: ignore Redundant do" :: String) #-}++spec :: Spec+spec = describe "HaskellWorks.Data.Bits.Broadword.Word8" $ do+  it "kBitDiff" $ requireProperty $ do+    a <- forAll $ G.word8 (R.linear 0 0xff)+    b <- forAll $ G.word8 (R.linear 0 0xff)++    W8.kBitDiff 8 a b === a - b+  describe "kBitDiffPos" $ do+    it "subword size 8" $ requireProperty $ do+      a <- forAll $ G.word8 (R.linear 0 0x7f)+      b <- forAll $ G.word8 (R.linear 0 0x7f)++      if a > b+        then W8.kBitDiffPos 8 a b === a - b+        else W8.kBitDiffPos 8 a b === 0+    it "subword size 4" $ requireProperty $ do+      a <- forAll $ G.word8 (R.linear 0 0x7)+      b <- forAll $ G.word8 (R.linear 0 0x7)+      c <- forAll $ G.word8 (R.linear 0 0x7)+      d <- forAll $ G.word8 (R.linear 0 0x7)++      ab <- forAll $ pure (a .|. (b .<. 4))+      cd <- forAll $ pure (c .|. (d .<. 4))++      r  <- forAll $ pure $ W8.kBitDiffPos 4 ab cd++      annotateShow $ "ab = " <> bitShow ab+      annotateShow $ "cd = " <> bitShow cd+      annotateShow $ "r  = " <> bitShow r++      if a > c+        then r .&. 0x0f === a - c+        else r .&. 0x0f === 0++      if b > d+        then (r .>. 4) .&. 0x0f === b - d+        else (r .>. 4) .&. 0x0f === 0++    it "subword size 4 test 1" $ requireProperty $ do+      a <- forAll $ pure (1 :: Word8)+      b <- forAll $ pure (0 :: Word8)+      c <- forAll $ pure (0 :: Word8)+      d <- forAll $ pure (0 :: Word8)++      ab <- forAll $ pure (a .|. (b .<. 4))+      cd <- forAll $ pure (c .|. (d .<. 4))++      r  <- forAll $ pure $ W8.kBitDiffPos 4 ab cd++      annotateShow $ "ab = " <> bitShow ab+      annotateShow $ "cd = " <> bitShow cd+      annotateShow $ "r  = " <> bitShow r++      if a > c+        then r .&. 0x0f === a - c+        else r .&. 0x0f === 0++      if b > d+        then (r .>. 4) .&. 0x0f === b - d+        else (r .>. 4) .&. 0x0f === 0