Debian Codegraph @

crt/aws-checksums/source/arm/crc64_arm.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/crc64_priv.h>
7 #include <aws/common/config.h>
8
9 #if INTPTR_MAX == INT64_MAX && defined(AWS_HAVE_ARMv8_1)
10
11 # include <arm_neon.h>
12
13 // Load a uint8x16_t neon register from uint8_t pointer
14 # define load_u8(uint8_t_ptr) vld1q_u8((uint8_t_ptr))
15 // Load a poly64x2_t neon register from a uint8_t pointer
16 # define load_p64_u8(uint8_t_ptr) vreinterpretq_p64_u8(load_u8(uint8_t_ptr))
17 // Load a poly64x2_t neon register from a uint64_t pointer
18 # define load_p64(uint64_t_ptr) vreinterpretq_p64_u64(vld1q_u64((uint64_t_ptr)))
19 // Mask the bytes in a neon uint8x16_t register and preserve 0 to 15 least significant bytes.
20 # define mask_low_u8(u8, count) vandq_u8(u8, load_u8(aws_checksums_masks_shifts[5] - (intptr_t)(count)))
21 // Mask the bytes in a neon uint8x16_t register and preserve 0 to 15 most significant bytes.
22 # define mask_high_u8(u8, count) vandq_u8(u8, load_u8(aws_checksums_masks_shifts[3] + (intptr_t)(count)))
23 // Mask the bytes in a neon poly64x2_t register and preserve 0 to 15 most significant bytes.
24 # define mask_high_p64(poly, count) vreinterpretq_p64_u8(mask_high_u8(vreinterpretq_u8_p64(poly), count))
25 // Left shift bytes in a neon uint8x16_t register - shift count from 0 to 15.
26 # define left_shift_u8(u8, count) vqtbl1q_u8(u8, load_u8(aws_checksums_masks_shifts[1] - (intptr_t)(count)))
27 // Right shift bytes in a neon uint8x16_t register - shift count from 0 to 15.
28 # define right_shift_u8(u8, count) vqtbl1q_u8(u8, load_u8(aws_checksums_masks_shifts[1] + (intptr_t)(count)))
29 // Left shift bytes in a neon poly64x2_t register - shift count from 0 to 15.
30 # define left_shift_p64(poly, count) vreinterpretq_p64_u8(left_shift_u8(vreinterpretq_u8_p64(poly), count))
31 // Right shift a neon poly64x2_t register 0 to 15 bytes - imm must be an immediate constant
32 # define right_shift_imm_p64(poly, imm) \
33 vreinterpretq_p64_u8(vextq_u8(vreinterpretq_u8_p64(poly), vdupq_n_u8(0), imm))
34 // Carryless multiply the lower 64-bit halves of two poly64x2_t neon registers
35 # define pmull_lo(a, b) \
36 (vreinterpretq_p64_p128(vmull_p64((poly64_t)vreinterpretq_p128_p64(a), (poly64_t)vreinterpretq_p128_p64(b))))
37 // Carryless multiply the upper 64-bit halves of two poly64x2_t neon registers
38 # define pmull_hi(a, b) (vreinterpretq_p64_p128(vmull_high_p64((a), (b))))
39 // XOR two neon poly64x2_t registers
40 # define xor_p64(a, b) vreinterpretq_p64_u8(veorq_u8(vreinterpretq_u8_p64(a), vreinterpretq_u8_p64(b)))
41 # if defined(__ARM_FEATURE_SHA3)
42 // The presence of the ARM SHA3 feature also implies the three-way xor instruction
43 # define xor3_p64(a, b, c) \
44 vreinterpretq_p64_u64( \
45 veor3q_u64(vreinterpretq_u64_p64(a), vreinterpretq_u64_p64(b), vreinterpretq_u64_p64(c)))
46 # else
47 // Without SHA3, implement three-way xor with two normal xors
48 # define xor3_p64(a, b, c) xor_p64(xor_p64(a, b), c)
49 # endif // defined(__ARM_FEATURE_SHA3)
50
51 /** Compute CRC64NVME using ARMv8 NEON +crypto/pmull64 instructions. */
52 uint64_t aws_checksums_crc64nvme_arm_pmull(const uint8_t *input, int length, const uint64_t previous_crc64) {
53 if (!input || length <= 0) {
54 return previous_crc64;
55 }
56
57 // the amount of complexity required to handle vector instructions on
58 // memory regions smaller than an xmm register does not justify the very negligible performance gains
59 // we would get for using it on an input this small.
60 if (length < 16) {
61 return aws_checksums_crc64nvme_sw(input, length, previous_crc64);
62 }
63
64 // Invert the previous crc bits and load into the lower half of a neon register
65 poly64x2_t a1 = vreinterpretq_p64_u64(vcombine_u64(vcreate_u64(~previous_crc64), vcreate_u64(0)));
66
67 // Load the x^128 and x^192 constants - they'll (very likely) be needed
68 const poly64x2_t x128 = load_p64(aws_checksums_crc64nvme_constants.x128);
69
70 // Load the next 16 bytes of input and XOR with the previous crc
71 a1 = xor_p64(a1, load_p64_u8(input));
72 input += 16;
73 length -= 16;
74
75 if (length < 112) {
76
77 const poly64x2_t x256 = load_p64(aws_checksums_crc64nvme_constants.x256);
78
79 if (length & 64) {
80 // Fold the current crc register with 64 bytes of input by multiplying 64-bit chunks by x^576 through
81 // x^128
82 const poly64x2_t x512 = load_p64(aws_checksums_crc64nvme_constants.x512);
83 const poly64x2_t x384 = load_p64(aws_checksums_crc64nvme_constants.x384);
84 poly64x2_t b1 = load_p64_u8(input + 0);
85 poly64x2_t c1 = load_p64_u8(input + 16);
86 poly64x2_t d1 = load_p64_u8(input + 32);
87 poly64x2_t e1 = load_p64_u8(input + 48);
88 a1 = xor3_p64(pmull_lo(x512, a1), pmull_hi(x512, a1), pmull_lo(x384, b1));
89 b1 = xor3_p64(pmull_hi(x384, b1), pmull_lo(x256, c1), pmull_hi(x256, c1));
90 c1 = xor3_p64(pmull_lo(x128, d1), pmull_hi(x128, d1), e1);
91 a1 = xor3_p64(a1, b1, c1);
92 input += 64;
93 }
94
95 if (length & 32) {
96 // Fold the current running value with 32 bytes of input by multiplying 64-bit chunks by x^320 through
97 // x^128
98 poly64x2_t b1 = load_p64_u8(input + 0);
99 poly64x2_t c1 = load_p64_u8(input + 16);
100 a1 = xor3_p64(c1, pmull_lo(x256, a1), pmull_hi(x256, a1));
101 a1 = xor3_p64(a1, pmull_lo(x128, b1), pmull_hi(x128, b1));
102 input += 32;
103 }
104 } else { // There are 112 or more bytes of input
105
106 const poly64x2_t x1024 = load_p64(aws_checksums_crc64nvme_constants.x1024);
107
108 // Load another 112 bytes of input
109 poly64x2_t b1 = load_p64_u8(input + 0);
110 poly64x2_t c1 = load_p64_u8(input + 16);
111 poly64x2_t d1 = load_p64_u8(input + 32);
112 poly64x2_t e1 = load_p64_u8(input + 48);
113 poly64x2_t f1 = load_p64_u8(input + 64);
114 poly64x2_t g1 = load_p64_u8(input + 80);
115 poly64x2_t h1 = load_p64_u8(input + 96);
116 input += 112;
117 length -= 112;
118
119 // Spin through additional chunks of 128 bytes, if any
120 int loops = length / 128;
121 while (loops--) {
122 // Fold input values in parallel by multiplying by x^1088 and x^1024 constants
123 a1 = xor3_p64(pmull_lo(x1024, a1), pmull_hi(x1024, a1), load_p64_u8(input + 0));
124 b1 = xor3_p64(pmull_lo(x1024, b1), pmull_hi(x1024, b1), load_p64_u8(input + 16));
125 c1 = xor3_p64(pmull_lo(x1024, c1), pmull_hi(x1024, c1), load_p64_u8(input + 32));
126 d1 = xor3_p64(pmull_lo(x1024, d1), pmull_hi(x1024, d1), load_p64_u8(input + 48));
127 e1 = xor3_p64(pmull_lo(x1024, e1), pmull_hi(x1024, e1), load_p64_u8(input + 64));
128 f1 = xor3_p64(pmull_lo(x1024, f1), pmull_hi(x1024, f1), load_p64_u8(input + 80));
129 g1 = xor3_p64(pmull_lo(x1024, g1), pmull_hi(x1024, g1), load_p64_u8(input + 96));
130 h1 = xor3_p64(pmull_lo(x1024, h1), pmull_hi(x1024, h1), load_p64_u8(input + 112));
131 input += 128;
132 }
133
134 // Fold 128 bytes down to 64 bytes by multiplying by the x^576 and x^512 constants
135 const poly64x2_t x512 = load_p64(aws_checksums_crc64nvme_constants.x512);
136 a1 = xor3_p64(e1, pmull_lo(x512, a1), pmull_hi(x512, a1));
137 b1 = xor3_p64(f1, pmull_lo(x512, b1), pmull_hi(x512, b1));
138 c1 = xor3_p64(g1, pmull_lo(x512, c1), pmull_hi(x512, c1));
139 d1 = xor3_p64(h1, pmull_lo(x512, d1), pmull_hi(x512, d1));
140
141 if (length & 64) {
142 // Fold the current 64 bytes with 64 bytes of input by multiplying by x^576 and x^512 constants
143 a1 = xor3_p64(pmull_lo(x512, a1), pmull_hi(x512, a1), load_p64_u8(input + 0));
144 b1 = xor3_p64(pmull_lo(x512, b1), pmull_hi(x512, b1), load_p64_u8(input + 16));
145 c1 = xor3_p64(pmull_lo(x512, c1), pmull_hi(x512, c1), load_p64_u8(input + 32));
146 d1 = xor3_p64(pmull_lo(x512, d1), pmull_hi(x512, d1), load_p64_u8(input + 48));
147 input += 64;
148 }
149
150 // Fold 64 bytes down to 32 bytes by multiplying by the x^320 and x^256 constants
151 const poly64x2_t x256 = load_p64(aws_checksums_crc64nvme_constants.x256);
152 a1 = xor3_p64(c1, pmull_lo(x256, a1), pmull_hi(x256, a1));
153 b1 = xor3_p64(d1, pmull_lo(x256, b1), pmull_hi(x256, b1));
154
155 if (length & 32) {
156 // Fold the current running value with 32 bytes of input by multiplying by x^320 and x^256 constants
157 a1 = xor3_p64(pmull_lo(x256, a1), pmull_hi(x256, a1), load_p64_u8(input + 0));
158 b1 = xor3_p64(pmull_lo(x256, b1), pmull_hi(x256, b1), load_p64_u8(input + 16));
159 input += 32;
160 }
161
162 // Fold 32 bytes down to 16 bytes by multiplying by x^192 and x^128 constants
163 a1 = xor3_p64(b1, pmull_lo(x128, a1), pmull_hi(x128, a1));
164 }
165
166 if (length & 16) {
167 // Fold the current 16 bytes with 16 bytes of input by multiplying by x^192 and x^128 constants
168 a1 = xor3_p64(pmull_lo(x128, a1), pmull_hi(x128, a1), load_p64_u8(input + 0));
169 input += 16;
170 }
171
172 // There must only be 0-15 bytes of input left
173 length &= 15;
174
175 if (length == 0) {
176 // Multiply the lower half of the crc register by x^128 (swapping upper and lower halves)
177 poly64x2_t mul_by_x128 = pmull_lo(a1, vextq_p64(x128, x128, 1));
178 // XOR the result with the right shifted upper half of the crc
179 a1 = xor_p64(right_shift_imm_p64(a1, 8), mul_by_x128);
180 } else {
181 // Handle any trailing input from 1-15 bytes
182 const poly64x2_t trailing_constants = load_p64(aws_checksums_crc64nvme_constants.trailing[length - 1]);
183 // Multiply the crc by a pair of trailing length constants in order to fold it into the trailing input
184 a1 = xor_p64(pmull_lo(a1, trailing_constants), pmull_hi(a1, trailing_constants));
185 // Safely load ending at the last byte of trailing input and mask out any leading garbage
186 poly64x2_t trailing_input = mask_high_p64(load_p64_u8(input + length - 16), length);
187 // Multiply the lower half of the trailing input register by x^128 (swapping x^192 and x^128 halves)
188 poly64x2_t mul_by_x128 = pmull_lo(trailing_input, vextq_p64(x128, x128, 1));
189 // XOR the results with the right shifted upper half of the trailing input
190 a1 = xor3_p64(a1, right_shift_imm_p64(trailing_input, 8), mul_by_x128);
191 }
192
193 // Barrett modular reduction
194
195 // Load the Barrett mu and (bit-reflected) polynomial
196 const poly64x2_t mu_poly = load_p64(aws_checksums_crc64nvme_constants.mu_poly);
197 // Multiply the lower half of the crc register by mu (mu is in the lower half of mu_poly)
198 poly64x2_t mul_by_mu = pmull_lo(a1, mu_poly);
199 // Multiply lower half of mul_by_mu result by poly (which is swapped into the lower half)
200 poly64x2_t mul_by_poly = pmull_lo(mul_by_mu, vextq_p64(mu_poly, mu_poly, 1));
201 // Swap halves of mul_by_mu and add the upper halves of everything
202 poly64x2_t result = xor3_p64(a1, vextq_p64(mul_by_mu, mul_by_mu, 1), mul_by_poly);
203
204 // Reduction result is the upper half - invert the bits before returning the crc
205 return ~vgetq_lane_u64(vreinterpretq_u64_p64(result), 1);
206 }
207
208 static inline uint64_t barrett_reduce_refl(const poly64x2_t kp_poly_mu, poly64x2_t input) {
209
210 // Multiply the lower half of input by mu (mu is already in the lower half of poly_mu)
211 poly64x2_t mul_by_u = pmull_lo(input, kp_poly_mu);
212
213 // Swapping poly_mu to get poly in the lower half
214 poly64x2_t poly = vextq_p64(kp_poly_mu, kp_poly_mu, 1);
215
216 // Swapping mul_by_u to get the lower half into the upper half
217 poly64x2_t mu_swap = vextq_p64(mul_by_u, mul_by_u, 1);
218
219 // Multiply lower half of mul_by_u result by poly
220 poly64x2_t mul_by_p = pmull_lo(mul_by_u, poly);
221
222 // Add the upper halves of everything
223 poly64x2_t result = xor3_p64(input, mu_swap, mul_by_p);
224
225 // Reduction result is the upper half
226 return vgetq_lane_u64(vreinterpretq_u64_p64(result), 1);
227 }
228
229 static inline poly64x2_t shift_crc_pmull(poly64x2_t shifted_crc, int64_t length) {
230 int nibble_idx = 0;
231 while (length > 0) {
232 uint8_t nibble_len = length & 0xf;
233 if (nibble_len) {
234 // Get the pair of 4 bit nibble shift factors for the least significant 4 bits in the length
235 poly64x2_t shift_factors =
236 load_p64(aws_checksums_crc64nvme_constants.shift_factors[nibble_idx][nibble_len]);
237 // Multiply both halves of the shifted value with the pair of pre-computed constants and fold them together
238 shifted_crc = xor_p64(pmull_lo(shifted_crc, shift_factors), pmull_hi(shifted_crc, shift_factors));
239 }
240 // advance the nibble index and right shift the length for the next 4 bit nibble
241 nibble_idx++;
242 length >>= 4;
243 }
244 // Caller is responsible for modular reduction
245 return shifted_crc;
246 }
247
248 uint64_t aws_checksums_crc64nvme_combine_arm_pmull(uint64_t crc1, uint64_t crc2, uint64_t len2) {
249 if (AWS_UNLIKELY(len2 == 0)) {
250 return crc1;
251 }
252
253 // Load the bit-reflected CRC into the upper half of the register
254 poly64x2_t shifted_crc = vcombine_p64(vcreate_p64(0), vcreate_p64(crc1));
255
256 // shift the CRC by the given length
257 shifted_crc = shift_crc_pmull(shifted_crc, len2);
258
259 // Barrett modular reduction
260 crc1 = barrett_reduce_refl(load_p64(aws_checksums_crc64nvme_constants.mu_poly), shifted_crc);
261
262 return crc1 ^ crc2;
263 }
264
265 #endif // INTPTR_MAX == INT64_MAX && defined(AWS_HAVE_ARMv8_1)