From bcf10cb89948bdeca533d23e58fa514f3ecb5b17 Mon Sep 17 00:00:00 2001 From: FunkyFr3sh Date: Thu, 22 Sep 2022 19:28:35 +0200 Subject: [PATCH] tweak avx supported check --- src/utils.c | 47 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/src/utils.c b/src/utils.c index e789703..b85bdd6 100644 --- a/src/utils.c +++ b/src/utils.c @@ -13,18 +13,51 @@ BOOL util_is_avx_supported() { - unsigned int xcr0 = 0; + const DWORD XMM_STATE_BIT = 1 << 1; + const DWORD YMM_STATE_BIT = 1 << 2; + const DWORD OS_AVX_BITS = XMM_STATE_BIT | YMM_STATE_BIT; -#if defined(_MSC_VER) - xcr0 = (unsigned int)_xgetbv(_XCR_XFEATURE_ENABLED_MASK); -#else - __asm__("xgetbv" : "=a" (xcr0) : "c" (0) : "%edx"); + const DWORD AVX_BIT = 1 << 28; + const DWORD OSXSAVE_BIT = 1 << 27; + const DWORD XSAVE_BIT = 1 << 26; + const DWORD CPU_AVX_BITS = AVX_BIT | OSXSAVE_BIT | XSAVE_BIT; + + BOOL result = FALSE; + +#ifdef _MSC_VER + __try + { #endif int info[4] = { 0 }; - __cpuid(info, 1); + __cpuid(info, 0); - return (info[2] & (1 << 27)) && (info[2] & (1 << 28)) && (xcr0 & 6); + if (info[0] >= 1) + { + __cpuid(info, 1); + + if ((info[2] & CPU_AVX_BITS) == CPU_AVX_BITS) + { + unsigned int xcr0 = 0; + +#if defined(_MSC_VER) + xcr0 = (unsigned int)_xgetbv(_XCR_XFEATURE_ENABLED_MASK); +#else + __asm__("xgetbv" : "=a" (xcr0) : "c" (0) : "%edx"); +#endif + + result = (xcr0 & OS_AVX_BITS) == OS_AVX_BITS; + } + } + +#ifdef _MSC_VER + } + __except (EXCEPTION_EXECUTE_HANDLER) + { + } +#endif + + return result; } void util_limit_game_ticks()