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