diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/cityhash.cpp | 178 | ||||
-rw-r--r-- | src/common/cityhash.h | 34 | ||||
-rw-r--r-- | src/common/uint128.cpp | 71 | ||||
-rw-r--r-- | src/common/uint128.h | 89 | ||||
-rw-r--r-- | src/common/wall_clock.cpp | 17 | ||||
-rw-r--r-- | src/common/x64/native_clock.cpp | 58 |
7 files changed, 191 insertions, 257 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 time_zone.cpp time_zone.h tree.h - uint128.cpp uint128.h uuid.cpp uuid.h diff --git a/src/common/cityhash.cpp b/src/common/cityhash.cpp index 4e1d874b5..66218fc21 100644 --- a/src/common/cityhash.cpp +++ b/src/common/cityhash.cpp @@ -28,8 +28,10 @@ // compromising on hash quality. #include <algorithm> -#include <string.h> // for memcpy and memset -#include "cityhash.h" +#include <cstring> +#include <utility> + +#include "common/cityhash.h" #include "common/swap.h" // #include "config.h" @@ -42,21 +44,17 @@ using namespace std; -typedef uint8_t uint8; -typedef uint32_t uint32; -typedef uint64_t uint64; - namespace Common { -static uint64 UNALIGNED_LOAD64(const char* p) { - uint64 result; - memcpy(&result, p, sizeof(result)); +static u64 unaligned_load64(const char* p) { + u64 result; + std::memcpy(&result, p, sizeof(result)); return result; } -static uint32 UNALIGNED_LOAD32(const char* p) { - uint32 result; - memcpy(&result, p, sizeof(result)); +static u32 unaligned_load32(const char* p) { + u32 result; + std::memcpy(&result, p, sizeof(result)); return result; } @@ -76,64 +74,64 @@ static uint32 UNALIGNED_LOAD32(const char* p) { #endif #endif -static uint64 Fetch64(const char* p) { - return uint64_in_expected_order(UNALIGNED_LOAD64(p)); +static u64 Fetch64(const char* p) { + return uint64_in_expected_order(unaligned_load64(p)); } -static uint32 Fetch32(const char* p) { - return uint32_in_expected_order(UNALIGNED_LOAD32(p)); +static u32 Fetch32(const char* p) { + return uint32_in_expected_order(unaligned_load32(p)); } // Some primes between 2^63 and 2^64 for various uses. -static const uint64 k0 = 0xc3a5c85c97cb3127ULL; -static const uint64 k1 = 0xb492b66fbe98f273ULL; -static const uint64 k2 = 0x9ae16a3b2f90404fULL; +static constexpr u64 k0 = 0xc3a5c85c97cb3127ULL; +static constexpr u64 k1 = 0xb492b66fbe98f273ULL; +static constexpr u64 k2 = 0x9ae16a3b2f90404fULL; // Bitwise right rotate. Normally this will compile to a single // instruction, especially if the shift is a manifest constant. -static uint64 Rotate(uint64 val, int shift) { +static u64 Rotate(u64 val, int shift) { // Avoid shifting by 64: doing so yields an undefined result. return shift == 0 ? val : ((val >> shift) | (val << (64 - shift))); } -static uint64 ShiftMix(uint64 val) { +static u64 ShiftMix(u64 val) { return val ^ (val >> 47); } -static uint64 HashLen16(uint64 u, uint64 v) { - return Hash128to64(uint128(u, v)); +static u64 HashLen16(u64 u, u64 v) { + return Hash128to64(u128{u, v}); } -static uint64 HashLen16(uint64 u, uint64 v, uint64 mul) { +static u64 HashLen16(u64 u, u64 v, u64 mul) { // Murmur-inspired hashing. - uint64 a = (u ^ v) * mul; + u64 a = (u ^ v) * mul; a ^= (a >> 47); - uint64 b = (v ^ a) * mul; + u64 b = (v ^ a) * mul; b ^= (b >> 47); b *= mul; return b; } -static uint64 HashLen0to16(const char* s, std::size_t len) { +static u64 HashLen0to16(const char* s, size_t len) { if (len >= 8) { - uint64 mul = k2 + len * 2; - uint64 a = Fetch64(s) + k2; - uint64 b = Fetch64(s + len - 8); - uint64 c = Rotate(b, 37) * mul + a; - uint64 d = (Rotate(a, 25) + b) * mul; + u64 mul = k2 + len * 2; + u64 a = Fetch64(s) + k2; + u64 b = Fetch64(s + len - 8); + u64 c = Rotate(b, 37) * mul + a; + u64 d = (Rotate(a, 25) + b) * mul; return HashLen16(c, d, mul); } if (len >= 4) { - uint64 mul = k2 + len * 2; - uint64 a = Fetch32(s); + u64 mul = k2 + len * 2; + u64 a = Fetch32(s); return HashLen16(len + (a << 3), Fetch32(s + len - 4), mul); } if (len > 0) { - uint8 a = s[0]; - uint8 b = s[len >> 1]; - uint8 c = s[len - 1]; - uint32 y = static_cast<uint32>(a) + (static_cast<uint32>(b) << 8); - uint32 z = static_cast<uint32>(len) + (static_cast<uint32>(c) << 2); + u8 a = s[0]; + u8 b = s[len >> 1]; + u8 c = s[len - 1]; + u32 y = static_cast<u32>(a) + (static_cast<u32>(b) << 8); + u32 z = static_cast<u32>(len) + (static_cast<u32>(c) << 2); return ShiftMix(y * k2 ^ z * k0) * k2; } return k2; @@ -141,22 +139,21 @@ static uint64 HashLen0to16(const char* s, std::size_t len) { // This probably works well for 16-byte strings as well, but it may be overkill // in that case. -static uint64 HashLen17to32(const char* s, std::size_t len) { - uint64 mul = k2 + len * 2; - uint64 a = Fetch64(s) * k1; - uint64 b = Fetch64(s + 8); - uint64 c = Fetch64(s + len - 8) * mul; - uint64 d = Fetch64(s + len - 16) * k2; +static u64 HashLen17to32(const char* s, size_t len) { + u64 mul = k2 + len * 2; + u64 a = Fetch64(s) * k1; + u64 b = Fetch64(s + 8); + u64 c = Fetch64(s + len - 8) * mul; + u64 d = Fetch64(s + len - 16) * k2; return HashLen16(Rotate(a + b, 43) + Rotate(c, 30) + d, a + Rotate(b + k2, 18) + c, mul); } // Return a 16-byte hash for 48 bytes. Quick and dirty. // Callers do best to use "random-looking" values for a and b. -static pair<uint64, uint64> WeakHashLen32WithSeeds(uint64 w, uint64 x, uint64 y, uint64 z, uint64 a, - uint64 b) { +static pair<u64, u64> WeakHashLen32WithSeeds(u64 w, u64 x, u64 y, u64 z, u64 a, u64 b) { a += w; b = Rotate(b + a + z, 21); - uint64 c = a; + u64 c = a; a += x; a += y; b += Rotate(a, 44); @@ -164,34 +161,34 @@ static pair<uint64, uint64> WeakHashLen32WithSeeds(uint64 w, uint64 x, uint64 y, } // Return a 16-byte hash for s[0] ... s[31], a, and b. Quick and dirty. -static pair<uint64, uint64> WeakHashLen32WithSeeds(const char* s, uint64 a, uint64 b) { +static pair<u64, u64> WeakHashLen32WithSeeds(const char* s, u64 a, u64 b) { return WeakHashLen32WithSeeds(Fetch64(s), Fetch64(s + 8), Fetch64(s + 16), Fetch64(s + 24), a, b); } // Return an 8-byte hash for 33 to 64 bytes. -static uint64 HashLen33to64(const char* s, std::size_t len) { - uint64 mul = k2 + len * 2; - uint64 a = Fetch64(s) * k2; - uint64 b = Fetch64(s + 8); - uint64 c = Fetch64(s + len - 24); - uint64 d = Fetch64(s + len - 32); - uint64 e = Fetch64(s + 16) * k2; - uint64 f = Fetch64(s + 24) * 9; - uint64 g = Fetch64(s + len - 8); - uint64 h = Fetch64(s + len - 16) * mul; - uint64 u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9; - uint64 v = ((a + g) ^ d) + f + 1; - uint64 w = swap64((u + v) * mul) + h; - uint64 x = Rotate(e + f, 42) + c; - uint64 y = (swap64((v + w) * mul) + g) * mul; - uint64 z = e + f + c; +static u64 HashLen33to64(const char* s, size_t len) { + u64 mul = k2 + len * 2; + u64 a = Fetch64(s) * k2; + u64 b = Fetch64(s + 8); + u64 c = Fetch64(s + len - 24); + u64 d = Fetch64(s + len - 32); + u64 e = Fetch64(s + 16) * k2; + u64 f = Fetch64(s + 24) * 9; + u64 g = Fetch64(s + len - 8); + u64 h = Fetch64(s + len - 16) * mul; + u64 u = Rotate(a + g, 43) + (Rotate(b, 30) + c) * 9; + u64 v = ((a + g) ^ d) + f + 1; + u64 w = swap64((u + v) * mul) + h; + u64 x = Rotate(e + f, 42) + c; + u64 y = (swap64((v + w) * mul) + g) * mul; + u64 z = e + f + c; a = swap64((x + z) * mul + y) + b; b = ShiftMix((z + a) * mul + d + h) * mul; return b + x; } -uint64 CityHash64(const char* s, std::size_t len) { +u64 CityHash64(const char* s, size_t len) { if (len <= 32) { if (len <= 16) { return HashLen0to16(s, len); @@ -204,15 +201,15 @@ uint64 CityHash64(const char* s, std::size_t len) { // For strings over 64 bytes we hash the end first, and then as we // loop we keep 56 bytes of state: v, w, x, y, and z. - uint64 x = Fetch64(s + len - 40); - uint64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56); - uint64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24)); - pair<uint64, uint64> v = WeakHashLen32WithSeeds(s + len - 64, len, z); - pair<uint64, uint64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x); + u64 x = Fetch64(s + len - 40); + u64 y = Fetch64(s + len - 16) + Fetch64(s + len - 56); + u64 z = HashLen16(Fetch64(s + len - 48) + len, Fetch64(s + len - 24)); + pair<u64, u64> v = WeakHashLen32WithSeeds(s + len - 64, len, z); + pair<u64, u64> w = WeakHashLen32WithSeeds(s + len - 32, y + k1, x); x = x * k1 + Fetch64(s); // Decrease len to the nearest multiple of 64, and operate on 64-byte chunks. - len = (len - 1) & ~static_cast<std::size_t>(63); + len = (len - 1) & ~static_cast<size_t>(63); do { x = Rotate(x + y + v.first + Fetch64(s + 8), 37) * k1; y = Rotate(y + v.second + Fetch64(s + 48), 42) * k1; @@ -229,21 +226,21 @@ uint64 CityHash64(const char* s, std::size_t len) { HashLen16(v.second, w.second) + x); } -uint64 CityHash64WithSeed(const char* s, std::size_t len, uint64 seed) { +u64 CityHash64WithSeed(const char* s, size_t len, u64 seed) { return CityHash64WithSeeds(s, len, k2, seed); } -uint64 CityHash64WithSeeds(const char* s, std::size_t len, uint64 seed0, uint64 seed1) { +u64 CityHash64WithSeeds(const char* s, size_t len, u64 seed0, u64 seed1) { return HashLen16(CityHash64(s, len) - seed0, seed1); } // A subroutine for CityHash128(). Returns a decent 128-bit hash for strings // of any length representable in signed long. Based on City and Murmur. -static uint128 CityMurmur(const char* s, std::size_t len, uint128 seed) { - uint64 a = Uint128Low64(seed); - uint64 b = Uint128High64(seed); - uint64 c = 0; - uint64 d = 0; +static u128 CityMurmur(const char* s, size_t len, u128 seed) { + u64 a = seed[0]; + u64 b = seed[1]; + u64 c = 0; + u64 d = 0; signed long l = static_cast<long>(len) - 16; if (l <= 0) { // len <= 16 a = ShiftMix(a * k1) * k1; @@ -266,20 +263,20 @@ static uint128 CityMurmur(const char* s, std::size_t len, uint128 seed) { } a = HashLen16(a, c); b = HashLen16(d, b); - return uint128(a ^ b, HashLen16(b, a)); + return u128{a ^ b, HashLen16(b, a)}; } -uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed) { +u128 CityHash128WithSeed(const char* s, size_t len, u128 seed) { if (len < 128) { return CityMurmur(s, len, seed); } // We expect len >= 128 to be the common case. Keep 56 bytes of state: // v, w, x, y, and z. - pair<uint64, uint64> v, w; - uint64 x = Uint128Low64(seed); - uint64 y = Uint128High64(seed); - uint64 z = len * k1; + pair<u64, u64> v, w; + u64 x = seed[0]; + u64 y = seed[1]; + u64 z = len * k1; v.first = Rotate(y ^ k1, 49) * k1 + Fetch64(s); v.second = Rotate(v.first, 42) * k1 + Fetch64(s + 8); w.first = Rotate(y + z, 35) * k1 + x; @@ -313,7 +310,7 @@ uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed) { w.first *= 9; v.first *= k0; // If 0 < len < 128, hash up to 4 chunks of 32 bytes each from the end of s. - for (std::size_t tail_done = 0; tail_done < len;) { + for (size_t tail_done = 0; tail_done < len;) { tail_done += 32; y = Rotate(x + y, 42) * k0 + v.second; w.first += Fetch64(s + len - tail_done + 16); @@ -328,13 +325,12 @@ uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed) { // different 56-byte-to-8-byte hashes to get a 16-byte final result. x = HashLen16(x, v.first); y = HashLen16(y + z, w.first); - return uint128(HashLen16(x + v.second, w.second) + y, HashLen16(x + w.second, y + v.second)); + return u128{HashLen16(x + v.second, w.second) + y, HashLen16(x + w.second, y + v.second)}; } -uint128 CityHash128(const char* s, std::size_t len) { - return len >= 16 - ? CityHash128WithSeed(s + 16, len - 16, uint128(Fetch64(s), Fetch64(s + 8) + k0)) - : CityHash128WithSeed(s, len, uint128(k0, k1)); +u128 CityHash128(const char* s, size_t len) { + return len >= 16 ? CityHash128WithSeed(s + 16, len - 16, u128{Fetch64(s), Fetch64(s + 8) + k0}) + : CityHash128WithSeed(s, len, u128{k0, k1}); } } // namespace Common diff --git a/src/common/cityhash.h b/src/common/cityhash.h index a00804e01..022d0f7cb 100644 --- a/src/common/cityhash.h +++ b/src/common/cityhash.h @@ -61,50 +61,38 @@ #pragma once -#include <cstddef> -#include <cstdint> -#include <utility> +#include "common/common_types.h" namespace Common { -using uint128 = std::pair<uint64_t, uint64_t>; - -[[nodiscard]] inline uint64_t Uint128Low64(const uint128& x) { - return x.first; -} -[[nodiscard]] inline uint64_t Uint128High64(const uint128& x) { - return x.second; -} - // Hash function for a byte array. -[[nodiscard]] uint64_t CityHash64(const char* buf, std::size_t len); +[[nodiscard]] u64 CityHash64(const char* buf, size_t len); // Hash function for a byte array. For convenience, a 64-bit seed is also // hashed into the result. -[[nodiscard]] uint64_t CityHash64WithSeed(const char* buf, std::size_t len, uint64_t seed); +[[nodiscard]] u64 CityHash64WithSeed(const char* buf, size_t len, u64 seed); // Hash function for a byte array. For convenience, two seeds are also // hashed into the result. -[[nodiscard]] uint64_t CityHash64WithSeeds(const char* buf, std::size_t len, uint64_t seed0, - uint64_t seed1); +[[nodiscard]] u64 CityHash64WithSeeds(const char* buf, size_t len, u64 seed0, u64 seed1); // Hash function for a byte array. -[[nodiscard]] uint128 CityHash128(const char* s, std::size_t len); +[[nodiscard]] u128 CityHash128(const char* s, size_t len); // Hash function for a byte array. For convenience, a 128-bit seed is also // hashed into the result. -[[nodiscard]] uint128 CityHash128WithSeed(const char* s, std::size_t len, uint128 seed); +[[nodiscard]] u128 CityHash128WithSeed(const char* s, size_t len, u128 seed); // Hash 128 input bits down to 64 bits of output. // This is intended to be a reasonably good hash function. -[[nodiscard]] inline uint64_t Hash128to64(const uint128& x) { +[[nodiscard]] inline u64 Hash128to64(const u128& x) { // Murmur-inspired hashing. - const uint64_t kMul = 0x9ddfea08eb382d69ULL; - uint64_t a = (Uint128Low64(x) ^ Uint128High64(x)) * kMul; + const u64 mul = 0x9ddfea08eb382d69ULL; + u64 a = (x[0] ^ x[1]) * mul; a ^= (a >> 47); - uint64_t b = (Uint128High64(x) ^ a) * kMul; + u64 b = (x[1] ^ a) * mul; b ^= (b >> 47); - b *= kMul; + b *= mul; return b; } 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 @@ -// Copyright 2019 yuzu Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#ifdef _MSC_VER -#include <intrin.h> - -#pragma intrinsic(_umul128) -#pragma intrinsic(_udiv128) -#endif -#include <cstring> -#include "common/uint128.h" - -namespace Common { - -#ifdef _MSC_VER - -u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) { - u128 r{}; - r[0] = _umul128(a, b, &r[1]); - u64 remainder; -#if _MSC_VER < 1923 - return udiv128(r[1], r[0], d, &remainder); -#else - return _udiv128(r[1], r[0], d, &remainder); -#endif -} - -#else - -u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) { - const u64 diva = a / d; - const u64 moda = a % d; - const u64 divb = b / d; - const u64 modb = b % d; - return diva * b + moda * divb + moda * modb / d; -} - -#endif - -u128 Multiply64Into128(u64 a, u64 b) { - u128 result; -#ifdef _MSC_VER - result[0] = _umul128(a, b, &result[1]); -#else - unsigned __int128 tmp = a; - tmp *= b; - std::memcpy(&result, &tmp, sizeof(u128)); -#endif - return result; -} - -std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor) { - u64 remainder = dividend[0] % divisor; - u64 accum = dividend[0] / divisor; - if (dividend[1] == 0) - return {accum, remainder}; - // We ignore dividend[1] / divisor as that overflows - const u64 first_segment = (dividend[1] % divisor) << 32; - accum += (first_segment / divisor) << 32; - const u64 second_segment = (first_segment % divisor) << 32; - accum += (second_segment / divisor); - remainder += second_segment % divisor; - if (remainder >= divisor) { - accum++; - remainder -= divisor; - } - return {accum, remainder}; -} - -} // 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 @@ #pragma once +#include <cstring> #include <utility> + +#ifdef _MSC_VER +#include <intrin.h> +#pragma intrinsic(__umulh) +#pragma intrinsic(_umul128) +#pragma intrinsic(_udiv128) +#else +#include <x86intrin.h> +#endif + #include "common/common_types.h" namespace Common { // This function multiplies 2 u64 values and divides it by a u64 value. -[[nodiscard]] u64 MultiplyAndDivide64(u64 a, u64 b, u64 d); +[[nodiscard]] static inline u64 MultiplyAndDivide64(u64 a, u64 b, u64 d) { +#ifdef _MSC_VER + u128 r{}; + r[0] = _umul128(a, b, &r[1]); + u64 remainder; +#if _MSC_VER < 1923 + return udiv128(r[1], r[0], d, &remainder); +#else + return _udiv128(r[1], r[0], d, &remainder); +#endif +#else + const u64 diva = a / d; + const u64 moda = a % d; + const u64 divb = b / d; + const u64 modb = b % d; + return diva * b + moda * divb + moda * modb / d; +#endif +} // This function multiplies 2 u64 values and produces a u128 value; -[[nodiscard]] u128 Multiply64Into128(u64 a, u64 b); +[[nodiscard]] static inline u128 Multiply64Into128(u64 a, u64 b) { + u128 result; +#ifdef _MSC_VER + result[0] = _umul128(a, b, &result[1]); +#else + unsigned __int128 tmp = a; + tmp *= b; + std::memcpy(&result, &tmp, sizeof(u128)); +#endif + return result; +} + +[[nodiscard]] static inline u64 GetFixedPoint64Factor(u64 numerator, u64 divisor) { +#ifdef __SIZEOF_INT128__ + const auto base = static_cast<unsigned __int128>(numerator) << 64ULL; + return static_cast<u64>(base / divisor); +#elif defined(_M_X64) || defined(_M_ARM64) + std::array<u64, 2> r = {0, numerator}; + u64 remainder; +#if _MSC_VER < 1923 + return udiv128(r[1], r[0], divisor, &remainder); +#else + return _udiv128(r[1], r[0], divisor, &remainder); +#endif +#else + // This one is bit more inaccurate. + return MultiplyAndDivide64(std::numeric_limits<u64>::max(), numerator, divisor); +#endif +} + +[[nodiscard]] static inline u64 MultiplyHigh(u64 a, u64 b) { +#ifdef __SIZEOF_INT128__ + return (static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b)) >> 64; +#elif defined(_M_X64) || defined(_M_ARM64) + return __umulh(a, b); // MSVC +#else + // Generic fallback + const u64 a_lo = u32(a); + const u64 a_hi = a >> 32; + const u64 b_lo = u32(b); + const u64 b_hi = b >> 32; + + const u64 a_x_b_hi = a_hi * b_hi; + const u64 a_x_b_mid = a_hi * b_lo; + const u64 b_x_a_mid = b_hi * a_lo; + const u64 a_x_b_lo = a_lo * b_lo; + + const u64 carry_bit = (static_cast<u64>(static_cast<u32>(a_x_b_mid)) + + static_cast<u64>(static_cast<u32>(b_x_a_mid)) + (a_x_b_lo >> 32)) >> + 32; + + const u64 multhi = a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit; -// This function divides a u128 by a u32 value and produces two u64 values: -// the result of division and the remainder -[[nodiscard]] std::pair<u64, u64> Divide128On32(u128 dividend, u32 divisor); + return multhi; +#endif +} } // namespace Common diff --git a/src/common/wall_clock.cpp b/src/common/wall_clock.cpp index a8c143f85..1545993bd 100644 --- a/src/common/wall_clock.cpp +++ b/src/common/wall_clock.cpp @@ -2,6 +2,8 @@ // Licensed under GPLv2 or any later version // Refer to the license.txt file included. +#include <cstdint> + #include "common/uint128.h" #include "common/wall_clock.h" @@ -18,7 +20,9 @@ using base_time_point = std::chrono::time_point<base_timer>; class StandardWallClock final : public WallClock { public: explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) - : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) { + : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false), + emulated_clock_factor{GetFixedPoint64Factor(emulated_clock_frequency, 1000000000)}, + emulated_cpu_factor{GetFixedPoint64Factor(emulated_cpu_frequency, 1000000000)} { start_time = base_timer::now(); } @@ -41,16 +45,11 @@ public: } u64 GetClockCycles() override { - std::chrono::nanoseconds time_now = GetTimeNS(); - const u128 temporary = - Common::Multiply64Into128(time_now.count(), emulated_clock_frequency); - return Common::Divide128On32(temporary, 1000000000).first; + return MultiplyHigh(GetTimeNS().count(), emulated_clock_factor); } u64 GetCPUCycles() override { - std::chrono::nanoseconds time_now = GetTimeNS(); - const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency); - return Common::Divide128On32(temporary, 1000000000).first; + return MultiplyHigh(GetTimeNS().count(), emulated_cpu_factor); } void Pause([[maybe_unused]] bool is_paused) override { @@ -59,6 +58,8 @@ public: private: base_time_point start_time; + const u64 emulated_clock_factor; + const u64 emulated_cpu_factor; }; #ifdef ARCHITECTURE_x86_64 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 @@ #include <mutex> #include <thread> -#ifdef _MSC_VER -#include <intrin.h> - -#pragma intrinsic(__umulh) -#pragma intrinsic(_udiv128) -#else -#include <x86intrin.h> -#endif - #include "common/atomic_ops.h" #include "common/uint128.h" #include "common/x64/native_clock.h" -namespace { - -[[nodiscard]] u64 GetFixedPoint64Factor(u64 numerator, u64 divisor) { -#ifdef __SIZEOF_INT128__ - const auto base = static_cast<unsigned __int128>(numerator) << 64ULL; - return static_cast<u64>(base / divisor); -#elif defined(_M_X64) || defined(_M_ARM64) - std::array<u64, 2> r = {0, numerator}; - u64 remainder; -#if _MSC_VER < 1923 - return udiv128(r[1], r[0], divisor, &remainder); -#else - return _udiv128(r[1], r[0], divisor, &remainder); -#endif -#else - // This one is bit more inaccurate. - return MultiplyAndDivide64(std::numeric_limits<u64>::max(), numerator, divisor); -#endif -} - -[[nodiscard]] u64 MultiplyHigh(u64 a, u64 b) { -#ifdef __SIZEOF_INT128__ - return (static_cast<unsigned __int128>(a) * static_cast<unsigned __int128>(b)) >> 64; -#elif defined(_M_X64) || defined(_M_ARM64) - return __umulh(a, b); // MSVC -#else - // Generic fallback - const u64 a_lo = u32(a); - const u64 a_hi = a >> 32; - const u64 b_lo = u32(b); - const u64 b_hi = b >> 32; - - const u64 a_x_b_hi = a_hi * b_hi; - const u64 a_x_b_mid = a_hi * b_lo; - const u64 b_x_a_mid = b_hi * a_lo; - const u64 a_x_b_lo = a_lo * b_lo; - - const u64 carry_bit = (static_cast<u64>(static_cast<u32>(a_x_b_mid)) + - static_cast<u64>(static_cast<u32>(b_x_a_mid)) + (a_x_b_lo >> 32)) >> - 32; - - const u64 multhi = a_x_b_hi + (a_x_b_mid >> 32) + (b_x_a_mid >> 32) + carry_bit; - - return multhi; -#endif -} - -} // namespace - namespace Common { u64 EstimateRDTSCFrequency() { |