| 1 |
//! The crc32 checksum algorithm. |
| 2 |
|
| 3 |
use crate::CRC32_INITIAL_VALUE; |
| 4 |
|
| 5 |
#[cfg(target_arch = "aarch64")] |
| 6 |
pub(crate) mod acle; |
| 7 |
mod braid; |
| 8 |
mod combine; |
| 9 |
#[cfg(target_arch = "loongarch64")] |
| 10 |
mod loongarch; |
| 11 |
#[cfg(target_arch = "x86_64")] |
| 12 |
mod pclmulqdq; |
| 13 |
#[cfg(target_arch = "x86_64")] |
| 14 |
#[cfg(feature = "vpclmulqdq")] |
| 15 |
mod vpclmulqdq; |
| 16 |
|
| 17 |
pub use combine::{crc32_combine, crc32_combine_gen, crc32_combine_op}; |
| 18 |
|
| 19 |
pub fn crc32(start: u32, buf: &[u8]) -> u32 { |
| 20 |
/* For lens < 64, crc32_braid method is faster. The CRC32 instruction for |
| 21 |
* these short lengths might also prove to be effective */ |
| 22 |
if buf.len() < 64 { |
| 23 |
return crc32_braid(start, buf); |
| 24 |
} |
| 25 |
|
| 26 |
let mut crc_state = Crc32Fold::new_with_initial(start); |
| 27 |
crc_state.fold(buf, start); |
| 28 |
crc_state.finish() |
| 29 |
} |
| 30 |
|
| 31 |
fn crc32_braid(start: u32, buf: &[u8]) -> u32 { |
| 32 |
braid::crc32_braid::<5>(start, buf) |
| 33 |
} |
| 34 |
|
| 35 |
pub fn get_crc_table() -> &'static [u32; 256] { |
| 36 |
braid::get_crc_table() |
| 37 |
} |
| 38 |
|
| 39 |
#[cfg(feature = "__internal-test")] |
| 40 |
#[derive(Debug, Clone, Copy)] |
| 41 |
pub struct Crc32Fold { |
| 42 |
#[cfg(target_arch = "x86_64")] |
| 43 |
fold: pclmulqdq::Accumulator, |
| 44 |
value: u32, |
| 45 |
} |
| 46 |
|
| 47 |
#[cfg(not(feature = "__internal-test"))] |
| 48 |
#[derive(Debug, Clone, Copy)] |
| 49 |
pub(crate) struct Crc32Fold { |
| 50 |
#[cfg(target_arch = "x86_64")] |
| 51 |
fold: pclmulqdq::Accumulator, |
| 52 |
value: u32, |
| 53 |
} |
| 54 |
|
| 55 |
impl Default for Crc32Fold { |
| 56 |
fn default() -> Self { |
| 57 |
Self::new() |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
impl Crc32Fold { |
| 62 |
pub const fn new() -> Self { |
| 63 |
Self::new_with_initial(CRC32_INITIAL_VALUE) |
| 64 |
} |
| 65 |
|
| 66 |
pub const fn new_with_initial(initial: u32) -> Self { |
| 67 |
Self { |
| 68 |
#[cfg(target_arch = "x86_64")] |
| 69 |
fold: pclmulqdq::Accumulator::new(), |
| 70 |
value: initial, |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
pub fn fold(&mut self, src: &[u8], _start: u32) { |
| 75 |
crate::cfg_select! { |
| 76 |
target_arch = "x86_64" => { |
| 77 |
if crate::cpu_features::is_enabled_pclmulqdq() { |
| 78 |
return unsafe { self.fold.fold(src, _start) }; |
| 79 |
} |
| 80 |
} |
| 81 |
target_arch = "aarch64" => { |
| 82 |
if crate::cpu_features::is_enabled_crc() { |
| 83 |
self.value = unsafe { self::acle::crc32_acle_aarch64(self.value, src) }; |
| 84 |
return; |
| 85 |
} |
| 86 |
} |
| 87 |
target_arch = "loongarch64" => { |
| 88 |
// On loongarch64 no target feature is needed for crc instructions. |
| 89 |
if true { |
| 90 |
// NOTE: the start value is ignored. |
| 91 |
self.value = self::loongarch::crc32_loongarch64(self.value, src); |
| 92 |
return; |
| 93 |
} |
| 94 |
} |
| 95 |
_ => { /* fall through */ } |
| 96 |
} |
| 97 |
|
| 98 |
// NOTE: the start value is ignored. |
| 99 |
self.value = braid::crc32_braid::<5>(self.value, src); |
| 100 |
} |
| 101 |
|
| 102 |
pub fn fold_copy(&mut self, dst: &mut [u8], src: &[u8]) { |
| 103 |
#[cfg(target_arch = "x86_64")] |
| 104 |
if crate::cpu_features::is_enabled_pclmulqdq() { |
| 105 |
return unsafe { self.fold.fold_copy(dst, src) }; |
| 106 |
} |
| 107 |
|
| 108 |
self.fold(src, 0); |
| 109 |
dst[..src.len()].copy_from_slice(src); |
| 110 |
} |
| 111 |
|
| 112 |
pub fn finish(self) -> u32 { |
| 113 |
#[cfg(target_arch = "x86_64")] |
| 114 |
if crate::cpu_features::is_enabled_pclmulqdq() { |
| 115 |
return unsafe { self.fold.finish() }; |
| 116 |
} |
| 117 |
|
| 118 |
self.value |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
#[cfg(test)] |
| 123 |
mod test { |
| 124 |
use braid::crc32_braid; |
| 125 |
|
| 126 |
use super::*; |
| 127 |
|
| 128 |
const INPUT: [u8; 1024] = { |
| 129 |
let mut array = [0; 1024]; |
| 130 |
let mut i = 0; |
| 131 |
while i < array.len() { |
| 132 |
array[i] = i as u8; |
| 133 |
i += 1; |
| 134 |
} |
| 135 |
|
| 136 |
array |
| 137 |
}; |
| 138 |
|
| 139 |
#[test] |
| 140 |
fn test_crc32_fold() { |
| 141 |
// input large enough to trigger the SIMD |
| 142 |
let mut h = crc32fast::Hasher::new_with_initial(CRC32_INITIAL_VALUE); |
| 143 |
h.update(&INPUT); |
| 144 |
assert_eq!(crc32(CRC32_INITIAL_VALUE, &INPUT), h.finalize()); |
| 145 |
} |
| 146 |
|
| 147 |
#[test] |
| 148 |
fn test_crc32_fold_align() { |
| 149 |
// SIMD algorithm is sensitive to alignment; |
| 150 |
for i in 0..16 { |
| 151 |
for start in [CRC32_INITIAL_VALUE, 42] { |
| 152 |
let mut h = crc32fast::Hasher::new_with_initial(start); |
| 153 |
h.update(&INPUT[i..]); |
| 154 |
assert_eq!( |
| 155 |
crc32(start, &INPUT[i..]), |
| 156 |
h.finalize(), |
| 157 |
"offset = {i}, start = {start}" |
| 158 |
); |
| 159 |
} |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
quickcheck::quickcheck! { |
| 164 |
fn crc_fold_is_crc32fast(v: Vec<u8>, start: u32) -> bool { |
| 165 |
let mut h = crc32fast::Hasher::new_with_initial(start); |
| 166 |
h.update(&v); |
| 167 |
|
| 168 |
let a = crc32(start, &v) ; |
| 169 |
let b = h.finalize(); |
| 170 |
|
| 171 |
a == b |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
#[test] |
| 176 |
fn chunked() { |
| 177 |
const INPUT: &[&[u8]] = &[ |
| 178 |
&[116], |
| 179 |
&[111, 107, 105, 111, 44, 32, 97, 115], |
| 180 |
&[121, 110, 99, 45, 115, 116, 100, 44], |
| 181 |
&[32, 97, 110, 100, 32, 115, 109, 111], |
| 182 |
&[108, 46, 32, 89, 111, 117, 226, 128], |
| 183 |
&[153, 118, 101, 32, 112, 114, 111, 98], |
| 184 |
&[97, 98, 108, 121, 32, 117, 115, 101], |
| 185 |
&[100, 32, 116, 104, 101, 109, 32, 97], |
| 186 |
&[116, 32, 115, 111, 109, 101, 32, 112], |
| 187 |
&[111, 105, 110, 116, 44, 32, 101, 105], |
| 188 |
&[116, 104, 101, 114, 32, 100, 105, 114], |
| 189 |
&[101, 99, 116, 108, 121, 32, 111, 114], |
| 190 |
&[0], |
| 191 |
]; |
| 192 |
|
| 193 |
const START: u32 = 2380683574; |
| 194 |
|
| 195 |
let mut in_chunks = START; |
| 196 |
for chunk in INPUT { |
| 197 |
in_chunks = crc32(in_chunks, chunk); |
| 198 |
} |
| 199 |
|
| 200 |
let flattened: Vec<_> = INPUT.iter().copied().flatten().copied().collect(); |
| 201 |
let flat = crc32(START, &flattened); |
| 202 |
|
| 203 |
assert_eq!(in_chunks, flat); |
| 204 |
} |
| 205 |
|
| 206 |
#[test] |
| 207 |
fn nasty_alignment() { |
| 208 |
const START: u32 = 2380683574; |
| 209 |
|
| 210 |
const FLAT: &[u8] = &[ |
| 211 |
116, 111, 107, 105, 111, 44, 32, 97, 115, 121, 110, 99, 45, 115, 116, 100, 44, 32, 97, |
| 212 |
110, 100, 32, 115, 109, 111, 108, 46, 32, 89, 111, 117, 226, 128, 153, 118, 101, 32, |
| 213 |
112, 114, 111, 98, 97, 98, 108, 121, 32, 117, 115, 101, 100, 32, 116, 104, 101, 109, |
| 214 |
32, 97, 116, 32, 115, 111, 109, 101, 32, 112, 111, 105, 110, 116, 44, 32, 101, 105, |
| 215 |
116, 104, 101, 114, 32, 100, 105, 114, 101, 99, 116, 108, 121, 32, 111, 114, 0, |
| 216 |
]; |
| 217 |
|
| 218 |
let mut i = 0; |
| 219 |
let mut flat = FLAT.to_vec(); |
| 220 |
while flat[i..].as_ptr() as usize % 16 != 15 { |
| 221 |
flat.insert(0, 0); |
| 222 |
i += 1; |
| 223 |
} |
| 224 |
|
| 225 |
let flat = &flat[i..]; |
| 226 |
|
| 227 |
assert_eq!(crc32_braid::<5>(START, flat), crc32(START, flat)); |
| 228 |
assert_eq!(crc32(2380683574, flat), 1175758345); |
| 229 |
} |
| 230 |
|
| 231 |
#[test] |
| 232 |
#[cfg_attr( |
| 233 |
not(target_arch = "x86_64"), |
| 234 |
ignore = "pclmulqdq is only used on x86_64" |
| 235 |
)] |
| 236 |
#[cfg_attr(not(miri), should_panic)] |
| 237 |
#[cfg_attr(miri, cfg_attr(target_feature = "pclmulqdq", should_panic))] |
| 238 |
fn crc32_fold_oob() { |
| 239 |
#[cfg(target_arch = "x86_64")] |
| 240 |
if !crate::cpu_features::is_enabled_pclmulqdq() { |
| 241 |
return; |
| 242 |
} |
| 243 |
|
| 244 |
// At least 31 bytes, first clause of assert! in pclmulqdq.rs:252 |
| 245 |
let buf = [0u8; 31 + 16]; |
| 246 |
|
| 247 |
// Force the buffer to be unaligned, such that align_to produces a 1 byte prefix, so that |
| 248 |
// we hit `before.len() < 4` of pclmulqdq.rs:287 and load an extra m128. |
| 249 |
let base = buf.as_ptr() as usize; |
| 250 |
let skip = (15usize.wrapping_sub(base % 16)) & 0xF; |
| 251 |
let buf = &buf[skip..][..31]; |
| 252 |
|
| 253 |
// Set initial nonzero, second part of the assert!() in pclmulqdq.rs:252. |
| 254 |
let mut fold = Crc32Fold::new_with_initial(1); |
| 255 |
fold.fold(buf, 1); |
| 256 |
let _ = fold.finish(); |
| 257 |
} |
| 258 |
} |