summaryrefslogtreecommitdiff
path: root/src/common/uint128.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/common/uint128.h')
-rw-r--r--src/common/uint128.h89
1 files changed, 84 insertions, 5 deletions
diff --git a/src/common/uint128.h b/src/common/uint128.h
index 969259ab6..83560a9ce 100644
--- a/src/common/uint128.h
+++ b/src/common/uint128.h
@@ -4,19 +4,98 @@
4 4
5#pragma once 5#pragma once
6 6
7#include <cstring>
7#include <utility> 8#include <utility>
9
10#ifdef _MSC_VER
11#include <intrin.h>
12#pragma intrinsic(__umulh)
13#pragma intrinsic(_umul128)
14#pragma intrinsic(_udiv128)
15#else
16#include <x86intrin.h>
17#endif
18
8#include "common/common_types.h" 19#include "common/common_types.h"
9 20
10namespace Common { 21namespace Common {
11 22
12// This function multiplies 2 u64 values and divides it by a u64 value. 23// This function multiplies 2 u64 values and divides it by a u64 value.
13[[nodiscard]] u64 MultiplyAndDivide64(u64 a, u64 b, u64 d); 24[[nodiscard]] static inline u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) {
25#ifdef _MSC_VER
26 u128 r{};
27 r[0] = _umul128(a, b, &r[1]);
28 u64 remainder;
29#if _MSC_VER < 1923
30 return udiv128(r[1], r[0], d, &remainder);
31#else
32 return _udiv128(r[1], r[0], d, &remainder);
33#endif
34#else
35 const u64 diva = a / d;
36 const u64 moda = a % d;
37 const u64 divb = b / d;
38 const u64 modb = b % d;
39 return diva * b + moda * divb + moda * modb / d;
40#endif
41}
14 42
15// This function multiplies 2 u64 values and produces a u128 value; 43// This function multiplies 2 u64 values and produces a u128 value;
16[[nodiscard]] u128 Multiply64Into128(u64 a, u64 b); 44[[nodiscard]] static inline u128 Multiply64Into128(u64 a, u64 b) {
45 u128 result;
46#ifdef _MSC_VER
47 result[0] = _umul128(a, b, &result[1]);
48#else
49 unsigned __int128 tmp = a;
50 tmp *= b;
51 std::memcpy(&result, &tmp, sizeof(u128));
52#endif
53 return result;
54}
55
56[[nodiscard]] static inline u64 GetFixedPoint64Factor(u64 numerator, u64 divisor) {
57#ifdef __SIZEOF_INT128__
58 const auto base = static_cast<unsigned __int128>(numerator) << 64ULL;
59 return static_cast<u64>(base / divisor);
60#elif defined(_M_X64) || defined(_M_ARM64)
61 std::array<u64, 2> r = {0, numerator};
62 u64 remainder;
63#if _MSC_VER < 1923
64 return udiv128(r[1], r[0], divisor, &remainder);
65#else
66 return _udiv128(r[1], r[0], divisor, &remainder);
67#endif
68#else
69 // This one is bit more inaccurate.
70 return MultiplyAndDivide64(std::numeric_limits<u64>::max(), numerator, divisor);
71#endif
72}
73
74[[nodiscard]] static inline u64 MultiplyHigh(u64 a, u64 b) {
75#ifdef __SIZEOF_INT128__
76 return (static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b)) >> 64;
77#elif defined(_M_X64) || defined(_M_ARM64)
78 return __umulh(a, b); // MSVC
79#else
80 // Generic fallback
81 const u64 a_lo = u32(a);
82 const u64 a_hi = a >> 32;
83 const u64 b_lo = u32(b);
84 const u64 b_hi = b >> 32;
85
86 const u64 a_x_b_hi = a_hi * b_hi;
87 const u64 a_x_b_mid = a_hi * b_lo;
88 const u64 b_x_a_mid = b_hi * a_lo;
89 const u64 a_x_b_lo = a_lo * b_lo;
90
91 const u64 carry_bit = (static_cast<u64>(static_cast<u32>(a_x_b_mid)) +
92 static_cast<u64>(static_cast<u32>(b_x_a_mid)) + (a_x_b_lo >> 32)) >>
93 32;
94
95 const u64 multhi = a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit;
17 96
18// This function divides a u128 by a u32 value and produces two u64 values: 97 return multhi;
19// the result of division and the remainder 98#endif
20[[nodiscard]] std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor); 99}
21 100
22} // namespace Common 101} // namespace Common