use crate::mmap::SharedWords;
pub(crate) fn crc32c(data: &[u8]) -> u32 {
crc32c_append(0, data)
}
pub(crate) fn crc32c_append(crc: u32, data: &[u8]) -> u32 {
match hw::append(crc, data) {
Some(crc) => crc,
None => crc32c::crc32c_append(crc, data),
}
}
const POLY: u32 = 0x82f6_3b78;
pub(crate) fn mul_mod(a: u32, mut b: u32) -> u32 {
let mut product = 0;
let mut bit = 1_u32 << 31;
while bit != 0 {
if a & bit != 0 {
product ^= b;
}
b = if b & 1 != 0 { (b >> 1) ^ POLY } else { b >> 1 };
bit >>= 1;
}
product
}
pub(crate) fn x8n(mut n: u64) -> u32 {
let mut power = 1_u32 << 31; let mut square = 1_u32 << 23; while n != 0 {
if n & 1 != 0 {
power = mul_mod(square, power);
}
square = mul_mod(square, square);
n >>= 1;
}
power
}
pub(crate) fn crc32c_append_parts(
crc: u32,
head: &[u8],
words: SharedWords<'_>,
tail: &[u8],
) -> u32 {
match hw::append_parts(crc, head, words, tail) {
Some(crc) => crc,
None => soft_append_parts(crc, head, words, tail),
}
}
fn soft_append_parts(crc: u32, head: &[u8], words: SharedWords<'_>, tail: &[u8]) -> u32 {
let mut crc = crc32c::crc32c_append(crc, head);
let mut buf = [0u8; 256];
let (chunks, rest) = words.chunks_exact(buf.len() / 8);
for chunk in chunks.chain(std::iter::once(rest)) {
let mut filled = 0;
for (dest, word) in buf.chunks_exact_mut(8).zip(chunk.iter()) {
dest.copy_from_slice(&word.to_le_bytes());
filled += 8;
}
crc = crc32c::crc32c_append(crc, &buf[..filled]);
}
crc32c::crc32c_append(crc, tail)
}
#[cfg_attr(
any(
miri,
tickertape_force_soft_crc,
not(any(target_arch = "aarch64", target_arch = "x86_64"))
),
allow(dead_code)
)]
const LANES: [usize; 2] = [256, 64];
#[cfg_attr(
any(
miri,
tickertape_force_soft_crc,
not(any(target_arch = "aarch64", target_arch = "x86_64"))
),
allow(dead_code)
)]
type ShiftTables = [[[u32; 256]; 4]; 2];
#[cfg_attr(
any(
miri,
tickertape_force_soft_crc,
not(any(target_arch = "aarch64", target_arch = "x86_64"))
),
allow(dead_code)
)]
#[inline]
fn shift(table: &[[u32; 256]; 4], crc: u32) -> u32 {
table[0][(crc & 0xff) as usize]
^ table[1][((crc >> 8) & 0xff) as usize]
^ table[2][((crc >> 16) & 0xff) as usize]
^ table[3][(crc >> 24) as usize]
}
#[cfg_attr(
any(
miri,
tickertape_force_soft_crc,
not(any(target_arch = "aarch64", target_arch = "x86_64"))
),
allow(dead_code)
)]
#[inline]
fn word(bytes: &[u8]) -> u64 {
let mut w = [0u8; 8];
w.copy_from_slice(bytes);
u64::from_le_bytes(w)
}
#[cfg(test)]
thread_local! {
static BLOCKS: std::cell::Cell<[u64; 2]> = const { std::cell::Cell::new([0; 2]) };
}
#[cfg(test)]
#[cfg_attr(
any(
miri,
tickertape_force_soft_crc,
not(any(target_arch = "aarch64", target_arch = "x86_64"))
),
allow(dead_code)
)]
fn note_block(lane_bytes: usize) {
BLOCKS.with(|b| {
let mut counts = b.get();
counts[usize::from(lane_bytes != LANES[0])] += 1;
b.set(counts);
});
}
#[cfg(all(
any(target_arch = "aarch64", target_arch = "x86_64"),
not(miri),
not(tickertape_force_soft_crc)
))]
macro_rules! three_lane_crc {
($feature:literal) => {
fn tables() -> &'static super::ShiftTables {
static TABLES: std::sync::OnceLock<Box<super::ShiftTables>> =
std::sync::OnceLock::new();
TABLES.get_or_init(|| unsafe { build_tables() })
}
#[target_feature(enable = $feature)]
#[allow(unused_unsafe)]
unsafe fn build_tables() -> Box<super::ShiftTables> {
let mut tables = Box::new([[[0u32; 256]; 4]; 2]);
for (table, lane) in tables.iter_mut().zip(super::LANES) {
for (j, row) in table.iter_mut().enumerate() {
for (b, entry) in row.iter_mut().enumerate() {
let mut crc = (b as u32) << (8 * j);
for _ in 0..lane / 8 {
crc = unsafe { step(crc, 0) };
}
*entry = crc;
}
}
}
tables
}
#[target_feature(enable = $feature)]
#[allow(unused_unsafe)]
unsafe fn update(
mut crc: u32,
mut data: &[u8],
tables: Option<&super::ShiftTables>,
) -> u32 {
for (table, lane) in tables.into_iter().flatten().zip(super::LANES) {
let mut blocks = data.chunks_exact(3 * lane);
for block in &mut blocks {
#[cfg(test)]
super::note_block(lane);
let (a, rest) = block.split_at(lane);
let (b, c) = rest.split_at(lane);
let (mut c0, mut c1, mut c2) = (crc, 0u32, 0u32);
for ((x, y), z) in a
.chunks_exact(8)
.zip(b.chunks_exact(8))
.zip(c.chunks_exact(8))
{
unsafe {
c0 = step(c0, super::word(x));
c1 = step(c1, super::word(y));
c2 = step(c2, super::word(z));
}
}
crc = super::shift(table, super::shift(table, c0) ^ c1) ^ c2;
}
data = blocks.remainder();
}
let mut words = data.chunks_exact(8);
for w in &mut words {
crc = unsafe { step(crc, super::word(w)) };
}
for &byte in words.remainder() {
crc = unsafe { step_byte(crc, byte) };
}
crc
}
#[target_feature(enable = $feature)]
#[allow(unused_unsafe)]
unsafe fn update_words(
mut crc: u32,
mut words: crate::mmap::SharedWords<'_>,
tables: Option<&super::ShiftTables>,
) -> u32 {
for (table, lane) in tables.into_iter().flatten().zip(super::LANES) {
let lane = lane / 8;
let (blocks, rest) = words.chunks_exact(3 * lane);
for block in blocks {
#[cfg(test)]
super::note_block(8 * lane);
let Some((a, rest)) = block.split_at_checked(lane) else {
continue;
};
let Some((b, c)) = rest.split_at_checked(lane) else {
continue;
};
let (mut c0, mut c1, mut c2) = (crc, 0u32, 0u32);
for ((x, y), z) in a.iter().zip(b.iter()).zip(c.iter()) {
unsafe {
c0 = step(c0, x);
c1 = step(c1, y);
c2 = step(c2, z);
}
}
crc = super::shift(table, super::shift(table, c0) ^ c1) ^ c2;
}
words = rest;
}
for w in words.iter() {
crc = unsafe { step(crc, w) };
}
crc
}
#[target_feature(enable = $feature)]
#[allow(unused_unsafe)]
unsafe fn update_parts(
crc: u32,
head: &[u8],
words: crate::mmap::SharedWords<'_>,
tail: &[u8],
tables: Option<&super::ShiftTables>,
) -> u32 {
unsafe {
let crc = update(crc, head, None);
let crc = update_words(crc, words, tables);
update(crc, tail, None)
}
}
pub(super) fn append_parts(
crc: u32,
head: &[u8],
words: crate::mmap::SharedWords<'_>,
tail: &[u8],
) -> Option<u32> {
if !detected() {
return None;
}
let tables = (words.len() * 8 >= 3 * super::LANES[1]).then(tables);
Some(!unsafe { update_parts(!crc, head, words, tail, tables) })
}
};
}
#[cfg(all(target_arch = "aarch64", not(miri), not(tickertape_force_soft_crc)))]
mod hw {
use std::arch::aarch64::{__crc32cb, __crc32cd};
three_lane_crc!("crc");
#[inline]
fn detected() -> bool {
std::arch::is_aarch64_feature_detected!("crc")
}
pub(super) fn append(crc: u32, data: &[u8]) -> Option<u32> {
if !detected() {
return None;
}
let tables = (data.len() >= 3 * super::LANES[1]).then(tables);
Some(!unsafe { update(!crc, data, tables) })
}
#[target_feature(enable = "crc")]
#[inline]
#[allow(unused_unsafe)]
unsafe fn step(crc: u32, word: u64) -> u32 {
unsafe { __crc32cd(crc, word) }
}
#[target_feature(enable = "crc")]
#[inline]
#[allow(unused_unsafe)]
unsafe fn step_byte(crc: u32, byte: u8) -> u32 {
unsafe { __crc32cb(crc, byte) }
}
}
#[cfg(all(target_arch = "x86_64", not(miri), not(tickertape_force_soft_crc)))]
mod hw {
use std::arch::x86_64::{_mm_crc32_u8, _mm_crc32_u64};
three_lane_crc!("sse4.2");
#[inline]
fn detected() -> bool {
std::arch::is_x86_feature_detected!("sse4.2")
}
pub(super) fn append(crc: u32, data: &[u8]) -> Option<u32> {
if !detected() {
return None;
}
let tables = (data.len() >= 3 * super::LANES[1]).then(tables);
Some(!unsafe { update(!crc, data, tables) })
}
#[target_feature(enable = "sse4.2")]
#[inline]
#[allow(unused_unsafe)]
unsafe fn step(crc: u32, word: u64) -> u32 {
unsafe { _mm_crc32_u64(u64::from(crc), word) as u32 }
}
#[target_feature(enable = "sse4.2")]
#[inline]
#[allow(unused_unsafe)]
unsafe fn step_byte(crc: u32, byte: u8) -> u32 {
unsafe { _mm_crc32_u8(crc, byte) }
}
}
#[cfg(any(
miri,
tickertape_force_soft_crc,
not(any(target_arch = "aarch64", target_arch = "x86_64"))
))]
mod hw {
pub(super) fn append(_crc: u32, _data: &[u8]) -> Option<u32> {
None
}
pub(super) fn append_parts(
_crc: u32,
_head: &[u8],
_words: crate::mmap::SharedWords<'_>,
_tail: &[u8],
) -> Option<u32> {
None
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
#[test]
fn check_value() {
assert_eq!(crc32c(b"123456789"), 0xE306_9283);
assert_eq!(crc32c(&[0; 32]), 0x8A91_36AA);
assert_eq!(crc32c(&[0xff; 32]), 0x62A8_AB43);
assert_eq!(crc32c(b""), 0);
}
#[test]
fn every_length_through_the_lane_sizes_matches_the_crate() {
const MAX: usize = 2 * 3 * (256 + 64) + 64;
let mut x = 0x9e37_79b9_7f4a_7c15u64;
let data: Vec<u8> = (0..MAX + 7)
.map(|_| {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x as u8
})
.collect();
let step = if cfg!(miri) { 61 } else { 1 };
for len in (0..=MAX).step_by(step) {
for start in 0..8 {
let slice = &data[start..start + len];
assert_eq!(
crc32c(slice),
crc32c::crc32c(slice),
"len {len} start {start}"
);
}
}
}
fn atomics(bytes: &[u8]) -> Vec<std::sync::atomic::AtomicU64> {
bytes
.chunks(8)
.map(|c| {
let mut w = [0u8; 8];
w[..c.len()].copy_from_slice(c);
std::sync::atomic::AtomicU64::new(u64::from_le_bytes(w))
})
.collect()
}
#[test]
fn parts_match_the_crate_for_every_word_count() {
const MAX_WORDS: usize = (2 * 3 * (256 + 64) + 64) / 8 + 1;
let mut x = 0x2545_f491_4f6c_dd1du64;
let data: Vec<u8> = (0..MAX_WORDS * 8 + 64)
.map(|_| {
x ^= x << 13;
x ^= x >> 7;
x ^= x << 17;
x as u8
})
.collect();
let step = if cfg!(miri) { 23 } else { 1 };
for n in (0..=MAX_WORDS).step_by(step) {
for (head, tail) in [(0, 0), (36, 0), (36, 1), (36, 7), (3, 5)] {
let h = &data[..head];
let w = &data[head..head + 8 * n];
let t = &data[head + 8 * n..head + 8 * n + tail];
let words = atomics(w);
let words = SharedWords::new(&words);
let whole = &data[..head + 8 * n + tail];
let init = n as u32 ^ 0x5a5a_5a5a;
let expected = crc32c::crc32c_append(init, whole);
assert_eq!(
crc32c_append_parts(init, h, words, t),
expected,
"{n} words, head {head}, tail {tail}"
);
assert_eq!(
soft_append_parts(init, h, words, t),
expected,
"software path: {n} words, head {head}, tail {tail}"
);
}
}
}
#[cfg(not(miri))]
#[test]
fn hardware_path_is_used_when_available() {
let atoms = atomics(&[0x5a; 16]);
let used = hw::append(0, b"x").is_some();
let used_parts = hw::append_parts(0, b"x", SharedWords::new(&atoms), b"y").is_some();
assert_eq!(
used_parts, used,
"the word-fed CRC must take the hardware path exactly when the slice CRC does"
);
#[cfg(target_arch = "x86_64")]
let detected = std::arch::is_x86_feature_detected!("sse4.2");
#[cfg(target_arch = "aarch64")]
let detected = std::arch::is_aarch64_feature_detected!("crc");
#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))]
let detected = false;
assert_eq!(used, detected && !cfg!(tickertape_force_soft_crc));
#[cfg(all(target_os = "macos", target_arch = "aarch64"))]
assert!(detected, "Apple silicon always has the CRC extension");
}
#[cfg(not(miri))]
#[test]
fn three_lane_blocks_run_for_long_inputs() {
if hw::append(0, b"").is_none() {
return; }
let data: Vec<u8> = (0..4000_u32)
.map(|i| (i.wrapping_mul(7) >> 1) as u8)
.collect();
let atoms = atomics(&data);
let take = || BLOCKS.with(|b| b.replace([0; 2]));
for (len, expected) in [
(0, [0, 0]),
(184, [0, 0]),
(192, [0, 1]),
(760, [0, 3]),
(768, [1, 0]),
(960, [1, 1]),
(2000, [2, 2]),
(3840, [5, 0]),
] {
take();
assert_eq!(crc32c(&data[..len]), crc32c::crc32c(&data[..len]));
assert_eq!(take(), expected, "slice CRC over {len} bytes");
let words = SharedWords::new(&atoms[..len / 8]);
let _ = crc32c_append_parts(0, b"head", words, b"t");
assert_eq!(take(), expected, "word-fed CRC over {len} bytes");
}
}
fn property_config() -> ProptestConfig {
let mut config = ProptestConfig::default();
if cfg!(miri) {
config.cases = 2;
config.failure_persistence = None;
}
config
}
proptest! {
#![proptest_config(property_config())]
#[test]
fn matches_the_crate(init in any::<u32>(), data in proptest::collection::vec(any::<u8>(), 0..5000)) {
prop_assert_eq!(crc32c_append(init, &data), crc32c::crc32c_append(init, &data));
}
#[test]
fn moving_a_crc_past_bytes_matches_one_pass(a in proptest::collection::vec(any::<u8>(), 0..300), b in proptest::collection::vec(any::<u8>(), 0..3000)) {
let mut joined = a.clone();
joined.extend_from_slice(&b);
prop_assert_eq!(mul_mod(x8n(b.len() as u64), crc32c(&a)) ^ crc32c(&b), crc32c(&joined));
}
#[test]
fn parts_match_one_pass(init in any::<u32>(), data in proptest::collection::vec(any::<u8>(), 0..5000), cuts in any::<(usize, usize)>()) {
let head = cuts.0 % (data.len() + 1);
let words = (data.len() - head) / 8;
let n = cuts.1 % (words + 1);
let atoms = atomics(&data[head..head + 8 * n]);
let parts = crc32c_append_parts(init, &data[..head], SharedWords::new(&atoms), &data[head + 8 * n..]);
prop_assert_eq!(parts, crc32c::crc32c_append(init, &data));
}
#[test]
fn appending_in_pieces_matches_one_pass(data in proptest::collection::vec(any::<u8>(), 0..5000), cut in any::<usize>()) {
let cut = cut % (data.len() + 1);
prop_assert_eq!(crc32c_append(crc32c(&data[..cut]), &data[cut..]), crc32c(&data));
}
}
}