/rust/registry/src/index.crates.io-6f17d22bba15001f/crc32fast-1.4.2/src/baseline.rs
Line | Count | Source (jump to first uncovered line) |
1 | | use table::CRC32_TABLE; |
2 | | |
3 | | #[derive(Clone)] |
4 | | pub struct State { |
5 | | state: u32, |
6 | | } |
7 | | |
8 | | impl State { |
9 | 0 | pub fn new(state: u32) -> Self { |
10 | 0 | State { state } |
11 | 0 | } |
12 | | |
13 | 0 | pub fn update(&mut self, buf: &[u8]) { |
14 | 0 | self.state = update_fast_16(self.state, buf); |
15 | 0 | } |
16 | | |
17 | 0 | pub fn finalize(self) -> u32 { |
18 | 0 | self.state |
19 | 0 | } |
20 | | |
21 | 0 | pub fn reset(&mut self) { |
22 | 0 | self.state = 0; |
23 | 0 | } |
24 | | |
25 | 0 | pub fn combine(&mut self, other: u32, amount: u64) { |
26 | 0 | self.state = ::combine::combine(self.state, other, amount); |
27 | 0 | } |
28 | | } |
29 | | |
30 | 297k | pub(crate) fn update_fast_16(prev: u32, mut buf: &[u8]) -> u32 { |
31 | | const UNROLL: usize = 4; |
32 | | const BYTES_AT_ONCE: usize = 16 * UNROLL; |
33 | | |
34 | 297k | let mut crc = !prev; |
35 | | |
36 | 298k | while buf.len() >= BYTES_AT_ONCE { |
37 | 3.89k | for _ in 0..UNROLL { |
38 | 3.11k | crc = CRC32_TABLE[0x0][buf[0xf] as usize] |
39 | 3.11k | ^ CRC32_TABLE[0x1][buf[0xe] as usize] |
40 | 3.11k | ^ CRC32_TABLE[0x2][buf[0xd] as usize] |
41 | 3.11k | ^ CRC32_TABLE[0x3][buf[0xc] as usize] |
42 | 3.11k | ^ CRC32_TABLE[0x4][buf[0xb] as usize] |
43 | 3.11k | ^ CRC32_TABLE[0x5][buf[0xa] as usize] |
44 | 3.11k | ^ CRC32_TABLE[0x6][buf[0x9] as usize] |
45 | 3.11k | ^ CRC32_TABLE[0x7][buf[0x8] as usize] |
46 | 3.11k | ^ CRC32_TABLE[0x8][buf[0x7] as usize] |
47 | 3.11k | ^ CRC32_TABLE[0x9][buf[0x6] as usize] |
48 | 3.11k | ^ CRC32_TABLE[0xa][buf[0x5] as usize] |
49 | 3.11k | ^ CRC32_TABLE[0xb][buf[0x4] as usize] |
50 | 3.11k | ^ CRC32_TABLE[0xc][buf[0x3] as usize ^ ((crc >> 0x18) & 0xFF) as usize] |
51 | 3.11k | ^ CRC32_TABLE[0xd][buf[0x2] as usize ^ ((crc >> 0x10) & 0xFF) as usize] |
52 | 3.11k | ^ CRC32_TABLE[0xe][buf[0x1] as usize ^ ((crc >> 0x08) & 0xFF) as usize] |
53 | 3.11k | ^ CRC32_TABLE[0xf][buf[0x0] as usize ^ ((crc >> 0x00) & 0xFF) as usize]; |
54 | 3.11k | buf = &buf[16..]; |
55 | 3.11k | } |
56 | | } |
57 | | |
58 | 297k | update_slow(!crc, buf) |
59 | 297k | } |
60 | | |
61 | 297k | pub(crate) fn update_slow(prev: u32, buf: &[u8]) -> u32 { |
62 | 297k | let mut crc = !prev; |
63 | | |
64 | 1.31M | for &byte in buf.iter() { |
65 | 1.31M | crc = CRC32_TABLE[0][((crc as u8) ^ byte) as usize] ^ (crc >> 8); |
66 | 1.31M | } |
67 | | |
68 | 297k | !crc |
69 | 297k | } |
70 | | |
71 | | #[cfg(test)] |
72 | | mod test { |
73 | | #[test] |
74 | | fn slow() { |
75 | | assert_eq!(super::update_slow(0, b""), 0); |
76 | | |
77 | | // test vectors from the iPXE project (input and output are bitwise negated) |
78 | | assert_eq!(super::update_slow(!0x12345678, b""), !0x12345678); |
79 | | assert_eq!(super::update_slow(!0xffffffff, b"hello world"), !0xf2b5ee7a); |
80 | | assert_eq!(super::update_slow(!0xffffffff, b"hello"), !0xc9ef5979); |
81 | | assert_eq!(super::update_slow(!0xc9ef5979, b" world"), !0xf2b5ee7a); |
82 | | |
83 | | // Some vectors found on Rosetta code |
84 | | assert_eq!(super::update_slow(0, b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"), 0x190A55AD); |
85 | | assert_eq!(super::update_slow(0, b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF"), 0xFF6CAB0B); |
86 | | assert_eq!(super::update_slow(0, b"\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F"), 0x91267E8A); |
87 | | } |
88 | | |
89 | | quickcheck! { |
90 | | fn fast_16_is_the_same_as_slow(crc: u32, bytes: Vec<u8>) -> bool { |
91 | | super::update_fast_16(crc, &bytes) == super::update_slow(crc, &bytes) |
92 | | } |
93 | | } |
94 | | } |