From 329fc2a911878ffe47472751cb40aae20ab2c00a Mon Sep 17 00:00:00 2001 From: Javier Sagredo Date: Wed, 30 Jul 2025 12:55:54 +0200 Subject: [PATCH] Depend on crc32c --- Data/Digest/CRC32C.hs | 43 +++++--------------------- digest.cabal | 72 ++----------------------------------------- 2 files changed, 9 insertions(+), 106 deletions(-) diff --git a/Data/Digest/CRC32C.hs b/Data/Digest/CRC32C.hs index ea4ee70..ba743ea 100644 --- a/Data/Digest/CRC32C.hs +++ b/Data/Digest/CRC32C.hs @@ -1,28 +1,21 @@ {-# LANGUAGE CPP #-} {-# LANGUAGE FlexibleInstances #-} -{-# LANGUAGE MagicHash #-} -{-# LANGUAGE UnliftedFFITypes #-} module Data.Digest.CRC32C - ( CRC32C - , crc32c - , crc32cUpdate + ( CRC32C (..) ) where import qualified Data.ByteString as BS import qualified Data.ByteString.Lazy as BL import qualified Data.ByteString.Short as BSS -import Data.ByteString.Unsafe (unsafeUseAsCStringLen) import Data.Word -import Foreign.C.Types -import Foreign.Ptr -import GHC.Exts (ByteArray#) -import System.IO.Unsafe (unsafeDupablePerformIO) #if !MIN_VERSION_bytestring(0, 11, 1) import qualified Data.ByteString.Short.Internal as BSS #endif +import qualified "crc32c" Data.Digest.CRC32C as CRC32C + class CRC32C a where -- | Compute CRC32C checksum crc32c :: a -> Word32 @@ -34,13 +27,9 @@ class CRC32C a where crc32cUpdate :: Word32 -> a -> Word32 instance CRC32C BS.ByteString where - crc32c bs = unsafeDupablePerformIO $ - unsafeUseAsCStringLen bs $ \(ptr, len) -> - crc32c_value (castPtr ptr) (fromIntegral len) + crc32c = CRC32C.crc32c - crc32cUpdate cks bs = unsafeDupablePerformIO $ - unsafeUseAsCStringLen bs $ \(ptr, len) -> - crc32c_extend cks (castPtr ptr) (fromIntegral len) + crc32cUpdate = CRC32C.crc32c_update instance CRC32C BL.ByteString where crc32cUpdate = BL.foldlChunks crc32cUpdate @@ -49,24 +38,6 @@ instance CRC32C [Word8] where crc32cUpdate n = (crc32cUpdate n) . BL.pack instance CRC32C BSS.ShortByteString where - crc32c sbs@(BSS.SBS ba#) = unsafeDupablePerformIO $ - -- Must be unsafe ffi - crc32c_value' ba# (fromIntegral $ BSS.length sbs) - - crc32cUpdate cks sbs@(BSS.SBS ba#) = unsafeDupablePerformIO $ - -- Must be unsafe ffi - crc32c_extend' cks ba# (fromIntegral $ BSS.length sbs) - -------------------------------------------------------------------------------- - -foreign import ccall unsafe "crc32c/crc32c.h crc32c_value" - crc32c_value :: Ptr Word8 -> CSize -> IO Word32 - -foreign import ccall unsafe "crc32c/crc32c.h crc32c_extend" - crc32c_extend :: Word32 -> Ptr Word8 -> CSize -> IO Word32 - -foreign import ccall unsafe "crc32c/crc32c.h crc32c_value" - crc32c_value' :: ByteArray# -> CSize -> IO Word32 + crc32c = crc32c . BSS.fromShort -foreign import ccall unsafe "crc32c/crc32c.h crc32c_extend" - crc32c_extend' :: Word32 -> ByteArray# -> CSize -> IO Word32 + crc32cUpdate cks = crc32cUpdate cks . BSS.fromShort diff --git a/digest.cabal b/digest.cabal index 17d9904..4e913c7 100644 --- a/digest.cabal +++ b/digest.cabal @@ -31,51 +31,6 @@ flag pkg-config manual: False description: Use @pkg-config(1)@ to locate @zlib@ library. --- TODO: auto detect -flag have_builtin_prefetch - default: False - manual: True - description: The cxx compiler has the __builtin_prefetch intrinsic. - --- TODO: auto detect -flag have_mm_prefetch - default: False - manual: True - description: - Targeting X86 and the compiler has the _mm_prefetch intrinsic. - --- TODO: auto detect -flag have_sse42 - default: False - manual: True - description: - Can be enabled to improve performance of CRC32C if targeting X86 and - the compiler has the _mm_crc32_u{8,32,64} intrinsics. - --- TODO: auto detect -flag have_arm64_crc32c - default: False - manual: True - description: - Targeting ARM and the compiler has the __crc32c{b,h,w,d} and the - vmull_p64 intrinsics. - --- TODO: auto detect -flag have_strong_getauxval - default: False - manual: True - description: - The system libraries have the getauxval function in the header. - Should be true on Linux and Android API level 20+. - --- TODO: auto detect -flag have_weak_getauxval - default: False - manual: True - description: - The compiler supports defining getauxval as a weak symbol. - Should be true for any compiler that supports __attribute__((weak)). - source-repository head type: git location: https://github.com/TeofilC/digest @@ -89,38 +44,15 @@ library default-extensions: CPP ForeignFunctionInterface + PackageImports default-language: Haskell2010 build-depends: , base >=4.12 && <5 , bytestring >=0.10 && <0.13 + , crc32c includes: zlib.h - include-dirs: include external/crc32c/include - cxx-options: -std=c++11 - cxx-sources: - external/crc32c/src/crc32c.cc - external/crc32c/src/crc32c_portable.cc - - if flag(have_builtin_prefetch) - cxx-options: -DHAVE_BUILTIN_PREFETCH - - if flag(have_mm_prefetch) - cxx-options: -DHAVE_MM_PREFETCH - - if (arch(x86_64) && flag(have_sse42)) - cxx-options: -DHAVE_SSE42 -msse4.2 - cxx-sources: external/crc32c/src/crc32c_sse42.cc - - if (arch(aarch64) && flag(have_arm64_crc32c)) - cxx-options: -DHAVE_ARM64_CRC32C - cxx-sources: external/crc32c/src/crc32c_arm64.cc - - if flag(have_strong_getauxval) - cxx-options: -DHAVE_STRONG_GETAUXVAL - - if flag(have_weak_getauxval) - cxx-options: -DHAVE_WEAK_GETAUXVAL ghc-options: -Wall