diff options
Diffstat (limited to 'src')
| -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/wall_clock.cpp | 17 | ||||
| -rw-r--r-- | src/common/x64/native_clock.cpp | 58 | ||||
| -rw-r--r-- | src/core/CMakeLists.txt | 1 | ||||
| -rw-r--r-- | src/core/core_timing_util.cpp | 84 | ||||
| -rw-r--r-- | src/core/core_timing_util.h | 61 |
8 files changed, 141 insertions, 241 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/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>; | |||
| 18 | class StandardWallClock final : public WallClock { | 20 | class StandardWallClock final : public WallClock { |
| 19 | public: | 21 | public: |
| 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 | ||
| 60 | private: | 59 | private: |
| 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 | ||
| 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() { |
diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index 28196d26a..c6bdf72ec 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt | |||
| @@ -19,7 +19,6 @@ add_library(core STATIC | |||
| 19 | core.h | 19 | core.h |
| 20 | core_timing.cpp | 20 | core_timing.cpp |
| 21 | core_timing.h | 21 | core_timing.h |
| 22 | core_timing_util.cpp | ||
| 23 | core_timing_util.h | 22 | core_timing_util.h |
| 24 | cpu_manager.cpp | 23 | cpu_manager.cpp |
| 25 | cpu_manager.h | 24 | cpu_manager.h |
diff --git a/src/core/core_timing_util.cpp b/src/core/core_timing_util.cpp deleted file mode 100644 index 8ce8e602e..000000000 --- a/src/core/core_timing_util.cpp +++ /dev/null | |||
| @@ -1,84 +0,0 @@ | |||
| 1 | // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project | ||
| 2 | // Licensed under GPLv2+ | ||
| 3 | // Refer to the license.txt file included. | ||
| 4 | |||
| 5 | #include "core/core_timing_util.h" | ||
| 6 | |||
| 7 | #include <cinttypes> | ||
| 8 | #include <limits> | ||
| 9 | #include "common/logging/log.h" | ||
| 10 | #include "common/uint128.h" | ||
| 11 | #include "core/hardware_properties.h" | ||
| 12 | |||
| 13 | namespace Core::Timing { | ||
| 14 | |||
| 15 | constexpr u64 MAX_VALUE_TO_MULTIPLY = std::numeric_limits<s64>::max() / Hardware::BASE_CLOCK_RATE; | ||
| 16 | |||
| 17 | s64 msToCycles(std::chrono::milliseconds ms) { | ||
| 18 | if (static_cast<u64>(ms.count() / 1000) > MAX_VALUE_TO_MULTIPLY) { | ||
| 19 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | ||
| 20 | return std::numeric_limits<s64>::max(); | ||
| 21 | } | ||
| 22 | if (static_cast<u64>(ms.count()) > MAX_VALUE_TO_MULTIPLY) { | ||
| 23 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | ||
| 24 | return Hardware::BASE_CLOCK_RATE * (ms.count() / 1000); | ||
| 25 | } | ||
| 26 | return (Hardware::BASE_CLOCK_RATE * ms.count()) / 1000; | ||
| 27 | } | ||
| 28 | |||
| 29 | s64 usToCycles(std::chrono::microseconds us) { | ||
| 30 | if (static_cast<u64>(us.count() / 1000000) > MAX_VALUE_TO_MULTIPLY) { | ||
| 31 | LOG_ERROR(Core_Timing, "Integer overflow, use max value"); | ||
| 32 | return std::numeric_limits<s64>::max(); | ||
| 33 | } | ||
| 34 | if (static_cast<u64>(us.count()) > MAX_VALUE_TO_MULTIPLY) { | ||
| 35 | LOG_DEBUG(Core_Timing, "Time very big, do rounding"); | ||
| 36 | return Hardware::BASE_CLOCK_RATE * (us.count() / 1000000); | ||
| 37 | } | ||
| 38 | return (Hardware::BASE_CLOCK_RATE * us.count()) / 1000000; | ||
| 39 | } | ||
| 40 | |||
| 41 | s64 nsToCycles(std::chrono::nanoseconds ns) { | ||
| 42 | const u128 temporal = Common::Multiply64Into128(ns.count(), Hardware::BASE_CLOCK_RATE); | ||
| 43 | return Common::Divide128On32(temporal, static_cast<u32>(1000000000)).first; | ||
| 44 | } | ||
| 45 | |||
| 46 | u64 msToClockCycles(std::chrono::milliseconds ns) { | ||
| 47 | const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | ||
| 48 | return Common::Divide128On32(temp, 1000).first; | ||
| 49 | } | ||
| 50 | |||
| 51 | u64 usToClockCycles(std::chrono::microseconds ns) { | ||
| 52 | const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | ||
| 53 | return Common::Divide128On32(temp, 1000000).first; | ||
| 54 | } | ||
| 55 | |||
| 56 | u64 nsToClockCycles(std::chrono::nanoseconds ns) { | ||
| 57 | const u128 temp = Common::Multiply64Into128(ns.count(), Hardware::CNTFREQ); | ||
| 58 | return Common::Divide128On32(temp, 1000000000).first; | ||
| 59 | } | ||
| 60 | |||
| 61 | u64 CpuCyclesToClockCycles(u64 ticks) { | ||
| 62 | const u128 temporal = Common::Multiply64Into128(ticks, Hardware::CNTFREQ); | ||
| 63 | return Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | ||
| 64 | } | ||
| 65 | |||
| 66 | std::chrono::milliseconds CyclesToMs(s64 cycles) { | ||
| 67 | const u128 temporal = Common::Multiply64Into128(cycles, 1000); | ||
| 68 | u64 ms = Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | ||
| 69 | return std::chrono::milliseconds(ms); | ||
| 70 | } | ||
| 71 | |||
| 72 | std::chrono::nanoseconds CyclesToNs(s64 cycles) { | ||
| 73 | const u128 temporal = Common::Multiply64Into128(cycles, 1000000000); | ||
| 74 | u64 ns = Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | ||
| 75 | return std::chrono::nanoseconds(ns); | ||
| 76 | } | ||
| 77 | |||
| 78 | std::chrono::microseconds CyclesToUs(s64 cycles) { | ||
| 79 | const u128 temporal = Common::Multiply64Into128(cycles, 1000000); | ||
| 80 | u64 us = Common::Divide128On32(temporal, static_cast<u32>(Hardware::BASE_CLOCK_RATE)).first; | ||
| 81 | return std::chrono::microseconds(us); | ||
| 82 | } | ||
| 83 | |||
| 84 | } // namespace Core::Timing | ||
diff --git a/src/core/core_timing_util.h b/src/core/core_timing_util.h index e4a046bf9..14c36a485 100644 --- a/src/core/core_timing_util.h +++ b/src/core/core_timing_util.h | |||
| @@ -1,24 +1,59 @@ | |||
| 1 | // Copyright 2008 Dolphin Emulator Project / 2017 Citra Emulator Project | 1 | // Copyright 2020 yuzu Emulator Project |
| 2 | // Licensed under GPLv2+ | 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 | #pragma once | 5 | #pragma once |
| 6 | 6 | ||
| 7 | #include <chrono> | 7 | #include <chrono> |
| 8 | |||
| 8 | #include "common/common_types.h" | 9 | #include "common/common_types.h" |
| 10 | #include "core/hardware_properties.h" | ||
| 9 | 11 | ||
| 10 | namespace Core::Timing { | 12 | namespace Core::Timing { |
| 11 | 13 | ||
| 12 | s64 msToCycles(std::chrono::milliseconds ms); | 14 | namespace detail { |
| 13 | s64 usToCycles(std::chrono::microseconds us); | 15 | constexpr u64 CNTFREQ_ADJUSTED = Hardware::CNTFREQ / 1000; |
| 14 | s64 nsToCycles(std::chrono::nanoseconds ns); | 16 | constexpr u64 BASE_CLOCK_RATE_ADJUSTED = Hardware::BASE_CLOCK_RATE / 1000; |
| 15 | u64 msToClockCycles(std::chrono::milliseconds ns); | 17 | } // namespace detail |
| 16 | u64 usToClockCycles(std::chrono::microseconds ns); | 18 | |
| 17 | u64 nsToClockCycles(std::chrono::nanoseconds ns); | 19 | [[nodiscard]] constexpr s64 msToCycles(std::chrono::milliseconds ms) { |
| 18 | std::chrono::milliseconds CyclesToMs(s64 cycles); | 20 | return ms.count() * detail::BASE_CLOCK_RATE_ADJUSTED; |
| 19 | std::chrono::nanoseconds CyclesToNs(s64 cycles); | 21 | } |
| 20 | std::chrono::microseconds CyclesToUs(s64 cycles); | 22 | |
| 21 | 23 | [[nodiscard]] constexpr s64 usToCycles(std::chrono::microseconds us) { | |
| 22 | u64 CpuCyclesToClockCycles(u64 ticks); | 24 | return us.count() * detail::BASE_CLOCK_RATE_ADJUSTED / 1000; |
| 25 | } | ||
| 26 | |||
| 27 | [[nodiscard]] constexpr s64 nsToCycles(std::chrono::nanoseconds ns) { | ||
| 28 | return ns.count() * detail::BASE_CLOCK_RATE_ADJUSTED / 1000000; | ||
| 29 | } | ||
| 30 | |||
| 31 | [[nodiscard]] constexpr u64 msToClockCycles(std::chrono::milliseconds ms) { | ||
| 32 | return static_cast<u64>(ms.count()) * detail::CNTFREQ_ADJUSTED; | ||
| 33 | } | ||
| 34 | |||
| 35 | [[nodiscard]] constexpr u64 usToClockCycles(std::chrono::microseconds us) { | ||
| 36 | return us.count() * detail::CNTFREQ_ADJUSTED / 1000; | ||
| 37 | } | ||
| 38 | |||
| 39 | [[nodiscard]] constexpr u64 nsToClockCycles(std::chrono::nanoseconds ns) { | ||
| 40 | return ns.count() * detail::CNTFREQ_ADJUSTED / 1000000; | ||
| 41 | } | ||
| 42 | |||
| 43 | [[nodiscard]] constexpr u64 CpuCyclesToClockCycles(u64 ticks) { | ||
| 44 | return ticks * detail::CNTFREQ_ADJUSTED / detail::BASE_CLOCK_RATE_ADJUSTED; | ||
| 45 | } | ||
| 46 | |||
| 47 | [[nodiscard]] constexpr std::chrono::milliseconds CyclesToMs(s64 cycles) { | ||
| 48 | return std::chrono::milliseconds(cycles / detail::BASE_CLOCK_RATE_ADJUSTED); | ||
| 49 | } | ||
| 50 | |||
| 51 | [[nodiscard]] constexpr std::chrono::nanoseconds CyclesToNs(s64 cycles) { | ||
| 52 | return std::chrono::nanoseconds(cycles * 1000000 / detail::BASE_CLOCK_RATE_ADJUSTED); | ||
| 53 | } | ||
| 54 | |||
| 55 | [[nodiscard]] constexpr std::chrono::microseconds CyclesToUs(s64 cycles) { | ||
| 56 | return std::chrono::microseconds(cycles * 1000 / detail::BASE_CLOCK_RATE_ADJUSTED); | ||
| 57 | } | ||
| 23 | 58 | ||
| 24 | } // namespace Core::Timing | 59 | } // namespace Core::Timing |