summaryrefslogtreecommitdiff
path: root/src/common/uint128.h
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/common/uint128.h
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/common/uint128.h')
-rw-r--r--src/common/uint128.h20
1 files changed, 20 insertions, 0 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