Skip to content

Commit

Permalink
Common: Fix AVX check on mingw/lower targets.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Feb 14, 2021
1 parent 622b078 commit 7aa4b53
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions Common/CPUDetect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include <sys/sysctl.h>
#endif

#include <cstdint>
#include <memory.h>
#include <set>

Expand All @@ -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 <Windows.h>
#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
Expand All @@ -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 <emmintrin.h>

#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)
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 7aa4b53

Please sign in to comment.