summaryrefslogtreecommitdiffstats
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
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
-rw-r--r--src/common/CMakeLists.txt2
-rw-r--r--src/common/x64/cpu_detect.cpp3
-rw-r--r--src/common/x64/cpu_wait.cpp20
-rw-r--r--src/common/x64/rdtsc.cpp39
-rw-r--r--src/common/x64/rdtsc.h37
5 files changed, 82 insertions, 19 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index efc4a9fe9..3adf13a3f 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -172,6 +172,8 @@ if(ARCHITECTURE_x86_64)
x64/cpu_wait.h
x64/native_clock.cpp
x64/native_clock.h
+ x64/rdtsc.cpp
+ x64/rdtsc.h
x64/xbyak_abi.h
x64/xbyak_util.h
)
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp
index 72ed6e96c..c998b1197 100644
--- a/src/common/x64/cpu_detect.cpp
+++ b/src/common/x64/cpu_detect.cpp
@@ -14,6 +14,7 @@
#include "common/common_types.h"
#include "common/logging/log.h"
#include "common/x64/cpu_detect.h"
+#include "common/x64/rdtsc.h"
#ifdef _WIN32
#include <windows.h>
@@ -187,6 +188,8 @@ static CPUCaps Detect() {
caps.tsc_frequency = static_cast<u64>(caps.crystal_frequency) *
caps.tsc_crystal_ratio_numerator /
caps.tsc_crystal_ratio_denominator;
+ } else {
+ caps.tsc_frequency = X64::EstimateRDTSCFrequency();
}
}
diff --git a/src/common/x64/cpu_wait.cpp b/src/common/x64/cpu_wait.cpp
index cfeef6a3d..c53dd4945 100644
--- a/src/common/x64/cpu_wait.cpp
+++ b/src/common/x64/cpu_wait.cpp
@@ -9,19 +9,11 @@
#include "common/x64/cpu_detect.h"
#include "common/x64/cpu_wait.h"
+#include "common/x64/rdtsc.h"
namespace Common::X64 {
#ifdef _MSC_VER
-__forceinline static u64 FencedRDTSC() {
- _mm_lfence();
- _ReadWriteBarrier();
- const u64 result = __rdtsc();
- _mm_lfence();
- _ReadWriteBarrier();
- return result;
-}
-
__forceinline static void TPAUSE() {
// 100,000 cycles is a reasonable amount of time to wait to save on CPU resources.
// For reference:
@@ -32,16 +24,6 @@ __forceinline static void TPAUSE() {
_tpause(0, FencedRDTSC() + PauseCycles);
}
#else
-static u64 FencedRDTSC() {
- u64 eax;
- u64 edx;
- asm volatile("lfence\n\t"
- "rdtsc\n\t"
- "lfence\n\t"
- : "=a"(eax), "=d"(edx));
- return (edx << 32) | eax;
-}
-
static void TPAUSE() {
// 100,000 cycles is a reasonable amount of time to wait to save on CPU resources.
// For reference:
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
diff --git a/src/common/x64/rdtsc.h b/src/common/x64/rdtsc.h
new file mode 100644
index 000000000..0ec4f52f9
--- /dev/null
+++ b/src/common/x64/rdtsc.h
@@ -0,0 +1,37 @@
+// SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project
+// SPDX-License-Identifier: GPL-2.0-or-later
+
+#pragma once
+
+#ifdef _MSC_VER
+#include <intrin.h>
+#endif
+
+#include "common/common_types.h"
+
+namespace Common::X64 {
+
+#ifdef _MSC_VER
+__forceinline static u64 FencedRDTSC() {
+ _mm_lfence();
+ _ReadWriteBarrier();
+ const u64 result = __rdtsc();
+ _mm_lfence();
+ _ReadWriteBarrier();
+ return result;
+}
+#else
+static inline u64 FencedRDTSC() {
+ u64 eax;
+ u64 edx;
+ asm volatile("lfence\n\t"
+ "rdtsc\n\t"
+ "lfence\n\t"
+ : "=a"(eax), "=d"(edx));
+ return (edx << 32) | eax;
+}
+#endif
+
+u64 EstimateRDTSCFrequency();
+
+} // namespace Common::X64