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.cpp112
1 files changed, 55 insertions, 57 deletions
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp
index fbeacc7e2..a95dc15a3 100644
--- a/src/common/x64/cpu_detect.cpp
+++ b/src/common/x64/cpu_detect.cpp
@@ -1,8 +1,12 @@
1// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project 1// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project / 2022 Yuzu Emulator
2// Licensed under GPLv2 or any later version 2// Project Licensed under GPLv2 or any later version Refer to the license.txt file included.
3// Refer to the license.txt file included.
4 3
4#include <array>
5#include <cstring> 5#include <cstring>
6#include <iterator>
7#include <span>
8#include <string_view>
9#include "common/bit_util.h"
6#include "common/common_types.h" 10#include "common/common_types.h"
7#include "common/x64/cpu_detect.h" 11#include "common/x64/cpu_detect.h"
8 12
@@ -17,7 +21,7 @@
17// clang-format on 21// clang-format on
18#endif 22#endif
19 23
20static inline void __cpuidex(int info[4], int function_id, int subfunction_id) { 24static inline void __cpuidex(const std::span<u32, 4> info, u32 function_id, u32 subfunction_id) {
21#if defined(__DragonFly__) || defined(__FreeBSD__) 25#if defined(__DragonFly__) || defined(__FreeBSD__)
22 // Despite the name, this is just do_cpuid() with ECX as second input. 26 // 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); 27 cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info);
@@ -30,7 +34,7 @@ static inline void __cpuidex(int info[4], int function_id, int subfunction_id) {
30#endif 34#endif
31} 35}
32 36
33static inline void __cpuid(int info[4], int function_id) { 37static inline void __cpuid(const std::span<u32, 4> info, u32 function_id) {
34 return __cpuidex(info, function_id, 0); 38 return __cpuidex(info, function_id, 0);
35} 39}
36 40
@@ -45,6 +49,17 @@ static inline u64 _xgetbv(u32 index) {
45 49
46namespace Common { 50namespace Common {
47 51
52CPUCaps::Manufacturer CPUCaps::ParseManufacturer(std::string_view brand_string) {
53 if (brand_string == "GenuineIntel") {
54 return Manufacturer::Intel;
55 } else if (brand_string == "AuthenticAMD") {
56 return Manufacturer::AMD;
57 } else if (brand_string == "HygonGenuine") {
58 return Manufacturer::Hygon;
59 }
60 return Manufacturer::Unknown;
61}
62
48// Detects the various CPU features 63// Detects the various CPU features
49static CPUCaps Detect() { 64static CPUCaps Detect() {
50 CPUCaps caps = {}; 65 CPUCaps caps = {};
@@ -52,58 +67,45 @@ static CPUCaps Detect() {
52 // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support 67 // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support
53 // yuzu at all anyway 68 // yuzu at all anyway
54 69
55 int cpu_id[4]; 70 std::array<u32, 4> cpu_id;
56 memset(caps.brand_string, 0, sizeof(caps.brand_string));
57 71
58 // Detect CPU's CPUID capabilities and grab CPU string 72 // Detect CPU's CPUID capabilities and grab manufacturer string
59 __cpuid(cpu_id, 0x00000000); 73 __cpuid(cpu_id, 0x00000000);
60 u32 max_std_fn = cpu_id[0]; // EAX 74 const 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 if (cpu_id[1] == 0x756e6547 && cpu_id[2] == 0x6c65746e && cpu_id[3] == 0x49656e69)
66 caps.manufacturer = Manufacturer::Intel;
67 else if (cpu_id[1] == 0x68747541 && cpu_id[2] == 0x444d4163 && cpu_id[3] == 0x69746e65)
68 caps.manufacturer = Manufacturer::AMD;
69 else if (cpu_id[1] == 0x6f677948 && cpu_id[2] == 0x656e6975 && cpu_id[3] == 0x6e65476e)
70 caps.manufacturer = Manufacturer::Hygon;
71 else
72 caps.manufacturer = Manufacturer::Unknown;
73 75
74 __cpuid(cpu_id, 0x80000000); 76 std::memset(caps.brand_string, 0, std::size(caps.brand_string));
77 std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(u32));
78 std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(u32));
79 std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(u32));
75 80
76 u32 max_ex_fn = cpu_id[0]; 81 caps.manufacturer = CPUCaps::ParseManufacturer(caps.brand_string);
77 82
78 // Set reasonable default brand string even if brand string not available 83 // Set reasonable default cpu string even if brand string not available
79 strcpy(caps.cpu_string, caps.brand_string); 84 std::strncpy(caps.cpu_string, caps.brand_string, std::size(caps.brand_string));
85
86 __cpuid(cpu_id, 0x80000000);
87
88 const u32 max_ex_fn = cpu_id[0];
80 89
81 // Detect family and other miscellaneous features 90 // Detect family and other miscellaneous features
82 if (max_std_fn >= 1) { 91 if (max_std_fn >= 1) {
83 __cpuid(cpu_id, 0x00000001); 92 __cpuid(cpu_id, 0x00000001);
84 if ((cpu_id[3] >> 25) & 1) 93 caps.sse = Common::Bit<25>(cpu_id[3]);
85 caps.sse = true; 94 caps.sse2 = Common::Bit<26>(cpu_id[3]);
86 if ((cpu_id[3] >> 26) & 1) 95 caps.sse3 = Common::Bit<0>(cpu_id[2]);
87 caps.sse2 = true; 96 caps.ssse3 = Common::Bit<9>(cpu_id[2]);
88 if ((cpu_id[2]) & 1) 97 caps.sse4_1 = Common::Bit<19>(cpu_id[2]);
89 caps.sse3 = true; 98 caps.sse4_2 = Common::Bit<20>(cpu_id[2]);
90 if ((cpu_id[2] >> 9) & 1) 99 caps.aes = Common::Bit<25>(cpu_id[2]);
91 caps.ssse3 = true;
92 if ((cpu_id[2] >> 19) & 1)
93 caps.sse4_1 = true;
94 if ((cpu_id[2] >> 20) & 1)
95 caps.sse4_2 = true;
96 if ((cpu_id[2] >> 25) & 1)
97 caps.aes = true;
98 100
99 // AVX support requires 3 separate checks: 101 // AVX support requires 3 separate checks:
100 // - Is the AVX bit set in CPUID? 102 // - Is the AVX bit set in CPUID?
101 // - Is the XSAVE bit set in CPUID? 103 // - Is the XSAVE bit set in CPUID?
102 // - XGETBV result has the XCR bit set. 104 // - XGETBV result has the XCR bit set.
103 if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) { 105 if (Common::Bit<28>(cpu_id[2]) && Common::Bit<27>(cpu_id[2])) {
104 if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) { 106 if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) {
105 caps.avx = true; 107 caps.avx = true;
106 if ((cpu_id[2] >> 12) & 1) 108 if (Common::Bit<12>(cpu_id[2]))
107 caps.fma = true; 109 caps.fma = true;
108 } 110 }
109 } 111 }
@@ -111,15 +113,13 @@ static CPUCaps Detect() {
111 if (max_std_fn >= 7) { 113 if (max_std_fn >= 7) {
112 __cpuidex(cpu_id, 0x00000007, 0x00000000); 114 __cpuidex(cpu_id, 0x00000007, 0x00000000);
113 // Can't enable AVX2 unless the XSAVE/XGETBV checks above passed 115 // Can't enable AVX2 unless the XSAVE/XGETBV checks above passed
114 if ((cpu_id[1] >> 5) & 1) 116 caps.avx2 = caps.avx && Common::Bit<5>(cpu_id[1]);
115 caps.avx2 = caps.avx; 117 caps.bmi1 = Common::Bit<3>(cpu_id[1]);
116 if ((cpu_id[1] >> 3) & 1) 118 caps.bmi2 = Common::Bit<8>(cpu_id[1]);
117 caps.bmi1 = true;
118 if ((cpu_id[1] >> 8) & 1)
119 caps.bmi2 = true;
120 // Checks for AVX512F, AVX512CD, AVX512VL, AVX512DQ, AVX512BW (Intel Skylake-X/SP) 119 // Checks for AVX512F, AVX512CD, AVX512VL, AVX512DQ, AVX512BW (Intel Skylake-X/SP)
121 if ((cpu_id[1] >> 16) & 1 && (cpu_id[1] >> 28) & 1 && (cpu_id[1] >> 31) & 1 && 120 if (Common::Bit<16>(cpu_id[1]) && Common::Bit<28>(cpu_id[1]) &&
122 (cpu_id[1] >> 17) & 1 && (cpu_id[1] >> 30) & 1) { 121 Common::Bit<31>(cpu_id[1]) && Common::Bit<17>(cpu_id[1]) &&
122 Common::Bit<30>(cpu_id[1])) {
123 caps.avx512 = caps.avx2; 123 caps.avx512 = caps.avx2;
124 } 124 }
125 } 125 }
@@ -128,25 +128,23 @@ static CPUCaps Detect() {
128 if (max_ex_fn >= 0x80000004) { 128 if (max_ex_fn >= 0x80000004) {
129 // Extract CPU model string 129 // Extract CPU model string
130 __cpuid(cpu_id, 0x80000002); 130 __cpuid(cpu_id, 0x80000002);
131 std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id)); 131 std::memcpy(caps.cpu_string, cpu_id.data(), sizeof(cpu_id));
132 __cpuid(cpu_id, 0x80000003); 132 __cpuid(cpu_id, 0x80000003);
133 std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id)); 133 std::memcpy(caps.cpu_string + 16, cpu_id.data(), sizeof(cpu_id));
134 __cpuid(cpu_id, 0x80000004); 134 __cpuid(cpu_id, 0x80000004);
135 std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id)); 135 std::memcpy(caps.cpu_string + 32, cpu_id.data(), sizeof(cpu_id));
136 } 136 }
137 137
138 if (max_ex_fn >= 0x80000001) { 138 if (max_ex_fn >= 0x80000001) {
139 // Check for more features 139 // Check for more features
140 __cpuid(cpu_id, 0x80000001); 140 __cpuid(cpu_id, 0x80000001);
141 if ((cpu_id[2] >> 16) & 1) 141 caps.lzcnt = Common::Bit<5>(cpu_id[2]);
142 caps.fma4 = true; 142 caps.fma4 = Common::Bit<16>(cpu_id[2]);
143 } 143 }
144 144
145 if (max_ex_fn >= 0x80000007) { 145 if (max_ex_fn >= 0x80000007) {
146 __cpuid(cpu_id, 0x80000007); 146 __cpuid(cpu_id, 0x80000007);
147 if (cpu_id[3] & (1 << 8)) { 147 caps.invariant_tsc = Common::Bit<8>(cpu_id[3]);
148 caps.invariant_tsc = true;
149 }
150 } 148 }
151 149
152 if (max_std_fn >= 0x16) { 150 if (max_std_fn >= 0x16) {