-
Notifications
You must be signed in to change notification settings - Fork 90
/
pkg2zip_crc32_x86.c
336 lines (289 loc) · 13 KB
/
pkg2zip_crc32_x86.c
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include "pkg2zip_crc32.h"
#include <wmmintrin.h> // PCLMUL
#include <tmmintrin.h> // SSSE3
#include <smmintrin.h> // SSS4
// Whitepaper: https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/fast-crc-computation-generic-polynomials-pclmulqdq-paper.pdf
// ZLIB licensed code from https://github.com/jtkukunas/zlib/blob/master/crc_folding.c
static const uint32_t PKG_ALIGN(16) shift_table[] = {
0x84838281, 0x88878685, 0x8c8b8a89, 0x008f8e8d,
0x85848382, 0x89888786, 0x8d8c8b8a, 0x01008f8e,
0x86858483, 0x8a898887, 0x8e8d8c8b, 0x0201008f,
0x87868584, 0x8b8a8988, 0x8f8e8d8c, 0x03020100,
0x88878685, 0x8c8b8a89, 0x008f8e8d, 0x04030201,
0x89888786, 0x8d8c8b8a, 0x01008f8e, 0x05040302,
0x8a898887, 0x8e8d8c8b, 0x0201008f, 0x06050403,
0x8b8a8988, 0x8f8e8d8c, 0x03020100, 0x07060504,
0x8c8b8a89, 0x008f8e8d, 0x04030201, 0x08070605,
0x8d8c8b8a, 0x01008f8e, 0x05040302, 0x09080706,
0x8e8d8c8b, 0x0201008f, 0x06050403, 0x0a090807,
0x8f8e8d8c, 0x03020100, 0x07060504, 0x0b0a0908,
0x008f8e8d, 0x04030201, 0x08070605, 0x0c0b0a09,
0x01008f8e, 0x05040302, 0x09080706, 0x0d0c0b0a,
0x0201008f, 0x06050403, 0x0a090807, 0x0e0d0c0b,
};
#define FOLD1(xmm0, xmm1, xmm2, xmm3) do \
{ \
const __m128i fold4 = _mm_set_epi32( \
0x00000001, 0x54442bd4, \
0x00000001, 0xc6e41596); \
\
__m128i r0, r1, r2, r3, a, b; \
\
r0 = xmm1; \
r1 = xmm2; \
r2 = xmm3; \
\
a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
r3 = _mm_xor_si128(a, b); \
\
xmm0 = r0; \
xmm1 = r1; \
xmm2 = r2; \
xmm3 = r3; \
} while (0)
#define FOLD2(xmm0, xmm1, xmm2, xmm3) do \
{ \
const __m128i fold4 = _mm_set_epi32( \
0x00000001, 0x54442bd4, \
0x00000001, 0xc6e41596); \
\
__m128i r0, r1, r2, r3, a, b; \
\
r0 = xmm2; \
r1 = xmm3; \
\
a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
r2 = _mm_xor_si128(a, b); \
\
a = _mm_clmulepi64_si128(xmm1, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm1, fold4, 0x10); \
r3 = _mm_xor_si128(a, b); \
\
xmm0 = r0; \
xmm1 = r1; \
xmm2 = r2; \
xmm3 = r3; \
} while (0)
#define FOLD3(xmm0, xmm1, xmm2, xmm3) do \
{ \
const __m128i fold4 = _mm_set_epi32( \
0x00000001, 0x54442bd4, \
0x00000001, 0xc6e41596); \
\
__m128i r0, r1, r2, r3, a, b; \
\
r0 = xmm3; \
\
a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
r1 = _mm_xor_si128(a, b); \
\
a = _mm_clmulepi64_si128(xmm1, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm1, fold4, 0x10); \
r2 = _mm_xor_si128(a, b); \
\
a = _mm_clmulepi64_si128(xmm2, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm2, fold4, 0x10); \
r3 = _mm_xor_si128(a, b); \
\
xmm0 = r0; \
xmm1 = r1; \
xmm2 = r2; \
xmm3 = r3; \
} while (0)
#define FOLD4(xmm0, xmm1, xmm2, xmm3) do \
{ \
const __m128i fold4 = _mm_set_epi32( \
0x00000001, 0x54442bd4, \
0x00000001, 0xc6e41596); \
\
__m128i a, b; \
\
a = _mm_clmulepi64_si128(xmm0, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm0, fold4, 0x10); \
xmm0 = _mm_xor_si128(a, b); \
\
a = _mm_clmulepi64_si128(xmm1, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm1, fold4, 0x10); \
xmm1 = _mm_xor_si128(a, b); \
\
a = _mm_clmulepi64_si128(xmm2, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm2, fold4, 0x10); \
xmm2 = _mm_xor_si128(a, b); \
\
a = _mm_clmulepi64_si128(xmm3, fold4, 0x01); \
b = _mm_clmulepi64_si128(xmm3, fold4, 0x10); \
xmm3 = _mm_xor_si128(a, b); \
} while (0)
#define PARTIAL(len, xmm0, xmm1, xmm2, xmm3, xmm4) do \
{ \
const __m128i fold4 = _mm_set_epi32( \
0x00000001, 0x54442bd4, \
0x00000001, 0xc6e41596); \
const __m128i mask = _mm_set1_epi32(0x80808080); \
\
__m128i shl = _mm_load_si128((__m128i *)shift_table + (len - 1)); \
__m128i shr = _mm_xor_si128(shl, mask); \
\
__m128i a, b, r; \
__m128i tmp = _mm_shuffle_epi8(xmm0, shl); \
\
a = _mm_shuffle_epi8(xmm0, shr); \
b = _mm_shuffle_epi8(xmm1, shl); \
xmm0 = _mm_or_si128(a, b); \
\
a = _mm_shuffle_epi8(xmm1, shr); \
b = _mm_shuffle_epi8(xmm2, shl); \
xmm1 = _mm_or_si128(a, b); \
\
a = _mm_shuffle_epi8(xmm2, shr); \
b = _mm_shuffle_epi8(xmm3, shl); \
xmm2 = _mm_or_si128(a, b); \
\
a = _mm_shuffle_epi8(xmm3, shr); \
b = _mm_shuffle_epi8(xmm4, shl); \
xmm4 = b; \
r = _mm_or_si128(a, b); \
\
a = _mm_clmulepi64_si128(tmp, fold4, 0x10); \
b = _mm_clmulepi64_si128(tmp, fold4, 0x01); \
\
r = _mm_xor_si128(r, a); \
r = _mm_xor_si128(r, b); \
xmm3 = r; \
} while(0)
void crc32_init_x86(crc32_ctx* ctx)
{
__m128i init = _mm_cvtsi32_si128(0x9db42487);
__m128i zero = _mm_setzero_si128();
_mm_store_si128((__m128i*)ctx->crc + 0, init);
_mm_store_si128((__m128i*)ctx->crc + 1, zero);
_mm_store_si128((__m128i*)ctx->crc + 2, zero);
_mm_store_si128((__m128i*)ctx->crc + 3, zero);
}
void crc32_update_x86(crc32_ctx* ctx, const void* buffer, size_t size)
{
const uint8_t* buffer8 = buffer;
__m128i xmm0 = _mm_load_si128((__m128i*)ctx->crc + 0);
__m128i xmm1 = _mm_load_si128((__m128i*)ctx->crc + 1);
__m128i xmm2 = _mm_load_si128((__m128i*)ctx->crc + 2);
__m128i xmm3 = _mm_load_si128((__m128i*)ctx->crc + 3);
__m128i xmm4 = _mm_load_si128((__m128i*)ctx->crc + 4);
if (size < 16)
{
if (size == 0)
{
return;
}
xmm4 = _mm_loadu_si128((__m128i *)buffer8);
goto partial;
}
uint32_t prefix = (0 - (uintptr_t)buffer8) & 0xF;
if (prefix != 0)
{
xmm4 = _mm_loadu_si128((__m128i *)buffer8);
buffer8 += prefix;
size -= prefix;
PARTIAL(prefix, xmm0, xmm1, xmm2, xmm3, xmm4);
}
while (size >= 64)
{
__m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
__m128i t1 = _mm_load_si128((__m128i *)buffer8 + 1);
__m128i t2 = _mm_load_si128((__m128i *)buffer8 + 2);
__m128i t3 = _mm_load_si128((__m128i *)buffer8 + 3);
FOLD4(xmm0, xmm1, xmm2, xmm3);
xmm0 = _mm_xor_si128(xmm0, t0);
xmm1 = _mm_xor_si128(xmm1, t1);
xmm2 = _mm_xor_si128(xmm2, t2);
xmm3 = _mm_xor_si128(xmm3, t3);
buffer8 += 64;
size -= 64;
}
if (size >= 48)
{
__m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
__m128i t1 = _mm_load_si128((__m128i *)buffer8 + 1);
__m128i t2 = _mm_load_si128((__m128i *)buffer8 + 2);
FOLD3(xmm0, xmm1, xmm2, xmm3);
xmm1 = _mm_xor_si128(xmm1, t0);
xmm2 = _mm_xor_si128(xmm2, t1);
xmm3 = _mm_xor_si128(xmm3, t2);
buffer8 += 48;
size -= 48;
}
else if (size >= 32)
{
__m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
__m128i t1 = _mm_load_si128((__m128i *)buffer8 + 1);
FOLD2(xmm0, xmm1, xmm2, xmm3);
xmm2 = _mm_xor_si128(xmm2, t0);
xmm3 = _mm_xor_si128(xmm3, t1);
buffer8 += 32;
size -= 32;
}
else if (size >= 16)
{
__m128i t0 = _mm_load_si128((__m128i *)buffer8 + 0);
FOLD1(xmm0, xmm1, xmm2, xmm3);
xmm3 = _mm_xor_si128(xmm3, t0);
buffer8 += 16;
size -= 16;
}
if (size == 0)
{
goto done;
}
xmm4 = _mm_load_si128((__m128i *)buffer8);
partial:
PARTIAL(size, xmm0, xmm1, xmm2, xmm3, xmm4);
done:
_mm_store_si128((__m128i*)ctx->crc + 0, xmm0);
_mm_store_si128((__m128i*)ctx->crc + 1, xmm1);
_mm_store_si128((__m128i*)ctx->crc + 2, xmm2);
_mm_store_si128((__m128i*)ctx->crc + 3, xmm3);
_mm_store_si128((__m128i*)ctx->crc + 4, xmm4);
}
uint32_t crc32_done_x86(crc32_ctx* ctx)
{
const __m128i mask1 = _mm_setr_epi32(0xFFFFFFFF, 0xFFFFFFFF, 0x00000000, 0x00000000);
const __m128i mask2 = _mm_setr_epi32(0x00000000, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
__m128i xmm0 = _mm_load_si128((__m128i*)ctx->crc + 0);
__m128i xmm1 = _mm_load_si128((__m128i*)ctx->crc + 1);
__m128i xmm2 = _mm_load_si128((__m128i*)ctx->crc + 2);
__m128i xmm3 = _mm_load_si128((__m128i*)ctx->crc + 3);
__m128i fold;
__m128i a, b, t;
fold = _mm_setr_epi32(0xccaa009e, 0x00000000, 0x751997d0, 0x00000001);
a = _mm_clmulepi64_si128(xmm0, fold, 0x10);
b = _mm_clmulepi64_si128(xmm0, fold, 0x01);
t = _mm_xor_si128(xmm1, a);
t = _mm_xor_si128(t, b);
a = _mm_clmulepi64_si128(t, fold, 0x10);
b = _mm_clmulepi64_si128(t, fold, 0x01);
t = _mm_xor_si128(xmm2, a);
t = _mm_xor_si128(t, b);
a = _mm_clmulepi64_si128(t, fold, 0x10);
b = _mm_clmulepi64_si128(t, fold, 0x01);
t = _mm_xor_si128(xmm3, a);
t = _mm_xor_si128(t, b);
fold = _mm_setr_epi32(0xccaa009e, 0x00000000, 0x63cd6124, 0x00000001);
a = _mm_clmulepi64_si128(t, fold, 0);
b = _mm_srli_si128(t, 8);
a = _mm_xor_si128(a, b);
b = _mm_slli_si128(a, 4);
b = _mm_clmulepi64_si128(b, fold, 0x10);
t = _mm_xor_si128(a, b);
t = _mm_and_si128(t, mask2);
fold = _mm_setr_epi32(0xf7011640, 0x00000001, 0xdb710640, 0x00000001);
a = _mm_clmulepi64_si128(t, fold, 0);
a = _mm_xor_si128(a, t);
a = _mm_and_si128(a, mask1);
b = _mm_clmulepi64_si128(a, fold, 0x10);
b = _mm_xor_si128(b, a);
b = _mm_xor_si128(b, t);
uint32_t crc = _mm_extract_epi32(b, 2);
return ~crc;
}