common: wall_clock: Optimize GetClockCycles/GetCPUCycles to use a single MUL instruction.

This commit is contained in:
bunnei 2021-02-15 14:51:43 -08:00
parent 0a91599aec
commit 592a649918

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2 or any later version // Licensed under GPLv2 or any later version
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <cstdint>
#include "common/uint128.h" #include "common/uint128.h"
#include "common/wall_clock.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 { class StandardWallClock final : public WallClock {
public: public:
explicit StandardWallClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequency_) 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(); start_time = base_timer::now();
} }
@ -41,16 +45,11 @@ public:
} }
u64 GetClockCycles() override { u64 GetClockCycles() override {
std::chrono::nanoseconds time_now = GetTimeNS(); return MultiplyHigh(GetTimeNS().count(), emulated_clock_factor);
const u128 temporary =
Common::Multiply64Into128(time_now.count(), emulated_clock_frequency);
return Common::Divide128On32(temporary, 1000000000).first;
} }
u64 GetCPUCycles() override { u64 GetCPUCycles() override {
std::chrono::nanoseconds time_now = GetTimeNS(); return MultiplyHigh(GetTimeNS().count(), emulated_cpu_factor);
const u128 temporary = Common::Multiply64Into128(time_now.count(), emulated_cpu_frequency);
return Common::Divide128On32(temporary, 1000000000).first;
} }
void Pause([[maybe_unused]] bool is_paused) override { void Pause([[maybe_unused]] bool is_paused) override {
@ -59,6 +58,8 @@ public:
private: private:
base_time_point start_time; base_time_point start_time;
const u64 emulated_clock_factor;
const u64 emulated_cpu_factor;
}; };
#ifdef ARCHITECTURE_x86_64 #ifdef ARCHITECTURE_x86_64