castagnoli 0.1.0.0 → 0.2.0.0
raw patch · 4 files changed
+41/−34 lines, 4 filesdep −primitive-slicedep ~primitive
Dependencies removed: primitive-slice
Dependency ranges changed: primitive
Files
- CHANGELOG.md +4/−0
- castagnoli.cabal +2/−3
- src/Crc32c.hs +33/−29
- test/Main.hs +2/−2
CHANGELOG.md view
@@ -1,5 +1,9 @@ # Revision history for castagnoli +## 0.2.0.0 -- 2023-05-01++* Remove dependency on primitive-slice.+ ## 0.1.0.0 -- 2019-11-05 * First version. Released on an unsuspecting world.
castagnoli.cabal view
@@ -1,6 +1,6 @@ cabal-version: 2.2 name: castagnoli-version: 0.1.0.0+version: 0.2.0.0 synopsis: Portable CRC-32C description: Portable implementation of CRC-32C. homepage: https://github.com/andrewthad/castagnoli@@ -22,8 +22,7 @@ , base >=4.12.0.0 && <5 , primitive >=0.7 && <0.8 , primitive-unlifted >=0.1 && <0.2- , primitive-slice >=0.1 && <0.2- , byteslice >=0.1.1 && <0.2+ , byteslice >=0.1.1 && <0.3 hs-source-dirs: src ghc-options: -O2 -Wall default-language: Haskell2010
src/Crc32c.hs view
@@ -5,18 +5,14 @@ ( bytes , mutableBytes , chunks- , mutableChunks ) where import Crc32c.Table (table) import Data.Word (Word8,Word32)-import Data.Primitive (ByteArray) import Data.Bytes.Types (Bytes(Bytes),MutableBytes(MutableBytes)) import Control.Monad.Primitive (PrimState,PrimMonad) import Data.Bits (shiftR,xor)-import Data.Primitive.Slice (UnliftedVector(UnliftedVector))-import Data.Primitive.Slice (MutableUnliftedVector(MutableUnliftedVector))-import qualified Data.Primitive.Unlifted.Array as PM+import Data.Bytes.Chunks (Chunks(ChunksCons,ChunksNil)) import qualified Data.Primitive.ByteArray as PM import qualified Data.Primitive.Ptr as PM @@ -28,6 +24,12 @@ else acc in xor 0xFFFFFFFF (go (xor acc0 0xFFFFFFFF) off (off + len)) +chunks :: Word32 -> Chunks -> Word32+chunks !acc ChunksNil = acc+chunks !acc (ChunksCons x xs) =+ let !acc' = bytes acc x+ in chunks acc' xs+ -- | Compute the checksum of a slice of mutable bytes. mutableBytes :: PrimMonad m => Word32@@ -43,30 +45,32 @@ r <- go (xor acc0 0xFFFFFFFF) off (off + len) pure (xor 0xFFFFFFFF r) --- | Compute the checksum of a slice into an array of unsliced byte arrays.-chunks :: Word32 -> UnliftedVector ByteArray -> Word32-chunks acc0 (UnliftedVector arr off len) = - let go !acc !ix !end = if ix < end- then- let b = PM.indexUnliftedArray arr ix- in go (bytes acc (Bytes b 0 (PM.sizeofByteArray b))) (ix + 1) end- else acc- in go acc0 off (off + len)---- | Compute the checksum of a slice into an mutable array of--- unsliced byte arrays.-mutableChunks :: PrimMonad m- => Word32- -> MutableUnliftedVector (PrimState m) ByteArray- -> m Word32-{-# inlineable mutableChunks #-}-mutableChunks acc0 (MutableUnliftedVector arr off len) = - let go !acc !ix !end = if ix < end- then do- b <- PM.readUnliftedArray arr ix- go (bytes acc (Bytes b 0 (PM.sizeofByteArray b))) (ix + 1) end- else pure acc- in go acc0 off (off + len)+-- This might be revived one day.+--+-- x -- | Compute the checksum of a slice into an array of unsliced byte arrays.+-- x byteArrays :: Word32 -> UnliftedVector ByteArray -> Word32+-- x byteArrays !acc0 (UnliftedVector arr off len) =+-- x let go !acc !ix !end = if ix < end+-- x then+-- x let b = PM.indexUnliftedArray arr ix+-- x in go (bytes acc (Bytes b 0 (PM.sizeofByteArray b))) (ix + 1) end+-- x else acc+-- x in go acc0 off (off + len)+-- x +-- x -- | Compute the checksum of a slice into an mutable array of+-- x -- unsliced byte arrays.+-- x mutableByteArrays :: PrimMonad m+-- x => Word32+-- x -> MutableUnliftedVector (PrimState m) ByteArray+-- x -> m Word32+-- x {-# inlineable mutableByteArrays #-}+-- x mutableByteArrays acc0 (MutableUnliftedVector arr off len) =+-- x let go !acc !ix !end = if ix < end+-- x then do+-- x b <- PM.readUnliftedArray arr ix+-- x go (bytes acc (Bytes b 0 (PM.sizeofByteArray b))) (ix + 1) end+-- x else pure acc+-- x in go acc0 off (off + len) step :: Word32 -> Word8 -> Word32 step !acc !w = xor
test/Main.hs view
@@ -1,4 +1,4 @@-import Crc32c (bytes,chunks)+import Crc32c (bytes,byteArrays) import Control.Monad (when) import Data.Primitive (ByteArray) import Data.Word (Word8)@@ -18,7 +18,7 @@ , (Exts.fromList [0x34 :: Word8,0x35,0x36,0x37,0x38,0x39]) ] let expected2 = 0xe3069283- let actual2 = chunks 0 sample2+ let actual2 = byteArrays 0 sample2 when (actual2 /= expected2) $ fail $ "ex2: expected " ++ show expected2 ++ " but got " ++ show actual2