Debian Codegraph @

simd-checksum-x86_64.cpp

raw
1 /*
2 * SSE2/SSSE3/AVX2-optimized routines to support checksumming of bytes.
3 *
4 * Copyright (C) 1996 Andrew Tridgell
5 * Copyright (C) 1996 Paul Mackerras
6 * Copyright (C) 2004-2020 Wayne Davison
7 * Copyright (C) 2020 Jorrit Jongma
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, visit the http://fsf.org website.
21 */
22 /*
23 * Optimization target for get_checksum1() was the Intel Atom D2700, the
24 * slowest CPU in the test set and the most likely to be CPU limited during
25 * transfers. The combination of intrinsics was chosen specifically for the
26 * most gain on that CPU, other combinations were occasionally slightly
27 * faster on the others.
28 *
29 * While on more modern CPUs transfers are less likely to be CPU limited
30 * (at least by this specific function), lower CPU usage is always better.
31 * Improvements may still be seen when matching chunks from NVMe storage
32 * even on newer CPUs.
33 *
34 * Benchmarks (in MB/s) C SSE2 SSSE3 AVX2
35 * - Intel Atom D2700 550 750 1000 N/A
36 * - Intel i7-7700hq 1850 2550 4050 6200
37 * - AMD ThreadRipper 2950x 2900 5600 8950 8100
38 *
39 * Curiously the AMD is slower with AVX2 than SSSE3, while the Intel is
40 * significantly faster. AVX2 is kept because it's more likely to relieve
41 * the bottleneck on the slower CPU.
42 *
43 * This optimization for get_checksum1() is intentionally limited to x86-64
44 * as no 32-bit CPU was available for testing. As 32-bit CPUs only have half
45 * the available xmm registers, this optimized version may not be faster than
46 * the pure C version anyway. Note that all x86-64 CPUs support at least SSE2.
47 *
48 * This file is compiled using GCC 4.8+/clang 6+'s C++ front end to allow the
49 * use of the target attribute, selecting the fastest code path based on
50 * dispatch priority (GCC 5) or runtime detection of CPU capabilities (GCC 6+).
51 * GCC 4.x are not supported to ease configure.ac logic.
52 */
53
54 #ifdef __x86_64__ /* { */
55 #ifdef __cplusplus /* { */
56
57 #include "rsync.h"
58
59 #ifdef USE_ROLL_SIMD /* { */
60
61 #include <immintrin.h>
62
63 /* Some clang versions don't like it when you use static with multi-versioned functions: linker errors */
64 #ifdef __clang__
65 #define MVSTATIC
66 #else
67 #define MVSTATIC static
68 #endif
69
70 // Missing from the headers on gcc 6 and older, clang 8 and older
71 typedef long long __m128i_u __attribute__((__vector_size__(16), __may_alias__, __aligned__(16)));
72 typedef long long __m256i_u __attribute__((__vector_size__(32), __may_alias__, __aligned__(16)));
73
74 /* Compatibility macros to let our SSSE3 algorithm run with only SSE2.
75 These used to be neat individual functions with target attributes switching between SSE2 and SSSE3 implementations
76 as needed, but though this works perfectly with GCC, clang fails to inline those properly leading to a near 50%
77 performance drop - combined with static and inline modifiers gets you linker errors and even compiler crashes...
78 */
79
80 #define SSE2_INTERLEAVE_ODD_EPI16(a, b) _mm_packs_epi32(_mm_srai_epi32(a, 16), _mm_srai_epi32(b, 16))
81 #define SSE2_INTERLEAVE_EVEN_EPI16(a, b) SSE2_INTERLEAVE_ODD_EPI16(_mm_slli_si128(a, 2), _mm_slli_si128(b, 2))
82 #define SSE2_MULU_ODD_EPI8(a, b) _mm_mullo_epi16(_mm_srli_epi16(a, 8), _mm_srai_epi16(b, 8))
83 #define SSE2_MULU_EVEN_EPI8(a, b) _mm_mullo_epi16(_mm_and_si128(a, _mm_set1_epi16(0xFF)), _mm_srai_epi16(_mm_slli_si128(b, 1), 8))
84
85 #define SSE2_HADDS_EPI16(a, b) _mm_adds_epi16(SSE2_INTERLEAVE_EVEN_EPI16(a, b), SSE2_INTERLEAVE_ODD_EPI16(a, b))
86 #define SSE2_MADDUBS_EPI16(a, b) _mm_adds_epi16(SSE2_MULU_EVEN_EPI8(a, b), SSE2_MULU_ODD_EPI8(a, b))
87
88 #ifndef USE_ROLL_ASM
89 __attribute__ ((target("default"))) MVSTATIC int32 get_checksum1_avx2_64(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2) { return i; }
90 #endif
91 __attribute__ ((target("default"))) MVSTATIC int32 get_checksum1_ssse3_32(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2) { return i; }
92 __attribute__ ((target("default"))) MVSTATIC int32 get_checksum1_sse2_32(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2) { return i; }
93
94 /*
95 Original loop per 4 bytes:
96 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
97 s1 += buf[i] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET;
98
99 SSE2/SSSE3 loop per 32 bytes:
100 int16 t1[8];
101 int16 t2[8];
102 for (int j = 0; j < 8; j++) {
103 t1[j] = buf[j*4 + i] + buf[j*4 + i+1] + buf[j*4 + i+2] + buf[j*4 + i+3];
104 t2[j] = 4*buf[j*4 + i] + 3*buf[j*4 + i+1] + 2*buf[j*4 + i+2] + buf[j*4 + i+3];
105 }
106 s2 += 32*s1 + (uint32)(
107 28*t1[0] + 24*t1[1] + 20*t1[2] + 16*t1[3] + 12*t1[4] + 8*t1[5] + 4*t1[6] +
108 t2[0] + t2[1] + t2[2] + t2[3] + t2[4] + t2[5] + t2[6] + t2[7]
109 ) + 528*CHAR_OFFSET;
110 s1 += (uint32)(t1[0] + t1[1] + t1[2] + t1[3] + t1[4] + t1[5] + t1[6] + t1[7]) +
111 32*CHAR_OFFSET;
112 */
113 __attribute__ ((target("ssse3"))) MVSTATIC int32 get_checksum1_ssse3_32(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2)
114 {
115 if (len > 32) {
116 int aligned = ((uintptr_t)buf & 15) == 0;
117
118 uint32 x[4] = {0};
119 x[0] = *ps1;
120 __m128i ss1 = _mm_loadu_si128((__m128i_u*)x);
121 x[0] = *ps2;
122 __m128i ss2 = _mm_loadu_si128((__m128i_u*)x);
123
124 const int16 mul_t1_buf[8] = {28, 24, 20, 16, 12, 8, 4, 0};
125 __m128i mul_t1 = _mm_loadu_si128((__m128i_u*)mul_t1_buf);
126
127 for (; i < (len-32); i+=32) {
128 // Load ... 2*[int8*16]
129 __m128i in8_1, in8_2;
130 if (!aligned) {
131 // Synonymous with _mm_loadu_si128 on all but a handful of old CPUs
132 in8_1 = _mm_lddqu_si128((__m128i_u*)&buf[i]);
133 in8_2 = _mm_lddqu_si128((__m128i_u*)&buf[i + 16]);
134 } else {
135 in8_1 = _mm_load_si128((__m128i_u*)&buf[i]);
136 in8_2 = _mm_load_si128((__m128i_u*)&buf[i + 16]);
137 }
138
139 // (1*buf[i] + 1*buf[i+1]), (1*buf[i+2], 1*buf[i+3]), ... 2*[int16*8]
140 // Fastest, even though multiply by 1
141 __m128i mul_one = _mm_set1_epi8(1);
142 __m128i add16_1 = _mm_maddubs_epi16(mul_one, in8_1);
143 __m128i add16_2 = _mm_maddubs_epi16(mul_one, in8_2);
144
145 // (4*buf[i] + 3*buf[i+1]), (2*buf[i+2], buf[i+3]), ... 2*[int16*8]
146 __m128i mul_const = _mm_set1_epi32(4 + (3 << 8) + (2 << 16) + (1 << 24));
147 __m128i mul_add16_1 = _mm_maddubs_epi16(mul_const, in8_1);
148 __m128i mul_add16_2 = _mm_maddubs_epi16(mul_const, in8_2);
149
150 // s2 += 32*s1
151 ss2 = _mm_add_epi32(ss2, _mm_slli_epi32(ss1, 5));
152
153 // [sum(t1[0]..t1[7]), X, X, X] [int32*4]; faster than multiple _mm_hadds_epi16
154 // Shifting left, then shifting right again and shuffling (rather than just
155 // shifting right as with mul32 below) to cheaply end up with the correct sign
156 // extension as we go from int16 to int32.
157 __m128i sum_add32 = _mm_add_epi16(add16_1, add16_2);
158 sum_add32 = _mm_add_epi16(sum_add32, _mm_slli_si128(sum_add32, 2));
159 sum_add32 = _mm_add_epi16(sum_add32, _mm_slli_si128(sum_add32, 4));
160 sum_add32 = _mm_add_epi16(sum_add32, _mm_slli_si128(sum_add32, 8));
161 sum_add32 = _mm_srai_epi32(sum_add32, 16);
162 sum_add32 = _mm_shuffle_epi32(sum_add32, 3);
163
164 // [sum(t2[0]..t2[7]), X, X, X] [int32*4]; faster than multiple _mm_hadds_epi16
165 __m128i sum_mul_add32 = _mm_add_epi16(mul_add16_1, mul_add16_2);
166 sum_mul_add32 = _mm_add_epi16(sum_mul_add32, _mm_slli_si128(sum_mul_add32, 2));
167 sum_mul_add32 = _mm_add_epi16(sum_mul_add32, _mm_slli_si128(sum_mul_add32, 4));
168 sum_mul_add32 = _mm_add_epi16(sum_mul_add32, _mm_slli_si128(sum_mul_add32, 8));
169 sum_mul_add32 = _mm_srai_epi32(sum_mul_add32, 16);
170 sum_mul_add32 = _mm_shuffle_epi32(sum_mul_add32, 3);
171
172 // s1 += t1[0] + t1[1] + t1[2] + t1[3] + t1[4] + t1[5] + t1[6] + t1[7]
173 ss1 = _mm_add_epi32(ss1, sum_add32);
174
175 // s2 += t2[0] + t2[1] + t2[2] + t2[3] + t2[4] + t2[5] + t2[6] + t2[7]
176 ss2 = _mm_add_epi32(ss2, sum_mul_add32);
177
178 // [t1[0] + t1[1], t1[2] + t1[3] ...] [int16*8]
179 // We could've combined this with generating sum_add32 above and
180 // save an instruction but benchmarking shows that as being slower
181 __m128i add16 = _mm_hadds_epi16(add16_1, add16_2);
182
183 // [t1[0], t1[1], ...] -> [t1[0]*28 + t1[1]*24, ...] [int32*4]
184 __m128i mul32 = _mm_madd_epi16(add16, mul_t1);
185
186 // [sum(mul32), X, X, X] [int32*4]; faster than multiple _mm_hadd_epi32
187 mul32 = _mm_add_epi32(mul32, _mm_srli_si128(mul32, 4));
188 mul32 = _mm_add_epi32(mul32, _mm_srli_si128(mul32, 8));
189
190 // s2 += 28*t1[0] + 24*t1[1] + 20*t1[2] + 16*t1[3] + 12*t1[4] + 8*t1[5] + 4*t1[6]
191 ss2 = _mm_add_epi32(ss2, mul32);
192
193 #if CHAR_OFFSET != 0
194 // s1 += 32*CHAR_OFFSET
195 __m128i char_offset_multiplier = _mm_set1_epi32(32 * CHAR_OFFSET);
196 ss1 = _mm_add_epi32(ss1, char_offset_multiplier);
197
198 // s2 += 528*CHAR_OFFSET
199 char_offset_multiplier = _mm_set1_epi32(528 * CHAR_OFFSET);
200 ss2 = _mm_add_epi32(ss2, char_offset_multiplier);
201 #endif
202 }
203
204 _mm_store_si128((__m128i_u*)x, ss1);
205 *ps1 = x[0];
206 _mm_store_si128((__m128i_u*)x, ss2);
207 *ps2 = x[0];
208 }
209 return i;
210 }
211
212 /*
213 Same as SSSE3 version, but using macros defined above to emulate SSSE3 calls that are not available with SSE2.
214 For GCC-only the SSE2 and SSSE3 versions could be a single function calling other functions with the right
215 target attributes to emulate SSSE3 calls on SSE2 if needed, but clang doesn't inline those properly leading
216 to a near 50% performance drop.
217 */
218 __attribute__ ((target("sse2"))) MVSTATIC int32 get_checksum1_sse2_32(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2)
219 {
220 if (len > 32) {
221 int aligned = ((uintptr_t)buf & 15) == 0;
222
223 uint32 x[4] = {0};
224 x[0] = *ps1;
225 __m128i ss1 = _mm_loadu_si128((__m128i_u*)x);
226 x[0] = *ps2;
227 __m128i ss2 = _mm_loadu_si128((__m128i_u*)x);
228
229 const int16 mul_t1_buf[8] = {28, 24, 20, 16, 12, 8, 4, 0};
230 __m128i mul_t1 = _mm_loadu_si128((__m128i_u*)mul_t1_buf);
231
232 for (; i < (len-32); i+=32) {
233 // Load ... 2*[int8*16]
234 __m128i in8_1, in8_2;
235 if (!aligned) {
236 in8_1 = _mm_loadu_si128((__m128i_u*)&buf[i]);
237 in8_2 = _mm_loadu_si128((__m128i_u*)&buf[i + 16]);
238 } else {
239 in8_1 = _mm_load_si128((__m128i_u*)&buf[i]);
240 in8_2 = _mm_load_si128((__m128i_u*)&buf[i + 16]);
241 }
242
243 // (1*buf[i] + 1*buf[i+1]), (1*buf[i+2], 1*buf[i+3]), ... 2*[int16*8]
244 // Fastest, even though multiply by 1
245 __m128i mul_one = _mm_set1_epi8(1);
246 __m128i add16_1 = SSE2_MADDUBS_EPI16(mul_one, in8_1);
247 __m128i add16_2 = SSE2_MADDUBS_EPI16(mul_one, in8_2);
248
249 // (4*buf[i] + 3*buf[i+1]), (2*buf[i+2], buf[i+3]), ... 2*[int16*8]
250 __m128i mul_const = _mm_set1_epi32(4 + (3 << 8) + (2 << 16) + (1 << 24));
251 __m128i mul_add16_1 = SSE2_MADDUBS_EPI16(mul_const, in8_1);
252 __m128i mul_add16_2 = SSE2_MADDUBS_EPI16(mul_const, in8_2);
253
254 // s2 += 32*s1
255 ss2 = _mm_add_epi32(ss2, _mm_slli_epi32(ss1, 5));
256
257 // [sum(t1[0]..t1[7]), X, X, X] [int32*4]; faster than multiple _mm_hadds_epi16
258 // Shifting left, then shifting right again and shuffling (rather than just
259 // shifting right as with mul32 below) to cheaply end up with the correct sign
260 // extension as we go from int16 to int32.
261 __m128i sum_add32 = _mm_add_epi16(add16_1, add16_2);
262 sum_add32 = _mm_add_epi16(sum_add32, _mm_slli_si128(sum_add32, 2));
263 sum_add32 = _mm_add_epi16(sum_add32, _mm_slli_si128(sum_add32, 4));
264 sum_add32 = _mm_add_epi16(sum_add32, _mm_slli_si128(sum_add32, 8));
265 sum_add32 = _mm_srai_epi32(sum_add32, 16);
266 sum_add32 = _mm_shuffle_epi32(sum_add32, 3);
267
268 // [sum(t2[0]..t2[7]), X, X, X] [int32*4]; faster than multiple _mm_hadds_epi16
269 __m128i sum_mul_add32 = _mm_add_epi16(mul_add16_1, mul_add16_2);
270 sum_mul_add32 = _mm_add_epi16(sum_mul_add32, _mm_slli_si128(sum_mul_add32, 2));
271 sum_mul_add32 = _mm_add_epi16(sum_mul_add32, _mm_slli_si128(sum_mul_add32, 4));
272 sum_mul_add32 = _mm_add_epi16(sum_mul_add32, _mm_slli_si128(sum_mul_add32, 8));
273 sum_mul_add32 = _mm_srai_epi32(sum_mul_add32, 16);
274 sum_mul_add32 = _mm_shuffle_epi32(sum_mul_add32, 3);
275
276 // s1 += t1[0] + t1[1] + t1[2] + t1[3] + t1[4] + t1[5] + t1[6] + t1[7]
277 ss1 = _mm_add_epi32(ss1, sum_add32);
278
279 // s2 += t2[0] + t2[1] + t2[2] + t2[3] + t2[4] + t2[5] + t2[6] + t2[7]
280 ss2 = _mm_add_epi32(ss2, sum_mul_add32);
281
282 // [t1[0] + t1[1], t1[2] + t1[3] ...] [int16*8]
283 // We could've combined this with generating sum_add32 above and
284 // save an instruction but benchmarking shows that as being slower
285 __m128i add16 = SSE2_HADDS_EPI16(add16_1, add16_2);
286
287 // [t1[0], t1[1], ...] -> [t1[0]*28 + t1[1]*24, ...] [int32*4]
288 __m128i mul32 = _mm_madd_epi16(add16, mul_t1);
289
290 // [sum(mul32), X, X, X] [int32*4]; faster than multiple _mm_hadd_epi32
291 mul32 = _mm_add_epi32(mul32, _mm_srli_si128(mul32, 4));
292 mul32 = _mm_add_epi32(mul32, _mm_srli_si128(mul32, 8));
293
294 // s2 += 28*t1[0] + 24*t1[1] + 20*t1[2] + 16*t1[3] + 12*t1[4] + 8*t1[5] + 4*t1[6]
295 ss2 = _mm_add_epi32(ss2, mul32);
296
297 #if CHAR_OFFSET != 0
298 // s1 += 32*CHAR_OFFSET
299 __m128i char_offset_multiplier = _mm_set1_epi32(32 * CHAR_OFFSET);
300 ss1 = _mm_add_epi32(ss1, char_offset_multiplier);
301
302 // s2 += 528*CHAR_OFFSET
303 char_offset_multiplier = _mm_set1_epi32(528 * CHAR_OFFSET);
304 ss2 = _mm_add_epi32(ss2, char_offset_multiplier);
305 #endif
306 }
307
308 _mm_store_si128((__m128i_u*)x, ss1);
309 *ps1 = x[0];
310 _mm_store_si128((__m128i_u*)x, ss2);
311 *ps2 = x[0];
312 }
313 return i;
314 }
315
316 #ifdef USE_ROLL_ASM /* { */
317
318 extern "C" __attribute__ ((target("avx2"))) int32 get_checksum1_avx2_asm(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2);
319
320 /* The asm routine is AVX2-only and, unlike the multi-versioned intrinsic
321 * paths, has no compiler-generated fallback, so it must not be called on a
322 * CPU without AVX2 (it would fault with SIGILL). Gate it on a cached runtime
323 * check; when AVX2 is absent we skip it and the SSSE3/SSE2/scalar steps,
324 * which are safe everywhere, do all the work. */
325 static int roll_asm_have_avx2(void)
326 {
327 static int have = -1;
328 if (have < 0) {
329 __builtin_cpu_init();
330 have = __builtin_cpu_supports("avx2") ? 1 : 0;
331 }
332 return have;
333 }
334
335 #else /* } { */
336
337 /*
338 AVX2 loop per 64 bytes:
339 int16 t1[16];
340 int16 t2[16];
341 for (int j = 0; j < 16; j++) {
342 t1[j] = buf[j*4 + i] + buf[j*4 + i+1] + buf[j*4 + i+2] + buf[j*4 + i+3];
343 t2[j] = 4*buf[j*4 + i] + 3*buf[j*4 + i+1] + 2*buf[j*4 + i+2] + buf[j*4 + i+3];
344 }
345 s2 += 64*s1 + (uint32)(
346 60*t1[0] + 56*t1[1] + 52*t1[2] + 48*t1[3] + 44*t1[4] + 40*t1[5] + 36*t1[6] + 32*t1[7] + 28*t1[8] + 24*t1[9] + 20*t1[10] + 16*t1[11] + 12*t1[12] + 8*t1[13] + 4*t1[14] +
347 t2[0] + t2[1] + t2[2] + t2[3] + t2[4] + t2[5] + t2[6] + t2[7] + t2[8] + t2[9] + t2[10] + t2[11] + t2[12] + t2[13] + t2[14] + t2[15]
348 ) + 2080*CHAR_OFFSET;
349 s1 += (uint32)(t1[0] + t1[1] + t1[2] + t1[3] + t1[4] + t1[5] + t1[6] + t1[7] + t1[8] + t1[9] + t1[10] + t1[11] + t1[12] + t1[13] + t1[14] + t1[15]) +
350 64*CHAR_OFFSET;
351 */
352
353 __attribute__ ((target("avx2"))) MVSTATIC int32 get_checksum1_avx2_64(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2)
354 {
355 if (len > 64) {
356
357 uint32 x[4] = {0};
358 __m128i ss1 = _mm_cvtsi32_si128(*ps1);
359 __m128i ss2 = _mm_cvtsi32_si128(*ps2);
360
361 const char mul_t1_buf[16] = {60, 56, 52, 48, 44, 40, 36, 32, 28, 24, 20, 16, 12, 8, 4, 0};
362 __m128i tmp = _mm_load_si128((__m128i*) mul_t1_buf);
363 __m256i mul_t1 = _mm256_cvtepu8_epi16(tmp);
364 __m256i mul_const = _mm256_broadcastd_epi32(_mm_cvtsi32_si128(4 | (3 << 8) | (2 << 16) | (1 << 24)));
365 __m256i mul_one = _mm256_set1_epi8(1);
366
367 for (; i < (len-64); i+=64) {
368 // Load ... 4*[int8*16]
369 __m256i in8_1, in8_2;
370 __m128i in8_1_low, in8_2_low, in8_1_high, in8_2_high;
371 in8_1_low = _mm_loadu_si128((__m128i_u*)&buf[i]);
372 in8_2_low = _mm_loadu_si128((__m128i_u*)&buf[i+16]);
373 in8_1_high = _mm_loadu_si128((__m128i_u*)&buf[i+32]);
374 in8_2_high = _mm_loadu_si128((__m128i_u*)&buf[i+48]);
375 in8_1 = _mm256_inserti128_si256(_mm256_castsi128_si256(in8_1_low), in8_1_high,1);
376 in8_2 = _mm256_inserti128_si256(_mm256_castsi128_si256(in8_2_low), in8_2_high,1);
377
378 // (1*buf[i] + 1*buf[i+1]), (1*buf[i+2], 1*buf[i+3]), ... 2*[int16*8]
379 // Fastest, even though multiply by 1
380 __m256i add16_1 = _mm256_maddubs_epi16(mul_one, in8_1);
381 __m256i add16_2 = _mm256_maddubs_epi16(mul_one, in8_2);
382
383 // (4*buf[i] + 3*buf[i+1]), (2*buf[i+2], buf[i+3]), ... 2*[int16*8]
384 __m256i mul_add16_1 = _mm256_maddubs_epi16(mul_const, in8_1);
385 __m256i mul_add16_2 = _mm256_maddubs_epi16(mul_const, in8_2);
386
387 // s2 += 64*s1
388 ss2 = _mm_add_epi32(ss2, _mm_slli_epi32(ss1, 6));
389
390 // [sum(t1[0]..t1[7]), X, X, X] [int32*4]; faster than multiple _mm_hadds_epi16
391 __m256i sum_add32 = _mm256_add_epi16(add16_1, add16_2);
392 sum_add32 = _mm256_add_epi16(sum_add32, _mm256_srli_epi32(sum_add32, 16));
393 sum_add32 = _mm256_add_epi16(sum_add32, _mm256_srli_si256(sum_add32, 4));
394 sum_add32 = _mm256_add_epi16(sum_add32, _mm256_srli_si256(sum_add32, 8));
395
396 // [sum(t2[0]..t2[7]), X, X, X] [int32*4]; faster than multiple _mm_hadds_epi16
397 __m256i sum_mul_add32 = _mm256_add_epi16(mul_add16_1, mul_add16_2);
398 sum_mul_add32 = _mm256_add_epi16(sum_mul_add32, _mm256_srli_epi32(sum_mul_add32, 16));
399 sum_mul_add32 = _mm256_add_epi16(sum_mul_add32, _mm256_srli_si256(sum_mul_add32, 4));
400 sum_mul_add32 = _mm256_add_epi16(sum_mul_add32, _mm256_srli_si256(sum_mul_add32, 8));
401
402 // s1 += t1[0] + t1[1] + t1[2] + t1[3] + t1[4] + t1[5] + t1[6] + t1[7]
403 __m128i sum_add32_hi = _mm256_extracti128_si256(sum_add32, 0x1);
404 ss1 = _mm_add_epi32(ss1, _mm256_castsi256_si128(sum_add32));
405 ss1 = _mm_add_epi32(ss1, sum_add32_hi);
406
407 // s2 += t2[0] + t2[1] + t2[2] + t2[3] + t2[4] + t2[5] + t2[6] + t2[7]
408 __m128i sum_mul_add32_hi = _mm256_extracti128_si256(sum_mul_add32, 0x1);
409 ss2 = _mm_add_epi32(ss2, _mm256_castsi256_si128(sum_mul_add32));
410 ss2 = _mm_add_epi32(ss2, sum_mul_add32_hi);
411
412 // [t1[0] + t1[1], t1[2] + t1[3] ...] [int16*8]
413 // We could've combined this with generating sum_add32 above and
414 // save an instruction but benchmarking shows that as being slower
415 __m256i add16 = _mm256_hadds_epi16(add16_1, add16_2);
416
417 // [t1[0], t1[1], ...] -> [t1[0]*28 + t1[1]*24, ...] [int32*4]
418 __m256i mul32 = _mm256_madd_epi16(add16, mul_t1);
419
420 // [sum(mul32), X, X, X] [int32*4]; faster than multiple _mm_hadd_epi32
421 mul32 = _mm256_add_epi32(mul32, _mm256_srli_si256(mul32, 4));
422 mul32 = _mm256_add_epi32(mul32, _mm256_srli_si256(mul32, 8));
423 // prefetch 2 cacheline ahead
424 _mm_prefetch(&buf[i + 160], _MM_HINT_T0);
425
426 // s2 += 28*t1[0] + 24*t1[1] + 20*t1[2] + 16*t1[3] + 12*t1[4] + 8*t1[5] + 4*t1[6]
427 __m128i mul32_hi = _mm256_extracti128_si256(mul32, 0x1);
428 ss2 = _mm_add_epi32(ss2, _mm256_castsi256_si128(mul32));
429 ss2 = _mm_add_epi32(ss2, mul32_hi);
430
431 #if CHAR_OFFSET != 0
432 // s1 += 32*CHAR_OFFSET
433 __m128i char_offset_multiplier = _mm_set1_epi32(32 * CHAR_OFFSET);
434 ss1 = _mm_add_epi32(ss1, char_offset_multiplier);
435
436 // s2 += 528*CHAR_OFFSET
437 char_offset_multiplier = _mm_set1_epi32(528 * CHAR_OFFSET);
438 ss2 = _mm_add_epi32(ss2, char_offset_multiplier);
439 #endif
440 }
441
442 _mm_store_si128((__m128i_u*)x, ss1);
443 *ps1 = x[0];
444 _mm_store_si128((__m128i_u*)x, ss2);
445 *ps2 = x[0];
446 }
447 return i;
448 }
449
450 #endif /* } !USE_ROLL_ASM */
451
452 static int32 get_checksum1_default_1(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2)
453 {
454 uint32 s1 = *ps1;
455 uint32 s2 = *ps2;
456 for (; i < (len-4); i+=4) {
457 s2 += 4*(s1 + buf[i]) + 3*buf[i+1] + 2*buf[i+2] + buf[i+3] + 10*CHAR_OFFSET;
458 s1 += (buf[i+0] + buf[i+1] + buf[i+2] + buf[i+3] + 4*CHAR_OFFSET);
459 }
460 for (; i < len; i++) {
461 s1 += (buf[i]+CHAR_OFFSET); s2 += s1;
462 }
463 *ps1 = s1;
464 *ps2 = s2;
465 return i;
466 }
467
468 /* With GCC 10 putting this implementation inside 'extern "C"' causes an
469 assembler error. That worked fine on GCC 5-9 and clang 6-10...
470 */
471 static inline uint32 get_checksum1_cpp(char *buf1, int32 len)
472 {
473 int32 i = 0;
474 uint32 s1 = 0;
475 uint32 s2 = 0;
476
477 // multiples of 64 bytes using AVX2 (if available)
478 #ifdef USE_ROLL_ASM
479 if (roll_asm_have_avx2())
480 i = get_checksum1_avx2_asm((schar*)buf1, len, i, &s1, &s2);
481 #else
482 i = get_checksum1_avx2_64((schar*)buf1, len, i, &s1, &s2);
483 #endif
484
485 // multiples of 32 bytes using SSSE3 (if available)
486 i = get_checksum1_ssse3_32((schar*)buf1, len, i, &s1, &s2);
487
488 // multiples of 32 bytes using SSE2 (if available)
489 i = get_checksum1_sse2_32((schar*)buf1, len, i, &s1, &s2);
490
491 // whatever is left (updates s1/s2; the returned offset is unused here)
492 get_checksum1_default_1((schar*)buf1, len, i, &s1, &s2);
493
494 return (s1 & 0xffff) + (s2 << 16);
495 }
496
497 extern "C" {
498
499 uint32 get_checksum1(char *buf1, int32 len)
500 {
501 return get_checksum1_cpp(buf1, len);
502 }
503
504 } // extern "C"
505
506 #ifdef BENCHMARK_SIMD_CHECKSUM1
507 #pragma clang optimize off
508 #pragma GCC push_options
509 #pragma GCC optimize ("O0")
510
511 #define ROUNDS 1024
512 #define BLOCK_LEN 1024*1024
513
514 #ifndef CLOCK_MONOTONIC_RAW
515 #define CLOCK_MONOTONIC_RAW CLOCK_MONOTONIC
516 #endif
517
518 static void benchmark(const char* desc, int32 (*func)(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2), schar* buf, int32 len) {
519 struct timespec start, end;
520 uint64_t us;
521 uint32_t cs, s1, s2;
522 int i, next;
523
524 clock_gettime(CLOCK_MONOTONIC_RAW, &start);
525 for (i = 0; i < ROUNDS; i++) {
526 s1 = s2 = 0;
527 next = func((schar*)buf, len, 0, &s1, &s2);
528 get_checksum1_default_1((schar*)buf, len, next, &s1, &s2);
529 }
530 clock_gettime(CLOCK_MONOTONIC_RAW, &end);
531 us = next == 0 ? 0 : (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_nsec - start.tv_nsec) / 1000;
532 cs = next == 0 ? 0 : (s1 & 0xffff) + (s2 << 16);
533 printf("%-5s :: %5.0f MB/s :: %08x\n", desc, us ? (float)(len / (1024 * 1024) * ROUNDS) / ((float)us / 1000000.0f) : 0, cs);
534 }
535
536 static int32 get_checksum1_auto(schar* buf, int32 len, int32 i, uint32* ps1, uint32* ps2) {
537 uint32 cs = get_checksum1((char*)buf, len);
538 *ps1 = cs & 0xffff;
539 *ps2 = cs >> 16;
540 return len;
541 }
542
543 int main() {
544 int i;
545 unsigned char* buf = (unsigned char*)aligned_alloc(64,BLOCK_LEN);
546 for (i = 0; i < BLOCK_LEN; i++) buf[i] = (i + (i % 3) + (i % 11)) % 256;
547
548 benchmark("Auto", get_checksum1_auto, (schar*)buf, BLOCK_LEN);
549 benchmark("Raw-C", get_checksum1_default_1, (schar*)buf, BLOCK_LEN);
550 benchmark("SSE2", get_checksum1_sse2_32, (schar*)buf, BLOCK_LEN);
551 benchmark("SSSE3", get_checksum1_ssse3_32, (schar*)buf, BLOCK_LEN);
552 #ifdef USE_ROLL_ASM
553 benchmark("AVX2-ASM", get_checksum1_avx2_asm, (schar*)buf, BLOCK_LEN);
554 #else
555 benchmark("AVX2", get_checksum1_avx2_64, (schar*)buf, BLOCK_LEN);
556 #endif
557
558 free(buf);
559 return 0;
560 }
561
562 #pragma GCC pop_options
563 #pragma clang optimize on
564 #endif /* BENCHMARK_SIMD_CHECKSUM1 */
565
566 #ifdef TEST_SIMD_CHECKSUM1
567 #include <sys/mman.h>
568 #include <unistd.h>
569
570 static uint32 checksum_via_default(char *buf, int32 len)
571 {
572 uint32 s1 = 0, s2 = 0;
573 get_checksum1_default_1((schar*)buf, len, 0, &s1, &s2);
574 return (s1 & 0xffff) + (s2 << 16);
575 }
576
577 static uint32 checksum_via_sse2(char *buf, int32 len)
578 {
579 int32 i;
580 uint32 s1 = 0, s2 = 0;
581 i = get_checksum1_sse2_32((schar*)buf, len, 0, &s1, &s2);
582 get_checksum1_default_1((schar*)buf, len, i, &s1, &s2);
583 return (s1 & 0xffff) + (s2 << 16);
584 }
585
586 static uint32 checksum_via_ssse3(char *buf, int32 len)
587 {
588 int32 i;
589 uint32 s1 = 0, s2 = 0;
590 i = get_checksum1_ssse3_32((schar*)buf, len, 0, &s1, &s2);
591 get_checksum1_default_1((schar*)buf, len, i, &s1, &s2);
592 return (s1 & 0xffff) + (s2 << 16);
593 }
594
595 static uint32 checksum_via_avx2(char *buf, int32 len)
596 {
597 int32 i;
598 uint32 s1 = 0, s2 = 0;
599 #ifdef USE_ROLL_ASM
600 if (roll_asm_have_avx2())
601 i = get_checksum1_avx2_asm((schar*)buf, len, 0, &s1, &s2);
602 else
603 i = 0;
604 #else
605 i = get_checksum1_avx2_64((schar*)buf, len, 0, &s1, &s2);
606 #endif
607 get_checksum1_default_1((schar*)buf, len, i, &s1, &s2);
608 return (s1 & 0xffff) + (s2 << 16);
609 }
610
611
612 /* Run every implementation on a buffer placed flush against an unreadable page,
613 * so any read past buf+len faults here instead of in a user's transfer. The
614 * buffers above deliberately carry 64 spare bytes for the unaligned case, which
615 * is exactly what let a 64-byte over-read in the AVX2 assembly go unnoticed:
616 * it only crashed when an allocation happened to end at a page boundary. */
617 static int test_no_overread(void)
618 {
619 #if defined HAVE_SYS_MMAN_H || defined __linux__ || defined __APPLE__ || defined BSD
620 /* Setting the guard up is not optional on the platforms this is compiled
621 * for: if it fails we are not testing what the caller thinks, so say so and
622 * fail rather than return a pass that looks identical to a real one. */
623 long pagesz = sysconf(_SC_PAGESIZE);
624 if (pagesz <= 0) {
625 printf("FAIL guard-page: sysconf(_SC_PAGESIZE) gave %ld\n", pagesz);
626 return 1;
627 }
628 size_t region = (size_t)pagesz * 4;
629 char *base = (char *)mmap(NULL, region, PROT_READ | PROT_WRITE,
630 MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
631 if (base == MAP_FAILED) {
632 printf("FAIL guard-page: mmap failed\n");
633 return 1;
634 }
635 if (mprotect(base + region - pagesz, pagesz, PROT_NONE) != 0) {
636 printf("FAIL guard-page: mprotect failed\n");
637 munmap(base, region);
638 return 1;
639 }
640 /* Whether the ASSEMBLY was reached is a separate question from whether the
641 * checks passed. Without AVX2 the dispatcher falls back and the guard loop
642 * proves nothing about it, which must not read as coverage. */
643 __builtin_cpu_init();
644 printf("guard-page: AVX2 %s\n",
645 __builtin_cpu_supports("avx2")
646 ? "present, assembly and intrinsics exercised"
647 : "ABSENT -- fallback only, the AVX2 paths were NOT exercised");
648 int failures = 0;
649 /* 128 is the shortest the AVX2 paths will touch; step by 1 so every
650 * remainder mod 64, and both alignments, get covered. */
651 for (int32 len = 128; len <= 4096; len++) {
652 char *buf = base + region - pagesz - len; /* last byte abuts the guard */
653 for (int32 i = 0; i < len; i++)
654 buf[i] = (char)((i + (i % 3) + (i % 11)) % 256);
655 uint32 ref = checksum_via_default(buf, len);
656 if (checksum_via_sse2(buf, len) != ref
657 || checksum_via_ssse3(buf, len) != ref
658 || checksum_via_avx2(buf, len) != ref
659 || get_checksum1(buf, len) != ref) {
660 printf("FAIL guard-page size=%5d: mismatch at the page boundary\n", len);
661 if (++failures > 4)
662 break;
663 }
664 }
665 munmap(base, region);
666 return failures;
667 #else
668 printf("guard-page: no mmap on this platform, over-read check NOT RUN\n");
669 return 0;
670 #endif
671 }
672
673 int main()
674 {
675 static const int sizes[] = {1, 4, 31, 32, 33, 63, 64, 65, 128, 129, 256, 700, 1024, 4096, 65536};
676 int num_sizes = sizeof(sizes) / sizeof(sizes[0]);
677 int max_size = sizes[num_sizes - 1];
678 int failures = 0;
679
680 /* Allocate with extra bytes for unaligned test */
681 unsigned char *raw = (unsigned char *)malloc(max_size + 64 + 1);
682 if (!raw) {
683 fprintf(stderr, "malloc failed\n");
684 return 1;
685 }
686
687 /* Fill with deterministic data */
688 for (int i = 0; i < max_size + 64 + 1; i++)
689 raw[i] = (i + (i % 3) + (i % 11)) % 256;
690
691 /* Test with aligned buffer (64-byte aligned) */
692 unsigned char *aligned = raw + (64 - ((uintptr_t)raw % 64));
693
694 /* Test with unaligned buffer (+1 byte offset) */
695 unsigned char *unaligned = aligned + 1;
696
697 struct { const char *name; unsigned char *buf; } buffers[] = {
698 {"aligned", aligned},
699 {"unaligned", unaligned},
700 };
701
702 for (int b = 0; b < 2; b++) {
703 char *buf = (char *)buffers[b].buf;
704 const char *bname = buffers[b].name;
705
706 for (int s = 0; s < num_sizes; s++) {
707 int32 len = sizes[s];
708 uint32 ref = checksum_via_default(buf, len);
709 uint32 cs_sse2 = checksum_via_sse2(buf, len);
710 uint32 cs_ssse3 = checksum_via_ssse3(buf, len);
711 uint32 cs_avx2 = checksum_via_avx2(buf, len);
712 uint32 cs_auto = get_checksum1(buf, len);
713
714 if (cs_sse2 != ref) {
715 printf("FAIL %-9s size=%5d: SSE2=%08x ref=%08x\n", bname, len, cs_sse2, ref);
716 failures++;
717 }
718 if (cs_ssse3 != ref) {
719 printf("FAIL %-9s size=%5d: SSSE3=%08x ref=%08x\n", bname, len, cs_ssse3, ref);
720 failures++;
721 }
722 if (cs_avx2 != ref) {
723 printf("FAIL %-9s size=%5d: AVX2=%08x ref=%08x\n", bname, len, cs_avx2, ref);
724 failures++;
725 }
726 if (cs_auto != ref) {
727 printf("FAIL %-9s size=%5d: auto=%08x ref=%08x\n", bname, len, cs_auto, ref);
728 failures++;
729 }
730 }
731 }
732
733 free(raw);
734
735 failures += test_no_overread();
736
737 if (failures) {
738 printf("%d checksum mismatches!\n", failures);
739 return 1;
740 }
741 printf("All SIMD checksum tests passed.\n");
742 return 0;
743 }
744
745 #endif /* TEST_SIMD_CHECKSUM1 */
746
747 #endif /* } USE_ROLL_SIMD */
748 #endif /* } __cplusplus */
749 #endif /* } __x86_64__ */