diff options
Diffstat (limited to 'src/common/wall_clock.cpp')
| -rw-r--r-- | src/common/wall_clock.cpp | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp new file mode 100644 index 000000000..d4d35f4e7 --- /dev/null +++ b/src/common/wall_clock.cpp | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | // Copyright 2020 yuzu Emulator Project | ||
| 2 | // Licensed under GPLv2 or any later version | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "common/uint128.h" | ||
| 6 | #include "common/wall_clock.h" | ||
| 7 | |||
| 8 | #ifdef ARCHITECTURE_x86_64 | ||
| 9 | #include "common/x64/cpu_detect.h" | ||
| 10 | #include "common/x64/native_clock.h" | ||
| 11 | #endif | ||
| 12 | |||
| 13 | namespace Common { | ||
| 14 | |||
| 15 | using base_timer = std::chrono::steady_clock; | ||
| 16 | using base_time_point = std::chrono::time_point<base_timer>; | ||
| 17 | |||
| 18 | class StandardWallClock : public WallClock { | ||
| 19 | public: | ||
| 20 | StandardWallClock(u64 emulated_cpu_frequency, u64 emulated_clock_frequency) | ||
| 21 | : WallClock(emulated_cpu_frequency, emulated_clock_frequency, false) { | ||
| 22 | start_time = base_timer::now(); | ||
| 23 | } | ||
| 24 | |||
| 25 | std::chrono::nanoseconds GetTimeNS() override { | ||
| 26 | base_time_point current = base_timer::now(); | ||
| 27 | auto elapsed = current - start_time; | ||
| 28 | return std::chrono::duration_cast<std::chrono::nanoseconds>(elapsed); | ||
| 29 | } | ||
| 30 | |||
| 31 | std::chrono::microseconds GetTimeUS() override { | ||
| 32 | base_time_point current = base_timer::now(); | ||
| 33 | auto elapsed = current - start_time; | ||
| 34 | return std::chrono::duration_cast<std::chrono::microseconds>(elapsed); | ||
| 35 | } | ||
| 36 | |||
| 37 | std::chrono::milliseconds GetTimeMS() override { | ||
| 38 | base_time_point current = base_timer::now(); | ||
| 39 | auto elapsed = current - start_time; | ||
| 40 | return std::chrono::duration_cast<std::chrono::milliseconds>(elapsed); | ||
| 41 | } | ||
| 42 | |||
| 43 | u64 GetClockCycles() override { | ||
| 44 | std::chrono::nanoseconds time_now = GetTimeNS(); | ||
| 45 | const u128 temporary = | ||
| 46 | Common::Multiply64Into128(time_now.count(), emulated_clock_frequency); | ||
| 47 | return Common::Divide128On32(temporary, 1000000000).first; | ||
| 48 | } | ||
| 49 | |||
| 50 | u64 GetCPUCycles() override { | ||
| 51 | std::chrono::nanoseconds time_now = GetTimeNS(); | ||
| 52 | const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency); | ||
| 53 | return Common::Divide128On32(temporary, 1000000000).first; | ||
| 54 | } | ||
| 55 | |||
| 56 | private: | ||
| 57 | base_time_point start_time; | ||
| 58 | }; | ||
| 59 | |||
| 60 | #ifdef ARCHITECTURE_x86_64 | ||
| 61 | |||
| 62 | std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, | ||
| 63 | u32 emulated_clock_frequency) { | ||
| 64 | const auto& caps = GetCPUCaps(); | ||
| 65 | u64 rtsc_frequency = 0; | ||
| 66 | if (caps.invariant_tsc) { | ||
| 67 | if (caps.base_frequency != 0) { | ||
| 68 | rtsc_frequency = static_cast<u64>(caps.base_frequency) * 1000000U; | ||
| 69 | } | ||
| 70 | if (rtsc_frequency == 0) { | ||
| 71 | rtsc_frequency = EstimateRDTSCFrequency(); | ||
| 72 | } | ||
| 73 | } | ||
| 74 | if (rtsc_frequency == 0) { | ||
| 75 | return std::make_unique<StandardWallClock>(emulated_cpu_frequency, | ||
| 76 | emulated_clock_frequency); | ||
| 77 | } else { | ||
| 78 | return std::make_unique<X64::NativeClock>(emulated_cpu_frequency, emulated_clock_frequency, | ||
| 79 | rtsc_frequency); | ||
| 80 | } | ||
| 81 | } | ||
| 82 | |||
| 83 | #else | ||
| 84 | |||
| 85 | std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency, | ||
| 86 | u32 emulated_clock_frequency) { | ||
| 87 | return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency); | ||
| 88 | } | ||
| 89 | |||
| 90 | #endif | ||
| 91 | |||
| 92 | } // namespace Common | ||