OXYGENHUB
◆ bare-hashes oxygen / vendor / crc-fast / src / crc32 / fusion / x86 / iscsi / avx512_pclmulqdq.rs
6.4 KiB · Rust · last changed Strata: the cloud stores send through ureq, so every release target links 1 month ago source blame history raw
1
//! Converted to Rust from the original C code generated by https://github.com/corsix/fast-crc32/
2
//! with the help of Claude.ai.
3
//!
4
//! MIT licensed.
5
6
#![cfg(target_arch = "x86_64")]
7
8
/// Converted to Rust from the original C code generated by https://github.com/corsix/fast-crc32/
9
/// with the help of Claude.ai using:
10
///
11
/// ./generate -i avx512 -p crc32c -a v4s3x3
12
///
13
/// Modified as necessary for this Rust implementation.
14
///
15
/// Uses AVX-512 instructions
16
#[inline]
17
#[target_feature(enable = "avx512vl,pclmulqdq")]
18
pub unsafe fn crc32_iscsi_avx512_v4s3x3(mut crc0: u32, mut buf: *const u8, mut len: usize) -> u32 {
19
    use crate::fusion::x86::*;
20
21
    // Align to 8-byte boundary using hardware CRC32C instructions
22
    while len > 0 && (buf as usize & 7) != 0 {
23
        crc0 = _mm_crc32_u8(crc0, *buf);
24
        buf = buf.add(1);
25
        len -= 1;
26
    }
27
28
    // Handle 8-byte alignment
29
    if (buf as usize & 8) != 0 && len >= 8 {
30
        crc0 = _mm_crc32_u64(crc0.into(), *(buf as *const u64)) as u32;
31
        buf = buf.add(8);
32
        len -= 8;
33
    }
34
35
    if len >= 144 {
36
        let blk = (len - 8) / 136;
37
        let klen = blk * 24;
38
        let buf2 = buf;
39
        let mut crc1 = 0u32;
40
        let mut crc2 = 0u32;
41
42
        // First vector chunk - load four 128-bit vectors (64 bytes total)
43
        let mut x0 = _mm_loadu_si128(buf2 as *const __m128i);
44
        let mut x1 = _mm_loadu_si128(buf2.add(16) as *const __m128i);
45
        let mut x2 = _mm_loadu_si128(buf2.add(32) as *const __m128i);
46
        let mut x3 = _mm_loadu_si128(buf2.add(48) as *const __m128i);
47
48
        // iSCSI-specific folding constant (different from ISO-HDLC)
49
        let mut k = _mm_setr_epi32(0x740eef02u32 as i32, 0, 0x9e4addf8u32 as i32, 0);
50
51
        // XOR the CRC into the first vector's low 32 bits
52
        x0 = _mm_xor_si128(_mm_cvtsi32_si128(crc0 as i32), x0);
53
        crc0 = 0;
54
55
        let mut buf2 = buf2.add(64);
56
        len -= 136;
57
        buf = buf.add(blk * 64);
58
59
        // Main loop - process 144 bytes at a time
60
        while len >= 144 {
61
            let y0 = clmul_lo_sse(x0, k);
62
            x0 = clmul_hi_sse(x0, k);
63
            let y1 = clmul_lo_sse(x1, k);
64
            x1 = clmul_hi_sse(x1, k);
65
            let y2 = clmul_lo_sse(x2, k);
66
            x2 = clmul_hi_sse(x2, k);
67
            let y3 = clmul_lo_sse(x3, k);
68
            x3 = clmul_hi_sse(x3, k);
69
70
            // XOR with next chunk of data using ternary logic (A XOR B XOR C)
71
            x0 = _mm_ternarylogic_epi64(x0, y0, _mm_loadu_si128(buf2 as *const __m128i), 0x96);
72
            x1 = _mm_ternarylogic_epi64(
73
                x1,
74
                y1,
75
                _mm_loadu_si128(buf2.add(16) as *const __m128i),
76
                0x96,
77
            );
78
            x2 = _mm_ternarylogic_epi64(
79
                x2,
80
                y2,
81
                _mm_loadu_si128(buf2.add(32) as *const __m128i),
82
                0x96,
83
            );
84
            x3 = _mm_ternarylogic_epi64(
85
                x3,
86
                y3,
87
                _mm_loadu_si128(buf2.add(48) as *const __m128i),
88
                0x96,
89
            );
90
91
            // Process scalar data in parallel using hardware CRC32C
92
            crc0 = _mm_crc32_u64(crc0.into(), *(buf as *const u64)) as u32;
93
            crc1 = _mm_crc32_u64(crc1.into(), *(buf.add(klen) as *const u64)) as u32;
94
            crc2 = _mm_crc32_u64(crc2.into(), *(buf.add(klen * 2) as *const u64)) as u32;
95
            crc0 = _mm_crc32_u64(crc0.into(), *(buf.add(8) as *const u64)) as u32;
96
            crc1 = _mm_crc32_u64(crc1.into(), *(buf.add(klen + 8) as *const u64)) as u32;
97
            crc2 = _mm_crc32_u64(crc2.into(), *(buf.add(klen * 2 + 8) as *const u64)) as u32;
98
            crc0 = _mm_crc32_u64(crc0.into(), *(buf.add(16) as *const u64)) as u32;
99
            crc1 = _mm_crc32_u64(crc1.into(), *(buf.add(klen + 16) as *const u64)) as u32;
100
            crc2 = _mm_crc32_u64(crc2.into(), *(buf.add(klen * 2 + 16) as *const u64)) as u32;
101
102
            buf = buf.add(24);
103
            buf2 = buf2.add(64);
104
            len -= 136;
105
        }
106
107
        // Reduce x0 ... x3 to just x0 using iSCSI-specific constants
108
        k = _mm_setr_epi32(0xf20c0dfeu32 as i32, 0, 0x493c7d27u32 as i32, 0);
109
110
        let y0 = clmul_lo_sse(x0, k);
111
        x0 = clmul_hi_sse(x0, k);
112
        let y2 = clmul_lo_sse(x2, k);
113
        x2 = clmul_hi_sse(x2, k);
114
115
        x0 = _mm_ternarylogic_epi64(x0, y0, x1, 0x96);
116
        x2 = _mm_ternarylogic_epi64(x2, y2, x3, 0x96);
117
118
        k = _mm_setr_epi32(0x3da6d0cbu32 as i32, 0, 0xba4fc28eu32 as i32, 0);
119
120
        let y0 = clmul_lo_sse(x0, k);
121
        x0 = clmul_hi_sse(x0, k);
122
        x0 = _mm_ternarylogic_epi64(x0, y0, x2, 0x96);
123
124
        // Final scalar chunk
125
        crc0 = _mm_crc32_u64(crc0.into(), *(buf as *const u64)) as u32;
126
        crc1 = _mm_crc32_u64(crc1.into(), *(buf.add(klen) as *const u64)) as u32;
127
        crc2 = _mm_crc32_u64(crc2.into(), *(buf.add(klen * 2) as *const u64)) as u32;
128
        crc0 = _mm_crc32_u64(crc0.into(), *(buf.add(8) as *const u64)) as u32;
129
        crc1 = _mm_crc32_u64(crc1.into(), *(buf.add(klen + 8) as *const u64)) as u32;
130
        crc2 = _mm_crc32_u64(crc2.into(), *(buf.add(klen * 2 + 8) as *const u64)) as u32;
131
        crc0 = _mm_crc32_u64(crc0.into(), *(buf.add(16) as *const u64)) as u32;
132
        crc1 = _mm_crc32_u64(crc1.into(), *(buf.add(klen + 16) as *const u64)) as u32;
133
        crc2 = _mm_crc32_u64(crc2.into(), *(buf.add(klen * 2 + 16) as *const u64)) as u32;
134
        buf = buf.add(24);
135
136
        let vc0 = crc_shift_iscsi_sse(crc0, klen * 2 + 8);
137
        let vc1 = crc_shift_iscsi_sse(crc1, klen + 8);
138
        let mut vc = _mm_extract_epi64(_mm_xor_si128(vc0, vc1), 0) as u64;
139
140
        // Reduce 128 bits to 32 bits, and multiply by x^32
141
        let x0_low = _mm_extract_epi64(x0, 0) as u64;
142
        let x0_high = _mm_extract_epi64(x0, 1) as u64;
143
        let vec_crc = _mm_crc32_u64(_mm_crc32_u64(0, x0_low), x0_high);
144
        vc ^= _mm_extract_epi64(crc_shift_iscsi_sse(vec_crc as u32, klen * 3 + 8), 0) as u64;
145
146
        // Final 8 bytes
147
        buf = buf.add(klen * 2);
148
        crc0 = crc2;
149
        crc0 = _mm_crc32_u64(crc0.into(), *(buf as *const u64) ^ vc) as u32;
150
        buf = buf.add(8);
151
        len -= 8;
152
    }
153
154
    // Process remaining 8-byte chunks using hardware CRC32C
155
    while len >= 8 {
156
        crc0 = _mm_crc32_u64(crc0.into(), *(buf as *const u64)) as u32;
157
        buf = buf.add(8);
158
        len -= 8;
159
    }
160
161
    // Process remaining bytes using hardware CRC32C
162
    while len > 0 {
163
        crc0 = _mm_crc32_u8(crc0, *buf);
164
        buf = buf.add(1);
165
        len -= 1;
166
    }
167
168
    crc0
169
}