summaryrefslogtreecommitdiff
path: root/src/common
diff options
context:
space:
mode:
Diffstat (limited to 'src/common')
-rw-r--r--src/common/CMakeLists.txt3
-rw-r--r--src/common/uint128.cpp71
-rw-r--r--src/common/uint128.h89
-rw-r--r--src/common/wall_clock.cpp17
-rw-r--r--src/common/x64/native_clock.cpp58
5 files changed, 95 insertions, 143 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt
index bfd11e76d..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
@@ -206,6 +205,8 @@ if (MSVC)
206else() 205else()
207 target_compile_options(common PRIVATE 206 target_compile_options(common PRIVATE
208 -Werror 207 -Werror
208
209 $<$<CXX_COMPILER_ID:Clang>:-fsized-deallocation>
209 ) 210 )
210endif() 211endif()
211 212
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
14namespace Common {
15
16#ifdef _MSC_VER
17
18u64 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
31u64 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
41u128 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
53std::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
10namespace Common { 21namespace 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/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 @@
2// Licensed under GPLv2 or any later version 2// Licensed under GPLv2 or any later version
3// Refer to the license.txt file included. 3// Refer to the license.txt file included.
4 4
5#include <cstdint>
6
5#include "common/uint128.h" 7#include "common/uint128.h"
6#include "common/wall_clock.h" 8#include "common/wall_clock.h"
7 9
@@ -18,7 +20,9 @@ using base_time_point = std::chrono::time_point<base_timer>;
18class StandardWallClock final : public WallClock { 20class StandardWallClock final : public WallClock {
19public: 21public:
20 explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) 22 explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_)
21 : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false) { 23 : WallClock(emulated_cpu_frequency_, emulated_clock_frequency_, false),
24 emulated_clock_factor{GetFixedPoint64Factor(emulated_clock_frequency, 1000000000)},
25 emulated_cpu_factor{GetFixedPoint64Factor(emulated_cpu_frequency, 1000000000)} {
22 start_time = base_timer::now(); 26 start_time = base_timer::now();
23 } 27 }
24 28
@@ -41,16 +45,11 @@ public:
41 } 45 }
42 46
43 u64 GetClockCycles() override { 47 u64 GetClockCycles() override {
44 std::chrono::nanoseconds time_now = GetTimeNS(); 48 return MultiplyHigh(GetTimeNS().count(), emulated_clock_factor);
45 const u128 temporary =
46 Common::Multiply64Into128(time_now.count(), emulated_clock_frequency);
47 return Common::Divide128On32(temporary, 1000000000).first;
48 } 49 }
49 50
50 u64 GetCPUCycles() override { 51 u64 GetCPUCycles() override {
51 std::chrono::nanoseconds time_now = GetTimeNS(); 52 return MultiplyHigh(GetTimeNS().count(), emulated_cpu_factor);
52 const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency);
53 return Common::Divide128On32(temporary, 1000000000).first;
54 } 53 }
55 54
56 void Pause([[maybe_unused]] bool is_paused) override { 55 void Pause([[maybe_unused]] bool is_paused) override {
@@ -59,6 +58,8 @@ public:
59 58
60private: 59private:
61 base_time_point start_time; 60 base_time_point start_time;
61 const u64 emulated_clock_factor;
62 const u64 emulated_cpu_factor;
62}; 63};
63 64
64#ifdef ARCHITECTURE_x86_64 65#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 @@
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
24namespace {
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
73namespace Common { 15namespace Common {
74 16
75u64 EstimateRDTSCFrequency() { 17u64 EstimateRDTSCFrequency() {