summaryrefslogtreecommitdiff
path: root/src/common/x64/cpu_detect.cpp
diff options
context:
space:
mode:
authorGravatar bunnei2015-08-12 00:19:20 -0400
committerGravatar bunnei2015-08-15 18:03:26 -0400
commita1942238f5046811833e9636c64f991905960732 (patch)
tree47de97e98aa464cee8e9297f419ac99c289a2293 /src/common/x64/cpu_detect.cpp
parentx64: Refactor to remove fake interfaces and general cleanups. (diff)
downloadyuzu-a1942238f5046811833e9636c64f991905960732.tar.gz
yuzu-a1942238f5046811833e9636c64f991905960732.tar.xz
yuzu-a1942238f5046811833e9636c64f991905960732.zip
Common: Move cpu_detect to x64 directory.
Diffstat (limited to 'src/common/x64/cpu_detect.cpp')
-rw-r--r--src/common/x64/cpu_detect.cpp229
1 files changed, 229 insertions, 0 deletions
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp
new file mode 100644
index 000000000..72c8297a3
--- /dev/null
+++ b/src/common/x64/cpu_detect.cpp
@@ -0,0 +1,229 @@
1// Copyright 2008 Dolphin Emulator Project
2// Licensed under GPLv2+
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <string>
7
8#include "common/common_types.h"
9
10#include "cpu_detect.h"
11
12#ifndef _WIN32
13
14#ifdef __FreeBSD__
15#include <sys/types.h>
16#include <machine/cpufunc.h>
17#endif
18
19static inline void __cpuidex(int info[4], int function_id, int subfunction_id)
20{
21#ifdef __FreeBSD__
22 // Despite the name, this is just do_cpuid() with ECX as second input.
23 cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
24#else
25 info[0] = function_id; // eax
26 info[2] = subfunction_id; // ecx
27 __asm__(
28 "cpuid"
29 : "=a" (info[0]),
30 "=b" (info[1]),
31 "=c" (info[2]),
32 "=d" (info[3])
33 : "a" (function_id),
34 "c" (subfunction_id)
35 );
36#endif
37}
38
39static inline void __cpuid(int info[4], int function_id)
40{
41 return __cpuidex(info, function_id, 0);
42}
43
44#define _XCR_XFEATURE_ENABLED_MASK 0
45static u64 _xgetbv(u32 index)
46{
47 u32 eax, edx;
48 __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
49 return ((u64)edx << 32) | eax;
50}
51
52#endif // ifndef _WIN32
53
54namespace Common {
55
56CPUInfo cpu_info;
57
58CPUInfo::CPUInfo() {
59 Detect();
60}
61
62// Detects the various CPU features
63void CPUInfo::Detect() {
64 memset(this, 0, sizeof(*this));
65#ifdef ARCHITECTURE_X64
66 Mode64bit = true;
67 OS64bit = true;
68#endif
69 num_cores = 1;
70
71 // Set obvious defaults, for extra safety
72 if (Mode64bit) {
73 bSSE = true;
74 bSSE2 = true;
75 bLongMode = true;
76 }
77
78 // Assume CPU supports the CPUID instruction. Those that don't can barely
79 // boot modern OS:es anyway.
80 int cpu_id[4];
81 memset(brand_string, 0, sizeof(brand_string));
82
83 // Detect CPU's CPUID capabilities, and grab CPU string
84 __cpuid(cpu_id, 0x00000000);
85 u32 max_std_fn = cpu_id[0]; // EAX
86 *((int *)brand_string) = cpu_id[1];
87 *((int *)(brand_string + 4)) = cpu_id[3];
88 *((int *)(brand_string + 8)) = cpu_id[2];
89 __cpuid(cpu_id, 0x80000000);
90 u32 max_ex_fn = cpu_id[0];
91 if (!strcmp(brand_string, "GenuineIntel"))
92 vendor = VENDOR_INTEL;
93 else if (!strcmp(brand_string, "AuthenticAMD"))
94 vendor = VENDOR_AMD;
95 else
96 vendor = VENDOR_OTHER;
97
98 // Set reasonable default brand string even if brand string not available.
99 strcpy(cpu_string, brand_string);
100
101 // Detect family and other misc stuff.
102 bool ht = false;
103 HTT = ht;
104 logical_cpu_count = 1;
105 if (max_std_fn >= 1) {
106 __cpuid(cpu_id, 0x00000001);
107 int family = ((cpu_id[0] >> 8) & 0xf) + ((cpu_id[0] >> 20) & 0xff);
108 int model = ((cpu_id[0] >> 4) & 0xf) + ((cpu_id[0] >> 12) & 0xf0);
109 // Detect people unfortunate enough to be running Dolphin on an Atom
110 if (family == 6 && (model == 0x1C || model == 0x26 || model == 0x27 || model == 0x35 || model == 0x36 ||
111 model == 0x37 || model == 0x4A || model == 0x4D || model == 0x5A || model == 0x5D))
112 bAtom = true;
113 logical_cpu_count = (cpu_id[1] >> 16) & 0xFF;
114 ht = (cpu_id[3] >> 28) & 1;
115
116 if ((cpu_id[3] >> 25) & 1) bSSE = true;
117 if ((cpu_id[3] >> 26) & 1) bSSE2 = true;
118 if ((cpu_id[2]) & 1) bSSE3 = true;
119 if ((cpu_id[2] >> 9) & 1) bSSSE3 = true;
120 if ((cpu_id[2] >> 19) & 1) bSSE4_1 = true;
121 if ((cpu_id[2] >> 20) & 1) bSSE4_2 = true;
122 if ((cpu_id[2] >> 22) & 1) bMOVBE = true;
123 if ((cpu_id[2] >> 25) & 1) bAES = true;
124
125 if ((cpu_id[3] >> 24) & 1)
126 {
127 // We can use FXSAVE.
128 bFXSR = true;
129 }
130
131 // AVX support requires 3 separate checks:
132 // - Is the AVX bit set in CPUID?
133 // - Is the XSAVE bit set in CPUID?
134 // - XGETBV result has the XCR bit set.
135 if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) {
136 if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) {
137 bAVX = true;
138 if ((cpu_id[2] >> 12) & 1)
139 bFMA = true;
140 }
141 }
142
143 if (max_std_fn >= 7) {
144 __cpuidex(cpu_id, 0x00000007, 0x00000000);
145 // careful; we can't enable AVX2 unless the XSAVE/XGETBV checks above passed
146 if ((cpu_id[1] >> 5) & 1)
147 bAVX2 = bAVX;
148 if ((cpu_id[1] >> 3) & 1)
149 bBMI1 = true;
150 if ((cpu_id[1] >> 8) & 1)
151 bBMI2 = true;
152 }
153 }
154
155 bFlushToZero = bSSE;
156
157 if (max_ex_fn >= 0x80000004) {
158 // Extract CPU model string
159 __cpuid(cpu_id, 0x80000002);
160 memcpy(cpu_string, cpu_id, sizeof(cpu_id));
161 __cpuid(cpu_id, 0x80000003);
162 memcpy(cpu_string + 16, cpu_id, sizeof(cpu_id));
163 __cpuid(cpu_id, 0x80000004);
164 memcpy(cpu_string + 32, cpu_id, sizeof(cpu_id));
165 }
166 if (max_ex_fn >= 0x80000001) {
167 // Check for more features.
168 __cpuid(cpu_id, 0x80000001);
169 if (cpu_id[2] & 1) bLAHFSAHF64 = true;
170 if ((cpu_id[2] >> 5) & 1) bLZCNT = true;
171 if ((cpu_id[2] >> 16) & 1) bFMA4 = true;
172 if ((cpu_id[3] >> 29) & 1) bLongMode = true;
173 }
174
175 num_cores = (logical_cpu_count == 0) ? 1 : logical_cpu_count;
176
177 if (max_ex_fn >= 0x80000008) {
178 // Get number of cores. This is a bit complicated. Following AMD manual here.
179 __cpuid(cpu_id, 0x80000008);
180 int apic_id_core_id_size = (cpu_id[2] >> 12) & 0xF;
181 if (apic_id_core_id_size == 0) {
182 if (ht) {
183 // New mechanism for modern Intel CPUs.
184 if (vendor == VENDOR_INTEL) {
185 __cpuidex(cpu_id, 0x00000004, 0x00000000);
186 int cores_x_package = ((cpu_id[0] >> 26) & 0x3F) + 1;
187 HTT = (cores_x_package < logical_cpu_count);
188 cores_x_package = ((logical_cpu_count % cores_x_package) == 0) ? cores_x_package : 1;
189 num_cores = (cores_x_package > 1) ? cores_x_package : num_cores;
190 logical_cpu_count /= cores_x_package;
191 }
192 }
193 } else {
194 // Use AMD's new method.
195 num_cores = (cpu_id[2] & 0xFF) + 1;
196 }
197 }
198}
199
200// Turn the CPU info into a string we can show
201std::string CPUInfo::Summarize() {
202 std::string sum(cpu_string);
203 sum += " (";
204 sum += brand_string;
205 sum += ")";
206
207 if (bSSE) sum += ", SSE";
208 if (bSSE2) {
209 sum += ", SSE2";
210 if (!bFlushToZero)
211 sum += " (but not DAZ!)";
212 }
213 if (bSSE3) sum += ", SSE3";
214 if (bSSSE3) sum += ", SSSE3";
215 if (bSSE4_1) sum += ", SSE4.1";
216 if (bSSE4_2) sum += ", SSE4.2";
217 if (HTT) sum += ", HTT";
218 if (bAVX) sum += ", AVX";
219 if (bAVX2) sum += ", AVX2";
220 if (bBMI1) sum += ", BMI1";
221 if (bBMI2) sum += ", BMI2";
222 if (bFMA) sum += ", FMA";
223 if (bAES) sum += ", AES";
224 if (bMOVBE) sum += ", MOVBE";
225 if (bLongMode) sum += ", 64-bit support";
226 return sum;
227}
228
229} // namespace Common