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