OSDN Git Service

b791adc978ca8703f138088e994545fe006b055d
[mutilities/MUtilities.git] / src / CPUFeatures_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 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.
14 //
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
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 //Win32 API
23 #define WIN32_LEAN_AND_MEAN 1
24 #include <Windows.h>
25
26 //ASM
27 #include <intrin.h>
28
29 //MUtils
30 #include <MUtils/CPUFeatures.h>
31 #include <MUtils/OSSupport.h>
32 #include "Utils_Win32.h"
33
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)
37
38 MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(void)
39 {
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);
43
44         cpu_info_t  features;
45         SYSTEM_INFO systemInfo;
46         uint32_t    cpuInfo[4];
47
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));
52
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);
62
63         //Detect the CPU model and feature flags
64         if(max_basic_cap >= 1)
65         {
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);
75
76                 //Check for AVX
77                 if ((cpuInfo[2] & 0x18000000) == 0x18000000)
78                 {
79                         if((_xgetbv(0) & 0x6ui64) == 0x6ui64) /*AVX requires OS support!*/
80                         {
81                                 features.features |= FLAG_AVX;
82                                 features.features |= CHECK_FLAG(cpuInfo[2], 0x00001000, FLAG_FMA3);
83                         }
84                 }
85
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);
90         }
91
92         //Detect extended feature flags
93         if (max_basic_cap >= 7)
94         {
95                 MY_CPUID(&cpuInfo[0], 7);
96                 if (features.features & FLAG_AVX)
97                 {
98                         features.features |= CHECK_FLAG(cpuInfo[1], 0x00000020, FLAG_AVX2);
99                 }
100         }
101
102         //Read the CPU "brand" string
103         if (max_basic_cap > 0)
104         {
105                 MY_CPUID(&cpuInfo[0], 0x80000000);
106                 const uint32_t max_extended_cap = qBound(0x80000000, cpuInfo[0], 0x80000004);
107                 if (max_extended_cap >= 0x80000001)
108                 {
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)
112                         {
113                                 MY_CPUID(&cpuInfo[0], i);
114                                 memcpy(&features.brand[(i - 0x80000002) * sizeof(cpuInfo)], &cpuInfo[0], sizeof(cpuInfo));
115                         }
116                         features.brand[sizeof(features.brand) - 1] = '\0';
117                 }
118         }
119
120         //Detect 64-Bit processors
121 #if (!(defined(_M_X64) || defined(_M_IA64)))
122         const IsWow64ProcessFun isWow64ProcessPtr = MUtils::Win32Utils::resolve<IsWow64ProcessFun>(QLatin1String("kernel32"), QLatin1String("IsWow64Process"));
123         if(isWow64ProcessPtr)
124         {
125                 BOOL x64flag = FALSE;
126                 if(isWow64ProcessPtr(GetCurrentProcess(), &x64flag))
127                 {
128                         if (x64flag)
129                         {
130                                 features.x64 = true;
131                                 features.features |= FLAGS_X64; /*x86_64 implies SSE2*/
132                         }
133                 }
134         }
135 #else
136         features.x64 = true;
137         features.features |= FLAGS_X64;
138 #endif
139
140         //Make sure that (at least) the MMX flag has been set!
141         if (!(features.features & FLAG_MMX))
142         {
143                 qWarning("Warning: CPU does not seem to support MMX. Take care!\n");
144                 features.features = 0;
145         }
146
147         //Count the number of available(!) CPU cores
148         DWORD_PTR procAffinity, sysAffinity;
149         if(GetProcessAffinityMask(GetCurrentProcess(), &procAffinity, &sysAffinity))
150         {
151                 for(DWORD_PTR mask = 1; mask; mask <<= 1)
152                 {
153                         features.count += ((sysAffinity & mask) ? (1) : (0));
154                 }
155         }
156         if(features.count < 1)
157         {
158                 GetNativeSystemInfo(&systemInfo);
159                 features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
160         }
161
162         //Apply manual CPU overwrites
163         bool userFlag = false;
164         if (args.contains(QLatin1String("cpu-no-simd")))   { userFlag = true; features.features = 0U; }
165         if (args.contains(QLatin1String("cpu-no-vendor"))) { userFlag = true; features.vendor   = 0U; }
166         if (args.contains(QLatin1String("cpu-no-x64")))    { userFlag = true; features.x64      = 0U; }
167         if(userFlag)
168         {
169                 qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
170         }
171
172         return features;
173 }