| 1 | #include <stdbool.h> |
| 2 | #include <stddef.h> |
| 3 | #include <stdint.h> |
| 4 | |
| 5 | #include "blake3_impl.h" |
| 6 | |
| 7 | #if defined(_MSC_VER) |
| 8 | #include <Windows.h> |
| 9 | #endif |
| 10 | |
| 11 | #if defined(IS_X86) |
| 12 | #if defined(_MSC_VER) |
| 13 | #include <intrin.h> |
| 14 | #elif defined(__GNUC__) |
| 15 | #include <immintrin.h> |
| 16 | #else |
| 17 | #undef IS_X86 /* Unimplemented! */ |
| 18 | #endif |
| 19 | #endif |
| 20 | |
| 21 | #if !defined(BLAKE3_ATOMICS) |
| 22 | #if defined(__has_include) |
| 23 | #if __has_include(<stdatomic.h>) && !defined(_MSC_VER) |
| 24 | #define BLAKE3_ATOMICS 1 |
| 25 | #else |
| 26 | #define BLAKE3_ATOMICS 0 |
| 27 | #endif /* __has_include(<stdatomic.h>) && !defined(_MSC_VER) */ |
| 28 | #else |
| 29 | #define BLAKE3_ATOMICS 0 |
| 30 | #endif /* defined(__has_include) */ |
| 31 | #endif /* BLAKE3_ATOMICS */ |
| 32 | |
| 33 | #if BLAKE3_ATOMICS |
| 34 | #define ATOMIC_INT _Atomic int |
| 35 | #define ATOMIC_LOAD(x) x |
| 36 | #define ATOMIC_STORE(x, y) x = y |
| 37 | #elif defined(_MSC_VER) |
| 38 | #define ATOMIC_INT LONG |
| 39 | #define ATOMIC_LOAD(x) InterlockedOr(&x, 0) |
| 40 | #define ATOMIC_STORE(x, y) InterlockedExchange(&x, y) |
| 41 | #else |
| 42 | #define ATOMIC_INT int |
| 43 | #define ATOMIC_LOAD(x) x |
| 44 | #define ATOMIC_STORE(x, y) x = y |
| 45 | #endif |
| 46 | |
| 47 | #define MAYBE_UNUSED(x) (void)((x)) |
| 48 | |
| 49 | #if defined(IS_X86) |
| 50 | static uint64_t xgetbv(void) { |
| 51 | #if defined(_MSC_VER) |
| 52 | return _xgetbv(0); |
| 53 | #else |
| 54 | uint32_t eax = 0, edx = 0; |
| 55 | __asm__ __volatile__("xgetbv\n" : "=a"(eax), "=d"(edx) : "c"(0)); |
| 56 | return ((uint64_t)edx << 32) | eax; |
| 57 | #endif |
| 58 | } |
| 59 | |
| 60 | static void cpuid(uint32_t out[4], uint32_t id) { |
| 61 | #if defined(_MSC_VER) |
| 62 | __cpuid((int *)out, id); |
| 63 | #elif defined(__i386__) || defined(_M_IX86) |
| 64 | __asm__ __volatile__("movl %%ebx, %1\n" |
| 65 | "cpuid\n" |
| 66 | "xchgl %1, %%ebx\n" |
| 67 | : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3]) |
| 68 | : "a"(id)); |
| 69 | #else |
| 70 | __asm__ __volatile__("cpuid\n" |
| 71 | : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3]) |
| 72 | : "a"(id)); |
| 73 | #endif |
| 74 | } |
| 75 | |
| 76 | static void cpuidex(uint32_t out[4], uint32_t id, uint32_t sid) { |
| 77 | #if defined(_MSC_VER) |
| 78 | __cpuidex((int *)out, id, sid); |
| 79 | #elif defined(__i386__) || defined(_M_IX86) |
| 80 | __asm__ __volatile__("movl %%ebx, %1\n" |
| 81 | "cpuid\n" |
| 82 | "xchgl %1, %%ebx\n" |
| 83 | : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3]) |
| 84 | : "a"(id), "c"(sid)); |
| 85 | #else |
| 86 | __asm__ __volatile__("cpuid\n" |
| 87 | : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3]) |
| 88 | : "a"(id), "c"(sid)); |
| 89 | #endif |
| 90 | } |
| 91 | |
| 92 | |
| 93 | enum cpu_feature { |
| 94 | SSE2 = 1 << 0, |
| 95 | SSSE3 = 1 << 1, |
| 96 | SSE41 = 1 << 2, |
| 97 | AVX = 1 << 3, |
| 98 | AVX2 = 1 << 4, |
| 99 | AVX512F = 1 << 5, |
| 100 | AVX512VL = 1 << 6, |
| 101 | /* ... */ |
| 102 | UNDEFINED = 1 << 30 |
| 103 | }; |
| 104 | |
| 105 | #if !defined(BLAKE3_TESTING) |
| 106 | static /* Allow the variable to be controlled manually for testing */ |
| 107 | #endif |
| 108 | ATOMIC_INT g_cpu_features = UNDEFINED; |
| 109 | |
| 110 | #if !defined(BLAKE3_TESTING) |
| 111 | static |
| 112 | #endif |
| 113 | enum cpu_feature |
| 114 | get_cpu_features(void) { |
| 115 | |
| 116 | /* If TSAN detects a data race here, try compiling with -DBLAKE3_ATOMICS=1 */ |
| 117 | enum cpu_feature features = ATOMIC_LOAD(g_cpu_features); |
| 118 | if (features != UNDEFINED) { |
| 119 | return features; |
| 120 | } else { |
| 121 | #if defined(IS_X86) |
| 122 | uint32_t regs[4] = {0}; |
| 123 | uint32_t *eax = ®s[0], *ebx = ®s[1], *ecx = ®s[2], *edx = ®s[3]; |
| 124 | (void)edx; |
| 125 | features = 0; |
| 126 | cpuid(regs, 0); |
| 127 | const int max_id = *eax; |
| 128 | cpuid(regs, 1); |
| 129 | #if defined(__amd64__) || defined(_M_X64) |
| 130 | features |= SSE2; |
| 131 | #else |
| 132 | if (*edx & (1UL << 26)) |
| 133 | features |= SSE2; |
| 134 | #endif |
| 135 | if (*ecx & (1UL << 9)) |
| 136 | features |= SSSE3; |
| 137 | if (*ecx & (1UL << 19)) |
| 138 | features |= SSE41; |
| 139 | |
| 140 | if (*ecx & (1UL << 27)) { // OSXSAVE |
| 141 | const uint64_t mask = xgetbv(); |
| 142 | if ((mask & 6) == 6) { // SSE and AVX states |
| 143 | if (*ecx & (1UL << 28)) |
| 144 | features |= AVX; |
| 145 | if (max_id >= 7) { |
| 146 | cpuidex(regs, 7, 0); |
| 147 | if (*ebx & (1UL << 5)) |
| 148 | features |= AVX2; |
| 149 | if ((mask & 224) == 224) { // Opmask, ZMM_Hi256, Hi16_Zmm |
| 150 | if (*ebx & (1UL << 31)) |
| 151 | features |= AVX512VL; |
| 152 | if (*ebx & (1UL << 16)) |
| 153 | features |= AVX512F; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | ATOMIC_STORE(g_cpu_features, features); |
| 159 | return features; |
| 160 | #else |
| 161 | /* How to detect NEON? */ |
| 162 | return 0; |
| 163 | #endif |
| 164 | } |
| 165 | } |
| 166 | #endif |
| 167 | |
| 168 | void blake3_compress_in_place(uint32_t cv[8], |
| 169 | const uint8_t block[BLAKE3_BLOCK_LEN], |
| 170 | uint8_t block_len, uint64_t counter, |
| 171 | uint8_t flags) { |
| 172 | #if defined(IS_X86) |
| 173 | const enum cpu_feature features = get_cpu_features(); |
| 174 | MAYBE_UNUSED(features); |
| 175 | #if !defined(BLAKE3_NO_AVX512) |
| 176 | if (features & AVX512VL) { |
| 177 | blake3_compress_in_place_avx512(cv, block, block_len, counter, flags); |
| 178 | return; |
| 179 | } |
| 180 | #endif |
| 181 | #if !defined(BLAKE3_NO_SSE41) |
| 182 | if (features & SSE41) { |
| 183 | blake3_compress_in_place_sse41(cv, block, block_len, counter, flags); |
| 184 | return; |
| 185 | } |
| 186 | #endif |
| 187 | #if !defined(BLAKE3_NO_SSE2) |
| 188 | if (features & SSE2) { |
| 189 | blake3_compress_in_place_sse2(cv, block, block_len, counter, flags); |
| 190 | return; |
| 191 | } |
| 192 | #endif |
| 193 | #endif |
| 194 | blake3_compress_in_place_portable(cv, block, block_len, counter, flags); |
| 195 | } |
| 196 | |
| 197 | void blake3_compress_xof(const uint32_t cv[8], |
| 198 | const uint8_t block[BLAKE3_BLOCK_LEN], |
| 199 | uint8_t block_len, uint64_t counter, uint8_t flags, |
| 200 | uint8_t out[64]) { |
| 201 | #if defined(IS_X86) |
| 202 | const enum cpu_feature features = get_cpu_features(); |
| 203 | MAYBE_UNUSED(features); |
| 204 | #if !defined(BLAKE3_NO_AVX512) |
| 205 | if (features & AVX512VL) { |
| 206 | blake3_compress_xof_avx512(cv, block, block_len, counter, flags, out); |
| 207 | return; |
| 208 | } |
| 209 | #endif |
| 210 | #if !defined(BLAKE3_NO_SSE41) |
| 211 | if (features & SSE41) { |
| 212 | blake3_compress_xof_sse41(cv, block, block_len, counter, flags, out); |
| 213 | return; |
| 214 | } |
| 215 | #endif |
| 216 | #if !defined(BLAKE3_NO_SSE2) |
| 217 | if (features & SSE2) { |
| 218 | blake3_compress_xof_sse2(cv, block, block_len, counter, flags, out); |
| 219 | return; |
| 220 | } |
| 221 | #endif |
| 222 | #endif |
| 223 | blake3_compress_xof_portable(cv, block, block_len, counter, flags, out); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | void blake3_xof_many(const uint32_t cv[8], |
| 228 | const uint8_t block[BLAKE3_BLOCK_LEN], |
| 229 | uint8_t block_len, uint64_t counter, uint8_t flags, |
| 230 | uint8_t out[64], size_t outblocks) { |
| 231 | if (outblocks == 0) { |
| 232 | // The current assembly implementation always outputs at least 1 block. |
| 233 | return; |
| 234 | } |
| 235 | #if defined(IS_X86) |
| 236 | const enum cpu_feature features = get_cpu_features(); |
| 237 | MAYBE_UNUSED(features); |
| 238 | #if !defined(_WIN32) && !defined(__CYGWIN__) && !defined(BLAKE3_NO_AVX512) |
| 239 | if (features & AVX512VL) { |
| 240 | blake3_xof_many_avx512(cv, block, block_len, counter, flags, out, outblocks); |
| 241 | return; |
| 242 | } |
| 243 | #endif |
| 244 | #endif |
| 245 | for(size_t i = 0; i < outblocks; ++i) { |
| 246 | blake3_compress_xof(cv, block, block_len, counter + i, flags, out + 64*i); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | void blake3_hash_many(const uint8_t *const *inputs, size_t num_inputs, |
| 251 | size_t blocks, const uint32_t key[8], uint64_t counter, |
| 252 | bool increment_counter, uint8_t flags, |
| 253 | uint8_t flags_start, uint8_t flags_end, uint8_t *out) { |
| 254 | #if defined(IS_X86) |
| 255 | const enum cpu_feature features = get_cpu_features(); |
| 256 | MAYBE_UNUSED(features); |
| 257 | #if !defined(BLAKE3_NO_AVX512) |
| 258 | if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) { |
| 259 | blake3_hash_many_avx512(inputs, num_inputs, blocks, key, counter, |
| 260 | increment_counter, flags, flags_start, flags_end, |
| 261 | out); |
| 262 | return; |
| 263 | } |
| 264 | #endif |
| 265 | #if !defined(BLAKE3_NO_AVX2) |
| 266 | if (features & AVX2) { |
| 267 | blake3_hash_many_avx2(inputs, num_inputs, blocks, key, counter, |
| 268 | increment_counter, flags, flags_start, flags_end, |
| 269 | out); |
| 270 | return; |
| 271 | } |
| 272 | #endif |
| 273 | #if !defined(BLAKE3_NO_SSE41) |
| 274 | if (features & SSE41) { |
| 275 | blake3_hash_many_sse41(inputs, num_inputs, blocks, key, counter, |
| 276 | increment_counter, flags, flags_start, flags_end, |
| 277 | out); |
| 278 | return; |
| 279 | } |
| 280 | #endif |
| 281 | #if !defined(BLAKE3_NO_SSE2) |
| 282 | if (features & SSE2) { |
| 283 | blake3_hash_many_sse2(inputs, num_inputs, blocks, key, counter, |
| 284 | increment_counter, flags, flags_start, flags_end, |
| 285 | out); |
| 286 | return; |
| 287 | } |
| 288 | #endif |
| 289 | #endif |
| 290 | |
| 291 | #if BLAKE3_USE_NEON == 1 |
| 292 | blake3_hash_many_neon(inputs, num_inputs, blocks, key, counter, |
| 293 | increment_counter, flags, flags_start, flags_end, out); |
| 294 | return; |
| 295 | #endif |
| 296 | |
| 297 | blake3_hash_many_portable(inputs, num_inputs, blocks, key, counter, |
| 298 | increment_counter, flags, flags_start, flags_end, |
| 299 | out); |
| 300 | } |
| 301 | |
| 302 | // The dynamically detected SIMD degree of the current platform. |
| 303 | size_t blake3_simd_degree(void) { |
| 304 | #if defined(IS_X86) |
| 305 | const enum cpu_feature features = get_cpu_features(); |
| 306 | MAYBE_UNUSED(features); |
| 307 | #if !defined(BLAKE3_NO_AVX512) |
| 308 | if ((features & (AVX512F|AVX512VL)) == (AVX512F|AVX512VL)) { |
| 309 | return 16; |
| 310 | } |
| 311 | #endif |
| 312 | #if !defined(BLAKE3_NO_AVX2) |
| 313 | if (features & AVX2) { |
| 314 | return 8; |
| 315 | } |
| 316 | #endif |
| 317 | #if !defined(BLAKE3_NO_SSE41) |
| 318 | if (features & SSE41) { |
| 319 | return 4; |
| 320 | } |
| 321 | #endif |
| 322 | #if !defined(BLAKE3_NO_SSE2) |
| 323 | if (features & SSE2) { |
| 324 | return 4; |
| 325 | } |
| 326 | #endif |
| 327 | #endif |
| 328 | #if BLAKE3_USE_NEON == 1 |
| 329 | return 4; |
| 330 | #endif |
| 331 | return 1; |
| 332 | } |