diff options
Diffstat (limited to 'src/common/x64/native_clock.cpp')
| -rw-r--r-- | src/common/x64/native_clock.cpp | 128 |
1 files changed, 128 insertions, 0 deletions
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 | ||