Debian Codegraph @

ext/zlib-ng/arch/x86/adler32_avx2_tpl.h

raw symbols SCIP
1 /* adler32_avx2_tpl.h -- adler32 avx2 vectorized function templates
2 * Copyright (C) 2022 Adam Stylinski
3 * For conditions of distribution and use, see copyright notice in zlib.h
4 */
5
6 #include "../../zbuild.h"
7 #include <immintrin.h>
8 #include "../../adler32_fold.h"
9 #include "../../adler32_p.h"
10 #include "../../fallback_builtins.h"
11 #include "adler32_avx2_p.h"
12
13 #ifdef X86_SSE42_ADLER32
14 extern uint32_t adler32_fold_copy_sse42(uint32_t adler, uint8_t *dst, const uint8_t *src, uint64_t len);
15 extern uint32_t adler32_ssse3(uint32_t adler, const uint8_t *src, uint64_t len);
16
17 #define copy_sub32(a, b, c, d) adler32_fold_copy_sse42(a, b, c, d)
18 #define sub32(a, b, c) adler32_ssse3(a, b, c)
19 #else
20 #define copy_sub32(a, b, c, d) adler32_copy_len_16(adler0, c, b, d, adler1)
21 #define sub32(a, b, c) adler32_len_16(adler0, b, c, adler1)
22 #endif
23
24 #ifdef COPY
25 Z_INTERNAL uint32_t adler32_fold_copy_avx2(uint32_t adler, uint8_t *dst, const uint8_t *src, uint64_t len) {
26 #else
27 Z_INTERNAL uint32_t adler32_avx2(uint32_t adler, const uint8_t *src, uint64_t len) {
28 #endif
29 if (src == NULL) return 1L;
30 if (len == 0) return adler;
31
32 uint32_t adler0, adler1;
33 adler1 = (adler >> 16) & 0xffff;
34 adler0 = adler & 0xffff;
35
36 rem_peel:
37 if (len < 16) {
38 #ifdef COPY
39 return adler32_copy_len_16(adler0, src, dst, len, adler1);
40 #else
41 return adler32_len_16(adler0, src, len, adler1);
42 #endif
43 } else if (len < 32) {
44 #ifdef COPY
45 return copy_sub32(adler, dst, src, len);
46 #else
47 return sub32(adler, src, len);
48 #endif
49 }
50
51 __m256i vs1, vs2;
52
53 const __m256i dot2v = _mm256_setr_epi8(32, 31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
54 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1);
55 const __m256i dot3v = _mm256_set1_epi16(1);
56 const __m256i zero = _mm256_setzero_si256();
57
58 while (len >= 32) {
59 vs1 = _mm256_zextsi128_si256(_mm_cvtsi32_si128(adler0));
60 vs2 = _mm256_zextsi128_si256(_mm_cvtsi32_si128(adler1));
61 __m256i vs1_0 = vs1;
62 __m256i vs3 = _mm256_setzero_si256();
63
64 uint64_t k = MIN(len, NMAX);
65 k -= k % 32;
66 len -= k;
67
68 while (k >= 32) {
69 /*
70 vs1 = adler + sum(c[i])
71 vs2 = sum2 + 32 vs1 + sum( (32-i+1) c[i] )
72 */
73 __m256i vbuf = _mm256_loadu_si256((__m256i*)src);
74 src += 32;
75 k -= 32;
76
77 __m256i vs1_sad = _mm256_sad_epu8(vbuf, zero); // Sum of abs diff, resulting in 2 x int32's
78 //
79 #ifdef COPY
80 _mm256_storeu_si256((__m256i*)dst, vbuf);
81 dst += 32;
82 #endif
83 vs1 = _mm256_add_epi32(vs1, vs1_sad);
84 vs3 = _mm256_add_epi32(vs3, vs1_0);
85 __m256i v_short_sum2 = _mm256_maddubs_epi16(vbuf, dot2v); // sum 32 uint8s to 16 shorts
86 __m256i vsum2 = _mm256_madd_epi16(v_short_sum2, dot3v); // sum 16 shorts to 8 uint32s
87 vs2 = _mm256_add_epi32(vsum2, vs2);
88 vs1_0 = vs1;
89 }
90
91 /* Defer the multiplication with 32 to outside of the loop */
92 vs3 = _mm256_slli_epi32(vs3, 5);
93 vs2 = _mm256_add_epi32(vs2, vs3);
94
95 /* The compiler is generating the following sequence for this integer modulus
96 * when done the scalar way, in GPRs:
97
98 adler = (s1_unpack[0] % BASE) + (s1_unpack[1] % BASE) + (s1_unpack[2] % BASE) + (s1_unpack[3] % BASE) +
99 (s1_unpack[4] % BASE) + (s1_unpack[5] % BASE) + (s1_unpack[6] % BASE) + (s1_unpack[7] % BASE);
100
101 mov $0x80078071,%edi // move magic constant into 32 bit register %edi
102 ...
103 vmovd %xmm1,%esi // move vector lane 0 to 32 bit register %esi
104 mov %rsi,%rax // zero-extend this value to 64 bit precision in %rax
105 imul %rdi,%rsi // do a signed multiplication with magic constant and vector element
106 shr $0x2f,%rsi // shift right by 47
107 imul $0xfff1,%esi,%esi // do a signed multiplication with value truncated to 32 bits with 0xfff1
108 sub %esi,%eax // subtract lower 32 bits of original vector value from modified one above
109 ...
110 // repeats for each element with vpextract instructions
111
112 This is tricky with AVX2 for a number of reasons:
113 1.) There's no 64 bit multiplication instruction, but there is a sequence to get there
114 2.) There's ways to extend vectors to 64 bit precision, but no simple way to truncate
115 back down to 32 bit precision later (there is in AVX512)
116 3.) Full width integer multiplications aren't cheap
117
118 We can, however, and do a relatively cheap sequence for horizontal sums.
119 Then, we simply do the integer modulus on the resulting 64 bit GPR, on a scalar value. It was
120 previously thought that casting to 64 bit precision was needed prior to the horizontal sum, but
121 that is simply not the case, as NMAX is defined as the maximum number of scalar sums that can be
122 performed on the maximum possible inputs before overflow
123 */
124
125
126 /* In AVX2-land, this trip through GPRs will probably be unvoidable, as there's no cheap and easy
127 * conversion from 64 bit integer to 32 bit (needed for the inexpensive modulus with a constant).
128 * This casting to 32 bit is cheap through GPRs (just register aliasing). See above for exactly
129 * what the compiler is doing to avoid integer divisions. */
130 adler0 = partial_hsum256(vs1) % BASE;
131 adler1 = hsum256(vs2) % BASE;
132 }
133
134 adler = adler0 | (adler1 << 16);
135
136 if (len) {
137 goto rem_peel;
138 }
139
140 return adler;
141 }