Coverage Report

Created: 2026-09-04 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/zlib-ng/arch/x86/crc32_chorba_sse41.c
Line
Count
Source
1
#include "zbuild.h"
2
#include "arch_functions.h"
3
4
#if defined(X86_SSE41) && defined(CRC32_CHORBA_SSE_FALLBACK)
5
6
#include "crc32_chorba_p.h"
7
#include "crc32_braid_tbl.h"
8
#include <emmintrin.h>
9
#include <smmintrin.h>
10
#include "arch/x86/x86_intrins.h"
11
12
#define READ_NEXT(in, off, a, b) \
13
0
    do { \
14
0
        a = _mm_load_si128((__m128i*)(in + off / sizeof(uint64_t))); \
15
0
        b = _mm_load_si128((__m128i*)(in + off / sizeof(uint64_t) + 2)); \
16
0
    } while (0)
17
18
#define NEXT_ROUND(invec, a, b, c, d) \
19
0
    do { \
20
0
        a = _mm_xor_si128(_mm_slli_epi64(invec, 17), _mm_slli_epi64(invec, 55)); \
21
0
        b = _mm_xor_si128(_mm_xor_si128(_mm_srli_epi64(invec, 47), _mm_srli_epi64(invec, 9)), _mm_slli_epi64(invec, 19)); \
22
0
        c = _mm_xor_si128(_mm_srli_epi64(invec, 45), _mm_slli_epi64(invec, 44)); \
23
0
        d  = _mm_srli_epi64(invec, 20); \
24
0
    } while (0)
25
26
#define REALIGN_CHORBA(in0, in1, in2, in3, out0, out1, out2, out3, out4, shift) \
27
0
    do { \
28
0
        out0 = _mm_slli_si128(in0, shift); \
29
0
        out1 = _mm_alignr_epi8(in1, in0, shift); \
30
0
        out2 = _mm_alignr_epi8(in2, in1, shift); \
31
0
        out3 = _mm_alignr_epi8(in3, in2, shift); \
32
0
        out4 = _mm_srli_si128(in3, shift); \
33
0
    } while (0)
34
35
#define STORE4(out0, out1, out2, out3, out) \
36
0
    do { \
37
0
        _mm_store_si128(out++, out0); \
38
0
        _mm_store_si128(out++, out1); \
39
0
        _mm_store_si128(out++, out2); \
40
0
        _mm_store_si128(out++, out3); \
41
0
    } while (0)
42
43
#define READ4(out0, out1, out2, out3, in) \
44
0
    do { \
45
0
        out0 = _mm_load_si128(in++); \
46
0
        out1 = _mm_load_si128(in++); \
47
0
        out2 = _mm_load_si128(in++); \
48
0
        out3 = _mm_load_si128(in++); \
49
0
    } while (0)
50
51
/* This is intentionally shifted one down to compensate for the deferred store from
52
 * the last iteration */
53
#define READ4_WITHXOR(out0, out1, out2, out3, xor0, xor1, xor2, xor3, in) \
54
0
    do { \
55
0
        out0 = _mm_xor_si128(in[1], xor0); \
56
0
        out1 = _mm_xor_si128(in[2], xor1); \
57
0
        out2 = _mm_xor_si128(in[3], xor2); \
58
0
        out3 = _mm_xor_si128(in[4], xor3); \
59
0
    } while (0)
