summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar bunnei2021-02-19 19:11:05 -0800
committerGravatar GitHub2021-02-19 19:11:05 -0800
commitdef03d4075421d38f5cda255f2b6fb5495f57c32 (patch)
treea694010851607ea3aa341a7879db121236867bc7 /src
parentMerge pull request #5924 from ReinUsesLisp/inline-bindings (diff)
parentcommon: wall_clock: Fix integer overflow with StandardWallClock. (diff)
downloadyuzu-def03d4075421d38f5cda255f2b6fb5495f57c32.tar.gz
yuzu-def03d4075421d38f5cda255f2b6fb5495f57c32.tar.xz
yuzu-def03d4075421d38f5cda255f2b6fb5495f57c32.zip
Merge pull request #5964 from bunnei/timing-fix
common: wall_clock: Fix integer overflow with StandardWallClock.
Diffstat (limited to 'src')
-rw-r--r--src/common/uint128.h20
-rw-r--r--src/common/wall_clock.cpp15
2 files changed, 28 insertions, 7 deletions
diff --git a/src/common/uint128.h b/src/common/uint128.h
index 83560a9ce..4780b2f9d 100644
--- a/src/common/uint128.h
+++ b/src/common/uint128.h
@@ -98,4 +98,24 @@ namespace Common {
98#endif 98#endif
99} 99}
100 100
101// This function divides a u128 by a u32 value and produces two u64 values:
102// the result of division and the remainder
103[[nodiscard]] static inline std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor) {
104 u64 remainder = dividend[0] % divisor;
105 u64 accum = dividend[0] / divisor;
106 if (dividend[1] == 0)
107 return {accum, remainder};
108 // We ignore dividend[1] / divisor as that overflows
109 const u64 first_segment = (dividend[1] % divisor) << 32;
110 accum += (first_segment / divisor) << 32;
111 const u64 second_segment = (first_segment % divisor) << 32;
112 accum += (second_segment / divisor);
113 remainder += second_segment % divisor;
114 if (remainder >= divisor) {
115 accum++;
116 remainder -= divisor;
117 }
118 return {accum, remainder};
119}
120
101} // namespace Common 121} // namespace Common
diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp
index 1545993bd..49830b8ab 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>;
20class StandardWallClock final : public WallClock { 20class StandardWallClock final : public WallClock {
21public: 21public:
22 explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) 22 explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_)
23 : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false), 23 : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) {
24 emulated_clock_factor{GetFixedPoint64Factor(emulated_clock_frequency, 1000000000)},
25 emulated_cpu_factor{GetFixedPoint64Factor(emulated_cpu_frequency, 1000000000)} {
26 start_time = base_timer::now(); 24 start_time = base_timer::now();
27 } 25 }
28 26
@@ -45,11 +43,16 @@ public:
45 } 43 }
46 44
47 u64 GetClockCycles() override { 45 u64 GetClockCycles() override {
48 return MultiplyHigh(GetTimeNS().count(), emulated_clock_factor); 46 std::chrono::nanoseconds time_now = GetTimeNS();
47 const u128 temporary =
48 Common::Multiply64Into128(time_now.count(), emulated_clock_frequency);
49 return Common::Divide128On32(temporary, 1000000000).first;
49 } 50 }
50 51
51 u64 GetCPUCycles() override { 52 u64 GetCPUCycles() override {
52 return MultiplyHigh(GetTimeNS().count(), emulated_cpu_factor); 53 std::chrono::nanoseconds time_now = GetTimeNS();
54 const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency);
55 return Common::Divide128On32(temporary, 1000000000).first;
53 } 56 }
54 57
55 void Pause([[maybe_unused]] bool is_paused) override { 58 void Pause([[maybe_unused]] bool is_paused) override {
@@ -58,8 +61,6 @@ public:
58 61
59private: 62private:
60 base_time_point start_time; 63 base_time_point start_time;
61 const u64 emulated_clock_factor;
62 const u64 emulated_cpu_factor;
63}; 64};
64 65
65#ifdef ARCHITECTURE_x86_64 66#ifdef ARCHITECTURE_x86_64