leptonyu / crc32c Goto Github PK
View Code? Open in Web Editor NEWHaskell bindings for crc32c
License: BSD 3-Clause "New" or "Revised" License
Haskell bindings for crc32c
License: BSD 3-Clause "New" or "Revised" License
With the current .cabal file, the bundled C++ code does not build on OSX or Windows with default versions of the toolchain on those platforms.
There are two independent problems:
The -std=c++11
flag is not getting passed to the C++ compiler (on any platform). Apparently on OSX the default C++ language standard for clang is not C++11 and so the compilation fails, complaining about the use of some C++11 features (constexpr
). This is despite the crc32c.cabal
file saying:
if arch(x86_64)
cc-options: -std=c++11 -D__HAVE_SSE42=1 -D__HAVE_ARM64_CRC32C=0 -msse4.2
The problem is that the crc32c.cabal
file specifies cc-sources
and cc-options
which are for C code, not C++ code. It should use cxx-sources
and cxx-options
. Then the options do get passed to the C++ compiler.
The reason it sort-of works when one uses cc-sources
is that Cabal guesses that .cc
files are probably C++, but still it doesn't pass C++ options to those compilations. But the fix it simple: use cxx-sources
and cxx-options
and then it works.
Note also that this means that all of the cc flags that the .cabal tries to specify are being ignored, and thus presumably this means the generic implementation is being used rather than the arm or msse42 optimised versions.
Both FFI imports involve an argument of type size_t
:
uint32_t crc32c_extend(uint32_t crc, const uint8_t* data, size_t count);
uint32_t crc32c_value(const uint8_t* data, size_t count);
Windows uses C type unsigned long long
for a 64bit unsigned int, while most other platforms use C type unsigned long
for the same. The significance of this is that c2hs
will look through the size_t
type alias and bind the function with either the Haskell type CULong
or CULLong
. Given that the binding code assumes CULong
then this fails with a type error when c2hs (correctly) discovers that the C type is actually CULLong
.
lib_crc32c_extend :: CUInt -> Ptr CUChar -> CULong -> CUInt
lib_crc32c_extend = {#call pure crc32c_extend #}
lib_crc32c_value :: Ptr CUChar -> CULong -> CUInt
lib_crc32c_value = {#call pure crc32c_value #}
The simple solution is to use the Haskell type CSize
instead, and not to bother with using c2hs
at all.
foreign import ccall unsafe "crc32c/crc32c.h crc32c_extend"
lib_crc32c_extend :: CUInt -> Ptr CUChar -> CSize -> IO CUInt
foreign import ccall unsafe "crc32c/crc32c.h crc32c_value"
lib_crc32c_value :: Ptr CUChar -> CSize -> IO CUInt
Note also the use of unsafe
since these are usually short-running calls so the overhead of a safe call is rather high.
In addition, -D__HAVE_SSE42=1 -D__HAVE_ARM64_CRC32C=0
is using the wrong CPP define names. The names as used in the C++ code are AVE_SSE42
and HAVE_ARM64_CRC32C
without the __
prefix.
I have been testing fixes for these issues here IntersectMBO/lsm-tree#75
I plan to tidy this up and provide PRs here soonish.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.