diff options
Diffstat (limited to 'src/common/x64')
| -rw-r--r-- | src/common/x64/cpu_detect.cpp | 33 | ||||
| -rw-r--r-- | src/common/x64/cpu_detect.h | 12 | ||||
| -rw-r--r-- | src/common/x64/native_clock.cpp | 95 | ||||
| -rw-r--r-- | src/common/x64/native_clock.h | 41 |
4 files changed, 181 insertions, 0 deletions
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index f35dcb498..fccd2eee5 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp | |||
| @@ -62,6 +62,17 @@ static CPUCaps Detect() { | |||
| 62 | std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(int)); | 62 | std::memcpy(&caps.brand_string[0], &cpu_id[1], sizeof(int)); |
| 63 | std::memcpy(&caps.brand_string[4], &cpu_id[3], 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)); | 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 | |||
| 74 | u32 family = {}; | ||
| 75 | u32 model = {}; | ||
| 65 | 76 | ||
| 66 | __cpuid(cpu_id, 0x80000000); | 77 | __cpuid(cpu_id, 0x80000000); |
| 67 | 78 | ||
| @@ -73,6 +84,14 @@ static CPUCaps Detect() { | |||
| 73 | // Detect family and other miscellaneous features | 84 | // Detect family and other miscellaneous features |
| 74 | if (max_std_fn >= 1) { | 85 | if (max_std_fn >= 1) { |
| 75 | __cpuid(cpu_id, 0x00000001); | 86 | __cpuid(cpu_id, 0x00000001); |
| 87 | family = (cpu_id[0] >> 8) & 0xf; | ||
| 88 | model = (cpu_id[0] >> 4) & 0xf; | ||
| 89 | if (family == 0xf) { | ||
| 90 | family += (cpu_id[0] >> 20) & 0xff; | ||
| 91 | } | ||
| 92 | if (family >= 6) { | ||
| 93 | model += ((cpu_id[0] >> 16) & 0xf) << 4; | ||
| 94 | } | ||
| 76 | 95 | ||
| 77 | if ((cpu_id[3] >> 25) & 1) | 96 | if ((cpu_id[3] >> 25) & 1) |
| 78 | caps.sse = true; | 97 | caps.sse = true; |
| @@ -135,6 +154,20 @@ static CPUCaps Detect() { | |||
| 135 | caps.fma4 = true; | 154 | caps.fma4 = true; |
| 136 | } | 155 | } |
| 137 | 156 | ||
| 157 | if (max_ex_fn >= 0x80000007) { | ||
| 158 | __cpuid(cpu_id, 0x80000007); | ||
| 159 | if (cpu_id[3] & (1 << 8)) { | ||
| 160 | caps.invariant_tsc = true; | ||
| 161 | } | ||
| 162 | } | ||
| 163 | |||
| 164 | if (max_std_fn >= 0x16) { | ||
| 165 | __cpuid(cpu_id, 0x16); | ||
| 166 | caps.base_frequency = cpu_id[0]; | ||
| 167 | caps.max_frequency = cpu_id[1]; | ||
| 168 | caps.bus_frequency = cpu_id[2]; | ||
| 169 | } | ||
| 170 | |||
| 138 | return caps; | 171 | return caps; |
| 139 | } | 172 | } |
| 140 | 173 | ||
diff --git a/src/common/x64/cpu_detect.h b/src/common/x64/cpu_detect.h index 7606c3f7b..e3b63302e 100644 --- a/src/common/x64/cpu_detect.h +++ b/src/common/x64/cpu_detect.h | |||
| @@ -6,8 +6,16 @@ | |||
| 6 | 6 | ||
| 7 | namespace Common { | 7 | namespace Common { |
| 8 | 8 | ||
| 9 | enum class Manufacturer : u32 { | ||
| 10 | Intel = 0, | ||
| 11 | AMD = 1, | ||
| 12 | Hygon = 2, | ||
| 13 | Unknown = 3, | ||
| 14 | }; | ||
| 15 | |||
| 9 | /// x86/x64 CPU capabilities that may be detected by this module | 16 | /// x86/x64 CPU capabilities that may be detected by this module |
| 10 | struct CPUCaps { | 17 | struct CPUCaps { |
| 18 | Manufacturer manufacturer; | ||
| 11 | char cpu_string[0x21]; | 19 | char cpu_string[0x21]; |
| 12 | char brand_string[0x41]; | 20 | char brand_string[0x41]; |
| 13 | bool sse; | 21 | bool sse; |
| @@ -25,6 +33,10 @@ struct CPUCaps { | |||
| 25 | bool fma; | 33 | bool fma; |
| 26 | bool fma4; | 34 | bool fma4; |
| 27 | bool aes; | 35 | bool aes; |
| 36 | bool invariant_tsc; | ||
| 37 | u32 base_frequency; | ||
| 38 | u32 max_frequency; | ||
| 39 | u32 bus_frequency; | ||
| 28 | }; | 40 | }; |
| 29 | 41 | ||
| 30 | /** | 42 | /** |
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp new file mode 100644 index 000000000..26d4d0ba6 --- /dev/null +++ b/src/common/x64/native_clock.cpp | |||
| @@ -0,0 +1,95 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include <chrono> | ||
| 6 | #include <thread> | ||
| 7 | |||
| 8 | #ifdef _MSC_VER | ||
| 9 | #include <intrin.h> | ||
| 10 | #else | ||
| 11 | #include <x86intrin.h> | ||
| 12 | #endif | ||
| 13 | |||
| 14 | #include "common/uint128.h" | ||
| 15 | #include "common/x64/native_clock.h" | ||
| 16 | |||
| 17 | namespace Common { | ||
| 18 | |||
| 19 | u64 EstimateRDTSCFrequency() { | ||
| 20 | const auto milli_10 = std::chrono::milliseconds{10}; | ||
| 21 | // get current time | ||
| 22 | _mm_mfence(); | ||
| 23 | const u64 tscStart = __rdtsc(); | ||
| 24 | const auto startTime = std::chrono::high_resolution_clock::now(); | ||
| 25 | // wait roughly 3 seconds | ||
| 26 | while (true) { | ||
| 27 | auto milli = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 28 | std::chrono::high_resolution_clock::now() - startTime); | ||
| 29 | if (milli.count() >= 3000) | ||
| 30 | break; | ||
| 31 | std::this_thread::sleep_for(milli_10); | ||
| 32 | } | ||
| 33 | const auto endTime = std::chrono::high_resolution_clock::now(); | ||
| 34 | _mm_mfence(); | ||
| 35 | const u64 tscEnd = __rdtsc(); | ||
| 36 | // calculate difference | ||
| 37 | const u64 timer_diff = | ||
| 38 | std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count(); | ||
| 39 | const u64 tsc_diff = tscEnd - tscStart; | ||
| 40 | const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff); | ||
| 41 | return tsc_freq; | ||
| 42 | } | ||
| 43 | |||
| 44 | namespace X64 { | ||
| 45 | NativeClock::NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, | ||
| 46 | u64 rtsc_frequency) | ||
| 47 | : WallClock(emulated_cpu_frequency, emulated_clock_frequency, true), rtsc_frequency{ | ||
| 48 | rtsc_frequency} { | ||
| 49 | _mm_mfence(); | ||
| 50 | last_measure = __rdtsc(); | ||
| 51 | accumulated_ticks = 0U; | ||
| 52 | } | ||
| 53 | |||
| 54 | u64 NativeClock::GetRTSC() { | ||
| 55 | rtsc_serialize.lock(); | ||
| 56 | _mm_mfence(); | ||
| 57 | const u64 current_measure = __rdtsc(); | ||
| 58 | u64 diff = current_measure - last_measure; | ||
| 59 | diff = diff & ~static_cast<u64>(static_cast<s64>(diff) >> 63); // max(diff, 0) | ||
| 60 | if (current_measure > last_measure) { | ||
| 61 | last_measure = current_measure; | ||
| 62 | } | ||
| 63 | accumulated_ticks += diff; | ||
| 64 | rtsc_serialize.unlock(); | ||
| 65 | return accumulated_ticks; | ||
| 66 | } | ||
| 67 | |||
| 68 | std::chrono::nanoseconds NativeClock::GetTimeNS() { | ||
| 69 | const u64 rtsc_value = GetRTSC(); | ||
| 70 | return std::chrono::nanoseconds{MultiplyAndDivide64(rtsc_value, 1000000000, rtsc_frequency)}; | ||
| 71 | } | ||
| 72 | |||
| 73 | std::chrono::microseconds NativeClock::GetTimeUS() { | ||
| 74 | const u64 rtsc_value = GetRTSC(); | ||
| 75 | return std::chrono::microseconds{MultiplyAndDivide64(rtsc_value, 1000000, rtsc_frequency)}; | ||
| 76 | } | ||
| 77 | |||
| 78 | std::chrono::milliseconds NativeClock::GetTimeMS() { | ||
| 79 | const u64 rtsc_value = GetRTSC(); | ||
| 80 | return std::chrono::milliseconds{MultiplyAndDivide64(rtsc_value, 1000, rtsc_frequency)}; | ||
| 81 | } | ||
| 82 | |||
| 83 | u64 NativeClock::GetClockCycles() { | ||
| 84 | const u64 rtsc_value = GetRTSC(); | ||
| 85 | return MultiplyAndDivide64(rtsc_value, emulated_clock_frequency, rtsc_frequency); | ||
| 86 | } | ||
| 87 | |||
| 88 | u64 NativeClock::GetCPUCycles() { | ||
| 89 | const u64 rtsc_value = GetRTSC(); | ||
| 90 | return MultiplyAndDivide64(rtsc_value, emulated_cpu_frequency, rtsc_frequency); | ||
| 91 | } | ||
| 92 | |||
| 93 | } // namespace X64 | ||
| 94 | |||
| 95 | } // namespace Common | ||
diff --git a/src/common/x64/native_clock.h b/src/common/x64/native_clock.h new file mode 100644 index 000000000..b58cf9f5a --- /dev/null +++ b/src/common/x64/native_clock.h | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #pragma once | ||
| 6 | |||
| 7 | #include <optional> | ||
| 8 | |||
| 9 | #include "common/spin_lock.h" | ||
| 10 | #include "common/wall_clock.h" | ||
| 11 | |||
| 12 | namespace Common { | ||
| 13 | |||
| 14 | namespace X64 { | ||
| 15 | class NativeClock : public WallClock { | ||
| 16 | public: | ||
| 17 | NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, u64 rtsc_frequency); | ||
| 18 | |||
| 19 | std::chrono::nanoseconds GetTimeNS() override; | ||
| 20 | |||
| 21 | std::chrono::microseconds GetTimeUS() override; | ||
| 22 | |||
| 23 | std::chrono::milliseconds GetTimeMS() override; | ||
| 24 | |||
| 25 | u64 GetClockCycles() override; | ||
| 26 | |||
| 27 | u64 GetCPUCycles() override; | ||
| 28 | |||
| 29 | private: | ||
| 30 | u64 GetRTSC(); | ||
| 31 | |||
| 32 | SpinLock rtsc_serialize{}; | ||
| 33 | u64 last_measure{}; | ||
| 34 | u64 accumulated_ticks{}; | ||
| 35 | u64 rtsc_frequency; | ||
| 36 | }; | ||
| 37 | } // namespace X64 | ||
| 38 | |||
| 39 | u64 EstimateRDTSCFrequency(); | ||
| 40 | |||
| 41 | } // namespace Common | ||