Debian Codegraph @

crt/aws-checksums/source/intel/asm/crc32c_sse42_asm.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/checksums/private/crc32_priv.h>
7 #include <aws/checksums/private/crc_util.h>
8
9 /* clang-format off */
10
11 /* this implementation is only for the x86_64 intel architecture */
12 #if defined(__x86_64__)
13 # if defined(__clang__)
14 # pragma clang diagnostic push
15 # pragma clang diagnostic ignored "-Wdollar-in-identifier-extension"
16 # endif
17
18 /* use local labels, so that linker doesn't think these are functions it can deadstrip */
19 # ifdef __APPLE__
20 # define LABEL(label) "L_" #label "_%="
21 # else
22 # define LABEL(label) ".L_" #label "_%="
23 # endif
24
25 /*
26 * Factored out common inline asm for folding crc0,crc1,crc2 stripes in rcx, r11, r10 using
27 * the specified Magic Constants K1 and K2.
28 * Assumes rcx, r11, r10 contain crc0, crc1, crc2 that need folding
29 * Utilizes xmm1, xmm2, xmm3, xmm4 as well as clobbering r8, r9, r11
30 * Result is placed in ecx
31 */
32 # define FOLD_K1K2(K1, K2) \
33 "movl " #K1 ", %%r8d # Magic K1 constant \n" \
34 "movl " #K2 ", %%r9d # Magic K2 constant \n" \
35 "movq %%rcx, %%xmm1 # crc0 into lower dword of xmm1 \n" \
36 "movq %%r8, %%xmm3 # K1 into lower dword of xmm3 \n" \
37 "movq %%r11, %%xmm2 # crc1 into lower dword of xmm2 \n" \
38 "movq %%r9, %%xmm4 # K2 into lower dword of xmm4 \n" \
39 "pclmulqdq $0x00, %%xmm3, %%xmm1 # Multiply crc0 by K1 \n" \
40 "pclmulqdq $0x00, %%xmm4, %%xmm2 # Multiply crc1 by K2 \n" \
41 "xor %%rcx, %%rcx # \n" \
42 "xor %%r11, %%r11 # \n" \
43 "movq %%xmm1, %%r8 # \n" \
44 "movq %%xmm2, %%r9 # \n" \
45 "crc32q %%r8, %%rcx # folding crc0 \n" \
46 "crc32q %%r9, %%r11 # folding crc1 \n" \
47 "xor %%r10d, %%ecx # combine crc2 and crc0 \n" \
48 "xor %%r11d, %%ecx # combine crc1 and crc0 \n"
49
50 /**
51 * Private (static) function.
52 * Computes the Castagnoli CRC32c (iSCSI) of the specified data buffer using the Intel CRC32Q (quad word) machine
53 * instruction by operating on 24-byte stripes in parallel. The results are folded together using CLMUL. This function
54 * is optimized for exactly 256 byte blocks that are best aligned on 8-byte memory addresses. It MUST be passed a
55 * pointer to input data that is exactly 256 bytes in length. Note: this function does NOT invert bits of the input crc
56 * or return value.
57 */
58 static inline uint32_t s_crc32c_sse42_clmul_256(const uint8_t *input, uint32_t crc) {
59 __asm__ __volatile__(
60 "xor %%r11, %%r11 # zero all 64 bits in r11, will track crc1 \n"
61 "xor %%r10, %%r10 # zero all 64 bits in r10, will track crc2 \n"
62
63 "crc32q 0(%[in]), %%rcx # crc0 \n"
64 "crc32q 88(%[in]), %%r11 # crc1 \n"
65 "crc32q 176(%[in]), %%r10 # crc2 \n"
66
67 "crc32q 8(%[in]), %%rcx # crc0 \n"
68 "crc32q 96(%[in]), %%r11 # crc1 \n"
69 "crc32q 184(%[in]), %%r10 # crc2 \n"
70
71 "crc32q 16(%[in]), %%rcx # crc0 \n"
72 "crc32q 104(%[in]), %%r11 # crc1 \n"
73 "crc32q 192(%[in]), %%r10 # crc2 \n"
74
75 "crc32q 24(%[in]), %%rcx # crc0 \n"
76 "crc32q 112(%[in]), %%r11 # crc1 \n"
77 "crc32q 200(%[in]), %%r10 # crc2 \n"
78
79 "crc32q 32(%[in]), %%rcx # crc0 \n"
80 "crc32q 120(%[in]), %%r11 # crc1 \n"
81 "crc32q 208(%[in]), %%r10 # crc2 \n"
82
83 "crc32q 40(%[in]), %%rcx # crc0 \n"
84 "crc32q 128(%[in]), %%r11 # crc1 \n"
85 "crc32q 216(%[in]), %%r10 # crc2 \n"
86
87 "crc32q 48(%[in]), %%rcx # crc0 \n"
88 "crc32q 136(%[in]), %%r11 # crc1 \n"
89 "crc32q 224(%[in]), %%r10 # crc2 \n"
90
91 "crc32q 56(%[in]), %%rcx # crc0 \n"
92 "crc32q 144(%[in]), %%r11 # crc1 \n"
93 "crc32q 232(%[in]), %%r10 # crc2 \n"
94
95 "crc32q 64(%[in]), %%rcx # crc0 \n"
96 "crc32q 152(%[in]), %%r11 # crc1 \n"
97 "crc32q 240(%[in]), %%r10 # crc2 \n"
98
99 "crc32q 72(%[in]), %%rcx # crc0 \n"
100 "crc32q 160(%[in]), %%r11 # crc1 \n"
101 "crc32q 248(%[in]), %%r10 # crc2 \n"
102
103 "crc32q 80(%[in]), %%rcx # crc0 \n"
104 "crc32q 168(%[in]), %%r11 # crc2 \n"
105
106 FOLD_K1K2($0x1b3d8f29, $0x39d3b296) /* Magic Constants used to fold crc stripes into ecx */
107
108 /* output registers
109 [crc] is an input and and output so it is marked read/write (i.e. "+c")*/
110 : [ crc ] "+c"(crc)
111 /* input registers */
112 : [ in ] "d"(input)
113
114 /* additional clobbered registers */
115 : "%r8", "%r9", "%r11", "%r10", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "cc");
116 return crc;
117 }
118
119 /**
120 * Private (static) function.
121 * Computes the Castagnoli CRC32c (iSCSI) of the specified data buffer using the Intel CRC32Q (quad word) machine
122 * instruction by operating on 3 24-byte stripes in parallel. The results are folded together using CLMUL. This function
123 * is optimized for exactly 1024 byte blocks that are best aligned on 8-byte memory addresses. It MUST be passed a
124 * pointer to input data that is exactly 1024 bytes in length. Note: this function does NOT invert bits of the input crc
125 * or return value.
126 */
127 static inline uint32_t s_crc32c_sse42_clmul_1024(const uint8_t *input, uint32_t crc) {
128 __asm__ __volatile__(
129 "xor %%r11, %%r11 # zero all 64 bits in r11, will track crc1 \n"
130 "xor %%r10, %%r10 # zero all 64 bits in r10, will track crc2 \n"
131
132 "movl $5, %%r8d # Loop 5 times through 64 byte chunks in 3 parallel stripes \n"
133
134 LABEL(loop_1024) ": \n"
135
136 "prefetcht0 128(%[in]) # \n"
137 "prefetcht0 472(%[in]) # \n"
138 "prefetcht0 808(%[in]) # \n"
139
140 "crc32q 0(%[in]), %%rcx # crc0: stripe0 \n"
141 "crc32q 344(%[in]), %%r11 # crc1: stripe1 \n"
142 "crc32q 680(%[in]), %%r10 # crc2: stripe2 \n"
143
144 "crc32q 8(%[in]), %%rcx # crc0 \n"
145 "crc32q 352(%[in]), %%r11 # crc1 \n"
146 "crc32q 688(%[in]), %%r10 # crc2 \n"
147
148 "crc32q 16(%[in]), %%rcx # crc0 \n"
149 "crc32q 360(%[in]), %%r11 # crc1 \n"
150 "crc32q 696(%[in]), %%r10 # crc2 \n"
151
152 "crc32q 24(%[in]), %%rcx # crc0 \n"
153 "crc32q 368(%[in]), %%r11 # crc1 \n"
154 "crc32q 704(%[in]), %%r10 # crc2 \n"
155
156 "crc32q 32(%[in]), %%rcx # crc0 \n"
157 "crc32q 376(%[in]), %%r11 # crc1 \n"
158 "crc32q 712(%[in]), %%r10 # crc2 \n"
159
160 "crc32q 40(%[in]), %%rcx # crc0 \n"
161 "crc32q 384(%[in]), %%r11 # crc1 \n"
162 "crc32q 720(%[in]), %%r10 # crc2 \n"
163
164 "crc32q 48(%[in]), %%rcx # crc0 \n"
165 "crc32q 392(%[in]), %%r11 # crc1 \n"
166 "crc32q 728(%[in]), %%r10 # crc2 \n"
167
168 "crc32q 56(%[in]), %%rcx # crc0 \n"
169 "crc32q 400(%[in]), %%r11 # crc1 \n"
170 "crc32q 736(%[in]), %%r10 # crc2 \n"
171
172 "add $64, %[in] # \n"
173 "sub $1, %%r8d # \n"
174 "jnz " LABEL(loop_1024) " # \n"
175
176 "crc32q 0(%[in]), %%rcx # crc0 \n"
177 "crc32q 344(%[in]), %%r11 # crc1 \n"
178 "crc32q 680(%[in]), %%r10 # crc2 \n"
179
180 "crc32q 8(%[in]), %%rcx # crc0 \n"
181 "crc32q 352(%[in]), %%r11 # crc1 \n"
182 "crc32q 688(%[in]), %%r10 # crc2 \n"
183
184 "crc32q 16(%[in]), %%rcx # crc0 \n"
185 "crc32q 696(%[in]), %%r10 # crc2 \n"
186
187 FOLD_K1K2($0xe417f38a, $0x8f158014) /* Magic Constants used to fold crc stripes into ecx
188
189 output registers
190 [crc] is an input and and output so it is marked read/write (i.e. "+c")
191 we clobber the register for [input] (via add instruction) so we must also
192 tag it read/write (i.e. "+d") in the list of outputs to tell gcc about the clobber */
193 : [ crc ] "+c"(crc), [ in ] "+d"(input)
194 :
195 /* additional clobbered registers */
196 /* "cc" is the flags - we add and sub, so the flags are also clobbered */
197 : "%r8", "%r9", "%r11", "%r10", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "cc");
198 return crc;
199 }
200
201 /**
202 * Private (static) function.
203 * Computes the Castagnoli CRC32c (iSCSI) of the specified data buffer using the Intel CRC32Q (quad word) machine
204 * instruction by operating on 24-byte stripes in parallel. The results are folded together using CLMUL. This function
205 * is optimized for exactly 3072 byte blocks that are best aligned on 8-byte memory addresses. It MUST be passed a
206 * pointer to input data that is exactly 3072 bytes in length. Note: this function does NOT invert bits of the input crc
207 * or return value.
208 */
209 static inline uint32_t s_crc32c_sse42_clmul_3072(const uint8_t *input, uint32_t crc) {
210 __asm__ __volatile__(
211 "xor %%r11, %%r11 # zero all 64 bits in r11, will track crc1 \n"
212 "xor %%r10, %%r10 # zero all 64 bits in r10, will track crc2 \n"
213
214 "movl $16, %%r8d # Loop 16 times through 64 byte chunks in 3 parallel stripes \n"
215
216 LABEL(loop_3072) ": \n"
217
218 "prefetcht0 128(%[in]) # \n"
219 "prefetcht0 1152(%[in]) # \n"
220 "prefetcht0 2176(%[in]) # \n"
221
222 "crc32q 0(%[in]), %%rcx # crc0: stripe0 \n"
223 "crc32q 1024(%[in]), %%r11 # crc1: stripe1 \n"
224 "crc32q 2048(%[in]), %%r10 # crc2: stripe2 \n"
225
226 "crc32q 8(%[in]), %%rcx # crc0: stripe0 \n"
227 "crc32q 1032(%[in]), %%r11 # crc1: stripe1 \n"
228 "crc32q 2056(%[in]), %%r10 # crc2: stripe2 \n"
229
230 "crc32q 16(%[in]), %%rcx # crc0: stripe0 \n"
231 "crc32q 1040(%[in]), %%r11 # crc1: stripe1 \n"
232 "crc32q 2064(%[in]), %%r10 # crc2: stripe2 \n"
233
234 "crc32q 24(%[in]), %%rcx # crc0: stripe0 \n"
235 "crc32q 1048(%[in]), %%r11 # crc1: stripe1 \n"
236 "crc32q 2072(%[in]), %%r10 # crc2: stripe2 \n"
237
238 "crc32q 32(%[in]), %%rcx # crc0: stripe0 \n"
239 "crc32q 1056(%[in]), %%r11 # crc1: stripe1 \n"
240 "crc32q 2080(%[in]), %%r10 # crc2: stripe2 \n"
241
242 "crc32q 40(%[in]), %%rcx # crc0: stripe0 \n"
243 "crc32q 1064(%[in]), %%r11 # crc1: stripe1 \n"
244 "crc32q 2088(%[in]), %%r10 # crc2: stripe2 \n"
245
246 "crc32q 48(%[in]), %%rcx # crc0: stripe0 \n"
247 "crc32q 1072(%[in]), %%r11 # crc1: stripe1 \n"
248 "crc32q 2096(%[in]), %%r10 # crc2: stripe2 \n"
249
250 "crc32q 56(%[in]), %%rcx # crc0: stripe0 \n"
251 "crc32q 1080(%[in]), %%r11 # crc1: stripe1 \n"
252 "crc32q 2104(%[in]), %%r10 # crc2: stripe2 \n"
253
254 "add $64, %[in] # \n"
255 "sub $1, %%r8d # \n"
256 "jnz " LABEL(loop_3072) " # \n"
257
258 FOLD_K1K2(
259 $0xa51b6135,
260 $0x170076fa) /* Magic Constants used to fold crc stripes into ecx
261
262 output registers
263 [crc] is an input and and output so it is marked read/write (i.e. "+c")
264 we clobber the register for [input] (via add instruction) so we must also
265 tag it read/write (i.e. "+d") in the list of outputs to tell gcc about the clobber*/
266 : [ crc ] "+c"(crc), [ in ] "+d"(input)
267 :
268 /* additional clobbered registers
269 "cc" is the flags - we add and sub, so the flags are also clobbered */
270 : "%r8", "%r9", "%r11", "%r10", "%xmm1", "%xmm2", "%xmm3", "%xmm4", "cc");
271
272 return crc;
273 }
274
275 /*
276 * Computes the Castagnoli CRC32c (iSCSI) of the specified data buffer using the Intel CRC32Q (64-bit quad word) and
277 * PCLMULQDQ machine instructions (if present).
278 * Handles data that isn't 8-byte aligned as well as any trailing data with the CRC32B (byte) instruction.
279 * Pass 0 in the previousCrc32 parameter as an initial value unless continuing to update a running CRC in a subsequent
280 * call.
281 */
282 uint32_t aws_checksums_crc32c_clmul_sse42(const uint8_t *input, int length, uint32_t previousCrc32) {
283
284 /* this is called by a higher-level shim and previousCRC32 is already ~ */
285 uint32_t crc = ~previousCrc32;
286
287 /* For small input, forget about alignment checks - simply compute the CRC32c one byte at a time */
288 if (AWS_UNLIKELY(length < 8)) {
289 while (length-- > 0) {
290 __asm__("CRC32B (%[in]), %[crc]" : [ crc ] "+c"(crc) : [ in ] "r"(input));
291 input++;
292 }
293 return ~crc;
294 }
295
296 /* Get the 8-byte memory alignment of our input buffer by looking at the least significant 3 bits */
297 int input_alignment = (unsigned long int)input & 0x7;
298
299 /* Compute the number of unaligned bytes before the first aligned 8-byte chunk (will be in the range 0-7) */
300 int leading = (8 - input_alignment) & 0x7;
301
302 /* reduce the length by the leading unaligned bytes we are about to process */
303 length -= leading;
304
305 /* spin through the leading unaligned input bytes (if any) one-by-one */
306 while (leading-- > 0) {
307 __asm__("CRC32B (%[in]), %[crc]" : [ crc ] "+c"(crc) : [ in ] "r"(input));
308 input++;
309 }
310
311 /* Using likely to keep this code inlined */
312 if (AWS_LIKELY(aws_cpu_has_clmul_cached())) {
313
314 while (AWS_LIKELY(length >= 3072)) {
315 /* Compute crc32c on each block, chaining each crc result */
316 crc = s_crc32c_sse42_clmul_3072(input, crc);
317 input += 3072;
318 length -= 3072;
319 }
320 while (AWS_LIKELY(length >= 1024)) {
321 /* Compute crc32c on each block, chaining each crc result */
322 crc = s_crc32c_sse42_clmul_1024(input, crc);
323 input += 1024;
324 length -= 1024;
325 }
326 while (AWS_LIKELY(length >= 256)) {
327 /* Compute crc32c on each block, chaining each crc result */
328 crc = s_crc32c_sse42_clmul_256(input, crc);
329 input += 256;
330 length -= 256;
331 }
332 }
333
334 /* Spin through remaining (aligned) 8-byte chunks using the CRC32Q quad word instruction */
335 while (AWS_LIKELY(length >= 8)) {
336 /* Hardcoding %rcx register (i.e. "+c") to allow use of qword instruction */
337 __asm__ __volatile__("CRC32Q (%[in]), %%rcx" : [ crc ] "+c"(crc) : [ in ] "r"(input));
338 input += 8;
339 length -= 8;
340 }
341
342 /* Finish up with any trailing bytes using the CRC32B single byte instruction one-by-one */
343 while (length-- > 0) {
344 __asm__ __volatile__("CRC32B (%[in]), %[crc]" : [ crc ] "+c"(crc) : [ in ] "r"(input));
345 input++;
346 }
347
348 return ~crc;
349 }
350
351 # if defined(__clang__)
352 # pragma clang diagnostic pop
353 # endif
354
355 #endif /* x86_64 */
356 /* clang-format on */