60
61
0
Z_FORCEINLINE static uint32_t crc32_chorba_32768_nondestructive_sse41(uint32_t crc, const uint8_t *buf, size_t len) {
62
    /* The calling function ensured that this is aligned correctly */
63
0
    const uint64_t* input = (const uint64_t*)buf;
64
0
    ALIGNED_(16) uint64_t bitbuffer[32768 / sizeof(uint64_t)];
65
0
    __m128i *bitbuffer_v = (__m128i*)bitbuffer;
66
0
    const uint8_t *bitbuffer_bytes = (const uint8_t*)bitbuffer;
67
0
    __m128i z = _mm_setzero_si128();
68
69
0
    __m128i *bitbuf128 = &bitbuffer_v[64];
70
0
    __m128i *bitbuf144 = &bitbuffer_v[72];
71
0
    __m128i *bitbuf182 = &bitbuffer_v[91];
72
0
    __m128i *bitbuf210 = &bitbuffer_v[105];
73
0
    __m128i *bitbuf300 = &bitbuffer_v[150];
74
0
    __m128i *bitbuf0 = bitbuf128;
75
0
    __m128i *inptr = (__m128i*)input;
76
77
    /* We only need to zero out the bytes between the 128'th value and the 144th
78
     * that are actually read */
79
0
    __m128i *z_cursor = bitbuf128;
80
0
    for (size_t i = 0; i < 2; ++i) {
81
0
        STORE4(z, z, z, z, z_cursor);
82
0
    }
83
84
    /* We only need to zero out the bytes between the 144'th value and the 182nd that
85
     * are actually read */
86
0
    z_cursor = bitbuf144 + 8;
87
0
    for (size_t i = 0; i < 11; ++i) {
88
0
        _mm_store_si128(z_cursor++, z);
89
0
    }
90
91
    /* We only need to zero out the bytes between the 182nd value and the 210th that
92
     * are actually read. */
93
0
    z_cursor = bitbuf182;
94
0
    for (size_t i = 0; i < 4; ++i) {
95
0
        STORE4(z, z, z, z, z_cursor);
96
0
    }
97
98
    /* We need to mix this in */
99
0
    __m128i init_crc = _mm_cvtsi64_si128(~crc);
100
0
    crc = 0;
101
102
0
    size_t i = 0;
103
104
    /* Previous iteration runs carried over */
105
0
    __m128i buf144 = z;
106
0
    __m128i buf182 = z;
107
0
    __m128i buf210 = z;
108
109
0
    for (; i + 300*8+64 < len && i < 22 * 8; i += 64) {
110
0
        __m128i in12, in34, in56, in78,
111
0
                in_1, in23, in45, in67, in8_;
112
113
0
        READ4(in12, in34, in56, in78, inptr);
114
115
0
        if (i == 0) {
116
0
            in12 = _mm_xor_si128(in12, init_crc);
117
0
        }
118
119
0
        REALIGN_CHORBA(in12, in34, in56, in78,
120
0
                       in_1, in23, in45, in67, in8_, 8);
121
122
0
        __m128i a = _mm_xor_si128(buf144, in_1);
123
124
0
        STORE4(a, in23, in45, in67, bitbuf144);
125
0
        buf144 = in8_;
126
127
0
        __m128i e = _mm_xor_si128(buf182, in_1);
128
0
        STORE4(e, in23, in45, in67, bitbuf182);
129
0
        buf182 = in8_;
130
131
0
        __m128i m = _mm_xor_si128(buf210, in_1);
132
0
        STORE4(m, in23, in45, in67, bitbuf210);
133
0
        buf210 = in8_;
134
135
0
        STORE4(in12, in34, in56, in78, bitbuf300);
136
0
    }
137
138
0
    for (; i + 300*8+64 < len && i < 32 * 8; i += 64) {
139
0
        __m128i in12, in34, in56, in78,
140
0
                in_1, in23, in45, in67, in8_;
141
0
        READ4(in12, in34, in56, in78, inptr);
142
143
0
        REALIGN_CHORBA(in12, in34, in56, in78,
144
0
                       in_1, in23, in45, in67, in8_, 8);
145
146
0
        __m128i a = _mm_xor_si128(buf144, in_1);
147
148
0
        STORE4(a, in23, in45, in67, bitbuf144);
149
0
        buf144 = in8_;
150
151
0
        __m128i e, f, g, h;
152
0
        e = _mm_xor_si128(buf182, in_1);
153
0
        READ4_WITHXOR(f, g, h, buf182, in23, in45, in67, in8_, bitbuf182);
154
0
        STORE4(e, f, g, h, bitbuf182);
155
156
0
        __m128i m = _mm_xor_si128(buf210, in_1);
157
0
        STORE4(m, in23, in45, in67, bitbuf210);
158
0
        buf210 = in8_;
159
160
0
        STORE4(in12, in34, in56, in78, bitbuf300);
161
0
    }
162
163
0
    for (; i + 300*8+64 < len && i < 84 * 8; i += 64) {
164
0
        __m128i in12, in34, in56, in78,
165
0
                in_1, in23, in45, in67, in8_;
166
0
        READ4(in12, in34, in56, in78, inptr);
167
168
0
        REALIGN_CHORBA(in12, in34, in56, in78,
169
0
                       in_1, in23, in45, in67, in8_, 8);
170
171
0
        __m128i a, b, c, d;
172
0
        a = _mm_xor_si128(buf144, in_1);
173
0
        READ4_WITHXOR(b, c, d, buf144, in23, in45, in67, in8_, bitbuf144);
174
0
        STORE4(a, b, c, d, bitbuf144);
175
176
0
        __m128i e, f, g, h;
177
0
        e = _mm_xor_si128(buf182, in_1);
178
0
        READ4_WITHXOR(f, g, h, buf182, in23, in45, in67, in8_, bitbuf182);
179
0
        STORE4(e, f, g, h, bitbuf182);
180
181
0
        __m128i m = _mm_xor_si128(buf210, in_1);
182
0
        STORE4(m, in23, in45, in67, bitbuf210);
183
0
        buf210 = in8_;
184
185
0
        STORE4(in12, in34, in56, in78, bitbuf300);
186
0
    }
187
188
0
    for (; i + 300*8+64 < len; i += 64) {
189
0
        __m128i in12, in34, in56, in78,
190
0
                in_1, in23, in45, in67, in8_;
191
192
0
        if (i < 128 * 8) {
193
0
            READ4(in12, in34, in56, in78, inptr);
194
0
        } else {
195
0
            in12 = _mm_xor_si128(_mm_load_si128(inptr++), _mm_load_si128(bitbuf0++));
196
0
            in34 = _mm_xor_si128(_mm_load_si128(inptr++), _mm_load_si128(bitbuf0++));
197
0
            in56 = _mm_xor_si128(_mm_load_si128(inptr++), _mm_load_si128(bitbuf0++));
198
0
            in78 = _mm_xor_si128(_mm_load_si128(inptr++), _mm_load_si128(bitbuf0++));
199
0
        }
200
201
        // [0, 145, 183, 211]
202
203
        /* Pre Penryn CPUs the unpack should be faster */
204
0
        REALIGN_CHORBA(in12, in34, in56, in78,
205
0
                       in_1, in23, in45, in67, in8_, 8);
206
207
0
        __m128i a, b, c, d;
208
0
        a = _mm_xor_si128(buf144, in_1);
209
0
        READ4_WITHXOR(b, c, d, buf144, in23, in45, in67, in8_, bitbuf144);
210
0
        STORE4(a, b, c, d, bitbuf144);
211
212
0
        __m128i e, f, g, h;
213
0
        e = _mm_xor_si128(buf182, in_1);
214
0
        READ4_WITHXOR(f, g, h, buf182, in23, in45, in67, in8_, bitbuf182);
215
0
        STORE4(e, f, g, h, bitbuf182);
216
217
0
        __m128i n, o, p;
218
0
        __m128i m = _mm_xor_si128(buf210, in_1);
219
220
        /* Couldn't tell you why but despite knowing that this is always false,
221
         * removing this branch with GCC makes things significantly slower. Some
222
         * loop bodies must be being joined or something */
223
0
        if (i < 84 * 8) {
224
0
            n = in23;
225
0
            o = in45;
226
0
            p = in67;
227
0
            buf210 = in8_;
228
0
        } else {
229
0
            READ4_WITHXOR(n, o, p, buf210, in23, in45, in67, in8_, bitbuf210);
230
0
        }
231
232
0
        STORE4(m, n, o, p, bitbuf210);
233
0
        STORE4(in12, in34, in56, in78, bitbuf300);
234
0
    }
235
236
    /* Second half of stores bubbled out */
237
0
    _mm_store_si128(bitbuf144, buf144);
238
0
    _mm_store_si128(bitbuf182, buf182);
239
0
    _mm_store_si128(bitbuf210, buf210);
240
241
    /* We also have to zero out the tail */
242
0
    size_t left_to_z = len - (300*8 + i);
243
0
    __m128i *bitbuf_tail = (__m128i*)(bitbuffer + 300 + i/8);
244
0
    while (left_to_z >= 64) {
245
0
       STORE4(z, z, z, z, bitbuf_tail);
246
0
       left_to_z -= 64;
247
0
    }
248
249
0
    while (left_to_z >= 16) {
250
0
       _mm_store_si128(bitbuf_tail++, z);
251
0
       left_to_z -= 16;
252
0
    }
253
254
0
    uint8_t *tail_bytes = (uint8_t*)bitbuf_tail;
255
0
    while (left_to_z--) {
256
0
       *tail_bytes++ = 0;
257
0
    }
258
259
0
    ALIGNED_(16) uint64_t final[9] = {0};
260
0
    __m128i next12, next34, next56;
261
0
    next12 = z;
262
0
    next34 = z;
263
0
    next56 = z;
264
265
0
    for (; (i + 72 < len); i += 32) {
266
0
        __m128i in1in2, in3in4;
267
0
        __m128i in1in2_, in3in4_;
268
0
        __m128i ab1, ab2, ab3, ab4;
269
0
        __m128i cd1, cd2, cd3, cd4;
270
271
0
        READ_NEXT(input, i, in1in2, in3in4);
272
0
        READ_NEXT(bitbuffer, i, in1in2_, in3in4_);
273
274
0
        in1in2 = _mm_xor_si128(_mm_xor_si128(in1in2, in1in2_), next12);
275
0
        in3in4 = _mm_xor_si128(in3in4, in3in4_);
276
277
0
        NEXT_ROUND(in1in2, ab1, ab2, ab3, ab4);
278
279
0
        __m128i a2_ = _mm_slli_si128(ab2, 8);
280
0
        __m128i ab1_next34 = _mm_xor_si128(next34, ab1);
281
0
        in3in4 = _mm_xor_si128(in3in4, ab1_next34);
282
0
        in3in4 = _mm_xor_si128(a2_, in3in4);
283
0
        NEXT_ROUND(in3in4, cd1, cd2, cd3, cd4);
284
285
0
        __m128i b2c2 = _mm_alignr_epi8(cd2, ab2, 8);
286
0
        __m128i a4_ = _mm_slli_si128(ab4, 8);
287
0
        a4_ = _mm_xor_si128(b2c2, a4_);
288
0
        next12 = _mm_xor_si128(ab3, a4_);
289
0
        next12 = _mm_xor_si128(next12, cd1);
290
291
0
        __m128i d2_ = _mm_srli_si128(cd2, 8);
292
0
        __m128i b4c4 = _mm_alignr_epi8(cd4, ab4, 8);
293
0
        next12 = _mm_xor_si128(next12, next56);
294
0
        next34 = _mm_xor_si128(cd3, _mm_xor_si128(b4c4, d2_));
295
0
        next56 = _mm_srli_si128(cd4, 8);
296
0
    }
297
298
0
    memcpy(final, input+(i / sizeof(uint64_t)), len-i);
299
0
    __m128i *final128 = (__m128i*)final;
300
0
    _mm_store_si128(final128, _mm_xor_si128(_mm_load_si128(final128), next12));
301
0
    ++final128;
302
0
    _mm_store_si128(final128, _mm_xor_si128(_mm_load_si128(final128), next34));
303
0
    ++final128;
304
0
    _mm_store_si128(final128, _mm_xor_si128(_mm_load_si128(final128), next56));
305
306
0
    uint8_t *final_bytes = (uint8_t*)final;
307
308
0
    for (size_t j = 0; j < (len-i); j++) {
309
0
        crc = crc_table[(crc ^ final_bytes[j] ^ bitbuffer_bytes[(j+i)]) & 0xff] ^ (crc >> 8);
310
0
    }
311
0
    return ~crc;
312
0
}
313
314
0
Z_INTERNAL uint32_t crc32_chorba_sse41(uint32_t crc, const uint8_t *buf, size_t len) {
315
0
    uintptr_t align_diff = ALIGN_DIFF(buf, 16);
316
0
    if (len <= align_diff + CHORBA_SMALL_THRESHOLD_64BIT)
317
0
        return crc32_braid(crc, buf, len);
318
319
0
    if (align_diff) {
320
0
        crc = crc32_braid(crc, buf, align_diff);
321
0
        len -= align_diff;
322
0
        buf += align_diff;
323
0
    }
324
0
#ifdef CRC32_CHORBA_FALLBACK
325
0
    if (len > CHORBA_LARGE_THRESHOLD)
326
0
        return crc32_chorba_118960_nondestructive(crc, buf, len);
327
0
#endif
328
0
    if (len > CHORBA_MEDIUM_LOWER_THRESHOLD && len <= CHORBA_MEDIUM_UPPER_THRESHOLD)
329
0
        return crc32_chorba_32768_nondestructive_sse41(crc, buf, len);
330
0
    return chorba_small_nondestructive_sse2(crc, buf, len);
331
0
}
332
333
0
Z_INTERNAL uint32_t crc32_copy_chorba_sse41(uint32_t crc, uint8_t *dst, const uint8_t *src, size_t len) {
334
0
    crc = crc32_chorba_sse41(crc, src, len);
335
0
    memcpy(dst, src, len);
336
0
    return crc;
337
0
}
338
#endif