From 7aa4b53422e9519e7b3c8fbc570cf1e669affbdd Mon Sep 17 00:00:00 2001 From: "Unknown W. Brackets" Date: Sun, 14 Feb 2021 09:38:23 -0800 Subject: [PATCH] Common: Fix AVX check on mingw/lower targets. --- Common/CPUDetect.cpp | 35 +++++++++++++++++++++-------------- 1 file changed, 21 insertions(+), 14 deletions(-) diff --git a/Common/CPUDetect.cpp b/Common/CPUDetect.cpp index 70c640eee7..04780f5f7f 100644 --- a/Common/CPUDetect.cpp +++ b/Common/CPUDetect.cpp @@ -26,6 +26,7 @@ #include #endif +#include #include #include @@ -34,10 +35,9 @@ #include "Common/File/FileUtil.h" #include "Common/StringUtils.h" -#if defined(_WIN32) && !defined(__MINGW32__) +#if defined(_WIN32) +#include "Common/CommonWindows.h" -#define WIN32_LEAN_AND_MEAN -#include #define _interlockedbittestandset workaround_ms_header_bug_platform_sdk6_set #define _interlockedbittestandreset workaround_ms_header_bug_platform_sdk6_reset #define _interlockedbittestandset64 workaround_ms_header_bug_platform_sdk6_set64 @@ -55,21 +55,27 @@ void do_cpuid(u32 regs[4], u32 cpuid_leaf) { __cpuid((int *)regs, cpuid_leaf); } +#ifdef __MINGW32__ +static uint64_t do_xgetbv(unsigned int index) { + unsigned int eax, edx; + // This is xgetbv directly, so we can avoid compilers warning we need runtime checks. + asm(".byte 0x0f, 0x01, 0xd0" : "=a"(eax), "=d"(edx) : "c"(index)); + return ((uint64_t)edx << 32) | eax; +} +#else +#define do_xgetbv _xgetbv +#endif + #else // _WIN32 #ifdef _M_SSE #include -#define _XCR_XFEATURE_ENABLED_MASK 0 -static unsigned long long _xgetbv(unsigned int index) -{ +static uint64_t do_xgetbv(unsigned int index) { unsigned int eax, edx; __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index)); - return ((unsigned long long)edx << 32) | eax; + return ((uint64_t)edx << 32) | eax; } - -#else -#define _XCR_XFEATURE_ENABLED_MASK 0 #endif // _M_SSE #if !defined(MIPS) @@ -98,6 +104,9 @@ void do_cpuid(u32 regs[4], u32 cpuid_leaf) #endif // !win32 +#ifndef _XCR_XFEATURE_ENABLED_MASK +#define _XCR_XFEATURE_ENABLED_MASK 0 +#endif CPUInfo cpu_info; @@ -215,10 +224,8 @@ void CPUInfo::Detect() { // - Is the XSAVE bit set in CPUID? ( >>26) // - Is the OSXSAVE bit set in CPUID? ( >>27) // - XGETBV result has the XCR bit set. - if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1) && ((cpu_id[2] >> 26) & 1)) - { - if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) - { + if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1) && ((cpu_id[2] >> 26) & 1)) { + if ((do_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) { bAVX = true; if ((cpu_id[2] >> 12) & 1) bFMA3 = true;