More paranoid AVX512 detection

This commit is contained in:
Frank Denis 2019-01-02 17:33:57 +01:00
parent f0e9c368ea
commit 6fd78e4a39

View File

@ -39,8 +39,11 @@ static CPUFeatures _cpu_features;
#define CPUID_EDX_SSE2 0x04000000 #define CPUID_EDX_SSE2 0x04000000
#define XCR0_SSE 0x00000002 #define XCR0_SSE 0x00000002
#define XCR0_AVX 0x00000004 #define XCR0_AVX 0x00000004
#define XCR0_OPMASK 0x00000020
#define XCR0_ZMM_HI256 0x00000040
#define XCR0_HI16_ZMM 0x00000080
static int static int
_sodium_runtime_arm_cpu_features(CPUFeatures * const cpu_features) _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 cpu_info[4];
unsigned int id; unsigned int id;
uint32_t xcr0 = 0U;
_cpuid(cpu_info, 0x0); _cpuid(cpu_info, 0x0);
if ((id = cpu_info[0]) == 0U) { if ((id = cpu_info[0]) == 0U) {
@ -145,10 +149,12 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features)
#endif #endif
cpu_features->has_avx = 0; cpu_features->has_avx = 0;
(void) xcr0;
#ifdef HAVE_AVXINTRIN_H #ifdef HAVE_AVXINTRIN_H
if ((cpu_info[2] & (CPUID_ECX_AVX | CPUID_ECX_XSAVE | CPUID_ECX_OSXSAVE)) == if ((cpu_info[2] & (CPUID_ECX_AVX | CPUID_ECX_XSAVE | CPUID_ECX_OSXSAVE)) ==
(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) || \ # if defined(HAVE__XGETBV) || \
(defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) && _MSC_FULL_VER >= 160040219) (defined(_MSC_VER) && defined(_XCR_XFEATURE_ENABLED_MASK) && _MSC_FULL_VER >= 160040219)
xcr0 = (uint32_t) _xgetbv(0); xcr0 = (uint32_t) _xgetbv(0);
@ -197,7 +203,11 @@ _sodium_runtime_intel_cpu_features(CPUFeatures * const cpu_features)
unsigned int cpu_info7[4]; unsigned int cpu_info7[4];
_cpuid(cpu_info7, 0x00000007); _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 #endif