OSDN Git Service

Code refactoring.
[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 (OS::os_architecture() == OS::ARCH_X64)
122         {
123                 features.x64 = true;
124                 features.features |= FLAGS_X64; /*x86_64 implies SSE2*/
125         }
126
127         //Make sure that (at least) the MMX flag has been set!
128         if (!(features.features & FLAG_MMX))
129         {
130                 qWarning("Warning: CPU does not seem to support MMX. Take care!\n");
131                 features.features = 0;
132         }
133
134         //Count the number of available(!) CPU cores
135         DWORD_PTR procAffinity, sysAffinity;
136         if(GetProcessAffinityMask(GetCurrentProcess(), &procAffinity, &sysAffinity))
137         {
138                 for(DWORD_PTR mask = 1; mask; mask <<= 1)
139                 {
140                         features.count += ((sysAffinity & mask) ? (1) : (0));
141                 }
142         }
143         if(features.count < 1)
144         {
145                 GetNativeSystemInfo(&systemInfo);
146                 features.count = qBound(1UL, systemInfo.dwNumberOfProcessors, 64UL);
147         }
148
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)
155         {
156                 qWarning("CPU flags overwritten by user-defined parameters. Take care!\n");
157         }
158
159         return features;
160 }