summaryrefslogtreecommitdiffstats
path: root/src/common/x64/rdtsc.cpp
diff options
context:
space:
mode:
authorMorph <39850852+Morph1984@users.noreply.github.com>2023-04-23 04:55:03 +0200
committerMorph <39850852+Morph1984@users.noreply.github.com>2023-06-08 03:44:42 +0200
commitdd12dd4c67dd4bc6e7a7d071b925afc38e687f8b (patch)
tree82538e7a48fa6fb6a9a5d51aef925586195c21ed /src/common/x64/rdtsc.cpp
parentMerge pull request #10650 from qurious-pixel/android_tv (diff)
downloadyuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.tar
yuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.tar.gz
yuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.tar.bz2
yuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.tar.lz
yuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.tar.xz
yuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.tar.zst
yuzu-dd12dd4c67dd4bc6e7a7d071b925afc38e687f8b.zip
Diffstat (limited to 'src/common/x64/rdtsc.cpp')
-rw-r--r--src/common/x64/rdtsc.cpp39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/common/x64/rdtsc.cpp b/src/common/x64/rdtsc.cpp
new file mode 100644
index 000000000..9273274a3
--- /dev/null
+++ b/src/common/x64/rdtsc.cpp
@@ -0,0 +1,39 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#include <thread>
+
+#include "common/steady_clock.h"
+#include "common/uint128.h"
+#include "common/x64/rdtsc.h"
+
+namespace Common::X64 {
+
+template <u64 Nearest>
+static u64 RoundToNearest(u64 value) {
+ const auto mod = value % Nearest;
+ return mod >= (Nearest / 2) ? (value - mod + Nearest) : (value - mod);
+}
+
+u64 EstimateRDTSCFrequency() {
+ // Discard the first result measuring the rdtsc.
+ FencedRDTSC();
+ std::this_thread::sleep_for(std::chrono::milliseconds{1});
+ FencedRDTSC();
+
+ // Get the current time.
+ const auto start_time = RealTimeClock::Now();
+ const u64 tsc_start = FencedRDTSC();
+ // Wait for 100 milliseconds.
+ std::this_thread::sleep_for(std::chrono::milliseconds{100});
+ const auto end_time = RealTimeClock::Now();
+ const u64 tsc_end = FencedRDTSC();
+ // Calculate differences.
+ const u64 timer_diff = static_cast<u64>(
+ std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count());
+ const u64 tsc_diff = tsc_end - tsc_start;
+ const u64 tsc_freq = MultiplyAndDivide64(tsc_diff, 1000000000ULL, timer_diff);
+ return RoundToNearest<100'000>(tsc_freq);
+}
+
+} // namespace Common::X64