diff options
| author | 2021-02-15 14:46:04 -0800 | |
|---|---|---|
| committer | 2021-02-15 14:46:04 -0800 | |
| commit | 0a91599aec5a0c4d4c87d3e23fea07dbb6ced0f7 (patch) | |
| tree | 1319cde17514c49d01045379d3a7ce2b4ccace0a | |
| parent | Merge pull request #5939 from Morph1984/web_types (diff) | |
| download | yuzu-0a91599aec5a0c4d4c87d3e23fea07dbb6ced0f7.tar.gz yuzu-0a91599aec5a0c4d4c87d3e23fea07dbb6ced0f7.tar.xz yuzu-0a91599aec5a0c4d4c87d3e23fea07dbb6ced0f7.zip | |
common: Merge uint128 to a single header file with inlines.
| -rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/common/uint128.cpp | 71 | ||||
| -rw-r--r-- | src/common/uint128.h | 89 | ||||
| -rw-r--r-- | src/common/x64/native_clock.cpp | 58 |
4 files changed, 84 insertions, 135 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 263c457cd..b657506b1 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt | |||
| @@ -168,7 +168,6 @@ add_library(common STATIC | |||
| 168 | time_zone.cpp | 168 | time_zone.cpp |
| 169 | time_zone.h | 169 | time_zone.h |
| 170 | tree.h | 170 | tree.h |
| 171 | uint128.cpp | ||
| 172 | uint128.h | 171 | uint128.h |
| 173 | uuid.cpp | 172 | uuid.cpp |
| 174 | uuid.h | 173 | uuid.h |
diff --git a/src/common/uint128.cpp b/src/common/uint128.cpp deleted file mode 100644 index 16bf7c828..000000000 --- a/src/common/uint128.cpp +++ /dev/null | |||
| @@ -1,71 +0,0 @@ | |||
| 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 | #pragma intrinsic(_udiv128) | ||
| 10 | #endif | ||
| 11 | #include <cstring> | ||
| 12 | #include "common/uint128.h" | ||
| 13 | |||
| 14 | namespace Common { | ||
| 15 | |||
| 16 | #ifdef _MSC_VER | ||
| 17 | |||
| 18 | u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) { | ||
| 19 | u128 r{}; | ||
| 20 | r[0] = _umul128(a, b, &r[1]); | ||
| 21 | u64 remainder; | ||
| 22 | #if _MSC_VER < 1923 | ||
| 23 | return udiv128(r[1], r[0], d, &remainder); | ||
| 24 | #else | ||
| 25 | return _udiv128(r[1], r[0], d, &remainder); | ||
| 26 | #endif | ||
| 27 | } | ||
| 28 | |||
| 29 | #else | ||
| 30 | |||
| 31 | u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) { | ||
| 32 | const u64 diva = a / d; | ||
| 33 | const u64 moda = a % d; | ||
| 34 | const u64 divb = b / d; | ||
| 35 | const u64 modb = b % d; | ||
| 36 | return diva * b + moda * divb + moda * modb / d; | ||
| 37 | } | ||
| 38 | |||
| 39 | #endif | ||
| 40 | |||
| 41 | u128 Multiply64Into128(u64 a, u64 b) { | ||
| 42 | u128 result; | ||
| 43 | #ifdef _MSC_VER | ||
| 44 | result[0] = _umul128(a, b, &result[1]); | ||
| 45 | #else | ||
| 46 | unsigned __int128 tmp = a; | ||
| 47 | tmp *= b; | ||
| 48 | std::memcpy(&result, &tmp, sizeof(u128)); | ||
| 49 | #endif | ||
| 50 | return result; | ||
| 51 | } | ||
| 52 | |||
| 53 | std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor) { | ||
| 54 | u64 remainder = dividend[0] % divisor; | ||
| 55 | u64 accum = dividend[0] / divisor; | ||
| 56 | if (dividend[1] == 0) | ||
| 57 | return {accum, remainder}; | ||
| 58 | // We ignore dividend[1] / divisor as that overflows | ||
| 59 | const u64 first_segment = (dividend[1] % divisor) << 32; | ||
| 60 | accum += (first_segment / divisor) << 32; | ||
| 61 | const u64 second_segment = (first_segment % divisor) << 32; | ||
| 62 | accum += (second_segment / divisor); | ||
| 63 | remainder += second_segment % divisor; | ||
| 64 | if (remainder >= divisor) { | ||
| 65 | accum++; | ||
| 66 | remainder -= divisor; | ||
| 67 | } | ||
| 68 | return {accum, remainder}; | ||
| 69 | } | ||
| 70 | |||
| 71 | } // namespace Common | ||
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 | ||
| 10 | namespace Common { | 21 | namespace 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 |
diff --git a/src/common/x64/native_clock.cpp b/src/common/x64/native_clock.cpp index a65f6b832..87de40624 100644 --- a/src/common/x64/native_clock.cpp +++ b/src/common/x64/native_clock.cpp | |||
| @@ -8,68 +8,10 @@ | |||
| 8 | #include <mutex> | 8 | #include <mutex> |
| 9 | #include <thread> | 9 | #include <thread> |
| 10 | 10 | ||
| 11 | #ifdef _MSC_VER | ||
| 12 | #include <intrin.h> | ||
| 13 | |||
| 14 | #pragma intrinsic(__umulh) | ||
| 15 | #pragma intrinsic(_udiv128) | ||
| 16 | #else | ||
| 17 | #include <x86intrin.h> | ||
| 18 | #endif | ||
| 19 | |||
| 20 | #include "common/atomic_ops.h" | 11 | #include "common/atomic_ops.h" |
| 21 | #include "common/uint128.h" | 12 | #include "common/uint128.h" |
| 22 | #include "common/x64/native_clock.h" | 13 | #include "common/x64/native_clock.h" |
| 23 | 14 | ||
| 24 | namespace { | ||
| 25 | |||
| 26 | [[nodiscard]] u64 GetFixedPoint64Factor(u64 numerator, u64 divisor) { | ||
| 27 | #ifdef __SIZEOF_INT128__ | ||
| 28 | const auto base = static_cast<unsigned __int128>(numerator) << 64ULL; | ||
| 29 | return static_cast<u64>(base / divisor); | ||
| 30 | #elif defined(_M_X64) || defined(_M_ARM64) | ||
| 31 | std::array<u64, 2> r = {0, numerator}; | ||
| 32 | u64 remainder; | ||
| 33 | #if _MSC_VER < 1923 | ||
| 34 | return udiv128(r[1], r[0], divisor, &remainder); | ||
| 35 | #else | ||
| 36 | return _udiv128(r[1], r[0], divisor, &remainder); | ||
| 37 | #endif | ||
| 38 | #else | ||
| 39 | // This one is bit more inaccurate. | ||
| 40 | return MultiplyAndDivide64(std::numeric_limits<u64>::max(), numerator, divisor); | ||
| 41 | #endif | ||
| 42 | } | ||
| 43 | |||
| 44 | [[nodiscard]] u64 MultiplyHigh(u64 a, u64 b) { | ||
| 45 | #ifdef __SIZEOF_INT128__ | ||
| 46 | return (static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b)) >> 64; | ||
| 47 | #elif defined(_M_X64) || defined(_M_ARM64) | ||
| 48 | return __umulh(a, b); // MSVC | ||
| 49 | #else | ||
| 50 | // Generic fallback | ||
| 51 | const u64 a_lo = u32(a); | ||
| 52 | const u64 a_hi = a >> 32; | ||
| 53 | const u64 b_lo = u32(b); | ||
| 54 | const u64 b_hi = b >> 32; | ||
| 55 | |||
| 56 | const u64 a_x_b_hi = a_hi * b_hi; | ||
| 57 | const u64 a_x_b_mid = a_hi * b_lo; | ||
| 58 | const u64 b_x_a_mid = b_hi * a_lo; | ||
| 59 | const u64 a_x_b_lo = a_lo * b_lo; | ||
| 60 | |||
| 61 | const u64 carry_bit = (static_cast<u64>(static_cast<u32>(a_x_b_mid)) + | ||
| 62 | static_cast<u64>(static_cast<u32>(b_x_a_mid)) + (a_x_b_lo >> 32)) >> | ||
| 63 | 32; | ||
| 64 | |||
| 65 | const u64 multhi = a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit; | ||
| 66 | |||
| 67 | return multhi; | ||
| 68 | #endif | ||
| 69 | } | ||
| 70 | |||
| 71 | } // namespace | ||
| 72 | |||
| 73 | namespace Common { | 15 | namespace Common { |
| 74 | 16 | ||
| 75 | u64 EstimateRDTSCFrequency() { | 17 | u64 EstimateRDTSCFrequency() { |