diff options
Diffstat (limited to 'src/common/x64')
| -rw-r--r-- | src/common/x64/cpu_detect.cpp | 1 | ||||
| -rw-r--r-- | src/common/x64/cpu_detect.h | 1 | ||||
| -rw-r--r-- | src/common/x64/cpu_wait.cpp | 69 | ||||
| -rw-r--r-- | src/common/x64/cpu_wait.h | 10 | ||||
| -rw-r--r-- | src/common/x64/native_clock.cpp | 13 |
5 files changed, 86 insertions, 8 deletions
diff --git a/src/common/x64/cpu_detect.cpp b/src/common/x64/cpu_detect.cpp index e54383a4a..72ed6e96c 100644 --- a/src/common/x64/cpu_detect.cpp +++ b/src/common/x64/cpu_detect.cpp | |||
| @@ -144,6 +144,7 @@ static CPUCaps Detect() { | |||
| 144 | caps.bmi2 = Common::Bit<8>(cpu_id[1]); | 144 | caps.bmi2 = Common::Bit<8>(cpu_id[1]); |
| 145 | caps.sha = Common::Bit<29>(cpu_id[1]); | 145 | caps.sha = Common::Bit<29>(cpu_id[1]); |
| 146 | 146 | ||
| 147 | caps.waitpkg = Common::Bit<5>(cpu_id[2]); | ||
| 147 | caps.gfni = Common::Bit<8>(cpu_id[2]); | 148 | caps.gfni = Common::Bit<8>(cpu_id[2]); |
| 148 | 149 | ||
| 149 | __cpuidex(cpu_id, 0x00000007, 0x00000001); | 150 | __cpuidex(cpu_id, 0x00000007, 0x00000001); |
diff --git a/src/common/x64/cpu_detect.h b/src/common/x64/cpu_detect.h index ca8db19d6..8253944d6 100644 --- a/src/common/x64/cpu_detect.h +++ b/src/common/x64/cpu_detect.h | |||
| @@ -67,6 +67,7 @@ struct CPUCaps { | |||
| 67 | bool pclmulqdq : 1; | 67 | bool pclmulqdq : 1; |
| 68 | bool popcnt : 1; | 68 | bool popcnt : 1; |
| 69 | bool sha : 1; | 69 | bool sha : 1; |
| 70 | bool waitpkg : 1; | ||
| 70 | }; | 71 | }; |
| 71 | 72 | ||
| 72 | /** | 73 | /** |
diff --git a/src/common/x64/cpu_wait.cpp b/src/common/x64/cpu_wait.cpp new file mode 100644 index 000000000..cfeef6a3d --- /dev/null +++ b/src/common/x64/cpu_wait.cpp | |||
| @@ -0,0 +1,69 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #include <thread> | ||
| 5 | |||
| 6 | #ifdef _MSC_VER | ||
| 7 | #include <intrin.h> | ||
| 8 | #endif | ||
| 9 | |||
| 10 | #include "common/x64/cpu_detect.h" | ||
| 11 | #include "common/x64/cpu_wait.h" | ||
| 12 | |||
| 13 | namespace Common::X64 { | ||
| 14 | |||
| 15 | #ifdef _MSC_VER | ||
| 16 | __forceinline static u64 FencedRDTSC() { | ||
| 17 | _mm_lfence(); | ||
| 18 | _ReadWriteBarrier(); | ||
| 19 | const u64 result = __rdtsc(); | ||
| 20 | _mm_lfence(); | ||
| 21 | _ReadWriteBarrier(); | ||
| 22 | return result; | ||
| 23 | } | ||
| 24 | |||
| 25 | __forceinline static void TPAUSE() { | ||
| 26 | // 100,000 cycles is a reasonable amount of time to wait to save on CPU resources. | ||
| 27 | // For reference: | ||
| 28 | // At 1 GHz, 100K cycles is 100us | ||
| 29 | // At 2 GHz, 100K cycles is 50us | ||
| 30 | // At 4 GHz, 100K cycles is 25us | ||
| 31 | static constexpr auto PauseCycles = 100'000; | ||
| 32 | _tpause(0, FencedRDTSC() + PauseCycles); | ||
| 33 | } | ||
| 34 | #else | ||
| 35 | static u64 FencedRDTSC() { | ||
| 36 | u64 eax; | ||
| 37 | u64 edx; | ||
| 38 | asm volatile("lfence\n\t" | ||
| 39 | "rdtsc\n\t" | ||
| 40 | "lfence\n\t" | ||
| 41 | : "=a"(eax), "=d"(edx)); | ||
| 42 | return (edx << 32) | eax; | ||
| 43 | } | ||
| 44 | |||
| 45 | static void TPAUSE() { | ||
| 46 | // 100,000 cycles is a reasonable amount of time to wait to save on CPU resources. | ||
| 47 | // For reference: | ||
| 48 | // At 1 GHz, 100K cycles is 100us | ||
| 49 | // At 2 GHz, 100K cycles is 50us | ||
| 50 | // At 4 GHz, 100K cycles is 25us | ||
| 51 | static constexpr auto PauseCycles = 100'000; | ||
| 52 | const auto tsc = FencedRDTSC() + PauseCycles; | ||
| 53 | const auto eax = static_cast<u32>(tsc & 0xFFFFFFFF); | ||
| 54 | const auto edx = static_cast<u32>(tsc >> 32); | ||
| 55 | asm volatile("tpause %0" : : "r"(0), "d"(edx), "a"(eax)); | ||
| 56 | } | ||
| 57 | #endif | ||
| 58 | |||
| 59 | void MicroSleep() { | ||
| 60 | static const bool has_waitpkg = GetCPUCaps().waitpkg; | ||
| 61 | |||
| 62 | if (has_waitpkg) { | ||
| 63 | TPAUSE(); | ||
| 64 | } else { | ||
| 65 | std::this_thread::yield(); | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | } // namespace Common::X64 | ||
diff --git a/src/common/x64/cpu_wait.h b/src/common/x64/cpu_wait.h new file mode 100644 index 000000000..99d3757a7 --- /dev/null +++ b/src/common/x64/cpu_wait.h | |||
| @@ -0,0 +1,10 @@ | |||
| 1 | // SPDX-FileCopyrightText: Copyright 2023 yuzu Emulator Project | ||
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later | ||
| 3 | |||
| 4 | #pragma once | ||
| 5 | |||
| 6 | namespace Common::X64 { | ||
| 7 | |||
| 8 | void MicroSleep(); | ||
| 9 | |||
| 10 | } // namespace Common::X64 | ||
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index 76c66e7ee..277b00662 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp | |||
| @@ -27,16 +27,13 @@ __forceinline static u64 FencedRDTSC() { | |||
| 27 | } | 27 | } |
| 28 | #else | 28 | #else |
| 29 | static u64 FencedRDTSC() { | 29 | static u64 FencedRDTSC() { |
| 30 | u64 result; | 30 | u64 eax; |
| 31 | u64 edx; | ||
| 31 | asm volatile("lfence\n\t" | 32 | asm volatile("lfence\n\t" |
| 32 | "rdtsc\n\t" | 33 | "rdtsc\n\t" |
| 33 | "shl $32, %%rdx\n\t" | 34 | "lfence\n\t" |
| 34 | "or %%rdx, %0\n\t" | 35 | : "=a"(eax), "=d"(edx)); |
| 35 | "lfence" | 36 | return (edx << 32) | eax; |
| 36 | : "=a"(result) | ||
| 37 | : | ||
| 38 | : "rdx", "memory", "cc"); | ||
| 39 | return result; | ||
| 40 | } | 37 | } |
| 41 | #endif | 38 | #endif |
| 42 | 39 | ||