Debian Codegraph @

crt/aws-checksums/source/intel/intrin/crc32c_sse42_avx512.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 #include <aws/common/assert.h>
10 #include <aws/common/macros.h>
11
12 #include <emmintrin.h>
13 #include <immintrin.h>
14 #include <smmintrin.h>
15
16 #if defined(AWS_HAVE_AVX512_INTRINSICS) && defined(AWS_ARCH_INTEL_X64)
17
18 # include <wmmintrin.h>
19
20 AWS_ALIGNED_TYPEDEF(const uint64_t, aligned_512_u64[8], 64);
21
22 // This macro uses casting to ensure the compiler actually uses the unaligned load instructions
23 # define load_zmm(ptr) _mm512_loadu_si512((const uint8_t *)(const void *)(ptr))
24
25 /*
26 * crc32c_avx512(): compute the crc32c of the buffer, where the buffer
27 * length must be at least 256, and a multiple of 64. Based on:
28 *
29 * "Fast CRC Computation for Generic Polynomials Using PCLMULQDQ Instruction"
30 * V. Gopal, E. Ozturk, et al., 2009, http://download.intel.com/design/intarch/papers/323102.pdf
31 */
32 static uint32_t s_checksums_crc32c_avx512_impl(const uint8_t *input, int length, uint32_t previous_crc) {
33 AWS_ASSERT(
34 length >= 256 && "invariant violated. length must be greater than 255 bytes to use avx512 to compute crc.");
35
36 uint32_t crc = previous_crc;
37
38 /*
39 * Definitions of the bit-reflected domain constants k1,k2,k3,k4,k5,k6
40 * are similar to those given at the end of the paper
41 *
42 * k1 = ( x ^ ( 512 * 4 + 32 ) mod P(x) << 32 )' << 1
43 * k2 = ( x ^ ( 512 * 4 - 32 ) mod P(x) << 32 )' << 1
44 * k3 = ( x ^ ( 512 + 32 ) mod P(x) << 32 )' << 1
45 * k4 = ( x ^ ( 512 - 32 ) mod P(x) << 32 )' << 1
46 * k5 = ( x ^ ( 128 + 32 ) mod P(x) << 32 )' << 1
47 * k6 = ( x ^ ( 128 - 32 ) mod P(x) << 32 )' << 1
48 */
49
50 static aligned_512_u64 k1k2 = {
51 0xdcb17aa4, 0xb9e02b86, 0xdcb17aa4, 0xb9e02b86, 0xdcb17aa4, 0xb9e02b86, 0xdcb17aa4, 0xb9e02b86};
52 static aligned_512_u64 k3k4 = {
53 0x740eef02, 0x9e4addf8, 0x740eef02, 0x9e4addf8, 0x740eef02, 0x9e4addf8, 0x740eef02, 0x9e4addf8};
54 static aligned_512_u64 k9k10 = {
55 0x6992cea2, 0x0d3b6092, 0x6992cea2, 0x0d3b6092, 0x6992cea2, 0x0d3b6092, 0x6992cea2, 0x0d3b6092};
56 static aligned_512_u64 k1k4 = {
57 0x1c291d04, 0xddc0152b, 0x3da6d0cb, 0xba4fc28e, 0xf20c0dfe, 0x493c7d27, 0x00000000, 0x00000000};
58
59 __m512i x0, x1, x2, x3, x4, x5, x6, x7, x8, y5, y6, y7, y8;
60 __m128i a1;
61
62 /*
63 * There's at least one block of 256.
64 */
65 x1 = load_zmm(input + 0x00);
66 x2 = load_zmm(input + 0x40);
67 x3 = load_zmm(input + 0x80);
68 x4 = load_zmm(input + 0xC0);
69
70 // Load the crc into a zmm register and XOR with the first 64 bytes of input
71 x5 = _mm512_inserti32x4(_mm512_setzero_si512(), _mm_cvtsi32_si128((int)crc), 0);
72 x1 = _mm512_xor_si512(x1, x5);
73
74 x0 = load_zmm(k1k2);
75
76 input += 256;
77 length -= 256;
78
79 /*
80 * Parallel fold blocks of 256, if any.
81 */
82 while (length >= 256) {
83 x5 = _mm512_clmulepi64_epi128(x1, x0, 0x00);
84 x6 = _mm512_clmulepi64_epi128(x2, x0, 0x00);
85 x7 = _mm512_clmulepi64_epi128(x3, x0, 0x00);
86 x8 = _mm512_clmulepi64_epi128(x4, x0, 0x00);
87
88 x1 = _mm512_clmulepi64_epi128(x1, x0, 0x11);
89 x2 = _mm512_clmulepi64_epi128(x2, x0, 0x11);
90 x3 = _mm512_clmulepi64_epi128(x3, x0, 0x11);
91 x4 = _mm512_clmulepi64_epi128(x4, x0, 0x11);
92
93 y5 = load_zmm(input + 0x00);
94 y6 = load_zmm(input + 0x40);
95 y7 = load_zmm(input + 0x80);
96 y8 = load_zmm(input + 0xC0);
97
98 x1 = _mm512_ternarylogic_epi64(x1, x5, y5, 0x96);
99 x2 = _mm512_ternarylogic_epi64(x2, x6, y6, 0x96);
100 x3 = _mm512_ternarylogic_epi64(x3, x7, y7, 0x96);
101 x4 = _mm512_ternarylogic_epi64(x4, x8, y8, 0x96);
102
103 input += 256;
104 length -= 256;
105 }
106
107 /*
108 * Fold 256 bytes into 64 bytes.
109 */
110 x0 = load_zmm(k9k10);
111 x5 = _mm512_clmulepi64_epi128(x1, x0, 0x00);
112 x6 = _mm512_clmulepi64_epi128(x1, x0, 0x11);
113 x3 = _mm512_ternarylogic_epi64(x3, x5, x6, 0x96);
114
115 x7 = _mm512_clmulepi64_epi128(x2, x0, 0x00);
116 x8 = _mm512_clmulepi64_epi128(x2, x0, 0x11);
117 x4 = _mm512_ternarylogic_epi64(x4, x7, x8, 0x96);
118
119 x0 = load_zmm(k3k4);
120 y5 = _mm512_clmulepi64_epi128(x3, x0, 0x00);
121 y6 = _mm512_clmulepi64_epi128(x3, x0, 0x11);
122 x1 = _mm512_ternarylogic_epi64(x4, y5, y6, 0x96);
123
124 /*
125 * Single fold blocks of 64, if any.
126 */
127 while (length >= 64) {
128 x2 = load_zmm(input);
129
130 x5 = _mm512_clmulepi64_epi128(x1, x0, 0x00);
131 x1 = _mm512_clmulepi64_epi128(x1, x0, 0x11);
132 x1 = _mm512_ternarylogic_epi64(x1, x2, x5, 0x96);
133
134 input += 64;
135 length -= 64;
136 }
137
138 /*
139 * Fold 512-bits to 128-bits.
140 */
141 x0 = load_zmm(k1k4);
142 x4 = _mm512_clmulepi64_epi128(x1, x0, 0x00);
143 x3 = _mm512_clmulepi64_epi128(x1, x0, 0x11);
144 x2 = _mm512_xor_si512(x3, x4);
145 a1 = _mm_xor_si128(_mm512_extracti32x4_epi32(x1, 3), _mm512_extracti32x4_epi32(x2, 0));
146 a1 = _mm_ternarylogic_epi64(a1, _mm512_extracti32x4_epi32(x2, 1), _mm512_extracti32x4_epi32(x2, 2), 0x96);
147
148 /*
149 * Fold 128-bits to 32-bits.
150 */
151 uint64_t val;
152 val = _mm_crc32_u64(0, _mm_extract_epi64(a1, 0));
153 return (uint32_t)_mm_crc32_u64(val, _mm_extract_epi64(a1, 1));
154 }
155 #endif /* #if defined(AWS_HAVE_AVX512_INTRINSICS) && (INTPTR_MAX == INT64_MAX) */
156
157 uint32_t aws_checksums_crc32c_intel_avx512_with_sse_fallback(const uint8_t *input, int length, uint32_t previous_crc) {
158 /* this is the entry point. We should only do the bit flip once. It should not be done for the subfunctions and
159 * branches.*/
160 uint32_t crc = ~previous_crc;
161
162 /* For small input, forget about alignment checks - simply compute the CRC32c one byte at a time */
163 if (length < (int)sizeof(slice_ptr_int_type)) {
164 while (length-- > 0) {
165 crc = (uint32_t)_mm_crc32_u8(crc, *input++);
166 }
167 return ~crc;
168 }
169
170 /* Get the 8-byte memory alignment of our input buffer by looking at the least significant 3 bits */
171 int input_alignment = (uintptr_t)(input) & 0x7;
172
173 /* Compute the number of unaligned bytes before the first aligned 8-byte chunk (will be in the range 0-7) */
174 int leading = (8 - input_alignment) & 0x7;
175
176 /* reduce the length by the leading unaligned bytes we are about to process */
177 length -= leading;
178
179 /* spin through the leading unaligned input bytes (if any) one-by-one */
180 while (leading-- > 0) {
181 crc = (uint32_t)_mm_crc32_u8(crc, *input++);
182 }
183
184 #if defined(AWS_HAVE_AVX512_INTRINSICS) && defined(AWS_ARCH_INTEL_X64)
185 int chunk_size = length & ~63;
186
187 if (aws_cpu_has_avx512_cached() && aws_cpu_has_vpclmulqdq_cached() && aws_cpu_has_clmul_cached()) {
188 if (length >= 256) {
189 crc = s_checksums_crc32c_avx512_impl(input, length, crc);
190 /* check remaining data */
191 length -= chunk_size;
192 if (!length) {
193 return ~crc;
194 }
195
196 /* Fall into the default crc32 for the remaining data. */
197 input += chunk_size;
198 }
199 }
200 #endif
201
202 #if defined(AWS_ARCH_INTEL_X64) && !defined(_MSC_VER)
203 if (aws_cpu_has_sse42_cached() && aws_cpu_has_clmul_cached()) {
204 // this function is an entry point on its own. It inverts the crc passed to it
205 // does its thing and then inverts it upon return. In order to keep
206 // aws_checksums_crc32c_sse42 a standalone function (which it has to be due
207 // to the way its implemented) it's better that it doesn't need to know it's used
208 // in a larger computation fallback.
209 return aws_checksums_crc32c_clmul_sse42(input, length, ~crc);
210 }
211 #endif
212
213 /* Spin through remaining (aligned) 8-byte chunks using the CRC32Q quad word instruction */
214 while (length >= (int)sizeof(slice_ptr_int_type)) {
215 crc = (uint32_t)crc_intrin_fn(crc, *(slice_ptr_int_type *)(input));
216 input += sizeof(slice_ptr_int_type);
217 length -= (int)sizeof(slice_ptr_int_type);
218 }
219
220 /* Finish up with any trailing bytes using the CRC32B single byte instruction one-by-one */
221 while (length-- > 0) {
222 crc = (uint32_t)_mm_crc32_u8(crc, *input);
223 input++;
224 }
225
226 return ~crc;
227 }