summaryrefslogtreecommitdiff
path: root/src/common/x64/cpu_detect.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/x64/cpu_detect.cpp')
-rw-r--r--src/common/x64/cpu_detect.cpp187
1 files changed, 187 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..d9c430c67
--- /dev/null
+++ b/src/common/x64/cpu_detect.cpp
@@ -0,0 +1,187 @@
1// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project
2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included.
4
5#include <cstring>
6#include <string>
7#include <thread>
8
9#include "common/common_types.h"
10
11#include "cpu_detect.h"
12
13namespace Common {
14
15#ifndef _MSC_VER
16
17#ifdef __FreeBSD__
18#include <sys/types.h>
19#include <machine/cpufunc.h>
20#endif
21
22static inline void __cpuidex(int info[4], int function_id, int subfunction_id) {
23#ifdef __FreeBSD__
24 // Despite the name, this is just do_cpuid() with ECX as second input.
25 cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
26#else
27 info[0] = function_id; // eax
28 info[2] = subfunction_id; // ecx
29 __asm__(
30 "cpuid"
31 : "=a" (info[0]),
32 "=b" (info[1]),
33 "=c" (info[2]),
34 "=d" (info[3])
35 : "a" (function_id),
36 "c" (subfunction_id)
37 );
38#endif
39}
40
41static inline void __cpuid(int info[4], int function_id) {
42 return __cpuidex(info, function_id, 0);
43}
44
45#define _XCR_XFEATURE_ENABLED_MASK 0
46static u64 _xgetbv(u32 index) {
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 _MSC_VER
53
54// Detects the various CPU features
55static CPUCaps Detect() {
56 CPUCaps caps = {};
57
58 caps.num_cores = std::thread::hardware_concurrency();
59
60 // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support
61 // Citra at all anyway
62
63 int cpu_id[4];
64 memset(caps.brand_string, 0, sizeof(caps.brand_string));
65
66 // Detect CPU's CPUID capabilities and grab CPU string
67 __cpuid(cpu_id, 0x00000000);
68 u32 max_std_fn = cpu_id[0]; // EAX
69
70 std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(int));
71 std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(int));
72 std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(int));
73
74 __cpuid(cpu_id, 0x80000000);
75
76 u32 max_ex_fn = cpu_id[0];
77 if (!strcmp(caps.brand_string, "GenuineIntel"))
78 caps.vendor = CPUVendor::INTEL;
79 else if (!strcmp(caps.brand_string, "AuthenticAMD"))
80 caps.vendor = CPUVendor::AMD;
81 else
82 caps.vendor = CPUVendor::OTHER;
83
84 // Set reasonable default brand string even if brand string not available
85 strcpy(caps.cpu_string, caps.brand_string);
86
87 // Detect family and other miscellaneous features
88 if (max_std_fn >= 1) {
89 __cpuid(cpu_id, 0x00000001);
90
91 if ((cpu_id[3] >> 25) & 1) caps.sse = true;
92 if ((cpu_id[3] >> 26) & 1) caps.sse2 = true;
93 if ((cpu_id[2]) & 1) caps.sse3 = true;
94 if ((cpu_id[2] >> 9) & 1) caps.ssse3 = true;
95 if ((cpu_id[2] >> 19) & 1) caps.sse4_1 = true;
96 if ((cpu_id[2] >> 20) & 1) caps.sse4_2 = true;
97 if ((cpu_id[2] >> 22) & 1) caps.movbe = true;
98 if ((cpu_id[2] >> 25) & 1) caps.aes = true;
99
100 if ((cpu_id[3] >> 24) & 1) {
101 caps.fxsave_fxrstor = true;
102 }
103
104 // AVX support requires 3 separate checks:
105 // - Is the AVX bit set in CPUID?
106 // - Is the XSAVE bit set in CPUID?
107 // - XGETBV result has the XCR bit set.
108 if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) {
109 if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) {
110 caps.avx = true;
111 if ((cpu_id[2] >> 12) & 1)
112 caps.fma = true;
113 }
114 }
115
116 if (max_std_fn >= 7) {
117 __cpuidex(cpu_id, 0x00000007, 0x00000000);
118 // Can't enable AVX2 unless the XSAVE/XGETBV checks above passed
119 if ((cpu_id[1] >> 5) & 1)
120 caps.avx2 = caps.avx;
121 if ((cpu_id[1] >> 3) & 1)
122 caps.bmi1 = true;
123 if ((cpu_id[1] >> 8) & 1)
124 caps.bmi2 = true;
125 }
126 }
127
128 caps.flush_to_zero = caps.sse;
129
130 if (max_ex_fn >= 0x80000004) {
131 // Extract CPU model string
132 __cpuid(cpu_id, 0x80000002);
133 std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id));
134 __cpuid(cpu_id, 0x80000003);
135 std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id));
136 __cpuid(cpu_id, 0x80000004);
137 std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id));
138 }
139
140 if (max_ex_fn >= 0x80000001) {
141 // Check for more features
142 __cpuid(cpu_id, 0x80000001);
143 if (cpu_id[2] & 1) caps.lahf_sahf_64 = true;
144 if ((cpu_id[2] >> 5) & 1) caps.lzcnt = true;
145 if ((cpu_id[2] >> 16) & 1) caps.fma4 = true;
146 if ((cpu_id[3] >> 29) & 1) caps.long_mode = true;
147 }
148
149 return caps;
150}
151
152const CPUCaps& GetCPUCaps() {
153 static CPUCaps caps = Detect();
154 return caps;
155}
156
157std::string GetCPUCapsString() {
158 auto caps = GetCPUCaps();
159
160 std::string sum(caps.cpu_string);
161 sum += " (";
162 sum += caps.brand_string;
163 sum += ")";
164
165 if (caps.sse) sum += ", SSE";
166 if (caps.sse2) {
167 sum += ", SSE2";
168 if (!caps.flush_to_zero) sum += " (without DAZ)";
169 }
170
171 if (caps.sse3) sum += ", SSE3";
172 if (caps.ssse3) sum += ", SSSE3";
173 if (caps.sse4_1) sum += ", SSE4.1";
174 if (caps.sse4_2) sum += ", SSE4.2";
175 if (caps.avx) sum += ", AVX";
176 if (caps.avx2) sum += ", AVX2";
177 if (caps.bmi1) sum += ", BMI1";
178 if (caps.bmi2) sum += ", BMI2";
179 if (caps.fma) sum += ", FMA";
180 if (caps.aes) sum += ", AES";
181 if (caps.movbe) sum += ", MOVBE";
182 if (caps.long_mode) sum += ", 64-bit support";
183
184 return sum;
185}
186
187} // namespace Common