VERY experimental Nintendo Switch 2 emulator
C++
common: wall_clock: Fix integer overflow with StandardWallClock.
bunnei82eb7d8c157e
diff --git a/src/common/uint128.h b/src/common/uint128.hindex 83560a9cee..4780b2f9d3 100644--- a/src/common/uint128.h+++ b/src/common/uint128.h@@ -98,4 +98,24 @@ namespace Common {#endif}+// This function divides a u128 by a u32 value and produces two u64 values:+// the result of division and the remainder+[[nodiscard]] static inline std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor) {+ u64 remainder = dividend[0] % divisor;+ u64 accum = dividend[0] / divisor;+ if (dividend[1] == 0)+ return {accum, remainder};+ // We ignore dividend[1] / divisor as that overflows+ const u64 first_segment = (dividend[1] % divisor) << 32;+ accum += (first_segment / divisor) << 32;+ const u64 second_segment = (first_segment % divisor) << 32;+ accum += (second_segment / divisor);+ remainder += second_segment % divisor;+ if (remainder >= divisor) {+ accum++;+ remainder -= divisor;+ }+ return {accum, remainder};+}+} // namespace Commondiff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cppindex 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