Bump version.
[MUtilities.git] / src / CPUFeatures_Win32.cpp
blobc3ef8da1eb2067c66722f97fd9f5ff81dcd4feb2
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2023 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
22 //Win32 API
23 #define WIN32_LEAN_AND_MEAN 1
24 #include <Windows.h>
26 //ASM
27 #include <intrin.h>
29 //MUtils
30 #include <MUtils/CPUFeatures.h>
31 #include <MUtils/OSSupport.h>
32 #include "Utils_Win32.h"
34 #define MY_CPUID(X,Y) __cpuid(((int*)(X)), ((int)(Y)))
35 #define CHECK_VENDOR(X,Y,Z) (_stricmp((X), (Y)) ? 0U : (Z));
36 #define CHECK_FLAG(X,Y,Z) (((X) & (Y)) ? (Z) : 0U)
38 MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(void)
40 const OS::ArgumentMap &args = OS::arguments();
41 typedef BOOL (WINAPI *IsWow64ProcessFun)(__in HANDLE hProcess, __out PBOOL Wow64Process);
42 static const quint32 FLAGS_X64 = (FLAG_MMX | FLAG_SSE | FLAG_SSE2);
44 cpu_info_t features;
45 SYSTEM_INFO systemInfo;
46 uint32_t cpuInfo[4];
48 //Initialize variables to zero
49 memset(&features, 0, sizeof(cpu_info_t));
50 memset(&systemInfo, 0, sizeof(SYSTEM_INFO));
51 memset(&cpuInfo[0], 0, sizeof(cpuInfo));
53 //Detect the CPU identifier string
54 MY_CPUID(&cpuInfo[0], 0);
55 const uint32_t max_basic_cap = cpuInfo[0];
56 memcpy(&features.idstr[0U * sizeof(uint32_t)], &cpuInfo[1], sizeof(uint32_t));
57 memcpy(&features.idstr[1U * sizeof(uint32_t)], &cpuInfo[3], sizeof(uint32_t));
58 memcpy(&features.idstr[2U * sizeof(uint32_t)], &cpuInfo[2], sizeof(uint32_t));
59 features.idstr[3U * sizeof(uint32_t)] = '\0';
60 features.vendor |= CHECK_VENDOR(features.idstr, "GenuineIntel", VENDOR_INTEL);
61 features.vendor |= CHECK_VENDOR(features.idstr, "AuthenticAMD", VENDOR_AMD);
63 //Detect the CPU model and feature flags
64 if(max_basic_cap >= 1)
66 MY_CPUID(&cpuInfo[0], 1);
67 features.features |= CHECK_FLAG(cpuInfo[3], 0x00008000, FLAG_CMOV);
68 features.features |= CHECK_FLAG(cpuInfo[3], 0x00800000, FLAG_MMX);
69 features.features |= CHECK_FLAG(cpuInfo[3], 0x02000000, FLAG_SSE);
70 features.features |= CHECK_FLAG(cpuInfo[3], 0x04000000, FLAG_SSE2);
71 features.features |= CHECK_FLAG(cpuInfo[2], 0x00000001, FLAG_SSE3);
72 features.features |= CHECK_FLAG(cpuInfo[2], 0x00000200, FLAG_SSSE3);
73 features.features |= CHECK_FLAG(cpuInfo[2], 0x00080000, FLAG_SSE41);
74 features.features |= CHECK_FLAG(cpuInfo[2], 0x00100000, FLAG_SSE42);
76 //Check for AVX
77 if ((cpuInfo[2] & 0x18000000) == 0x18000000)
79 if((_xgetbv(0) & 0x6ui64) == 0x6ui64) /*AVX requires OS support!*/
81 features.features |= FLAG_AVX;
82 features.features |= CHECK_FLAG(cpuInfo[2], 0x00001000, FLAG_FMA3);
86 //Compute the CPU stepping, model and family
87 features.stepping = cpuInfo[0] & 0xf;
88 features.model = ((cpuInfo[0] >> 4) & 0xf) + (((cpuInfo[0] >> 16) & 0xf) << 4);
89 features.family = ((cpuInfo[0] >> 8) & 0xf) + ((cpuInfo[0] >> 20) & 0xff);
92 //Detect extended feature flags
93 if (max_basic_cap >= 7)
95 MY_CPUID(&cpuInfo[0], 7);
96 if (features.features & FLAG_AVX)
98 features.features |= CHECK_FLAG(cpuInfo[1], 0x00000020, FLAG_AVX2);
102 //Read the CPU "brand" string
103 if (max_basic_cap > 0)
105 MY_CPUID(&cpuInfo[0], 0x80000000);
106 const uint32_t max_extended_cap = qBound(0x80000000, cpuInfo[0], 0x80000004);
107 if (max_extended_cap >= 0x80000001)
109 MY_CPUID(&cpuInfo[0], 0x80000001);
110 features.features |= CHECK_FLAG(cpuInfo[2], 0x00000020, FLAG_LZCNT);
111 for (uint32_t i = 0x80000002; i <= max_extended_cap; ++i)
113 MY_CPUID(&cpuInfo[0], i);
114 memcpy(&features.brand[(i - 0x80000002) * sizeof(cpuInfo)], &cpuInfo[0], sizeof(cpuInfo));
116 features.brand[sizeof(features.brand) - 1] = '\0';
120 //Detect 64-Bit processors
121 if (OS::os_architecture() == OS::ARCH_X64)
123 features.x64 = true;
124 features.features |= FLAGS_X64; /*x86_64 implies SSE2*/
127 //Make sure that (at least) the MMX flag has been set!
128 if (!(features.features & FLAG_MMX))
130 qWarning("Warning: CPU does not seem to support MMX. Take care!\n");
131 features.features = 0;
134 //Count the number of available(!) CPU cores
135 DWORD_PTR procAffinity, sysAffinity;
136 if(GetProcessAffinityMask(GetCurrentProcess(), &procAffinity, &sysAffinity))
138 for(DWORD_PTR mask = 1; mask; mask <<= 1)
140 features.count += ((sysAffinity & mask) ? (1) : (0));
143 if(features.count < 1)
145 GetNativeSystemInfo(&systemInfo);
146 features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
149 //Apply manual CPU overwrites
150 bool userFlag = false;
151 if (args.contains(QLatin1String("cpu-no-simd"))) { userFlag = true; features.features = 0U; }
152 if (args.contains(QLatin1String("cpu-no-vendor"))) { userFlag = true; features.vendor = 0U; }
153 if (args.contains(QLatin1String("cpu-no-x64"))) { userFlag = true; features.x64 = 0U; }
154 if(userFlag)
156 qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
159 return features;