From a4a3f44806ffecd2655354345111ea042656848c Mon Sep 17 00:00:00 2001 From: Gres Date: Thu, 7 Apr 2022 22:56:22 +0300 Subject: [PATCH] Fixed avx support detection --- src/settings.cpp | 47 +++++++++++++++++------------------------------ 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/src/settings.cpp b/src/settings.cpp index fcd5bc2..8f8d833 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -7,6 +7,8 @@ #include #include +#include + namespace { const QString iniFileName() @@ -133,47 +135,32 @@ void cleanupOutdated(QSettings& settings) #ifdef _MSC_VER #include -#endif -#ifdef __GNUC__ -void __cpuid(int* cpuinfo, int info) +void cpuid(int leaf, int subleaf, std::array& cpuinfo) { - __asm__ __volatile__( - "xchg %%ebx, %%edi;" - "cpuid;" - "xchg %%ebx, %%edi;" - : "=a"(cpuinfo[0]), "=D"(cpuinfo[1]), "=c"(cpuinfo[2]), "=d"(cpuinfo[3]) - : "0"(info)); + __cpuidex(reinterpret_cast(cpuinfo.data()), leaf, subleaf); } - -unsigned long long _xgetbv(unsigned int index) +#else +#include +void cpuid(int leaf, int subleaf, std::array& cpuinfo) { - unsigned int eax, edx; - __asm__ __volatile__("xgetbv;" : "=a"(eax), "=d"(edx) : "c"(index)); - return ((unsigned long long)edx << 32) | eax; + __get_cpuid_count(leaf, subleaf, &cpuinfo[0], &cpuinfo[1], &cpuinfo[2], + &cpuinfo[3]); } #endif bool checkOptimizedTesseractSupport() { - bool sse4_1Supportted = false; - bool sse4_2Supportted = false; - bool avxSupportted = false; + std::array cpuinfo{0}; - int cpuinfo[4]; - __cpuid(cpuinfo, 1); + cpuid(1, 0, cpuinfo); + const bool sse4_1 = cpuinfo[2] & (1 << 19); + const bool sse4_2 = cpuinfo[2] & (1 << 20); + const bool avx = cpuinfo[2] & (1 << 28); - sse4_1Supportted = cpuinfo[2] & (1 << 19) || false; - sse4_2Supportted = cpuinfo[2] & (1 << 20) || false; + cpuid(7, 0, cpuinfo); + const bool avx2 = cpuinfo[1] & (1 << 5); - avxSupportted = cpuinfo[2] & (1 << 28) || false; - bool osxsaveSupported = cpuinfo[2] & (1 << 27) || false; - if (osxsaveSupported && avxSupportted) { - // _XCR_XFEATURE_ENABLED_MASK = 0 - unsigned long long xcrFeatureMask = _xgetbv(0); - avxSupportted = (xcrFeatureMask & 0x6) == 0x6; - } - - return sse4_1Supportted && sse4_2Supportted && avxSupportted; + return sse4_1 && sse4_2 && avx && avx2; } } // namespace