summaryrefslogtreecommitdiffstats
path: root/src/common/uint128.h
diff options
context:
space:
mode:
authorbunnei <bunneidev@gmail.com>2021-02-20 03:04:23 +0100
committerbunnei <bunneidev@gmail.com>2021-02-20 03:04:23 +0100
commit3acb265c9ee8ac26ab43361997a5db0e4b9fa47c (patch)
treea694010851607ea3aa341a7879db121236867bc7 /src/common/uint128.h
parentMerge pull request #5924 from ReinUsesLisp/inline-bindings (diff)
downloadyuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.tar
yuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.tar.gz
yuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.tar.bz2
yuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.tar.lz
yuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.tar.xz
yuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.tar.zst
yuzu-3acb265c9ee8ac26ab43361997a5db0e4b9fa47c.zip
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 {
#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 Common