| 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 conditions above ensure 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 = ::combine::combine(self.state, other, amount); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
// target_feature is necessary to allow rustc to inline the crc32* wrappers |
| 51 |
#[target_feature(enable = "crc")] |
| 52 |
pub unsafe fn calculate(crc: u32, data: &[u8]) -> u32 { |
| 53 |
let mut c32 = !crc; |
| 54 |
let (pre_quad, quads, post_quad) = data.align_to::<u64>(); |
| 55 |
|
| 56 |
c32 = pre_quad.iter().fold(c32, |acc, &b| arch::__crc32b(acc, b)); |
| 57 |
|
| 58 |
// unrolling increases performance by a lot |
| 59 |
let mut quad_iter = quads.chunks_exact(8); |
| 60 |
for chunk in &mut quad_iter { |
| 61 |
c32 = arch::__crc32d(c32, chunk[0]); |
| 62 |
c32 = arch::__crc32d(c32, chunk[1]); |
| 63 |
c32 = arch::__crc32d(c32, chunk[2]); |
| 64 |
c32 = arch::__crc32d(c32, chunk[3]); |
| 65 |
c32 = arch::__crc32d(c32, chunk[4]); |
| 66 |
c32 = arch::__crc32d(c32, chunk[5]); |
| 67 |
c32 = arch::__crc32d(c32, chunk[6]); |
| 68 |
c32 = arch::__crc32d(c32, chunk[7]); |
| 69 |
} |
| 70 |
c32 = quad_iter |
| 71 |
.remainder() |
| 72 |
.iter() |
| 73 |
.fold(c32, |acc, &q| arch::__crc32d(acc, q)); |
| 74 |
|
| 75 |
c32 = post_quad.iter().fold(c32, |acc, &b| arch::__crc32b(acc, b)); |
| 76 |
|
| 77 |
!c32 |
| 78 |
} |
| 79 |
|
| 80 |
#[cfg(test)] |
| 81 |
mod test { |
| 82 |
quickcheck! { |
| 83 |
fn check_against_baseline(init: u32, chunks: Vec<(Vec<u8>, usize)>) -> bool { |
| 84 |
let mut baseline = super::super::super::baseline::State::new(init); |
| 85 |
let mut aarch64 = super::State::new(init).expect("not supported"); |
| 86 |
for (chunk, mut offset) in chunks { |
| 87 |
// simulate random alignments by offsetting the slice by up to 15 bytes |
| 88 |
offset &= 0xF; |
| 89 |
if chunk.len() <= offset { |
| 90 |
baseline.update(&chunk); |
| 91 |
aarch64.update(&chunk); |
| 92 |
} else { |
| 93 |
baseline.update(&chunk[offset..]); |
| 94 |
aarch64.update(&chunk[offset..]); |
| 95 |
} |
| 96 |
} |
| 97 |
aarch64.finalize() == baseline.finalize() |
| 98 |
} |
| 99 |
} |
| 100 |
} |