diff --git a/src/libsodium/sodium/runtime.c b/src/libsodium/sodium/runtime.c index f5c805cf..a5a89bca 100644 --- a/src/libsodium/sodium/runtime.c +++ b/src/libsodium/sodium/runtime.c @@ -39,8 +39,11 @@ static CPUFeatures _cpu_features; #define CPUID_EDX_SSE2 0x04000000 -#define XCR0_SSE 0x00000002 -#define XCR0_AVX 0x00000004 +#define XCR0_SSE 0x00000002 +#define XCR0_AVX 0x00000004 +#define XCR0_OPMASK 0x00000020 +#define XCR0_ZMM_HI256 0x00000040 +#define XCR0_HI16_ZMM 0x00000080 static int _sodium_runtime_arm_cpu_features(CPUFeatures * const cpu_features) @@ -114,6 +117,7 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) { unsigned int cpu_info[4]; unsigned int id; + uint32_t xcr0 = 0U; _cpuid(cpu_info, 0x0); if ((id = cpu_info[0]) == 0U) { @@ -145,10 +149,12 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) #endif cpu_features->has_avx = 0; + + (void) xcr0; #ifdef HAVE_AVXINTRIN_H if ((cpu_info[2] & (CPUID_ECX_AVX | CPUID_ECX_XSAVE | CPUID_ECX_OSXSAVE)) == (CPUID_ECX_AVX | CPUID_ECX_XSAVE | CPUID_ECX_OSXSAVE)) { - uint32_t xcr0 = 0U; + xcr0 = 0U; # if defined(HAVE__XGETBV) || \ (defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) && _MSC_FULL_VER >= 160040219) xcr0 = (uint32_t) _xgetbv(0); @@ -197,7 +203,11 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features) unsigned int cpu_info7[4]; _cpuid(cpu_info7, 0x00000007); - cpu_features->has_avx512f = ((cpu_info7[1] & CPUID_EBX_AVX512F) != 0x0); + if ((cpu_info7[1] & CPUID_EBX_AVX512F) == CPUID_EBX_AVX512F && + (xcr0 & (XCR0_OPMASK | XCR0_ZMM_HI256 | XCR0_HI16_ZMM)) + == (XCR0_OPMASK | XCR0_ZMM_HI256 | XCR0_HI16_ZMM)) { + cpu_features->has_avx512f = 1; + } } #endif