Explorar o código

common: wall_clock: Utilize constants for ms, us, and ns ratios

Morph %!s(int64=4) %!d(string=hai) anos
pai
achega
4e766280c4
Modificáronse 3 ficheiros con 9 adicións e 5 borrados
  1. 2 2
      src/common/wall_clock.cpp
  2. 4 0
      src/common/wall_clock.h
  3. 3 3
      src/common/x64/native_clock.cpp

+ 2 - 2
src/common/wall_clock.cpp

@@ -73,8 +73,8 @@ std::unique_ptr<WallClock> CreateBestMatchingClock(u32 emulated_cpu_frequency,
         rtsc_frequency = EstimateRDTSCFrequency();
     }
 
-    // Fallback to StandardWallClock if rtsc period is higher than a nano second
-    if (rtsc_frequency <= 1000000000) {
+    // Fallback to StandardWallClock if the hardware TSC does not have nanosecond precision.
+    if (rtsc_frequency <= WallClock::NS_RATIO) {
         return std::make_unique<StandardWallClock>(emulated_cpu_frequency,
                                                    emulated_clock_frequency);
     } else {

+ 4 - 0
src/common/wall_clock.h

@@ -13,6 +13,10 @@ namespace Common {
 
 class WallClock {
 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;
 
     /// Returns current wall time in nanoseconds

+ 3 - 3
src/common/x64/native_clock.cpp

@@ -47,9 +47,9 @@ NativeClock::NativeClock(u64 emulated_cpu_frequency_, u64 emulated_clock_frequen
     _mm_mfence();
     time_point.inner.last_measure = __rdtsc();
     time_point.inner.accumulated_ticks = 0U;
-    ns_rtsc_factor = GetFixedPoint64Factor(1000000000, rtsc_frequency);
-    us_rtsc_factor = GetFixedPoint64Factor(1000000, rtsc_frequency);
-    ms_rtsc_factor = GetFixedPoint64Factor(1000, rtsc_frequency);
+    ns_rtsc_factor = GetFixedPoint64Factor(NS_RATIO, rtsc_frequency);
+    us_rtsc_factor = GetFixedPoint64Factor(US_RATIO, rtsc_frequency);
+    ms_rtsc_factor = GetFixedPoint64Factor(MS_RATIO, rtsc_frequency);
     clock_rtsc_factor = GetFixedPoint64Factor(emulated_clock_frequency, rtsc_frequency);
     cpu_rtsc_factor = GetFixedPoint64Factor(emulated_cpu_frequency, rtsc_frequency);
 }