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 "common/common_types.h"
7 #include "common/x64/cpu_detect.h"
8
9 #ifdef _MSC_VER
10 #include <intrin.h>
11 #else
12
13 #if defined(__DragonFly__) || defined(__FreeBSD__)
14 // clang-format off
15 #include <sys/types.h>
16 #include <machine/cpufunc.h>
17 // clang-format on
18 #endif
19
__cpuidex(int info[4],int function_id,int subfunction_id)20 static inline void __cpuidex(int info[4], int function_id, int subfunction_id) {
21 #if defined(__DragonFly__) || defined(__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__("cpuid"
28 : "=a"(info[0]), "=b"(info[1]), "=c"(info[2]), "=d"(info[3])
29 : "a"(function_id), "c"(subfunction_id));
30 #endif
31 }
32
__cpuid(int info[4],int function_id)33 static inline void __cpuid(int info[4], int function_id) {
34 return __cpuidex(info, function_id, 0);
35 }
36
37 #define _XCR_XFEATURE_ENABLED_MASK 0
_xgetbv(u32 index)38 static inline u64 _xgetbv(u32 index) {
39 u32 eax, edx;
40 __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
41 return ((u64)edx << 32) | eax;
42 }
43
44 #endif // _MSC_VER
45
46 namespace Common {
47
48 // Detects the various CPU features
Detect()49 static CPUCaps Detect() {
50 CPUCaps caps = {};
51
52 // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support
53 // Citra at all anyway
54
55 int cpu_id[4];
56 memset(caps.brand_string, 0, sizeof(caps.brand_string));
57
58 // Detect CPU's CPUID capabilities and grab CPU string
59 __cpuid(cpu_id, 0x00000000);
60 u32 max_std_fn = cpu_id[0]; // EAX
61
62 std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(int));
63 std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(int));
64 std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(int));
65
66 __cpuid(cpu_id, 0x80000000);
67
68 u32 max_ex_fn = cpu_id[0];
69
70 // Set reasonable default brand string even if brand string not available
71 strcpy(caps.cpu_string, caps.brand_string);
72
73 // Detect family and other miscellaneous features
74 if (max_std_fn >= 1) {
75 __cpuid(cpu_id, 0x00000001);
76
77 if ((cpu_id[3] >> 25) & 1)
78 caps.sse = true;
79 if ((cpu_id[3] >> 26) & 1)
80 caps.sse2 = true;
81 if ((cpu_id[2]) & 1)
82 caps.sse3 = true;
83 if ((cpu_id[2] >> 9) & 1)
84 caps.ssse3 = true;
85 if ((cpu_id[2] >> 19) & 1)
86 caps.sse4_1 = true;
87 if ((cpu_id[2] >> 20) & 1)
88 caps.sse4_2 = true;
89 if ((cpu_id[2] >> 25) & 1)
90 caps.aes = true;
91
92 // AVX support requires 3 separate checks:
93 // - Is the AVX bit set in CPUID?
94 // - Is the XSAVE bit set in CPUID?
95 // - XGETBV result has the XCR bit set.
96 if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) {
97 if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) {
98 caps.avx = true;
99 if ((cpu_id[2] >> 12) & 1)
100 caps.fma = true;
101 }
102 }
103
104 if (max_std_fn >= 7) {
105 __cpuidex(cpu_id, 0x00000007, 0x00000000);
106 // Can't enable AVX2 unless the XSAVE/XGETBV checks above passed
107 if ((cpu_id[1] >> 5) & 1)
108 caps.avx2 = caps.avx;
109 if ((cpu_id[1] >> 3) & 1)
110 caps.bmi1 = true;
111 if ((cpu_id[1] >> 8) & 1)
112 caps.bmi2 = true;
113 // Checks for AVX512F, AVX512CD, AVX512VL, AVX512DQ, AVX512BW (Intel Skylake-X/SP)
114 if ((cpu_id[1] >> 16) & 1 && (cpu_id[1] >> 28) & 1 && (cpu_id[1] >> 31) & 1 &&
115 (cpu_id[1] >> 17) & 1 && (cpu_id[1] >> 30) & 1) {
116 caps.avx512 = caps.avx2;
117 }
118 }
119 }
120
121 if (max_ex_fn >= 0x80000004) {
122 // Extract CPU model string
123 __cpuid(cpu_id, 0x80000002);
124 std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id));
125 __cpuid(cpu_id, 0x80000003);
126 std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id));
127 __cpuid(cpu_id, 0x80000004);
128 std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id));
129 }
130
131 if (max_ex_fn >= 0x80000001) {
132 // Check for more features
133 __cpuid(cpu_id, 0x80000001);
134 if ((cpu_id[2] >> 16) & 1)
135 caps.fma4 = true;
136 }
137
138 return caps;
139 }
140
GetCPUCaps()141 const CPUCaps& GetCPUCaps() {
142 static CPUCaps caps = Detect();
143 return caps;
144 }
145
146 } // namespace Common
147