From add2cfcb96d75cd16f7b0bf554bb30d97e720111 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Wed, 23 Feb 2022 10:08:32 -0800 Subject: [PATCH 1/6] bit_util: Add `bit` utility function Extracts a singular bit, as a bool, from the specified compile-time index. --- src/common/bit_util.h | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/common/bit_util.h b/src/common/bit_util.h index f50d3308a..f37538e06 100644 --- a/src/common/bit_util.h +++ b/src/common/bit_util.h @@ -57,4 +57,11 @@ requires std::is_integral_v return static_cast(1ULL << ((8U * sizeof(T)) - std::countl_zero(value - 1U))); } +template +requires std::is_integral_v +[[nodiscard]] constexpr bool Bit(const T value) { + static_assert(bit_index < BitSize(), "bit_index must be smaller than size of T"); + return ((value >> bit_index) & T(1)) == T(1); +} + } // namespace Common From d233de8194db9c7ce5042bb809c88a0b6cb02c83 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Wed, 23 Feb 2022 10:17:47 -0800 Subject: [PATCH 2/6] cpu_detect: Compact capability fields As this structure gets more explicit, bools can be bitfields and small enums can use smaller types for their span of values. --- src/common/x64/cpu_detect.h | 41 +++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/common/x64/cpu_detect.h b/src/common/x64/cpu_detect.h index e3b63302e..e4f90bee1 100644 --- a/src/common/x64/cpu_detect.h +++ b/src/common/x64/cpu_detect.h @@ -1,12 +1,13 @@ -// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project / 2022 Yuzu Emulator +// Project Project Licensed under GPLv2 or any later version Refer to the license.txt file included. #pragma once +#include "common/common_types.h" + namespace Common { -enum class Manufacturer : u32 { +enum class Manufacturer : u8 { Intel = 0, AMD = 1, Hygon = 2, @@ -18,25 +19,25 @@ struct CPUCaps { Manufacturer manufacturer; char cpu_string[0x21]; char brand_string[0x41]; - bool sse; - bool sse2; - bool sse3; - bool ssse3; - bool sse4_1; - bool sse4_2; - bool lzcnt; - bool avx; - bool avx2; - bool avx512; - bool bmi1; - bool bmi2; - bool fma; - bool fma4; - bool aes; - bool invariant_tsc; u32 base_frequency; u32 max_frequency; u32 bus_frequency; + bool sse : 1; + bool sse2 : 1; + bool sse3 : 1; + bool ssse3 : 1; + bool sse4_1 : 1; + bool sse4_2 : 1; + bool lzcnt : 1; + bool avx : 1; + bool avx2 : 1; + bool avx512 : 1; + bool bmi1 : 1; + bool bmi2 : 1; + bool fma : 1; + bool fma4 : 1; + bool aes : 1; + bool invariant_tsc : 1; }; /** From 3c33ba7f189702d1f5395251d4f3c5cbebee4840 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Wed, 23 Feb 2022 10:24:53 -0800 Subject: [PATCH 3/6] cpu_detect: Utilize `Bit` utility function --- src/common/x64/cpu_detect.cpp | 52 ++++++++++++++--------------------- 1 file changed, 20 insertions(+), 32 deletions(-) diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index fbeacc7e2..fee00914a 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp @@ -1,8 +1,8 @@ -// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. +// Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project / 2022 Yuzu Emulator +// Project Licensed under GPLv2 or any later version Refer to the license.txt file included. #include +#include "common/bit_util.h" #include "common/common_types.h" #include "common/x64/cpu_detect.h" @@ -81,29 +81,22 @@ static CPUCaps Detect() { // Detect family and other miscellaneous features if (max_std_fn >= 1) { __cpuid(cpu_id, 0x00000001); - if ((cpu_id[3] >> 25) & 1) - caps.sse = true; - if ((cpu_id[3] >> 26) & 1) - caps.sse2 = true; - if ((cpu_id[2]) & 1) - caps.sse3 = true; - if ((cpu_id[2] >> 9) & 1) - caps.ssse3 = true; - if ((cpu_id[2] >> 19) & 1) - caps.sse4_1 = true; - if ((cpu_id[2] >> 20) & 1) - caps.sse4_2 = true; - if ((cpu_id[2] >> 25) & 1) - caps.aes = true; + caps.sse = Common::Bit<25>(cpu_id[3]); + caps.sse2 = Common::Bit<26>(cpu_id[3]); + caps.sse3 = Common::Bit<0>(cpu_id[2]); + caps.ssse3 = Common::Bit<9>(cpu_id[2]); + caps.sse4_1 = Common::Bit<19>(cpu_id[2]); + caps.sse4_2 = Common::Bit<20>(cpu_id[2]); + caps.aes = Common::Bit<25>(cpu_id[2]); // AVX support requires 3 separate checks: // - Is the AVX bit set in CPUID? // - Is the XSAVE bit set in CPUID? // - XGETBV result has the XCR bit set. - if (((cpu_id[2] >> 28) & 1) && ((cpu_id[2] >> 27) & 1)) { + if (Common::Bit<28>(cpu_id[2]) && Common::Bit<27>(cpu_id[2])) { if ((_xgetbv(_XCR_XFEATURE_ENABLED_MASK) & 0x6) == 0x6) { caps.avx = true; - if ((cpu_id[2] >> 12) & 1) + if (Common::Bit<12>(cpu_id[2])) caps.fma = true; } } @@ -111,15 +104,13 @@ static CPUCaps Detect() { if (max_std_fn >= 7) { __cpuidex(cpu_id, 0x00000007, 0x00000000); // Can't enable AVX2 unless the XSAVE/XGETBV checks above passed - if ((cpu_id[1] >> 5) & 1) - caps.avx2 = caps.avx; - if ((cpu_id[1] >> 3) & 1) - caps.bmi1 = true; - if ((cpu_id[1] >> 8) & 1) - caps.bmi2 = true; + caps.avx2 = caps.avx && Common::Bit<5>(cpu_id[1]); + caps.bmi1 = Common::Bit<3>(cpu_id[1]); + caps.bmi2 = Common::Bit<8>(cpu_id[1]); // Checks for AVX512F, AVX512CD, AVX512VL, AVX512DQ, AVX512BW (Intel Skylake-X/SP) - if ((cpu_id[1] >> 16) & 1 && (cpu_id[1] >> 28) & 1 && (cpu_id[1] >> 31) & 1 && - (cpu_id[1] >> 17) & 1 && (cpu_id[1] >> 30) & 1) { + if (Common::Bit<16>(cpu_id[1]) && Common::Bit<28>(cpu_id[1]) && + Common::Bit<31>(cpu_id[1]) && Common::Bit<17>(cpu_id[1]) && + Common::Bit<30>(cpu_id[1])) { caps.avx512 = caps.avx2; } } @@ -138,15 +129,12 @@ static CPUCaps Detect() { if (max_ex_fn >= 0x80000001) { // Check for more features __cpuid(cpu_id, 0x80000001); - if ((cpu_id[2] >> 16) & 1) - caps.fma4 = true; + caps.fma4 = Common::Bit<16>(cpu_id[2]); } if (max_ex_fn >= 0x80000007) { __cpuid(cpu_id, 0x80000007); - if (cpu_id[3] & (1 << 8)) { - caps.invariant_tsc = true; - } + caps.invariant_tsc = Common::Bit<8>(cpu_id[3]); } if (max_std_fn >= 0x16) { From 86e9e60f07d59d6e3bf5d3a9cabf7db3fedb40c7 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Wed, 23 Feb 2022 10:43:41 -0800 Subject: [PATCH 4/6] cpu_detect: Update array-types to `span` and `array` Update some uses of `int` into some more explicitly sized types as well --- src/common/x64/cpu_detect.cpp | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index fee00914a..23adc5c75 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp @@ -1,7 +1,9 @@ // Copyright 2013 Dolphin Emulator Project / 2015 Citra Emulator Project / 2022 Yuzu Emulator // Project Licensed under GPLv2 or any later version Refer to the license.txt file included. +#include #include +#include #include "common/bit_util.h" #include "common/common_types.h" #include "common/x64/cpu_detect.h" @@ -17,7 +19,7 @@ // clang-format on #endif -static inline void __cpuidex(int info[4], int function_id, int subfunction_id) { +static inline void __cpuidex(const std::span info, u32 function_id, u32 subfunction_id) { #if defined(__DragonFly__) || defined(__FreeBSD__) // Despite the name, this is just do_cpuid() with ECX as second input. cpuid_count((u_int)function_id, (u_int)subfunction_id, (u_int*)info); @@ -30,7 +32,7 @@ static inline void __cpuidex(int info[4], int function_id, int subfunction_id) { #endif } -static inline void __cpuid(int info[4], int function_id) { +static inline void __cpuid(const std::span info, u32 function_id) { return __cpuidex(info, function_id, 0); } @@ -52,16 +54,16 @@ static CPUCaps Detect() { // Assumes the CPU supports the CPUID instruction. Those that don't would likely not support // yuzu at all anyway - int cpu_id[4]; - memset(caps.brand_string, 0, sizeof(caps.brand_string)); + std::array cpu_id; + std::memset(caps.brand_string, 0, sizeof(caps.brand_string)); // Detect CPU's CPUID capabilities and grab CPU string __cpuid(cpu_id, 0x00000000); u32 max_std_fn = cpu_id[0]; // EAX - std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(int)); - std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(int)); - std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(int)); + std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(u32)); + std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(u32)); + std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(u32)); if (cpu_id[1] == 0x756e6547 && cpu_id[2] == 0x6c65746e && cpu_id[3] == 0x49656e69) caps.manufacturer = Manufacturer::Intel; else if (cpu_id[1] == 0x68747541 && cpu_id[2] == 0x444d4163 && cpu_id[3] == 0x69746e65) @@ -76,7 +78,7 @@ static CPUCaps Detect() { u32 max_ex_fn = cpu_id[0]; // Set reasonable default brand string even if brand string not available - strcpy(caps.cpu_string, caps.brand_string); + std::strcpy(caps.cpu_string, caps.brand_string); // Detect family and other miscellaneous features if (max_std_fn >= 1) { @@ -119,11 +121,11 @@ static CPUCaps Detect() { if (max_ex_fn >= 0x80000004) { // Extract CPU model string __cpuid(cpu_id, 0x80000002); - std::memcpy(caps.cpu_string, cpu_id, sizeof(cpu_id)); + std::memcpy(caps.cpu_string, cpu_id.data(), sizeof(cpu_id)); __cpuid(cpu_id, 0x80000003); - std::memcpy(caps.cpu_string + 16, cpu_id, sizeof(cpu_id)); + std::memcpy(caps.cpu_string + 16, cpu_id.data(), sizeof(cpu_id)); __cpuid(cpu_id, 0x80000004); - std::memcpy(caps.cpu_string + 32, cpu_id, sizeof(cpu_id)); + std::memcpy(caps.cpu_string + 32, cpu_id.data(), sizeof(cpu_id)); } if (max_ex_fn >= 0x80000001) { From ec5f3351b69b6427d1e411fcc849e98705a044c3 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Wed, 23 Feb 2022 19:41:27 -0800 Subject: [PATCH 5/6] cpu_detect: Refactor cpu/manufacturer identification Set the zero-enum value to Unknown Move the Manufacterer enum into the CPUCaps structure namespace Add "ParseManufacturer" utility-function Fix cpu/brand string buffer sizes(!) --- src/common/x64/cpu_detect.cpp | 37 +++++++++++++++++++++-------------- src/common/x64/cpu_detect.h | 25 ++++++++++++++--------- 2 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index 23adc5c75..65369bfbe 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp @@ -3,7 +3,9 @@ #include #include +#include #include +#include #include "common/bit_util.h" #include "common/common_types.h" #include "common/x64/cpu_detect.h" @@ -47,6 +49,17 @@ static inline u64 _xgetbv(u32 index) { namespace Common { +CPUCaps::Manufacturer CPUCaps::ParseManufacturer(std::string_view brand_string) { + if (brand_string == "GenuineIntel") { + return Manufacturer::Intel; + } else if (brand_string == "AuthenticAMD") { + return Manufacturer::AMD; + } else if (brand_string == "HygonGenuine") { + return Manufacturer::Hygon; + } + return Manufacturer::Unknown; +} + // Detects the various CPU features static CPUCaps Detect() { CPUCaps caps = {}; @@ -55,30 +68,24 @@ static CPUCaps Detect() { // yuzu at all anyway std::array cpu_id; - std::memset(caps.brand_string, 0, sizeof(caps.brand_string)); - // Detect CPU's CPUID capabilities and grab CPU string + // Detect CPU's CPUID capabilities and grab manufacturer string __cpuid(cpu_id, 0x00000000); - u32 max_std_fn = cpu_id[0]; // EAX + const u32 max_std_fn = cpu_id[0]; // EAX + std::memset(caps.brand_string, 0, std::size(caps.brand_string)); std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(u32)); std::memcpy(&caps.brand_string[4], &cpu_id[3], sizeof(u32)); std::memcpy(&caps.brand_string[8], &cpu_id[2], sizeof(u32)); - if (cpu_id[1] == 0x756e6547 && cpu_id[2] == 0x6c65746e && cpu_id[3] == 0x49656e69) - caps.manufacturer = Manufacturer::Intel; - else if (cpu_id[1] == 0x68747541 && cpu_id[2] == 0x444d4163 && cpu_id[3] == 0x69746e65) - caps.manufacturer = Manufacturer::AMD; - else if (cpu_id[1] == 0x6f677948 && cpu_id[2] == 0x656e6975 && cpu_id[3] == 0x6e65476e) - caps.manufacturer = Manufacturer::Hygon; - else - caps.manufacturer = Manufacturer::Unknown; + + caps.manufacturer = CPUCaps::ParseManufacturer(caps.brand_string); + + // Set reasonable default cpu string even if brand string not available + std::strncpy(caps.cpu_string, caps.brand_string, std::size(caps.brand_string)); __cpuid(cpu_id, 0x80000000); - u32 max_ex_fn = cpu_id[0]; - - // Set reasonable default brand string even if brand string not available - std::strcpy(caps.cpu_string, caps.brand_string); + const u32 max_ex_fn = cpu_id[0]; // Detect family and other miscellaneous features if (max_std_fn >= 1) { diff --git a/src/common/x64/cpu_detect.h b/src/common/x64/cpu_detect.h index e4f90bee1..3e6d808f3 100644 --- a/src/common/x64/cpu_detect.h +++ b/src/common/x64/cpu_detect.h @@ -3,25 +3,32 @@ #pragma once +#include #include "common/common_types.h" namespace Common { -enum class Manufacturer : u8 { - Intel = 0, - AMD = 1, - Hygon = 2, - Unknown = 3, -}; - /// x86/x64 CPU capabilities that may be detected by this module struct CPUCaps { + + enum class Manufacturer : u8 { + Unknown = 0, + Intel = 1, + AMD = 2, + Hygon = 3, + }; + + static Manufacturer ParseManufacturer(std::string_view brand_string); + Manufacturer manufacturer; - char cpu_string[0x21]; - char brand_string[0x41]; + char brand_string[13]; + + char cpu_string[48]; + u32 base_frequency; u32 max_frequency; u32 bus_frequency; + bool sse : 1; bool sse2 : 1; bool sse3 : 1; From 873a9fa7e5083de46f37718d0805aeec176af584 Mon Sep 17 00:00:00 2001 From: Wunkolo Date: Wed, 23 Feb 2022 20:28:25 -0800 Subject: [PATCH 6/6] cpu_detect: Add missing `lzcnt` detection --- src/common/x64/cpu_detect.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index 65369bfbe..a95dc15a3 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp @@ -138,6 +138,7 @@ static CPUCaps Detect() { if (max_ex_fn >= 0x80000001) { // Check for more features __cpuid(cpu_id, 0x80000001); + caps.lzcnt = Common::Bit<5>(cpu_id[2]); caps.fma4 = Common::Bit<16>(cpu_id[2]); }