| 1 |
|
| 2 |
|
| 3 |
|
| 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 |
|
| 23 |
# define load_zmm(ptr) _mm512_loadu_si512((const uint8_t *)(const void *)(ptr)) |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 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 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
| 156 |
|
| 157 |
uint32_t aws_checksums_crc32c_intel_avx512_with_sse_fallback(const uint8_t *input, int length, uint32_t previous_crc) { |
| 158 |
|
| 159 |
|
| 160 |
uint32_t crc = ~previous_crc; |
| 161 |
|
| 162 |
|
| 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 |
|
| 171 |
int input_alignment = (uintptr_t)(input) & 0x7; |
| 172 |
|
| 173 |
|
| 174 |
int leading = (8 - input_alignment) & 0x7; |
| 175 |
|
| 176 |
|
| 177 |
length -= leading; |
| 178 |
|
| 179 |
|
| 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 |
|
| 191 |
length -= chunk_size; |
| 192 |
if (!length) { |
| 193 |
return ~crc; |
| 194 |
} |
| 195 |
|
| 196 |
|
| 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 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
return aws_checksums_crc32c_clmul_sse42(input, length, ~crc); |
| 210 |
} |
| 211 |
#endif |
| 212 |
|
| 213 |
|
| 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 |
|
| 221 |
while (length-- > 0) { |
| 222 |
crc = (uint32_t)_mm_crc32_u8(crc, *input); |
| 223 |
input++; |
| 224 |
} |
| 225 |
|
| 226 |
return ~crc; |
| 227 |
} |