mirror of
https://git.suyu.dev/suyu/suyu
synced 2024-11-02 05:17:52 +00:00
Merge pull request #7809 from Morph1984/clock-constants
common: wall_clock: Utilize constants for ms, us, and ns ratios
This commit is contained in:
commit
50e9ba34b4
3 changed files with 19 additions and 11 deletions
|
@ -65,16 +65,20 @@ private:
|
||||||
|
|
||||||
#ifdef ARCHITECTURE_x86_64
|
#ifdef ARCHITECTURE_x86_64
|
||||||
|
|
||||||
std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
|
std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
|
||||||
u32 emulated_clock_frequency) {
|
u64 emulated_clock_frequency) {
|
||||||
const auto& caps = GetCPUCaps();
|
const auto& caps = GetCPUCaps();
|
||||||
u64 rtsc_frequency = 0;
|
u64 rtsc_frequency = 0;
|
||||||
if (caps.invariant_tsc) {
|
if (caps.invariant_tsc) {
|
||||||
rtsc_frequency = EstimateRDTSCFrequency();
|
rtsc_frequency = EstimateRDTSCFrequency();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallback to StandardWallClock if rtsc period is higher than a nano second
|
// Fallback to StandardWallClock if the hardware TSC does not have the precision greater than:
|
||||||
if (rtsc_frequency <= 1000000000) {
|
// - A nanosecond
|
||||||
|
// - The emulated CPU frequency
|
||||||
|
// - The emulated clock counter frequency (CNTFRQ)
|
||||||
|
if (rtsc_frequency <= WallClock::NS_RATIO || rtsc_frequency <= emulated_cpu_frequency ||
|
||||||
|
rtsc_frequency <= emulated_clock_frequency) {
|
||||||
return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
|
||||||
emulated_clock_frequency);
|
emulated_clock_frequency);
|
||||||
} else {
|
} else {
|
||||||
|
@ -85,8 +89,8 @@ std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
|
||||||
|
|
||||||
#else
|
#else
|
||||||
|
|
||||||
std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
|
std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
|
||||||
u32 emulated_clock_frequency) {
|
u64 emulated_clock_frequency) {
|
||||||
return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
|
return std::make_unique<StandardWallClock>(emulated_cpu_frequency, emulated_clock_frequency);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,10 @@ namespace Common {
|
||||||
|
|
||||||
class WallClock {
|
class WallClock {
|
||||||
public:
|
public:
|
||||||
|
static constexpr u64 NS_RATIO = 1'000'000'000;
|
||||||
|
static constexpr u64 US_RATIO = 1'000'000;
|
||||||
|
static constexpr u64 MS_RATIO = 1'000;
|
||||||
|
|
||||||
virtual ~WallClock() = default;
|
virtual ~WallClock() = default;
|
||||||
|
|
||||||
/// Returns current wall time in nanoseconds
|
/// Returns current wall time in nanoseconds
|
||||||
|
@ -49,7 +53,7 @@ private:
|
||||||
bool is_native;
|
bool is_native;
|
||||||
};
|
};
|
||||||
|
|
||||||
[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
|
[[nodiscard]] std::unique_ptr<WallClock> CreateBestMatchingClock(u64 emulated_cpu_frequency,
|
||||||
u32 emulated_clock_frequency);
|
u64 emulated_clock_frequency);
|
||||||
|
|
||||||
} // namespace Common
|
} // namespace Common
|
||||||
|
|
|
@ -47,9 +47,9 @@ NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequen
|
||||||
_mm_mfence();
|
_mm_mfence();
|
||||||
time_point.inner.last_measure = __rdtsc();
|
time_point.inner.last_measure = __rdtsc();
|
||||||
time_point.inner.accumulated_ticks = 0U;
|
time_point.inner.accumulated_ticks = 0U;
|
||||||
ns_rtsc_factor = GetFixedPoint64Factor(1000000000, rtsc_frequency);
|
ns_rtsc_factor = GetFixedPoint64Factor(NS_RATIO, rtsc_frequency);
|
||||||
us_rtsc_factor = GetFixedPoint64Factor(1000000, rtsc_frequency);
|
us_rtsc_factor = GetFixedPoint64Factor(US_RATIO, rtsc_frequency);
|
||||||
ms_rtsc_factor = GetFixedPoint64Factor(1000, rtsc_frequency);
|
ms_rtsc_factor = GetFixedPoint64Factor(MS_RATIO, rtsc_frequency);
|
||||||
clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency);
|
clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency);
|
||||||
cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency);
|
cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue