Debian Codegraph @

debian/rust-vendor/crc-fast/src/crc32/fusion/x86/mod.rs

raw
1 //! Provides CRC-32/ISCSI calculations using a fusion of native CLMUL
2 //! instructions and native CRC calculation instructions on x86 / x86_64.
3 //!
4 //! https://www.corsix.org/content/fast-crc32c-4k
5 //! https://www.corsix.org/content/alternative-exposition-crc32_4k_pclmulqdq
6 //!
7 //! Converted to Rust from the original C code generated by https://github.com/corsix/fast-crc32/
8 //! with the help of Claude.ai.
9 //!
10 //! Modified as necessary for this Rust implementation.
11 //!
12 //! MIT licensed.
13
14 #![cfg(any(target_arch = "x86", target_arch = "x86_64"))]
15
16 mod iscsi;
17
18 use iscsi::sse_pclmulqdq::crc32_iscsi_sse_v4s3x3;
19
20 #[cfg(target_arch = "x86")]
21 use core::arch::x86::*;
22 #[cfg(target_arch = "x86_64")]
23 use core::arch::x86_64::*;
24 #[cfg(target_arch = "x86_64")]
25 #[rustversion::since(1.89)]
26 use iscsi::avx512_pclmulqdq::crc32_iscsi_avx512_v4s3x3;
27 #[cfg(target_arch = "x86_64")]
28 #[rustversion::since(1.89)]
29 use iscsi::avx512_vpclmulqdq::crc32_iscsi_avx512_vpclmulqdq_v3x2;
30
31 /// CRC32 iSCSI calculation for Rust versions before 1.89 (pre-AVX-512 support)
32 ///
33 ///
34 /// This function is called by the wrapper layer after feature detection has been performed.
35 /// For older Rust versions, only SSE implementation is available.
36 #[rustversion::before(1.89)]
37 #[inline(always)]
38 pub fn crc32_iscsi(crc: u32, data: &[u8]) -> u32 {
39 let data_len = data.len();
40
41 // SSE4.2 is required for native CRC32 instructions used in small buffer path
42 if data_len <= 256 && is_x86_feature_detected!("sse4.2") {
43 unsafe {
44 return crc32_iscsi_small_fast(crc, data);
45 }
46 }
47
48 // Only SSE implementation is available for Rust versions before 1.89
49 // Runtime feature detection is handled by the wrapper layer
50 unsafe { crc32_iscsi_sse_v4s3x3(crc, data.as_ptr(), data_len) }
51 }
52
53 /// CRC32 iSCSI calculation using the highest available instruction set after Rust 1.89
54 /// (post-AVX-512 support)
55 ///
56 /// This function is called by the wrapper layer after feature detection has been performed.
57 /// The wrapper layer ensures that only the appropriate implementation is called based on
58 /// cached feature detection results, removing runtime checks from the hot path.
59 #[rustversion::since(1.89)]
60 #[inline(always)]
61 pub fn crc32_iscsi(crc: u32, data: &[u8]) -> u32 {
62 let data_len = data.len();
63
64 // SSE4.2 is required for native CRC32 instructions used in small buffer path
65 if data_len <= 256 && is_x86_feature_detected!("sse4.2") {
66 unsafe {
67 return crc32_iscsi_small_fast(crc, data);
68 }
69 }
70
71 #[cfg(target_arch = "x86_64")]
72 {
73 // AVX512 + VPCLMULQDQ
74
75 if is_x86_feature_detected!("avx512vl") && is_x86_feature_detected!("vpclmulqdq") {
76 unsafe {
77 return crc32_iscsi_avx512_vpclmulqdq_v3x2(crc, data.as_ptr(), data_len);
78 }
79 }
80
81 // AVX512
82 if is_x86_feature_detected!("avx512vl") {
83 unsafe {
84 return crc32_iscsi_avx512_v4s3x3(crc, data.as_ptr(), data_len);
85 }
86 }
87 }
88
89 // Fallback to SSE implementation
90 unsafe { crc32_iscsi_sse_v4s3x3(crc, data.as_ptr(), data_len) }
91 }
92
93 #[rustversion::since(1.89)]
94 #[cfg(target_arch = "x86_64")]
95 #[inline]
96 #[target_feature(enable = "avx512vl,vpclmulqdq")]
97 unsafe fn clmul_lo_avx512_vpclmulqdq(a: __m512i, b: __m512i) -> __m512i {
98 _mm512_clmulepi64_epi128(a, b, 0)
99 }
100
101 #[rustversion::since(1.89)]
102 #[cfg(target_arch = "x86_64")]
103 #[inline]
104 #[target_feature(enable = "avx512vl,vpclmulqdq")]
105 unsafe fn clmul_hi_avx512_vpclmulqdq(a: __m512i, b: __m512i) -> __m512i {
106 _mm512_clmulepi64_epi128(a, b, 17)
107 }
108
109 #[inline]
110 #[target_feature(enable = "pclmulqdq")]
111 unsafe fn clmul_lo_sse(a: __m128i, b: __m128i) -> __m128i {
112 _mm_clmulepi64_si128(a, b, 0)
113 }
114
115 #[inline]
116 #[target_feature(enable = "pclmulqdq")]
117 unsafe fn clmul_hi_sse(a: __m128i, b: __m128i) -> __m128i {
118 _mm_clmulepi64_si128(a, b, 17)
119 }
120
121 #[inline]
122 #[target_feature(enable = "pclmulqdq")]
123 unsafe fn clmul_scalar_sse(a: u32, b: u32) -> __m128i {
124 _mm_clmulepi64_si128(_mm_cvtsi32_si128(a as i32), _mm_cvtsi32_si128(b as i32), 0)
125 }
126
127 // x^n mod P, in log(n) time
128 #[target_feature(enable = "sse4.2,pclmulqdq")]
129 unsafe fn xnmodp_iscsi_sse(mut n: u64) -> u32 {
130 let mut stack = !1u64;
131 let mut acc: u32;
132 let mut low: u32;
133
134 while n > 191 {
135 stack = (stack << 1) + (n & 1);
136 n = (n >> 1) - 16;
137 }
138 stack = !stack;
139 acc = 0x80000000u32 >> (n & 31);
140 n >>= 5;
141
142 while n > 0 {
143 // Use hardware CRC32C instruction
144 acc = _mm_crc32_u32(acc, 0);
145 n -= 1;
146 }
147
148 while {
149 low = (stack & 1) as u32;
150 stack >>= 1;
151 stack != 0
152 } {
153 let x = _mm_cvtsi32_si128(acc as i32);
154 let clmul_result = _mm_clmulepi64_si128(x, x, 0);
155 let y = mm_extract_epi64(clmul_result, 0);
156 acc = mm_crc32_u64(0, y << low);
157 }
158 acc
159 }
160
161 #[inline]
162 #[target_feature(enable = "pclmulqdq")]
163 unsafe fn crc_shift_iscsi_sse(crc: u32, nbytes: usize) -> __m128i {
164 clmul_scalar_sse(crc, xnmodp_iscsi_sse((nbytes * 8 - 33) as u64))
165 }
166
167 #[inline]
168 #[target_feature(enable = "sse4.1")]
169 unsafe fn mm_extract_epi64(val: __m128i, idx: i32) -> u64 {
170 #[cfg(target_arch = "x86_64")]
171 {
172 if idx == 0 {
173 _mm_cvtsi128_si64(val) as u64
174 } else {
175 _mm_cvtsi128_si64(_mm_srli_si128(val, 8)) as u64
176 }
177 }
178 #[cfg(target_arch = "x86")]
179 {
180 // On 32-bit x86, extract two 32-bit values and combine them
181 let shifted = if idx == 0 {
182 val
183 } else {
184 _mm_srli_si128(val, 8)
185 };
186 let low = _mm_cvtsi128_si32(shifted) as u32;
187 let high = _mm_cvtsi128_si32(_mm_srli_si128(shifted, 4)) as u32;
188 (low as u64) | ((high as u64) << 32)
189 }
190 }
191
192 #[inline]
193 #[target_feature(enable = "sse4.2")]
194 unsafe fn mm_crc32_u64(crc: u32, val: u64) -> u32 {
195 #[cfg(target_arch = "x86_64")]
196 {
197 _mm_crc32_u64(crc.into(), val) as u32
198 }
199 #[cfg(target_arch = "x86")]
200 {
201 // On 32-bit x86, process 64-bit value as two 32-bit CRC operations
202 let low = val as u32;
203 let high = (val >> 32) as u32;
204 let crc = _mm_crc32_u32(crc, low);
205 _mm_crc32_u32(crc, high)
206 }
207 }
208
209 /// CRC-32/ISCSI calculation for small buffers (<= 256 bytes) using unrolled native CRC instructions
210 #[inline]
211 #[target_feature(enable = "sse4.2")]
212 pub unsafe fn crc32_iscsi_small_fast(mut crc: u32, data: &[u8]) -> u32 {
213 let (prefix, aligned, suffix) = data.align_to::<u64>();
214
215 for &byte in prefix {
216 crc = _mm_crc32_u8(crc, byte);
217 }
218
219 let mut chunks = aligned.chunks_exact(8);
220 for chunk in &mut chunks {
221 crc = mm_crc32_u64(crc, chunk[0]);
222 crc = mm_crc32_u64(crc, chunk[1]);
223 crc = mm_crc32_u64(crc, chunk[2]);
224 crc = mm_crc32_u64(crc, chunk[3]);
225 crc = mm_crc32_u64(crc, chunk[4]);
226 crc = mm_crc32_u64(crc, chunk[5]);
227 crc = mm_crc32_u64(crc, chunk[6]);
228 crc = mm_crc32_u64(crc, chunk[7]);
229 }
230
231 for &val in chunks.remainder() {
232 crc = mm_crc32_u64(crc, val);
233 }
234
235 for &byte in suffix {
236 crc = _mm_crc32_u8(crc, byte);
237 }
238
239 crc
240 }
241
242 #[cfg(test)]
243 mod tests {
244 use super::*;
245 use crate::test::consts::TEST_CHECK_STRING;
246 use crc::{Crc, Table};
247 use rand::{rng, Rng};
248
249 const RUST_CRC32_ISCSI: Crc<u32, Table<16>> = Crc::<u32, Table<16>>::new(&crc::CRC_32_ISCSI);
250
251 #[test]
252 fn test_crc32_iscsi_check() {
253 if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
254 assert_eq!(
255 crc32_iscsi(0xffffffff, TEST_CHECK_STRING) ^ 0xffffffff,
256 0xe3069283
257 );
258 }
259 }
260
261 #[test]
262 fn test_crc32_iscsi_small_fast_check() {
263 if is_x86_feature_detected!("sse4.2") {
264 unsafe {
265 assert_eq!(
266 crc32_iscsi_small_fast(0xffffffff, TEST_CHECK_STRING) ^ 0xffffffff,
267 0xe3069283
268 );
269 }
270 }
271 }
272
273 #[test]
274 fn test_crc32_iscsi_small_fast_all_lengths() {
275 if is_x86_feature_detected!("sse4.2") {
276 for len in 1..=255 {
277 test_crc32_iscsi_small_fast_random(len);
278 }
279 }
280 }
281
282 fn test_crc32_iscsi_small_fast_random(len: usize) {
283 let mut data = vec![0u8; len];
284 rng().fill(&mut data[..]);
285
286 let checksum = RUST_CRC32_ISCSI.checksum(&data);
287
288 if is_x86_feature_detected!("sse4.2") {
289 unsafe {
290 assert_eq!(
291 crc32_iscsi_small_fast(0xffffffff, &data) ^ 0xffffffff,
292 checksum
293 );
294 }
295 }
296 }
297
298 #[test]
299 fn test_crc32_iscsi_small_all_lengths() {
300 if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
301 for len in 1..=255 {
302 test_crc32_iscsi_random(len);
303 }
304 }
305 }
306
307 #[test]
308 fn test_crc32_iscsi_medium_lengths() {
309 if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
310 // Test each length from 256 to 1024, which should fold and include handling remainders
311 for len in 256..=1024 {
312 test_crc32_iscsi_random(len);
313 }
314 }
315 }
316
317 #[test]
318 fn test_crc32_iscsi_large_lengths() {
319 if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
320 // Test 1 MiB just before, at, and just after the folding boundaries
321 for len in 1048575..1048577 {
322 test_crc32_iscsi_random(len);
323 }
324 }
325 }
326
327 #[rustversion::since(1.89)]
328 fn test_crc32_iscsi_random(len: usize) {
329 let mut data = vec![0u8; len];
330 rng().fill(&mut data[..]);
331
332 let checksum = RUST_CRC32_ISCSI.checksum(&data);
333
334 if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
335 assert_eq!(crc32_iscsi(0xffffffff, &data) ^ 0xffffffff, checksum);
336 }
337
338 unsafe {
339 #[cfg(target_arch = "x86_64")]
340 {
341 if is_x86_feature_detected!("vpclmulqdq")
342 && is_x86_feature_detected!("avx512vl")
343 && is_x86_feature_detected!("avx512f")
344 {
345 assert_eq!(
346 crc32_iscsi_avx512_vpclmulqdq_v3x2(0xffffffff, data.as_ptr(), data.len())
347 ^ 0xffffffff,
348 checksum
349 );
350 }
351
352 if is_x86_feature_detected!("avx512vl")
353 && is_x86_feature_detected!("avx512f")
354 && is_x86_feature_detected!("pclmulqdq")
355 {
356 assert_eq!(
357 crc32_iscsi_avx512_v4s3x3(0xffffffff, data.as_ptr(), data.len())
358 ^ 0xffffffff,
359 checksum
360 );
361 }
362 }
363
364 assert_eq!(
365 crc32_iscsi_sse_v4s3x3(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
366 checksum
367 );
368 }
369 }
370
371 #[rustversion::before(1.89)]
372 fn test_crc32_iscsi_random(len: usize) {
373 let mut data = vec![0u8; len];
374 rng().fill(&mut data[..]);
375
376 let checksum = RUST_CRC32_ISCSI.checksum(&data);
377
378 if is_x86_feature_detected!("sse4.2") && is_x86_feature_detected!("pclmulqdq") {
379 assert_eq!(crc32_iscsi(0xffffffff, &data) ^ 0xffffffff, checksum);
380
381 unsafe {
382 assert_eq!(
383 crc32_iscsi_sse_v4s3x3(0xffffffff, data.as_ptr(), data.len()) ^ 0xffffffff,
384 checksum
385 );
386 }
387 }
388 }
389 }