Debian Codegraph @

crt/aws-checksums/source/intel/intrin/crc64nvme_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/crc64_priv.h>
7
8 #if defined(AWS_HAVE_AVX512_INTRINSICS) && defined(AWS_ARCH_INTEL_X64)
9
10 # include <emmintrin.h>
11 # include <immintrin.h>
12 # include <smmintrin.h>
13 # include <wmmintrin.h>
14
15 # define load_xmm(ptr) _mm_loadu_si128((const __m128i *)(const void *)(ptr))
16 # define mask_high_bytes(xmm, count) \
17 _mm_and_si128((xmm), load_xmm(aws_checksums_masks_shifts[3] + (intptr_t)(count)))
18 # define cmull_xmm_hi(xmm1, xmm2) _mm_clmulepi64_si128((xmm1), (xmm2), 0x11)
19 # define cmull_xmm_lo(xmm1, xmm2) _mm_clmulepi64_si128((xmm1), (xmm2), 0x00)
20 # define cmull_xmm_pair(xmm1, xmm2) _mm_xor_si128(cmull_xmm_hi((xmm1), (xmm2)), cmull_xmm_lo((xmm1), (xmm2)))
21 # define xor_xmm(xmm1, xmm2, xmm3) \
22 _mm_ternarylogic_epi64((xmm1), (xmm2), (xmm3), 0x96) // The constant 0x96 produces a 3-way XOR
23
24 # define load_zmm(ptr) _mm512_loadu_si512((const uint8_t *)(const void *)(ptr))
25 # define cmull_zmm_hi(zmm1, zmm2) _mm512_clmulepi64_epi128((zmm1), (zmm2), 0x11)
26 # define cmull_zmm_lo(zmm1, zmm2) _mm512_clmulepi64_epi128((zmm1), (zmm2), 0x00)
27 # define cmull_zmm_pair(zmm1, zmm2) _mm512_xor_si512(cmull_zmm_hi((zmm1), (zmm2)), cmull_zmm_lo((zmm1), (zmm2)))
28 # define xor_zmm(zmm1, zmm2, zmm3) \
29 _mm512_ternarylogic_epi64((zmm1), (zmm2), (zmm3), 0x96) // The constant 0x96 produces a 3-way XOR
30
31 uint64_t aws_checksums_crc64nvme_intel_avx512(const uint8_t *input, int length, const uint64_t previous_crc64) {
32
33 if (length < 512) {
34 return aws_checksums_crc64nvme_intel_clmul(input, length, previous_crc64);
35 }
36
37 // The following code assumes a minimum of 256 bytes of input
38
39 // Load the (inverted) CRC into a ZMM register
40 __m512i x1 = _mm512_inserti32x4(_mm512_setzero_si512(), _mm_cvtsi64_si128((int64_t)~previous_crc64), 0);
41 // Load the first 64 bytes into a zmm register and XOR with the (inverted) crc
42 x1 = _mm512_xor_si512(x1, load_zmm(input));
43 // Load 192 more bytes of input
44 __m512i x2 = load_zmm(input + 0x40);
45 __m512i x3 = load_zmm(input + 0x80);
46 __m512i x4 = load_zmm(input + 0xc0);
47 input += 256;
48 length -= 256;
49
50 const __m512i kp_2048 = load_zmm(aws_checksums_crc64nvme_constants.x2048);
51 const __m512i kp_512 = load_zmm(aws_checksums_crc64nvme_constants.x512);
52
53 int loops = length / 256;
54 length &= 255;
55
56 // Parallel fold blocks of 256 bytes, if any
57 while (loops--) {
58 x1 = xor_zmm(cmull_zmm_lo(kp_2048, x1), cmull_zmm_hi(kp_2048, x1), load_zmm(input + 0x00));
59 x2 = xor_zmm(cmull_zmm_lo(kp_2048, x2), cmull_zmm_hi(kp_2048, x2), load_zmm(input + 0x40));
60 x3 = xor_zmm(cmull_zmm_lo(kp_2048, x3), cmull_zmm_hi(kp_2048, x3), load_zmm(input + 0x80));
61 x4 = xor_zmm(cmull_zmm_lo(kp_2048, x4), cmull_zmm_hi(kp_2048, x4), load_zmm(input + 0xc0));
62 input += 256;
63 }
64
65 // Fold 2048 bits into 512 bits
66 const __m512i kp_1536 = load_zmm(aws_checksums_crc64nvme_constants.x1536);
67 const __m512i kp_1024 = load_zmm(aws_checksums_crc64nvme_constants.x1024);
68
69 x1 = xor_zmm(cmull_zmm_lo(kp_1536, x1), cmull_zmm_hi(kp_1536, x1), cmull_zmm_lo(kp_1024, x2));
70 x2 = xor_zmm(cmull_zmm_hi(kp_1024, x2), cmull_zmm_lo(kp_512, x3), cmull_zmm_hi(kp_512, x3));
71 x1 = xor_zmm(x1, x2, x4);
72
73 // Fold blocks of 512 bits, if any
74 loops = length / 64;
75 length &= 63;
76 while (loops--) {
77 x1 = xor_zmm(cmull_zmm_lo(kp_512, x1), cmull_zmm_hi(kp_512, x1), load_zmm(input));
78 input += 64;
79 }
80
81 // Load 64 bytes of constants: x^448, x^384, x^320, x^256, x^192, x^128, N/A, N/A
82 const __m512i kp_384 = load_zmm(aws_checksums_crc64nvme_constants.x384);
83
84 // Fold 512 bits to 128 bits
85 x2 = cmull_zmm_pair(kp_384, x1);
86 __m128i a1 = _mm_xor_si128(_mm512_extracti32x4_epi32(x1, 3), _mm512_extracti32x4_epi32(x2, 0));
87 a1 = xor_xmm(a1, _mm512_extracti32x4_epi32(x2, 1), _mm512_extracti32x4_epi32(x2, 2));
88
89 // Single fold blocks of 128 bits, if any
90 loops = length / 16;
91 __m128i kp_128 = _mm512_extracti32x4_epi32(kp_384, 2);
92 while (loops--) {
93 a1 = xor_xmm(cmull_xmm_lo(kp_128, a1), cmull_xmm_hi(kp_128, a1), load_xmm(input));
94 input += 16;
95 }
96
97 // The remaining length can be only 0-15 bytes
98 length &= 15;
99
100 // Load the x^128 constant (note that we don't need x^192).
101 const __m128i x128 = _mm_set_epi64x(0, aws_checksums_crc64nvme_constants.x128[1]);
102 if (length == 0) {
103 // Multiply the lower half of the crc register by x^128 and XOR the result with the upper half of the crc.
104 a1 = _mm_xor_si128(_mm_bsrli_si128(a1, 8), cmull_xmm_lo(a1, x128));
105 } else {
106 // Handle any trailing input from 1-15 bytes.
107 __m128i trailing_constants = load_xmm(aws_checksums_crc64nvme_constants.trailing[length - 1]);
108 // Multiply the crc by a pair of trailing length constants in order to fold it into the trailing input.
109 a1 = cmull_xmm_pair(a1, trailing_constants);
110 // Safely load ending at the trailing input and mask out any leading garbage
111 __m128i trailing_input = mask_high_bytes(load_xmm(input + length - 16), length);
112 // Multiply the lower half of the trailing input register by x^128
113 __m128i mul_by_x128 = cmull_xmm_lo(trailing_input, x128);
114 // XOR the results with the upper half of the trailing input
115 a1 = xor_xmm(a1, _mm_bsrli_si128(trailing_input, 8), mul_by_x128);
116 }
117
118 // Barrett modular reduction
119 const __m128i mu_poly = load_xmm(&aws_checksums_crc64nvme_constants.mu_poly);
120 // Multiply the lower half of input by mu
121 __m128i mul_by_mu = _mm_clmulepi64_si128(mu_poly, a1, 0x00);
122 // Multiply the lower half of the mul_by_mu result by poly (it's in the upper half)
123 __m128i mul_by_poly = _mm_clmulepi64_si128(mu_poly, mul_by_mu, 0x01);
124 // Left shift mul_by_mu to get the low half into the upper half and XOR all the upper halves
125 __m128i reduced = xor_xmm(a1, _mm_bslli_si128(mul_by_mu, 8), mul_by_poly);
126 // After the XORs, the CRC falls in the upper half of the register - invert the bits before returning the crc
127 return ~(uint64_t)_mm_extract_epi64(reduced, 1);
128 }
129
130 #endif /* defined(AWS_HAVE_AVX512_INTRINSICS) && defined(AWS_ARCH_INTEL_X64)*/