From f827d9141913b95a96340bccc360d21475d4a3a2 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 28 Sep 2023 18:05:25 +0200 Subject: [PATCH] bump: Upgrade to BLAKE3 1.5.0 --- LICENSE.adoc | 2 +- src/third_party/blake3/blake3.h | 2 +- src/third_party/blake3/blake3_dispatch.c | 39 +++++++++++++++++++++--- src/third_party/blake3/blake3_impl.h | 6 +++- 4 files changed, 41 insertions(+), 8 deletions(-) diff --git a/LICENSE.adoc b/LICENSE.adoc index 01cac3dfc..9a8b702f3 100644 --- a/LICENSE.adoc +++ b/LICENSE.adoc @@ -72,7 +72,7 @@ along with this program. If not, see . === src/third_party/blake3/blake3_* -This is a subset of https://github.com/BLAKE3-team/BLAKE3[BLAKE3] 1.4.1 with the +This is a subset of https://github.com/BLAKE3-team/BLAKE3[BLAKE3] 1.5.0 with the following license: ---- diff --git a/src/third_party/blake3/blake3.h b/src/third_party/blake3/blake3.h index 21e0d7b9d..f694dcf27 100644 --- a/src/third_party/blake3/blake3.h +++ b/src/third_party/blake3/blake3.h @@ -30,7 +30,7 @@ extern "C" { #endif -#define BLAKE3_VERSION_STRING "1.4.1" +#define BLAKE3_VERSION_STRING "1.5.0" #define BLAKE3_KEY_LEN 32 #define BLAKE3_OUT_LEN 32 #define BLAKE3_BLOCK_LEN 64 diff --git a/src/third_party/blake3/blake3_dispatch.c b/src/third_party/blake3/blake3_dispatch.c index 2ab0093ee..af6c3dadc 100644 --- a/src/third_party/blake3/blake3_dispatch.c +++ b/src/third_party/blake3/blake3_dispatch.c @@ -6,6 +6,7 @@ #if defined(IS_X86) #if defined(_MSC_VER) +#include #include #elif defined(__GNUC__) #include @@ -14,6 +15,32 @@ #endif #endif +#if !defined(BLAKE3_ATOMICS) +#if defined(__has_include) +#if __has_include() && !defined(_MSC_VER) +#define BLAKE3_ATOMICS 1 +#else +#define BLAKE3_ATOMICS 0 +#endif /* __has_include() && !defined(_MSC_VER) */ +#else +#define BLAKE3_ATOMICS 0 +#endif /* defined(__has_include) */ +#endif /* BLAKE3_ATOMICS */ + +#if BLAKE3_ATOMICS +#define ATOMIC_INT _Atomic int +#define ATOMIC_LOAD(x) x +#define ATOMIC_STORE(x, y) x = y +#elif defined(_MSC_VER) +#define ATOMIC_INT LONG +#define ATOMIC_LOAD(x) InterlockedOr(&x, 0) +#define ATOMIC_STORE(x, y) InterlockedExchange(&x, y) +#else +#define ATOMIC_INT int +#define ATOMIC_LOAD(x) x +#define ATOMIC_STORE(x, y) x = y +#endif + #define MAYBE_UNUSED(x) (void)((x)) #if defined(IS_X86) @@ -76,7 +103,7 @@ enum cpu_feature { #if !defined(BLAKE3_TESTING) static /* Allow the variable to be controlled manually for testing */ #endif - enum cpu_feature g_cpu_features = UNDEFINED; + ATOMIC_INT g_cpu_features = UNDEFINED; #if !defined(BLAKE3_TESTING) static @@ -84,14 +111,16 @@ static enum cpu_feature get_cpu_features(void) { - if (g_cpu_features != UNDEFINED) { - return g_cpu_features; + /* If TSAN detects a data race here, try compiling with -DBLAKE3_ATOMICS=1 */ + enum cpu_feature features = ATOMIC_LOAD(g_cpu_features); + if (features != UNDEFINED) { + return features; } else { #if defined(IS_X86) uint32_t regs[4] = {0}; uint32_t *eax = ®s[0], *ebx = ®s[1], *ecx = ®s[2], *edx = ®s[3]; (void)edx; - enum cpu_feature features = 0; + features = 0; cpuid(regs, 0); const int max_id = *eax; cpuid(regs, 1); @@ -124,7 +153,7 @@ static } } } - g_cpu_features = features; + ATOMIC_STORE(g_cpu_features, features); return features; #else /* How to detect NEON? */ diff --git a/src/third_party/blake3/blake3_impl.h b/src/third_party/blake3/blake3_impl.h index 3ba9ceb04..beab5cf53 100644 --- a/src/third_party/blake3/blake3_impl.h +++ b/src/third_party/blake3/blake3_impl.h @@ -51,7 +51,11 @@ enum blake3_flags { #if !defined(BLAKE3_USE_NEON) // If BLAKE3_USE_NEON not manually set, autodetect based on AArch64ness #if defined(IS_AARCH64) - #define BLAKE3_USE_NEON 1 + #if defined(__ARM_BIG_ENDIAN) + #define BLAKE3_USE_NEON 0 + #else + #define BLAKE3_USE_NEON 1 + #endif #else #define BLAKE3_USE_NEON 0 #endif -- 2.47.2