diff options
author | bunnei <bunneidev@gmail.com> | 2021-02-19 18:04:23 -0800 |
---|---|---|
committer | bunnei <bunneidev@gmail.com> | 2021-02-19 18:04:23 -0800 |
commit | 3acb265c9ee8ac26ab43361997a5db0e4b9fa47c (patch) | |
tree | a694010851607ea3aa341a7879db121236867bc7 /src/common/wall_clock.cpp | |
parent | 728ee181ebdabf61ef7d4d9bd688cdfde4a9eaff (diff) |
common: wall_clock: Fix integer overflow with StandardWallClock.
- Previous optimized impl. resulted in an integer overflow, so revert.
- This is our slow/fallback path that should never be really be used, so the optimization in unimportant.
Diffstat (limited to 'src/common/wall_clock.cpp')
-rw-r--r-- | src/common/wall_clock.cpp | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index 1545993bd7..49830b8ab6 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -20,9 +20,7 @@ using base_time_point = std::chrono::time_point<base_timer>; class StandardWallClock final : public WallClock { public: explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) - : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false), - emulated_clock_factor{GetFixedPoint64Factor(emulated_clock_frequency, 1000000000)}, - emulated_cpu_factor{GetFixedPoint64Factor(emulated_cpu_frequency, 1000000000)} { + : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) { start_time = base_timer::now(); } @@ -45,11 +43,16 @@ public: } u64 GetClockCycles() override { - return MultiplyHigh(GetTimeNS().count(), emulated_clock_factor); + std::chrono::nanoseconds time_now = GetTimeNS(); + const u128 temporary = + Common::Multiply64Into128(time_now.count(), emulated_clock_frequency); + return Common::Divide128On32(temporary, 1000000000).first; } u64 GetCPUCycles() override { - return MultiplyHigh(GetTimeNS().count(), emulated_cpu_factor); + std::chrono::nanoseconds time_now = GetTimeNS(); + const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency); + return Common::Divide128On32(temporary, 1000000000).first; } void Pause([[maybe_unused]] bool is_paused) override { @@ -58,8 +61,6 @@ public: private: base_time_point start_time; - const u64 emulated_clock_factor; - const u64 emulated_cpu_factor; }; #ifdef ARCHITECTURE_x86_64 |