1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
macro_rules! imp_update {
($ty: ty) => {
#[inline]
const fn update(poly: $ty, reflect: bool, mut value: $ty) -> $ty {
const BITS: usize = ::core::mem::size_of::<$ty>() * 8;
if reflect {
let mut i = 0;
while i < 8 {
value = (value >> 1) ^ ((value & 1) * poly);
i += 1;
}
} else {
value <<= (BITS - 8);
let mut i = 0;
while i < 8 {
value = (value << 1) ^ (((value >> (BITS - 1)) & 1) * poly);
i += 1;
}
}
value
}
};
}
macro_rules! imp_update_no_lut {
($ty: ty) => {
/// Process `bytes` without using a lookup table.
/// It not require any additional memory but it is very slow.
///
/// The function process the single byte at time.
#[inline]
pub const fn update_no_lut(
mut crc: $ty, width: u8, poly: $ty, reflect: bool, bytes: &[u8],
) -> $ty {
const BITS: usize = ::core::mem::size_of::<$ty>() * 8;
const SHIFT: usize = if BITS > 8 { 8 } else { 0 };
let poly = if reflect {
let poly = poly.reverse_bits();
poly >> (BITS - width as usize)
} else {
poly << (BITS - width as usize)
};
if BITS > 8 {
if reflect {
let mut i = 0;
while i < bytes.len() {
let value = (crc ^ bytes[i] as $ty) & 0xFF;
crc = update(poly, reflect, value) ^ (crc >> SHIFT);
i += 1;
}
} else {
let mut i = 0;
while i < bytes.len() {
let value = ((crc >> (BITS - 8)) ^ bytes[i] as $ty) & 0xFF;
crc = update(poly, reflect, value) ^ (crc << SHIFT);
i += 1;
}
}
} else {
let mut i = 0;
while i < bytes.len() {
crc = update(poly, reflect, crc ^ bytes[i] as $ty);
i += 1;
}
}
crc
}
};
}
macro_rules! imp_update_lut_32 {
($ty: ty) => {
/// Process `bytes` using a lookup table with 32 entries.
///
/// The function process the single byte at time.
#[inline]
pub const fn update_lut_32(
mut crc: $ty, bytes: &[u8], lut: &[$ty; 32], reflect: bool,
) -> $ty {
const BITS: usize = ::core::mem::size_of::<$ty>() * 8;
const SHIFT: usize = if BITS > 8 { 8 } else { 0 };
if BITS > 8 {
if reflect {
let mut i = 0;
while i < bytes.len() {
let index = ((crc & 0xFF) ^ (bytes[i] as $ty)) as usize;
crc = lut[index & 0xF] ^ lut[16 + ((index >> 4) & 0xF)] ^ (crc >> SHIFT);
i += 1;
}
} else {
let mut i = 0;
while i < bytes.len() {
let index = (((crc >> (BITS - 8)) & 0xFF) ^ (bytes[i] as $ty)) as usize;
crc = lut[index & 0xF] ^ lut[16 + ((index >> 4) & 0xF)] ^ (crc << SHIFT);
i += 1;
}
}
} else {
let mut i = 0;
while i < bytes.len() {
let index = ((crc & 0xFF) ^ (bytes[i] as $ty)) as usize;
crc = lut[index & 0xF] ^ lut[16 + ((index >> 4) & 0xF)];
i += 1;
}
}
crc
}
};
}
macro_rules! imp_update_lut_256 {
($ty: ty) => {
/// Process `bytes` using a lookup table with 256 entries.
///
/// The function process the single byte at time.
#[inline]
pub const fn update_lut_256(
mut crc: $ty, bytes: &[u8], lut: &[$ty; 256], reflect: bool,
) -> $ty {
const BITS: usize = ::core::mem::size_of::<$ty>() * 8;
const SHIFT: usize = if BITS > 8 { 8 } else { 0 };
if BITS > 8 {
if reflect {
let mut i = 0;
while i < bytes.len() {
let index = ((crc ^ bytes[i] as $ty) & 0xFF) as usize;
crc = lut[index] ^ (crc >> SHIFT);
i += 1;
}
} else {
let mut i = 0;
while i < bytes.len() {
let index = (((crc >> (BITS - 8)) ^ bytes[i] as $ty) & 0xFF) as usize;
crc = lut[index] ^ (crc << SHIFT);
i += 1;
}
}
} else {
let mut i = 0;
while i < bytes.len() {
let index = ((crc ^ bytes[i] as $ty) & 0xFF) as usize;
crc = lut[index];
i += 1;
}
}
crc
}
};
}
macro_rules! imp_update_lut_256x_n {
($ty: ty) => {
/// Process `bytes` using a lookup table with `256xSLICES` entries.
///
/// The function use a slicing technique to process multiple bytes in single step.
///
/// NOTE: The `SLICES` must be multiple of two less or equal to 32. The constraint is validated at compile time.
pub fn update_lut_256x_n<const SLICES: usize>(
mut crc: $ty, mut bytes: &[u8], lut: &[[$ty; 256]; SLICES], reflect: bool,
) -> $ty {
$crate::internals::cg_assert::assert_lt_eq::<SLICES, { $crate::internals::MAX_SLICES }>(
);
$crate::internals::cg_assert::assert_power_of_two::<SLICES>();
if SLICES >= 32 {
(crc, bytes) = update_slice_by_32(crc, bytes, lut, reflect);
}
if SLICES >= 16 {
(crc, bytes) = update_slice_by_16(crc, bytes, lut, reflect);
}
if SLICES >= 8 {
(crc, bytes) = update_slice_by_8(crc, bytes, lut, reflect);
}
if SLICES >= 4 {
(crc, bytes) = update_slice_by_4(crc, bytes, lut, reflect);
}
update_lut_256(crc, bytes, &lut[0], reflect)
}
};
}