| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 14 |
# define load_u8(uint8_t_ptr) vld1q_u8((uint8_t_ptr)) |
| 15 |
|
| 16 |
# define load_p64_u8(uint8_t_ptr) vreinterpretq_p64_u8(load_u8(uint8_t_ptr)) |
| 17 |
|
| 18 |
# define load_p64(uint64_t_ptr) vreinterpretq_p64_u64(vld1q_u64((uint64_t_ptr))) |
| 19 |
|
| 20 |
# define mask_low_u8(u8, count) vandq_u8(u8, load_u8(aws_checksums_masks_shifts[5] - (intptr_t)(count))) |
| 21 |
|
| 22 |
# define mask_high_u8(u8, count) vandq_u8(u8, load_u8(aws_checksums_masks_shifts[3] + (intptr_t)(count))) |
| 23 |
|
| 24 |
# define mask_high_p64(poly, count) vreinterpretq_p64_u8(mask_high_u8(vreinterpretq_u8_p64(poly), count)) |
| 25 |
|
| 26 |
# define left_shift_u8(u8, count) vqtbl1q_u8(u8, load_u8(aws_checksums_masks_shifts[1] - (intptr_t)(count))) |
| 27 |
|
| 28 |
# define right_shift_u8(u8, count) vqtbl1q_u8(u8, load_u8(aws_checksums_masks_shifts[1] + (intptr_t)(count))) |
| 29 |
|
| 30 |
# define left_shift_p64(poly, count) vreinterpretq_p64_u8(left_shift_u8(vreinterpretq_u8_p64(poly), count)) |
| 31 |
|
| 32 |
# define right_shift_imm_p64(poly, imm) \ |
| 33 |
vreinterpretq_p64_u8(vextq_u8(vreinterpretq_u8_p64(poly), vdupq_n_u8(0), imm)) |
| 34 |
|
| 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 |
|
| 38 |
# define pmull_hi(a, b) (vreinterpretq_p64_p128(vmull_high_p64((a), (b)))) |
| 39 |
|
| 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 |
|
| 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 |
|
| 48 |
# define xor3_p64(a, b, c) xor_p64(xor_p64(a, b), c) |
| 49 |
# endif |
| 50 |
|
| 51 |
|
| 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 |
|
| 58 |
|
| 59 |
|
| 60 |
if (length < 16) { |
| 61 |
return aws_checksums_crc64nvme_sw(input, length, previous_crc64); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
poly64x2_t a1 = vreinterpretq_p64_u64(vcombine_u64(vcreate_u64(~previous_crc64), vcreate_u64(0))); |
| 66 |
|
| 67 |
|
| 68 |
const poly64x2_t x128 = load_p64(aws_checksums_crc64nvme_constants.x128); |
| 69 |
|
| 70 |
|
| 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 |
|
| 81 |
|
| 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 |
|
| 97 |
|
| 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 { |
| 105 |
|
| 106 |
const poly64x2_t x1024 = load_p64(aws_checksums_crc64nvme_constants.x1024); |
| 107 |
|
| 108 |
|
| 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 |
|
| 120 |
int loops = length / 128; |
| 121 |
while (loops--) { |
| 122 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 163 |
a1 = xor3_p64(b1, pmull_lo(x128, a1), pmull_hi(x128, a1)); |
| 164 |
} |
| 165 |
|
| 166 |
if (length & 16) { |
| 167 |
|
| 168 |
a1 = xor3_p64(pmull_lo(x128, a1), pmull_hi(x128, a1), load_p64_u8(input + 0)); |
| 169 |
input += 16; |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
length &= 15; |
| 174 |
|
| 175 |
if (length == 0) { |
| 176 |
|
| 177 |
poly64x2_t mul_by_x128 = pmull_lo(a1, vextq_p64(x128, x128, 1)); |
| 178 |
|
| 179 |
a1 = xor_p64(right_shift_imm_p64(a1, 8), mul_by_x128); |
| 180 |
} else { |
| 181 |
|
| 182 |
const poly64x2_t trailing_constants = load_p64(aws_checksums_crc64nvme_constants.trailing[length - 1]); |
| 183 |
|
| 184 |
a1 = xor_p64(pmull_lo(a1, trailing_constants), pmull_hi(a1, trailing_constants)); |
| 185 |
|
| 186 |
poly64x2_t trailing_input = mask_high_p64(load_p64_u8(input + length - 16), length); |
| 187 |
|
| 188 |
poly64x2_t mul_by_x128 = pmull_lo(trailing_input, vextq_p64(x128, x128, 1)); |
| 189 |
|
| 190 |
a1 = xor3_p64(a1, right_shift_imm_p64(trailing_input, 8), mul_by_x128); |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
const poly64x2_t mu_poly = load_p64(aws_checksums_crc64nvme_constants.mu_poly); |
| 197 |
|
| 198 |
poly64x2_t mul_by_mu = pmull_lo(a1, mu_poly); |
| 199 |
|
| 200 |
poly64x2_t mul_by_poly = pmull_lo(mul_by_mu, vextq_p64(mu_poly, mu_poly, 1)); |
| 201 |
|
| 202 |
poly64x2_t result = xor3_p64(a1, vextq_p64(mul_by_mu, mul_by_mu, 1), mul_by_poly); |
| 203 |
|
| 204 |
|
| 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 |
|
| 211 |
poly64x2_t mul_by_u = pmull_lo(input, kp_poly_mu); |
| 212 |
|
| 213 |
|
| 214 |
poly64x2_t poly = vextq_p64(kp_poly_mu, kp_poly_mu, 1); |
| 215 |
|
| 216 |
|
| 217 |
poly64x2_t mu_swap = vextq_p64(mul_by_u, mul_by_u, 1); |
| 218 |
|
| 219 |
|
| 220 |
poly64x2_t mul_by_p = pmull_lo(mul_by_u, poly); |
| 221 |
|
| 222 |
|
| 223 |
poly64x2_t result = xor3_p64(input, mu_swap, mul_by_p); |
| 224 |
|
| 225 |
|
| 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 |
|
| 235 |
poly64x2_t shift_factors = |
| 236 |
load_p64(aws_checksums_crc64nvme_constants.shift_factors[nibble_idx][nibble_len]); |
| 237 |
|
| 238 |
shifted_crc = xor_p64(pmull_lo(shifted_crc, shift_factors), pmull_hi(shifted_crc, shift_factors)); |
| 239 |
} |
| 240 |
|
| 241 |
nibble_idx++; |
| 242 |
length >>= 4; |
| 243 |
} |
| 244 |
|
| 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 |
|
| 254 |
poly64x2_t shifted_crc = vcombine_p64(vcreate_p64(0), vcreate_p64(crc1)); |
| 255 |
|
| 256 |
|
| 257 |
shifted_crc = shift_crc_pmull(shifted_crc, len2); |
| 258 |
|
| 259 |
|
| 260 |
crc1 = barrett_reduce_refl(load_p64(aws_checksums_crc64nvme_constants.mu_poly), shifted_crc); |
| 261 |
|
| 262 |
return crc1 ^ crc2; |
| 263 |
} |
| 264 |
|
| 265 |
#endif |