diff options
Diffstat (limited to '')
| -rw-r--r-- | src/common/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | src/common/uint128.cpp | 41 | ||||
| -rw-r--r-- | src/common/uint128.h | 14 |
3 files changed, 57 insertions, 0 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 3d30f0e3e..c538c6415 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -114,6 +114,8 @@ add_library(common STATIC | |||
| 114 | threadsafe_queue.h | 114 | threadsafe_queue.h |
| 115 | timer.cpp | 115 | timer.cpp |
| 116 | timer.h | 116 | timer.h |
| 117 | uint128.cpp | ||
| 118 | uint128.h | ||
| 117 | vector_math.h | 119 | vector_math.h |
| 118 | web_result.h | 120 | web_result.h |
| 119 | ) | 121 | ) |
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 | ||
diff --git a/src/common/uint128.h b/src/common/uint128.h new file mode 100644 index 000000000..52e6b46eb --- /dev/null +++ b/src/common/uint128.h | |||
| @@ -0,0 +1,14 @@ | |||
| 1 | |||
| 2 | #include <utility> | ||
| 3 | #include "common/common_types.h" | ||
| 4 | |||
| 5 | namespace Common { | ||
| 6 | |||
| 7 | // This function multiplies 2 u64 values and produces a u128 value; | ||
| 8 | u128 Multiply64Into128(u64 a, u64 b); | ||
| 9 | |||
| 10 | // This function divides a u128 by a u32 value and produces two u64 values: | ||
| 11 | // the result of division and the remainder | ||
| 12 | std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor); | ||
| 13 | |||
| 14 | } // namespace Common | ||