| 1 |
use core::arch::aarch64 as arch; |
| 2 |
|
| 3 |
#[derive(Clone)] |
| 4 |
pub struct State { |
| 5 |
state: u32, |
| 6 |
} |
| 7 |
|
| 8 |
impl State { |
| 9 |
#[cfg(not(feature = "std"))] |
| 10 |
pub fn new(state: u32) -> Option<Self> { |
| 11 |
if cfg!(target_feature = "crc") { |
| 12 |
// SAFETY: The conditions above ensure that all |
| 13 |
// required instructions are supported by the CPU. |
| 14 |
Some(Self { state }) |
| 15 |
} else { |
| 16 |
None |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
#[cfg(feature = "std")] |
| 21 |
pub fn new(state: u32) -> Option<Self> { |
| 22 |
if std::arch::is_aarch64_feature_detected!("crc") { |
| 23 |
// SAFETY: The condition above ensures that all |
| 24 |
// required instructions are supported by the CPU. |
| 25 |
Some(Self { state }) |
| 26 |
} else { |
| 27 |
None |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
pub fn update(&mut self, buf: &[u8]) { |
| 32 |
// SAFETY: The `State::new` constructor ensures that all |
| 33 |
// required instructions are supported by the CPU. |
| 34 |
self.state = unsafe { calculate(self.state, buf) } |
| 35 |
} |
| 36 |
|
| 37 |
pub fn finalize(self) -> u32 { |
| 38 |
self.state |
| 39 |
} |
| 40 |
|
| 41 |
pub fn reset(&mut self) { |
| 42 |
self.state = 0; |
| 43 |
} |
| 44 |
|
| 45 |
pub fn combine(&mut self, other: u32, amount: u64) { |
| 46 |
self.state = crate::combine::combine(self.state, other, amount); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// Conservative crossover: 64 words per stream gives the interleaved loop enough work to |
| 51 |
// amortize its two `combine` calls (3 * 64 * 8 bytes = 1.5 KiB). |
| 52 |
const MIN_TRIPLE_QUADS: usize = 192; |
| 53 |
|
| 54 |
// target_feature is necessary to allow rustc to inline the crc32* wrappers |
| 55 |
#[target_feature(enable = "crc")] |
| 56 |
pub unsafe fn calculate(crc: u32, data: &[u8]) -> u32 { |
| 57 |
let (pre_quad, quads, post_quad) = data.align_to::<u64>(); |
| 58 |
|
| 59 |
// Running (finalized) CRC. |
| 60 |
let mut r = crc; |
| 61 |
|
| 62 |
// Unaligned leading bytes. |
| 63 |
r = crc_bytes(r, pre_quad); |
| 64 |
|
| 65 |
if quads.len() >= MIN_TRIPLE_QUADS { |
| 66 |
// A single `crc32d` chain is latency-bound: each instruction depends on the previous |
| 67 |
// result. Splitting the aligned region into three equal, contiguous blocks and advancing |
| 68 |
// three independent accumulators in lockstep keeps several `crc32d` in flight, then |
| 69 |
// `combine` reassembles them (appending the zero-seeded later blocks to the first). |
| 70 |
let n3 = quads.len() / 3; |
| 71 |
let block_len = (n3 * 8) as u64; |
| 72 |
|
| 73 |
let a = &quads[0..n3]; |
| 74 |
let b = &quads[n3..2 * n3]; |
| 75 |
let c = &quads[2 * n3..3 * n3]; |
| 76 |
|
| 77 |
// Internal (bit-inverted) states. A carries the running CRC; B and C start from zero. |
| 78 |
let mut c0 = !r; |
| 79 |
let mut c1 = !0u32; |
| 80 |
let mut c2 = !0u32; |
| 81 |
|
| 82 |
// Unroll each stream by 4 while keeping the three chains independent. |
| 83 |
let mut chunks_a = a.chunks_exact(4); |
| 84 |
let mut chunks_b = b.chunks_exact(4); |
| 85 |
let mut chunks_c = c.chunks_exact(4); |
| 86 |
for ((qa, qb), qc) in (&mut chunks_a).zip(&mut chunks_b).zip(&mut chunks_c) { |
| 87 |
for k in 0..4 { |
| 88 |
c0 = arch::__crc32d(c0, qa[k]); |
| 89 |
c1 = arch::__crc32d(c1, qb[k]); |
| 90 |
c2 = arch::__crc32d(c2, qc[k]); |
| 91 |
} |
| 92 |
} |
| 93 |
for ((&qa, &qb), &qc) in chunks_a |
| 94 |
.remainder() |
| 95 |
.iter() |
| 96 |
.zip(chunks_b.remainder()) |
| 97 |
.zip(chunks_c.remainder()) |
| 98 |
{ |
| 99 |
c0 = arch::__crc32d(c0, qa); |
| 100 |
c1 = arch::__crc32d(c1, qb); |
| 101 |
c2 = arch::__crc32d(c2, qc); |
| 102 |
} |
| 103 |
|
| 104 |
r = crate::combine::combine(!c0, !c1, block_len); |
| 105 |
r = crate::combine::combine(r, !c2, block_len); |
| 106 |
|
| 107 |
// Words left over after the three equal blocks. |
| 108 |
r = crc_quads(r, &quads[3 * n3..]); |
| 109 |
} else { |
| 110 |
r = crc_quads(r, quads); |
| 111 |
} |
| 112 |
|
| 113 |
// Unaligned trailing bytes. |
| 114 |
crc_bytes(r, post_quad) |
| 115 |
} |
| 116 |
|
| 117 |
#[target_feature(enable = "crc")] |
| 118 |
unsafe fn crc_quads(crc: u32, quads: &[u64]) -> u32 { |
| 119 |
let mut c = !crc; |
| 120 |
|
| 121 |
// unrolling increases performance by a lot |
| 122 |
let mut quad_iter = quads.chunks_exact(8); |
| 123 |
for chunk in &mut quad_iter { |
| 124 |
c = arch::__crc32d(c, chunk[0]); |
| 125 |
c = arch::__crc32d(c, chunk[1]); |
| 126 |
c = arch::__crc32d(c, chunk[2]); |
| 127 |
c = arch::__crc32d(c, chunk[3]); |
| 128 |
c = arch::__crc32d(c, chunk[4]); |
| 129 |
c = arch::__crc32d(c, chunk[5]); |
| 130 |
c = arch::__crc32d(c, chunk[6]); |
| 131 |
c = arch::__crc32d(c, chunk[7]); |
| 132 |
} |
| 133 |
c = quad_iter |
| 134 |
.remainder() |
| 135 |
.iter() |
| 136 |
.fold(c, |acc, &q| arch::__crc32d(acc, q)); |
| 137 |
|
| 138 |
!c |
| 139 |
} |
| 140 |
|
| 141 |
#[target_feature(enable = "crc")] |
| 142 |
unsafe fn crc_bytes(crc: u32, bytes: &[u8]) -> u32 { |
| 143 |
let c = bytes.iter().fold(!crc, |acc, &b| arch::__crc32b(acc, b)); |
| 144 |
!c |
| 145 |
} |
| 146 |
|
| 147 |
#[cfg(test)] |
| 148 |
mod test { |
| 149 |
quickcheck::quickcheck! { |
| 150 |
fn check_against_baseline(init: u32, chunks: Vec<(Vec<u8>, usize)>) -> bool { |
| 151 |
let mut baseline = super::super::super::baseline::State::new(init); |
| 152 |
let mut aarch64 = super::State::new(init).expect("not supported"); |
| 153 |
for (chunk, mut offset) in chunks { |
| 154 |
// simulate random alignments by offsetting the slice by up to 15 bytes |
| 155 |
offset &= 0xF; |
| 156 |
if chunk.len() <= offset { |
| 157 |
baseline.update(&chunk); |
| 158 |
aarch64.update(&chunk); |
| 159 |
} else { |
| 160 |
baseline.update(&chunk[offset..]); |
| 161 |
aarch64.update(&chunk[offset..]); |
| 162 |
} |
| 163 |
} |
| 164 |
aarch64.finalize() == baseline.finalize() |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
// Exercises the 3-way interleaved path across the sizes and alignments where it switches. |
| 169 |
#[test] |
| 170 |
fn check_large_inputs_against_baseline() { |
| 171 |
let mut data = vec![0u8; 8200]; |
| 172 |
let mut s: u32 = 0x1234_5678; |
| 173 |
for byte in data.iter_mut() { |
| 174 |
s = s.wrapping_mul(1_664_525).wrapping_add(1_013_904_223); |
| 175 |
*byte = (s >> 24) as u8; |
| 176 |
} |
| 177 |
|
| 178 |
for &len in &[ |
| 179 |
0usize, 1, 7, 8, 15, 16, 63, 64, 127, 128, 1535, 1536, 1537, 4096, 8192, 8199, |
| 180 |
] { |
| 181 |
for &offset in &[0usize, 1, 3, 7, 8, 15] { |
| 182 |
if offset + len > data.len() { |
| 183 |
continue; |
| 184 |
} |
| 185 |
let slice = &data[offset..offset + len]; |
| 186 |
let mut baseline = super::super::super::baseline::State::new(0); |
| 187 |
baseline.update(slice); |
| 188 |
let mut specialized = super::State::new(0).expect("not supported"); |
| 189 |
specialized.update(slice); |
| 190 |
assert_eq!( |
| 191 |
specialized.finalize(), |
| 192 |
baseline.finalize(), |
| 193 |
"mismatch for len={len} offset={offset}", |
| 194 |
); |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |