#crc32 #checksum #simd #crc #performance

no-std crc32fast

Fast, SIMD-accelerated CRC32 (IEEE) checksum computation

16 stable releases

Uses old Rust 2015

1.4.2 May 20, 2024
1.4.0 Feb 12, 2024
1.3.2 Feb 8, 2022
1.3.0 Nov 30, 2021
1.0.4 Nov 30, 2018

#3 in Hardware support

Download history 1884236/week @ 2024-08-18 1856536/week @ 2024-08-25 1878879/week @ 2024-09-01 1870643/week @ 2024-09-08 1729441/week @ 2024-09-15 1881657/week @ 2024-09-22 1868979/week @ 2024-09-29 1937030/week @ 2024-10-06 2007993/week @ 2024-10-13 2005164/week @ 2024-10-20 1896694/week @ 2024-10-27 1916040/week @ 2024-11-03 1956039/week @ 2024-11-10 1992981/week @ 2024-11-17 1726996/week @ 2024-11-24 1877106/week @ 2024-12-01

7,677,282 downloads per month
Used in 15,289 crates (317 directly)

MIT/Apache

84KB
1K SLoC

crc32fast Build Status Crates.io Documentation

Fast, SIMD-accelerated CRC32 (IEEE) checksum computation

Usage

Simple usage

For simple use-cases, you can call the hash convenience function to directly compute the CRC32 checksum for a given byte slice:

let checksum = crc32fast::hash(b"foo bar baz");

Advanced usage

For use-cases that require more flexibility or performance, for example when processing large amounts of data, you can create and manipulate a Hasher:

use crc32fast::Hasher;

let mut hasher = Hasher::new();
hasher.update(b"foo bar baz");
let checksum = hasher.finalize();

Performance

This crate contains multiple CRC32 implementations:

  • A fast baseline implementation which processes up to 16 bytes per iteration
  • An optimized implementation for modern x86 using sse and pclmulqdq instructions
  • An optimized implementation for aarch64 using crc32 instructions

Calling the Hasher::new constructor at runtime will perform a feature detection to select the most optimal implementation for the current CPU feature set.

crate version variant ns/iter MB/s
crc 1.8.1 n/a 4,926 207
crc32fast (this crate) 1.0.0 baseline 683 1499
crc32fast (this crate) 1.0.0 pclmulqdq 140 7314

Memory Safety

Due to the use of SIMD intrinsics for the optimized implementations, this crate contains some amount of unsafe code.

In order to ensure memory safety, the relevant code has been fuzz tested using afl.rs with millions of iterations in both debug and release build settings. You can inspect the test setup in the fuzz sub-directory, which also has instructions on how to run the tests yourself.

On top of that, every commit is tested using an address sanitizer in CI to catch any out of bounds memory accesses.

Even though neither fuzzing nor sanitization has revealed any safety bugs yet, please don't hesitate to file an issue if you run into any crashes or other unexpected behaviour.

Available feature flags

std (default: enabled)

This library supports being built without the Rust std library, which is useful for low-level use-cases such as embedded where no operating system is available. To build the crate in a no_std context, disable the default std feature.

Note: Because runtime CPU feature detection requires OS support, the specialized SIMD implementations will be unavailable when the std feature is disabled.

nightly (default: disabled)

This feature flag enables unstable features that are only available on the nightly channel. Keep in mind that when enabling this feature flag, you might experience breaking changes when updating compiler versions.

Currently, enabling this feature flag will make the optimized aarch64 implementation available.

License

This project is licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies