diff options
| author | 2015-08-12 17:42:13 -0400 | |
|---|---|---|
| committer | 2015-08-15 18:03:26 -0400 | |
| commit | 0ee00861f6747f8946972a91539e857f493e9cc6 (patch) | |
| tree | e0e2fca87c90f5d58d411c7a8f27fdf316162308 /src/common/x64/cpu_detect.cpp | |
| parent | Common: Move cpu_detect to x64 directory. (diff) | |
| download | yuzu-0ee00861f6747f8946972a91539e857f493e9cc6.tar.gz yuzu-0ee00861f6747f8946972a91539e857f493e9cc6.tar.xz yuzu-0ee00861f6747f8946972a91539e857f493e9cc6.zip | |
Common: Cleanup CPU capability detection code.
Diffstat (limited to 'src/common/x64/cpu_detect.cpp')
| -rw-r--r-- | src/common/x64/cpu_detect.cpp | 220 |
1 files changed, 89 insertions, 131 deletions
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index 72c8297a3..d9c430c67 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp | |||
| @@ -1,23 +1,25 @@ | |||
| 1 | // Copyright 2008 Dolphin Emulator Project | 1 | // Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project |
| 2 | // Licensed under GPLv2+ | 2 | // Licensed under GPLv2 or any later version |
| 3 | // Refer to the license.txt file included. | 3 | // Refer to the license.txt file included. |
| 4 | 4 | ||
| 5 | #include <cstring> | 5 | #include <cstring> |
| 6 | #include <string> | 6 | #include <string> |
| 7 | #include <thread> | ||
| 7 | 8 | ||
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 9 | 10 | ||
| 10 | #include "cpu_detect.h" | 11 | #include "cpu_detect.h" |
| 11 | 12 | ||
| 12 | #ifndef _WIN32 | 13 | namespace Common { |
| 14 | |||
| 15 | #ifndef _MSC_VER | ||
| 13 | 16 | ||
| 14 | #ifdef __FreeBSD__ | 17 | #ifdef __FreeBSD__ |
| 15 | #include <sys/types.h> | 18 | #include <sys/types.h> |
| 16 | #include <machine/cpufunc.h> | 19 | #include <machine/cpufunc.h> |
| 17 | #endif | 20 | #endif |
| 18 | 21 | ||
| 19 | static inline void __cpuidex(int info[4], int function_id, int subfunction_id) | 22 | static inline void __cpuidex(int info[4], int function_id, int subfunction_id) { |
| 20 | { | ||
| 21 | #ifdef __FreeBSD__ | 23 | #ifdef __FreeBSD__ |
| 22 | // Despite the name, this is just do_cpuid() with ECX as second input. | 24 | // 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); | 25 | cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info); |
| @@ -36,96 +38,67 @@ static inline void __cpuidex(int info[4], int function_id, int subfunction_id) | |||
| 36 | #endif | 38 | #endif |
| 37 | } | 39 | } |
| 38 | 40 | ||
| 39 | static inline void __cpuid(int info[4], int function_id) | 41 | static inline void __cpuid(int info[4], int function_id) { |
| 40 | { | ||
| 41 | return __cpuidex(info, function_id, 0); | 42 | return __cpuidex(info, function_id, 0); |
| 42 | } | 43 | } |
| 43 | 44 | ||
| 44 | #define _XCR_XFEATURE_ENABLED_MASK 0 | 45 | #define _XCR_XFEATURE_ENABLED_MASK 0 |
| 45 | static u64 _xgetbv(u32 index) | 46 | static u64 _xgetbv(u32 index) { |
| 46 | { | ||
| 47 | u32 eax, edx; | 47 | u32 eax, edx; |
| 48 | __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index)); | 48 | __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index)); |
| 49 | return ((u64)edx << 32) | eax; | 49 | return ((u64)edx << 32) | eax; |
| 50 | } | 50 | } |
| 51 | 51 | ||
| 52 | #endif // ifndef _WIN32 | 52 | #endif // ifndef _MSC_VER |
| 53 | |||
| 54 | namespace Common { | ||
| 55 | |||
| 56 | CPUInfo cpu_info; | ||
| 57 | |||
| 58 | CPUInfo::CPUInfo() { | ||
| 59 | Detect(); | ||
| 60 | } | ||
| 61 | 53 | ||
| 62 | // Detects the various CPU features | 54 | // Detects the various CPU features |
| 63 | void CPUInfo::Detect() { | 55 | static CPUCaps Detect() { |
| 64 | memset(this, 0, sizeof(*this)); | 56 | CPUCaps caps = {}; |
| 65 | #ifdef ARCHITECTURE_X64 | ||
| 66 | Mode64bit = true; | ||
| 67 | OS64bit = true; | ||
| 68 | #endif | ||
| 69 | num_cores = 1; | ||
| 70 | 57 | ||
| 71 | // Set obvious defaults, for extra safety | 58 | caps.num_cores = std::thread::hardware_concurrency(); |
| 72 | if (Mode64bit) { | 59 | |
| 73 | bSSE = true; | 60 | // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support |
| 74 | bSSE2 = true; | 61 | // Citra at all anyway |
| 75 | bLongMode = true; | ||
| 76 | } | ||
| 77 | 62 | ||
| 78 | // Assume CPU supports the CPUID instruction. Those that don't can barely | ||
| 79 | // boot modern OS:es anyway. | ||
| 80 | int cpu_id[4]; | 63 | int cpu_id[4]; |
| 81 | memset(brand_string, 0, sizeof(brand_string)); | 64 | memset(caps.brand_string, 0, sizeof(caps.brand_string)); |
| 82 | 65 | ||
| 83 | // Detect CPU's CPUID capabilities, and grab CPU string | 66 | // Detect CPU's CPUID capabilities and grab CPU string |
| 84 | __cpuid(cpu_id, 0x00000000); | 67 | __cpuid(cpu_id, 0x00000000); |
| 85 | u32 max_std_fn = cpu_id[0]; // EAX | 68 | u32 max_std_fn = cpu_id[0]; // EAX |
| 86 | *((int *)brand_string) = cpu_id[1]; | 69 | |
| 87 | *((int *)(brand_string + 4)) = cpu_id[3]; | 70 | std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(int)); |
| 88 | *((int *)(brand_string + 8)) = cpu_id[2]; | 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 | |||
| 89 | __cpuid(cpu_id, 0x80000000); | 74 | __cpuid(cpu_id, 0x80000000); |
| 75 | |||
| 90 | u32 max_ex_fn = cpu_id[0]; | 76 | u32 max_ex_fn = cpu_id[0]; |
| 91 | if (!strcmp(brand_string, "GenuineIntel")) | 77 | if (!strcmp(caps.brand_string, "GenuineIntel")) |
| 92 | vendor = VENDOR_INTEL; | 78 | caps.vendor = CPUVendor::INTEL; |
| 93 | else if (!strcmp(brand_string, "AuthenticAMD")) | 79 | else if (!strcmp(caps.brand_string, "AuthenticAMD")) |
| 94 | vendor = VENDOR_AMD; | 80 | caps.vendor = CPUVendor::AMD; |
| 95 | else | 81 | else |
| 96 | vendor = VENDOR_OTHER; | 82 | caps.vendor = CPUVendor::OTHER; |
| 97 | 83 | ||
| 98 | // Set reasonable default brand string even if brand string not available. | 84 | // Set reasonable default brand string even if brand string not available |
| 99 | strcpy(cpu_string, brand_string); | 85 | strcpy(caps.cpu_string, caps.brand_string); |
| 100 | 86 | ||
| 101 | // Detect family and other misc stuff. | 87 | // Detect family and other miscellaneous features |
| 102 | bool ht = false; | ||
| 103 | HTT = ht; | ||
| 104 | logical_cpu_count = 1; | ||
| 105 | if (max_std_fn >= 1) { | 88 | if (max_std_fn >= 1) { |
| 106 | __cpuid(cpu_id, 0x00000001); | 89 | __cpuid(cpu_id, 0x00000001); |
| 107 | int family = ((cpu_id[0] >> 8) & 0xf) + ((cpu_id[0] >> 20) & 0xff); | 90 | |
| 108 | int model = ((cpu_id[0] >> 4) & 0xf) + ((cpu_id[0] >> 12) & 0xf0); | 91 | if ((cpu_id[3] >> 25) & 1) caps.sse = true; |
| 109 | // Detect people unfortunate enough to be running Dolphin on an Atom | 92 | if ((cpu_id[3] >> 26) & 1) caps.sse2 = true; |
| 110 | if (family == 6 && (model == 0x1C || model == 0x26 || model == 0x27 || model == 0x35 || model == 0x36 || | 93 | if ((cpu_id[2]) & 1) caps.sse3 = true; |
| 111 | model == 0x37 || model == 0x4A || model == 0x4D || model == 0x5A || model == 0x5D)) | 94 | if ((cpu_id[2] >> 9) & 1) caps.ssse3 = true; |
| 112 | bAtom = true; | 95 | if ((cpu_id[2] >> 19) & 1) caps.sse4_1 = true; |
| 113 | logical_cpu_count = (cpu_id[1] >> 16) & 0xFF; | 96 | if ((cpu_id[2] >> 20) & 1) caps.sse4_2 = true; |
| 114 | ht = (cpu_id[3] >> 28) & 1; | 97 | if ((cpu_id[2] >> 22) & 1) caps.movbe = true; |
| 115 | 98 | if ((cpu_id[2] >> 25) & 1) caps.aes = true; | |
| 116 | if ((cpu_id[3] >> 25) & 1) bSSE = true; | 99 | |
| 117 | if ((cpu_id[3] >> 26) & 1) bSSE2 = true; | 100 | if ((cpu_id[3] >> 24) & 1) { |
| 118 | if ((cpu_id[2]) & 1) bSSE3 = true; | 101 | caps.fxsave_fxrstor = 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 | } | 102 | } |
| 130 | 103 | ||
| 131 | // AVX support requires 3 separate checks: | 104 | // AVX support requires 3 separate checks: |
| @@ -134,95 +107,80 @@ void CPUInfo::Detect() { | |||
| 134 | // - XGETBV result has the XCR bit set. | 107 | // - XGETBV result has the XCR bit set. |
| 135 | if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) { | 108 | if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) { |
| 136 | if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) { | 109 | if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) { |
| 137 | bAVX = true; | 110 | caps.avx = true; |
| 138 | if ((cpu_id[2] >> 12) & 1) | 111 | if ((cpu_id[2] >> 12) & 1) |
| 139 | bFMA = true; | 112 | caps.fma = true; |
| 140 | } | 113 | } |
| 141 | } | 114 | } |
| 142 | 115 | ||
| 143 | if (max_std_fn >= 7) { | 116 | if (max_std_fn >= 7) { |
| 144 | __cpuidex(cpu_id, 0x00000007, 0x00000000); | 117 | __cpuidex(cpu_id, 0x00000007, 0x00000000); |
| 145 | // careful; we can't enable AVX2 unless the XSAVE/XGETBV checks above passed | 118 | // Can't enable AVX2 unless the XSAVE/XGETBV checks above passed |
| 146 | if ((cpu_id[1] >> 5) & 1) | 119 | if ((cpu_id[1] >> 5) & 1) |
| 147 | bAVX2 = bAVX; | 120 | caps.avx2 = caps.avx; |
| 148 | if ((cpu_id[1] >> 3) & 1) | 121 | if ((cpu_id[1] >> 3) & 1) |
| 149 | bBMI1 = true; | 122 | caps.bmi1 = true; |
| 150 | if ((cpu_id[1] >> 8) & 1) | 123 | if ((cpu_id[1] >> 8) & 1) |
| 151 | bBMI2 = true; | 124 | caps.bmi2 = true; |
| 152 | } | 125 | } |
| 153 | } | 126 | } |
| 154 | 127 | ||
| 155 | bFlushToZero = bSSE; | 128 | caps.flush_to_zero = caps.sse; |
| 156 | 129 | ||
| 157 | if (max_ex_fn >= 0x80000004) { | 130 | if (max_ex_fn >= 0x80000004) { |
| 158 | // Extract CPU model string | 131 | // Extract CPU model string |
| 159 | __cpuid(cpu_id, 0x80000002); | 132 | __cpuid(cpu_id, 0x80000002); |
| 160 | memcpy(cpu_string, cpu_id, sizeof(cpu_id)); | 133 | std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id)); |
| 161 | __cpuid(cpu_id, 0x80000003); | 134 | __cpuid(cpu_id, 0x80000003); |
| 162 | memcpy(cpu_string + 16, cpu_id, sizeof(cpu_id)); | 135 | std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id)); |
| 163 | __cpuid(cpu_id, 0x80000004); | 136 | __cpuid(cpu_id, 0x80000004); |
| 164 | memcpy(cpu_string + 32, cpu_id, sizeof(cpu_id)); | 137 | std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id)); |
| 165 | } | 138 | } |
| 139 | |||
| 166 | if (max_ex_fn >= 0x80000001) { | 140 | if (max_ex_fn >= 0x80000001) { |
| 167 | // Check for more features. | 141 | // Check for more features |
| 168 | __cpuid(cpu_id, 0x80000001); | 142 | __cpuid(cpu_id, 0x80000001); |
| 169 | if (cpu_id[2] & 1) bLAHFSAHF64 = true; | 143 | if (cpu_id[2] & 1) caps.lahf_sahf_64 = true; |
| 170 | if ((cpu_id[2] >> 5) & 1) bLZCNT = true; | 144 | if ((cpu_id[2] >> 5) & 1) caps.lzcnt = true; |
| 171 | if ((cpu_id[2] >> 16) & 1) bFMA4 = true; | 145 | if ((cpu_id[2] >> 16) & 1) caps.fma4 = true; |
| 172 | if ((cpu_id[3] >> 29) & 1) bLongMode = true; | 146 | if ((cpu_id[3] >> 29) & 1) caps.long_mode = true; |
| 173 | } | 147 | } |
| 174 | 148 | ||
| 175 | num_cores = (logical_cpu_count == 0) ? 1 : logical_cpu_count; | 149 | return caps; |
| 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 | } | 150 | } |
| 199 | 151 | ||
| 200 | // Turn the CPU info into a string we can show | 152 | const CPUCaps& GetCPUCaps() { |
| 201 | std::string CPUInfo::Summarize() { | 153 | static CPUCaps caps = Detect(); |
| 202 | std::string sum(cpu_string); | 154 | return caps; |
| 155 | } | ||
| 156 | |||
| 157 | std::string GetCPUCapsString() { | ||
| 158 | auto caps = GetCPUCaps(); | ||
| 159 | |||
| 160 | std::string sum(caps.cpu_string); | ||
| 203 | sum += " ("; | 161 | sum += " ("; |
| 204 | sum += brand_string; | 162 | sum += caps.brand_string; |
| 205 | sum += ")"; | 163 | sum += ")"; |
| 206 | 164 | ||
| 207 | if (bSSE) sum += ", SSE"; | 165 | if (caps.sse) sum += ", SSE"; |
| 208 | if (bSSE2) { | 166 | if (caps.sse2) { |
| 209 | sum += ", SSE2"; | 167 | sum += ", SSE2"; |
| 210 | if (!bFlushToZero) | 168 | if (!caps.flush_to_zero) sum += " (without DAZ)"; |
| 211 | sum += " (but not DAZ!)"; | ||
| 212 | } | 169 | } |
| 213 | if (bSSE3) sum += ", SSE3"; | 170 | |
| 214 | if (bSSSE3) sum += ", SSSE3"; | 171 | if (caps.sse3) sum += ", SSE3"; |
| 215 | if (bSSE4_1) sum += ", SSE4.1"; | 172 | if (caps.ssse3) sum += ", SSSE3"; |
| 216 | if (bSSE4_2) sum += ", SSE4.2"; | 173 | if (caps.sse4_1) sum += ", SSE4.1"; |
| 217 | if (HTT) sum += ", HTT"; | 174 | if (caps.sse4_2) sum += ", SSE4.2"; |
| 218 | if (bAVX) sum += ", AVX"; | 175 | if (caps.avx) sum += ", AVX"; |
| 219 | if (bAVX2) sum += ", AVX2"; | 176 | if (caps.avx2) sum += ", AVX2"; |
| 220 | if (bBMI1) sum += ", BMI1"; | 177 | if (caps.bmi1) sum += ", BMI1"; |
| 221 | if (bBMI2) sum += ", BMI2"; | 178 | if (caps.bmi2) sum += ", BMI2"; |
| 222 | if (bFMA) sum += ", FMA"; | 179 | if (caps.fma) sum += ", FMA"; |
| 223 | if (bAES) sum += ", AES"; | 180 | if (caps.aes) sum += ", AES"; |
| 224 | if (bMOVBE) sum += ", MOVBE"; | 181 | if (caps.movbe) sum += ", MOVBE"; |
| 225 | if (bLongMode) sum += ", 64-bit support"; | 182 | if (caps.long_mode) sum += ", 64-bit support"; |
| 183 | |||
| 226 | return sum; | 184 | return sum; |
| 227 | } | 185 | } |
| 228 | 186 | ||