Debian Codegraph @

crt/aws-checksums/source/intel/intrin/crc64nvme_clmul.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/assert.h>
8
9 // msvc compilers older than 2019 are missing some intrinsics. Gate those off.
10 #if defined(AWS_ARCH_INTEL_X64) && defined(AWS_HAVE_CLMUL) && !(defined(_MSC_VER) && _MSC_VER < 1920)
11
12 # include <emmintrin.h>
13 # include <immintrin.h>
14 # include <smmintrin.h>
15 # include <wmmintrin.h>
16
17 # define load_xmm(ptr) _mm_loadu_si128((const __m128i *)(const void *)(ptr))
18 # define mask_high_bytes(xmm, count) \
19 _mm_and_si128((xmm), load_xmm(aws_checksums_masks_shifts[3] + (intptr_t)(count)))
20 # define cmull_xmm_hi(xmm1, xmm2) _mm_clmulepi64_si128((xmm1), (xmm2), 0x11)
21 # define cmull_xmm_lo(xmm1, xmm2) _mm_clmulepi64_si128((xmm1), (xmm2), 0x00)
22 # define cmull_xmm_pair(xmm1, xmm2) _mm_xor_si128(cmull_xmm_hi((xmm1), (xmm2)), cmull_xmm_lo((xmm1), (xmm2)))
23
24 uint64_t aws_checksums_crc64nvme_intel_clmul(const uint8_t *input, int length, uint64_t previous_crc64) {
25
26 // the amount of complexity required to handle vector instructions on
27 // memory regions smaller than an xmm register does not justify the very negligible performance gains
28 // we would get for using it on an input this small.
29 if (length < 16) {
30 return aws_checksums_crc64nvme_sw(input, length, previous_crc64);
31 }
32
33 // Invert the previous crc bits and load into the lower half of an xmm register
34 __m128i a1 = _mm_cvtsi64_si128((int64_t)(~previous_crc64));
35
36 // There are 16 or more bytes of input - load the first 16 bytes and XOR with the previous crc
37 a1 = _mm_xor_si128(a1, load_xmm(input));
38 input += 16;
39 length -= 16;
40
41 // Load the folding constants x^128 and x^192
42 const __m128i x128 = load_xmm(aws_checksums_crc64nvme_constants.x128);
43
44 if (length >= 48) {
45 // Load the next 48 bytes
46 __m128i b1 = load_xmm(input + 0x00);
47 __m128i c1 = load_xmm(input + 0x10);
48 __m128i d1 = load_xmm(input + 0x20);
49
50 input += 48;
51 length -= 48;
52
53 // Load the folding constants x^512 and x^576
54 const __m128i x512 = load_xmm(aws_checksums_crc64nvme_constants.x512);
55
56 if (length >= 64) {
57 // Load the next 64 bytes
58 __m128i e1 = load_xmm(input + 0x00);
59 __m128i f1 = load_xmm(input + 0x10);
60 __m128i g1 = load_xmm(input + 0x20);
61 __m128i h1 = load_xmm(input + 0x30);
62 input += 64;
63 length -= 64;
64
65 // Load the folding constants x^1024 and x^1088
66 const __m128i x1024 = load_xmm(aws_checksums_crc64nvme_constants.x1024);
67
68 // Spin through 128 bytes and fold in parallel
69 int loops = length / 128;
70 length &= 127;
71 while (loops--) {
72 a1 = _mm_xor_si128(cmull_xmm_pair(x1024, a1), load_xmm(input + 0x00));
73 b1 = _mm_xor_si128(cmull_xmm_pair(x1024, b1), load_xmm(input + 0x10));
74 c1 = _mm_xor_si128(cmull_xmm_pair(x1024, c1), load_xmm(input + 0x20));
75 d1 = _mm_xor_si128(cmull_xmm_pair(x1024, d1), load_xmm(input + 0x30));
76 e1 = _mm_xor_si128(cmull_xmm_pair(x1024, e1), load_xmm(input + 0x40));
77 f1 = _mm_xor_si128(cmull_xmm_pair(x1024, f1), load_xmm(input + 0x50));
78 g1 = _mm_xor_si128(cmull_xmm_pair(x1024, g1), load_xmm(input + 0x60));
79 h1 = _mm_xor_si128(cmull_xmm_pair(x1024, h1), load_xmm(input + 0x70));
80 input += 128;
81 }
82
83 // Fold 128 to 64 bytes - e1 through h1 fold into a1 through d1
84 a1 = _mm_xor_si128(cmull_xmm_pair(x512, a1), e1);
85 b1 = _mm_xor_si128(cmull_xmm_pair(x512, b1), f1);
86 c1 = _mm_xor_si128(cmull_xmm_pair(x512, c1), g1);
87 d1 = _mm_xor_si128(cmull_xmm_pair(x512, d1), h1);
88 }
89
90 if (length & 64) {
91 a1 = _mm_xor_si128(cmull_xmm_pair(x512, a1), load_xmm(input + 0x00));
92 b1 = _mm_xor_si128(cmull_xmm_pair(x512, b1), load_xmm(input + 0x10));
93 c1 = _mm_xor_si128(cmull_xmm_pair(x512, c1), load_xmm(input + 0x20));
94 d1 = _mm_xor_si128(cmull_xmm_pair(x512, d1), load_xmm(input + 0x30));
95 input += 64;
96 }
97 length &= 63;
98
99 // Load the x^256, x^320, x^384, and x^448 constants
100 const __m128i x384 = load_xmm(aws_checksums_crc64nvme_constants.x384);
101 const __m128i x256 = load_xmm(aws_checksums_crc64nvme_constants.x256);
102
103 // Fold 64 bytes to 16 bytes
104 a1 = _mm_xor_si128(d1, cmull_xmm_pair(x384, a1));
105 a1 = _mm_xor_si128(a1, cmull_xmm_pair(x256, b1));
106 a1 = _mm_xor_si128(a1, cmull_xmm_pair(x128, c1));
107 }
108
109 // Process any remaining chunks of 16 bytes
110 int loops = length / 16;
111 while (loops--) {
112 a1 = _mm_xor_si128(cmull_xmm_pair(a1, x128), load_xmm(input));
113 input += 16;
114 }
115
116 // The remaining length can be only 0-15 bytes
117 length &= 15;
118 if (length == 0) {
119 // Multiply the lower half of the crc register by x^128 (it's in the upper half)
120 __m128i mul_by_x128 = _mm_clmulepi64_si128(a1, x128, 0x10);
121 // XOR the result with the upper half of the crc
122 a1 = _mm_xor_si128(_mm_bsrli_si128(a1, 8), mul_by_x128);
123 } else { // Handle any trailing input from 1-15 bytes
124 // Multiply the crc by a pair of trailing length constants in order to fold it into the trailing input
125 a1 = cmull_xmm_pair(a1, load_xmm(aws_checksums_crc64nvme_constants.trailing[length - 1]));
126 // Safely load (ending at the trailing input) and mask out any leading garbage
127 __m128i trailing_input = mask_high_bytes(load_xmm(input + length - 16), length);
128 // Multiply the lower half of the trailing input register by x^128 (it's in the upper half)
129 __m128i mul_by_x128 = _mm_clmulepi64_si128(trailing_input, x128, 0x10);
130 // XOR the results with the upper half of the trailing input
131 a1 = _mm_xor_si128(a1, _mm_bsrli_si128(trailing_input, 8));
132 a1 = _mm_xor_si128(a1, mul_by_x128);
133 }
134
135 // Barrett modular reduction
136 const __m128i mu_poly = load_xmm(aws_checksums_crc64nvme_constants.mu_poly);
137 // Multiply the lower half of input by mu
138 __m128i mul_by_mu = _mm_clmulepi64_si128(mu_poly, a1, 0x00);
139 // Multiply the lower half of the mul_by_mu result by poly (it's in the upper half)
140 __m128i mul_by_poly = _mm_clmulepi64_si128(mu_poly, mul_by_mu, 0x01);
141 // Left shift mul_by_mu to get the low half into the upper half and XOR all the upper halves
142 __m128i reduced = _mm_xor_si128(_mm_xor_si128(a1, _mm_bslli_si128(mul_by_mu, 8)), mul_by_poly);
143 // After the XORs, the CRC falls in the upper half of the register - invert the bits before returning the crc
144 return ~(uint64_t)_mm_extract_epi64(reduced, 1);
145 }
146
147 #endif /* defined(AWS_ARCH_INTEL_X64) && defined(AWS_HAVE_CLMUL) && !(defined(_MSC_VER) && _MSC_VER < 1920) */