summaryrefslogtreecommitdiff
path: root/src/common/x64/rdtsc.h
diff options
context:
space:
mode:
authorMorph <39850852+Morph1984@users.noreply.github.com>2023-04-22 22:55:03 -0400
committerMorph <39850852+Morph1984@users.noreply.github.com>2023-06-07 21:44:42 -0400
commitdd12dd4c67dd4bc6e7a7d071b925afc38e687f8b (patch)
tree82538e7a48fa6fb6a9a5d51aef925586195c21ed /src/common/x64/rdtsc.h
parent9c6fc44a5904bfb158a08407958954ac101a6aaf (diff)
x64: Deduplicate RDTSC usage
Diffstat (limited to 'src/common/x64/rdtsc.h')
-rw-r--r--src/common/x64/rdtsc.h37
1 files changed, 37 insertions, 0 deletions
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