diff options
Diffstat (limited to 'src/common/uint128.cpp')
| -rw-r--r-- | src/common/uint128.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/src/common/uint128.cpp b/src/common/uint128.cpp new file mode 100644 index 000000000..2238a52c5 --- /dev/null +++ b/src/common/uint128.cpp | |||
| @@ -0,0 +1,41 @@ | |||
| 1 | #ifdef _MSC_VER | ||
| 2 | #include <intrin.h> | ||
| 3 | |||
| 4 | #pragma intrinsic(_umul128) | ||
| 5 | #endif | ||
| 6 | #include <cstring> | ||
| 7 | #include "common/uint128.h" | ||
| 8 | |||
| 9 | namespace Common { | ||
| 10 | |||
| 11 | u128 Multiply64Into128(u64 a, u64 b) { | ||
| 12 | u128 result; | ||
| 13 | #ifdef _MSC_VER | ||
| 14 | result[0] = _umul128(a, b, &result[1]); | ||
| 15 | #else | ||
| 16 | unsigned __int128 tmp = a; | ||
| 17 | tmp *= b; | ||
| 18 | std::memcpy(&result, &tmp, sizeof(u128)); | ||
| 19 | #endif | ||
| 20 | return result; | ||
| 21 | } | ||
| 22 | |||
| 23 | std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor) { | ||
| 24 | u64 remainder = dividend[0] % divisor; | ||
| 25 | u64 accum = dividend[0] / divisor; | ||
| 26 | if (dividend[1] == 0) | ||
| 27 | return {accum, remainder}; | ||
| 28 | // We ignore dividend[1] / divisor as that overflows | ||
| 29 | const u64 first_segment = (dividend[1] % divisor) << 32; | ||
| 30 | accum += (first_segment / divisor) << 32; | ||
| 31 | const u64 second_segment = (first_segment % divisor) << 32; | ||
| 32 | accum += (second_segment / divisor); | ||
| 33 | remainder += second_segment % divisor; | ||
| 34 | if (remainder >= divisor) { | ||
| 35 | accum++; | ||
| 36 | remainder -= divisor; | ||
| 37 | } | ||
| 38 | return {accum, remainder}; | ||
| 39 | } | ||
| 40 | |||
| 41 | } // namespace Common | ||