summaryrefslogtreecommitdiffstats
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt4
-rw-r--r--src/common/alignment.h5
-rw-r--r--src/common/cityhash.cpp178
-rw-r--r--src/common/cityhash.h33
-rw-r--r--src/common/string_util.cpp14
-rw-r--r--src/common/tiny_mt.h250
-rw-r--r--src/common/uint128.cpp71
-rw-r--r--src/common/uint128.h105
-rw-r--r--src/common/wall_clock.cpp2
-rw-r--r--src/common/x64/native_clock.cpp58
10 files changed, 460 insertions, 260 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index bfd11e76d..788516ded 100644
--- a/src/common/CMakeLists.txt
+++ b/src/common/CMakeLists.txt
@@ -167,8 +167,8 @@ add_library(common STATIC
threadsafe_queue.h
time_zone.cpp
time_zone.h
+ tiny_mt.h
tree.h
- uint128.cpp
uint128.h
uuid.cpp
uuid.h
@@ -206,6 +206,8 @@ if (MSVC)
else()
target_compile_options(common PRIVATE
-Werror
+
+ $<$<CXX_COMPILER_ID:Clang>:-fsized-deallocation>
)
endif()
diff --git a/src/common/alignment.h b/src/common/alignment.h
index fb81f10d8..32d796ffa 100644
--- a/src/common/alignment.h
+++ b/src/common/alignment.h
@@ -42,6 +42,11 @@ requires std::is_integral_v<T>[[nodiscard]] constexpr bool IsAligned(T value, si
return (value & mask) == 0;
}
+template <typename T, typename U>
+requires std::is_integral_v<T>[[nodiscard]] constexpr T DivideUp(T x, U y) {
+ return (x + (y - 1)) / y;
+}
+
template <typename T, size_t Align = 16>
class AlignmentAllocator {
public:
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..d74fc7639 100644
--- a/src/common/cityhash.h
+++ b/src/common/cityhash.h
@@ -62,49 +62,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/string_util.cpp b/src/common/string_util.cpp
index 4cba2aaa4..7b614ad89 100644
--- a/src/common/string_util.cpp
+++ b/src/common/string_util.cpp
@@ -141,27 +141,13 @@ std::string ReplaceAll(std::string result, const std::string& src, const std::st
}
std::string UTF16ToUTF8(const std::u16string& input) {
-#ifdef _MSC_VER
- // Workaround for missing char16_t/char32_t instantiations in MSVC2017
- std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
- std::basic_string<__int16> tmp_buffer(input.cbegin(), input.cend());
- return convert.to_bytes(tmp_buffer);
-#else
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.to_bytes(input);
-#endif
}
std::u16string UTF8ToUTF16(const std::string& input) {
-#ifdef _MSC_VER
- // Workaround for missing char16_t/char32_t instantiations in MSVC2017
- std::wstring_convert<std::codecvt_utf8_utf16<__int16>, __int16> convert;
- auto tmp_buffer = convert.from_bytes(input);
- return std::u16string(tmp_buffer.cbegin(), tmp_buffer.cend());
-#else
std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t> convert;
return convert.from_bytes(input);
-#endif
}
#ifdef _WIN32
diff --git a/src/common/tiny_mt.h b/src/common/tiny_mt.h
new file mode 100644
index 000000000..19ae5b7d6
--- /dev/null
+++ b/src/common/tiny_mt.h
@@ -0,0 +1,250 @@
+// Copyright 2021 yuzu Emulator Project
+// Licensed under GPLv2 or any later version
+// Refer to the license.txt file included.
+
+#pragma once
+
+#include <array>
+
+#include "common/alignment.h"
+#include "common/common_types.h"
+
+namespace Common {
+
+// Implementation of TinyMT (mersenne twister RNG).
+// Like Nintendo, we will use the sample parameters.
+class TinyMT {
+public:
+ static constexpr std::size_t NumStateWords = 4;
+
+ struct State {
+ std::array<u32, NumStateWords> data{};
+ };
+
+private:
+ static constexpr u32 ParamMat1 = 0x8F7011EE;
+ static constexpr u32 ParamMat2 = 0xFC78FF1F;
+ static constexpr u32 ParamTmat = 0x3793FDFF;
+
+ static constexpr u32 ParamMult = 0x6C078965;
+ static constexpr u32 ParamPlus = 0x0019660D;
+ static constexpr u32 ParamXor = 0x5D588B65;
+
+ static constexpr u32 TopBitmask = 0x7FFFFFFF;
+
+ static constexpr int MinimumInitIterations = 8;
+ static constexpr int NumDiscardedInitOutputs = 8;
+
+ static constexpr u32 XorByShifted27(u32 value) {
+ return value ^ (value >> 27);
+ }
+
+ static constexpr u32 XorByShifted30(u32 value) {
+ return value ^ (value >> 30);
+ }
+
+private:
+ State state{};
+
+private:
+ // Internal API.
+ void FinalizeInitialization() {
+ const u32 state0 = this->state.data[0] & TopBitmask;
+ const u32 state1 = this->state.data[1];
+ const u32 state2 = this->state.data[2];
+ const u32 state3 = this->state.data[3];
+
+ if (state0 == 0 && state1 == 0 && state2 == 0 && state3 == 0) {
+ this->state.data[0] = 'T';
+ this->state.data[1] = 'I';
+ this->state.data[2] = 'N';
+ this->state.data[3] = 'Y';
+ }
+
+ for (int i = 0; i < NumDiscardedInitOutputs; i++) {
+ this->GenerateRandomU32();
+ }
+ }
+
+ u32 GenerateRandomU24() {
+ return (this->GenerateRandomU32() >> 8);
+ }
+
+ static void GenerateInitialValuePlus(TinyMT::State* state, int index, u32 value) {
+ u32& state0 = state->data[(index + 0) % NumStateWords];
+ u32& state1 = state->data[(index + 1) % NumStateWords];
+ u32& state2 = state->data[(index + 2) % NumStateWords];
+ u32& state3 = state->data[(index + 3) % NumStateWords];
+
+ const u32 x = XorByShifted27(state0 ^ state1 ^ state3) * ParamPlus;
+ const u32 y = x + index + value;
+
+ state0 = y;
+ state1 += x;
+ state2 += y;
+ }
+
+ static void GenerateInitialValueXor(TinyMT::State* state, int index) {
+ u32& state0 = state->data[(index + 0) % NumStateWords];
+ u32& state1 = state->data[(index + 1) % NumStateWords];
+ u32& state2 = state->data[(index + 2) % NumStateWords];
+ u32& state3 = state->data[(index + 3) % NumStateWords];
+
+ const u32 x = XorByShifted27(state0 + state1 + state3) * ParamXor;
+ const u32 y = x - index;
+
+ state0 = y;
+ state1 ^= x;
+ state2 ^= y;
+ }
+
+public:
+ constexpr TinyMT() = default;
+
+ // Public API.
+
+ // Initialization.
+ void Initialize(u32 seed) {
+ this->state.data[0] = seed;
+ this->state.data[1] = ParamMat1;
+ this->state.data[2] = ParamMat2;
+ this->state.data[3] = ParamTmat;
+
+ for (int i = 1; i < MinimumInitIterations; i++) {
+ const u32 mixed = XorByShifted30(this->state.data[(i - 1) % NumStateWords]);
+ this->state.data[i % NumStateWords] ^= mixed * ParamMult + i;
+ }
+
+ this->FinalizeInitialization();
+ }
+
+ void Initialize(const u32* seed, int seed_count) {
+ this->state.data[0] = 0;
+ this->state.data[1] = ParamMat1;
+ this->state.data[2] = ParamMat2;
+ this->state.data[3] = ParamTmat;
+
+ {
+ const int num_init_iterations = std::max(seed_count + 1, MinimumInitIterations) - 1;
+
+ GenerateInitialValuePlus(&this->state, 0, seed_count);
+
+ for (int i = 0; i < num_init_iterations; i++) {
+ GenerateInitialValuePlus(&this->state, (i + 1) % NumStateWords,
+ (i < seed_count) ? seed[i] : 0);
+ }
+
+ for (int i = 0; i < static_cast<int>(NumStateWords); i++) {
+ GenerateInitialValueXor(&this->state,
+ (i + 1 + num_init_iterations) % NumStateWords);
+ }
+ }
+
+ this->FinalizeInitialization();
+ }
+
+ // State management.
+ void GetState(TinyMT::State& out) const {
+ out.data = this->state.data;
+ }
+
+ void SetState(const TinyMT::State& state_) {
+ this->state.data = state_.data;
+ }
+
+ // Random generation.
+ void GenerateRandomBytes(void* dst, std::size_t size) {
+ const uintptr_t start = reinterpret_cast<uintptr_t>(dst);
+ const uintptr_t end = start + size;
+ const uintptr_t aligned_start = Common::AlignUp(start, 4);
+ const uintptr_t aligned_end = Common::AlignDown(end, 4);
+
+ // Make sure we're aligned.
+ if (start < aligned_start) {
+ const u32 rnd = this->GenerateRandomU32();
+ std::memcpy(dst, &rnd, aligned_start - start);
+ }
+
+ // Write as many aligned u32s as we can.
+ {
+ u32* cur_dst = reinterpret_cast<u32*>(aligned_start);
+ u32* const end_dst = reinterpret_cast<u32*>(aligned_end);
+
+ while (cur_dst < end_dst) {
+ *(cur_dst++) = this->GenerateRandomU32();
+ }
+ }
+
+ // Handle any leftover unaligned data.
+ if (aligned_end < end) {
+ const u32 rnd = this->GenerateRandomU32();
+ std::memcpy(reinterpret_cast<void*>(aligned_end), &rnd, end - aligned_end);
+ }
+ }
+
+ u32 GenerateRandomU32() {
+ // Advance state.
+ const u32 x0 =
+ (this->state.data[0] & TopBitmask) ^ this->state.data[1] ^ this->state.data[2];
+ const u32 y0 = this->state.data[3];
+ const u32 x1 = x0 ^ (x0 << 1);
+ const u32 y1 = y0 ^ (y0 >> 1) ^ x1;
+
+ const u32 state0 = this->state.data[1];
+ u32 state1 = this->state.data[2];
+ u32 state2 = x1 ^ (y1 << 10);
+ const u32 state3 = y1;
+
+ if ((y1 & 1) != 0) {
+ state1 ^= ParamMat1;
+ state2 ^= ParamMat2;
+ }
+
+ this->state.data[0] = state0;
+ this->state.data[1] = state1;
+ this->state.data[2] = state2;
+ this->state.data[3] = state3;
+
+ // Temper.
+ const u32 t1 = state0 + (state2 >> 8);
+ u32 t0 = state3 ^ t1;
+
+ if ((t1 & 1) != 0) {
+ t0 ^= ParamTmat;
+ }
+
+ return t0;
+ }
+
+ u64 GenerateRandomU64() {
+ const u32 lo = this->GenerateRandomU32();
+ const u32 hi = this->GenerateRandomU32();
+ return (u64{hi} << 32) | u64{lo};
+ }
+
+ float GenerateRandomF32() {
+ // Floats have 24 bits of mantissa.
+ constexpr u32 MantissaBits = 24;
+ return static_cast<float>(GenerateRandomU24()) * (1.0f / (1U << MantissaBits));
+ }
+
+ double GenerateRandomF64() {
+ // Doubles have 53 bits of mantissa.
+ // The smart way to generate 53 bits of random would be to use 32 bits
+ // from the first rnd32() call, and then 21 from the second.
+ // Nintendo does not. They use (32 - 5) = 27 bits from the first rnd32()
+ // call, and (32 - 6) bits from the second. We'll do what they do, but
+ // There's not a clear reason why.
+ constexpr u32 MantissaBits = 53;
+ constexpr u32 Shift1st = (64 - MantissaBits) / 2;
+ constexpr u32 Shift2nd = (64 - MantissaBits) - Shift1st;
+
+ const u32 first = (this->GenerateRandomU32() >> Shift1st);
+ const u32 second = (this->GenerateRandomU32() >> Shift2nd);
+
+ return (1.0 * first * (u64{1} << (32 - Shift2nd)) + second) *
+ (1.0 / (u64{1} << MantissaBits));
+ }
+};
+
+} // namespace Common
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..4780b2f9d 100644
--- a/src/common/uint128.h
+++ b/src/common/uint128.h
@@ -4,19 +4,118 @@
#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;
+
+ return multhi;
+#endif
+}
// 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);
+[[nodiscard]] static inline 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/wall_clock.cpp b/src/common/wall_clock.cpp
index a8c143f85..49830b8ab 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"
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() {