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 | 128 | ||||
| -rw-r--r-- | src/common/x64/native_clock.h | 41 |
4 files changed, 214 insertions, 0 deletions
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index c9349a6b4..d767c544c 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; |
| @@ -130,6 +149,20 @@ static CPUCaps Detect() { | |||
| 130 | caps.fma4 = true; | 149 | caps.fma4 = true; |
| 131 | } | 150 | } |
| 132 | 151 | ||
| 152 | if (max_ex_fn >= 0x80000007) { | ||
| 153 | __cpuid(cpu_id, 0x80000007); | ||
| 154 | if (cpu_id[3] & (1 << 8)) { | ||
| 155 | caps.invariant_tsc = true; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | if (max_std_fn >= 0x16) { | ||
| 160 | __cpuid(cpu_id, 0x16); | ||
| 161 | caps.base_frequency = cpu_id[0]; | ||
| 162 | caps.max_frequency = cpu_id[1]; | ||
| 163 | caps.bus_frequency = cpu_id[2]; | ||
| 164 | } | ||
| 165 | |||
| 133 | return caps; | 166 | return caps; |
| 134 | } | 167 | } |
| 135 | 168 | ||
diff --git a/src/common/x64/cpu_detect.h b/src/common/x64/cpu_detect.h index 20f2ba234..f0676fa5e 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; |
| @@ -24,6 +32,10 @@ struct CPUCaps { | |||
| 24 | bool fma; | 32 | bool fma; |
| 25 | bool fma4; | 33 | bool fma4; |
| 26 | bool aes; | 34 | bool aes; |
| 35 | bool invariant_tsc; | ||
| 36 | u32 base_frequency; | ||
| 37 | u32 max_frequency; | ||
| 38 | u32 bus_frequency; | ||
| 27 | }; | 39 | }; |
| 28 | 40 | ||
| 29 | /** | 41 | /** |
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp new file mode 100644 index 000000000..c799111fd --- /dev/null +++ b/src/common/x64/native_clock.cpp | |||
| @@ -0,0 +1,128 @@ | |||
| 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/x64/native_clock.h" | ||
| 15 | |||
| 16 | namespace Common { | ||
| 17 | |||
| 18 | #ifdef _MSC_VER | ||
| 19 | |||
| 20 | namespace { | ||
| 21 | |||
| 22 | struct uint128 { | ||
| 23 | u64 low; | ||
| 24 | u64 high; | ||
| 25 | }; | ||
| 26 | |||
| 27 | u64 umuldiv64(u64 a, u64 b, u64 d) { | ||
| 28 | uint128 r{}; | ||
| 29 | r.low = _umul128(a, b, &r.high); | ||
| 30 | u64 remainder; | ||
| 31 | return _udiv128(r.high, r.low, d, &remainder); | ||
| 32 | } | ||
| 33 | |||
| 34 | } // namespace | ||
| 35 | |||
| 36 | #else | ||
| 37 | |||
| 38 | namespace { | ||
| 39 | |||
| 40 | u64 umuldiv64(u64 a, u64 b, u64 d) { | ||
| 41 | const u64 diva = a / d; | ||
| 42 | const u64 moda = a % d; | ||
| 43 | const u64 divb = b / d; | ||
| 44 | const u64 modb = b % d; | ||
| 45 | return diva * b + moda * divb + moda * modb / d; | ||
| 46 | } | ||
| 47 | |||
| 48 | } // namespace | ||
| 49 | |||
| 50 | #endif | ||
| 51 | |||
| 52 | u64 EstimateRDTSCFrequency() { | ||
| 53 | const auto milli_10 = std::chrono::milliseconds{10}; | ||
| 54 | // get current time | ||
| 55 | _mm_mfence(); | ||
| 56 | const u64 tscStart = __rdtsc(); | ||
| 57 | const auto startTime = std::chrono::high_resolution_clock::now(); | ||
| 58 | // wait roughly 3 seconds | ||
| 59 | while (true) { | ||
| 60 | auto milli = std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| 61 | std::chrono::high_resolution_clock::now() - startTime); | ||
| 62 | if (milli.count() >= 3000) | ||
| 63 | break; | ||
| 64 | std::this_thread::sleep_for(milli_10); | ||
| 65 | } | ||
| 66 | const auto endTime = std::chrono::high_resolution_clock::now(); | ||
| 67 | _mm_mfence(); | ||
| 68 | const u64 tscEnd = __rdtsc(); | ||
| 69 | // calculate difference | ||
| 70 | const u64 timer_diff = | ||
| 71 | std::chrono::duration_cast<std::chrono::nanoseconds>(endTime - startTime).count(); | ||
| 72 | const u64 tsc_diff = tscEnd - tscStart; | ||
| 73 | const u64 tsc_freq = umuldiv64(tsc_diff, 1000000000ULL, timer_diff); | ||
| 74 | return tsc_freq; | ||
| 75 | } | ||
| 76 | |||
| 77 | namespace X64 { | ||
| 78 | NativeClock::NativeClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency, | ||
| 79 | u64 rtsc_frequency) | ||
| 80 | : WallClock(emulated_cpu_frequency, emulated_clock_frequency, true), rtsc_frequency{ | ||
| 81 | rtsc_frequency} { | ||
| 82 | _mm_mfence(); | ||
| 83 | last_measure = __rdtsc(); | ||
| 84 | accumulated_ticks = 0U; | ||
| 85 | } | ||
| 86 | |||
| 87 | u64 NativeClock::GetRTSC() { | ||
| 88 | rtsc_serialize.lock(); | ||
| 89 | _mm_mfence(); | ||
| 90 | const u64 current_measure = __rdtsc(); | ||
| 91 | u64 diff = current_measure - last_measure; | ||
| 92 | diff = diff & ~static_cast<u64>(static_cast<s64>(diff) >> 63); // max(diff, 0) | ||
| 93 | if (current_measure > last_measure) { | ||
| 94 | last_measure = current_measure; | ||
| 95 | } | ||
| 96 | accumulated_ticks += diff; | ||
| 97 | rtsc_serialize.unlock(); | ||
| 98 | return accumulated_ticks; | ||
| 99 | } | ||
| 100 | |||
| 101 | std::chrono::nanoseconds NativeClock::GetTimeNS() { | ||
| 102 | const u64 rtsc_value = GetRTSC(); | ||
| 103 | return std::chrono::nanoseconds{umuldiv64(rtsc_value, 1000000000, rtsc_frequency)}; | ||
| 104 | } | ||
| 105 | |||
| 106 | std::chrono::microseconds NativeClock::GetTimeUS() { | ||
| 107 | const u64 rtsc_value = GetRTSC(); | ||
| 108 | return std::chrono::microseconds{umuldiv64(rtsc_value, 1000000, rtsc_frequency)}; | ||
| 109 | } | ||
| 110 | |||
| 111 | std::chrono::milliseconds NativeClock::GetTimeMS() { | ||
| 112 | const u64 rtsc_value = GetRTSC(); | ||
| 113 | return std::chrono::milliseconds{umuldiv64(rtsc_value, 1000, rtsc_frequency)}; | ||
| 114 | } | ||
| 115 | |||
| 116 | u64 NativeClock::GetClockCycles() { | ||
| 117 | const u64 rtsc_value = GetRTSC(); | ||
| 118 | return umuldiv64(rtsc_value, emulated_clock_frequency, rtsc_frequency); | ||
| 119 | } | ||
| 120 | |||
| 121 | u64 NativeClock::GetCPUCycles() { | ||
| 122 | const u64 rtsc_value = GetRTSC(); | ||
| 123 | return umuldiv64(rtsc_value, emulated_cpu_frequency, rtsc_frequency); | ||
| 124 | } | ||
| 125 | |||
| 126 | } // namespace X64 | ||
| 127 | |||
| 128 | } // 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 | ||