diff options
| author | 2023-03-03 21:02:24 -0500 | |
|---|---|---|
| committer | 2023-03-05 02:36:31 -0500 | |
| commit | 376a414f5bc6d27e5e0fbda323caf5520436bf39 (patch) | |
| tree | ef26607ff1d0fcc4c9d1040a79f018c03ec98c3e /src | |
| parent | timer_resolution: Set current process to High QoS (diff) | |
| download | yuzu-376a414f5bc6d27e5e0fbda323caf5520436bf39.tar.gz yuzu-376a414f5bc6d27e5e0fbda323caf5520436bf39.tar.xz yuzu-376a414f5bc6d27e5e0fbda323caf5520436bf39.zip | |
native_clock: Round RDTSC frequency to the nearest 1000
Diffstat (limited to 'src')
| -rw-r--r-- | src/common/x64/native_clock.cpp | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 8b08332ab..bc1a973b0 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp | |||
| @@ -6,6 +6,7 @@ | |||
| 6 | #include <thread> | 6 | #include <thread> |
| 7 | 7 | ||
| 8 | #include "common/atomic_ops.h" | 8 | #include "common/atomic_ops.h" |
| 9 | #include "common/steady_clock.h" | ||
| 9 | #include "common/uint128.h" | 10 | #include "common/uint128.h" |
| 10 | #include "common/x64/native_clock.h" | 11 | #include "common/x64/native_clock.h" |
| 11 | 12 | ||
| @@ -39,6 +40,12 @@ static u64 FencedRDTSC() { | |||
| 39 | } | 40 | } |
| 40 | #endif | 41 | #endif |
| 41 | 42 | ||
| 43 | template <u64 Nearest> | ||
| 44 | static u64 RoundToNearest(u64 value) { | ||
| 45 | const auto mod = value % Nearest; | ||
| 46 | return mod >= (Nearest / 2) ? (value - mod + Nearest) : (value - mod); | ||
| 47 | } | ||
| 48 | |||
| 42 | u64 EstimateRDTSCFrequency() { | 49 | u64 EstimateRDTSCFrequency() { |
| 43 | // Discard the first result measuring the rdtsc. | 50 | // Discard the first result measuring the rdtsc. |
| 44 | FencedRDTSC(); | 51 | FencedRDTSC(); |
| @@ -46,18 +53,18 @@ u64 EstimateRDTSCFrequency() { | |||
| 46 | FencedRDTSC(); | 53 | FencedRDTSC(); |
| 47 | 54 | ||
| 48 | // Get the current time. | 55 | // Get the current time. |
| 49 | const auto start_time = std::chrono::steady_clock::now(); | 56 | const auto start_time = Common::SteadyClock::Now(); |
| 50 | const u64 tsc_start = FencedRDTSC(); | 57 | const u64 tsc_start = FencedRDTSC(); |
| 51 | // Wait for 200 milliseconds. | 58 | // Wait for 250 milliseconds. |
| 52 | std::this_thread::sleep_for(std::chrono::milliseconds{200}); | 59 | std::this_thread::sleep_for(std::chrono::milliseconds{250}); |
| 53 | const auto end_time = std::chrono::steady_clock::now(); | 60 | const auto end_time = Common::SteadyClock::Now(); |
| 54 | const u64 tsc_end = FencedRDTSC(); | 61 | const u64 tsc_end = FencedRDTSC(); |
| 55 | // Calculate differences. | 62 | // Calculate differences. |
| 56 | const u64 timer_diff = static_cast<u64>( | 63 | const u64 timer_diff = static_cast<u64>( |
| 57 | std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count()); | 64 | std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count()); |
| 58 | const u64 tsc_diff = tsc_end - tsc_start; | 65 | const u64 tsc_diff = tsc_end - tsc_start; |
| 59 | const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff); | 66 | const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff); |
| 60 | return tsc_freq; | 67 | return RoundToNearest<1000>(tsc_freq); |
| 61 | } | 68 | } |
| 62 | 69 | ||
| 63 | namespace X64 { | 70 | namespace X64 { |