From 04b76b00a71550455e5db87143d8854f033a4e0c Mon Sep 17 00:00:00 2001 From: libretroadmin <> Date: Fri, 26 Jun 2026 23:53:49 +0000 Subject: [PATCH] common/timer: drop floating-point time APIs With the GPU loop and CD reader converted, the only remaining user of the float time readouts was the texture-preload progress throttle. Switch it to the same integer pattern (a Value threshold from ConvertSecondsToValue, compared against GetValue() deltas), then remove the now-dead floating-point surface entirely: - ConvertValueToSeconds / ConvertValueToMilliseconds / ConvertValueToNanoseconds - the instance Timer (ctor, Reset, GetTimeSeconds, GetTimeMilliseconds, m_tvStartValue) What remains is just GetValue() and ConvertSecondsToValue(), so time is kept in integer Value units throughout and never stored in or compared as a float (#116). Storing time in float is fragile (see the issue), and a libretro core should keep host time-handling minimal and deterministic regardless. Also make the Windows counter-frequency init independent of GetValue() call order: ConvertSecondsToValue() previously read s_counter_frequency, which was only initialized inside GetValue(). Callers that build a threshold before the first GetValue() (e.g. the GPU loop and shader-compile tracker) would have read a zero frequency. Both paths now lazily initialize via a shared helper. --- src/common/timer.cpp | 66 ++++++------------------------- src/common/timer.h | 17 ++------ src/core/texture_replacements.cpp | 9 ++--- 3 files changed, 21 insertions(+), 71 deletions(-) diff --git a/src/common/timer.cpp b/src/common/timer.cpp index 6db98ae8..5c583bff 100644 --- a/src/common/timer.cpp +++ b/src/common/timer.cpp @@ -12,10 +12,11 @@ namespace Common { #ifdef _WIN32 static double s_counter_frequency; +static bool s_counter_initialized = false; -Timer::Value Timer::GetValue(void) +// counter frequency does not change after system startup, so we can cache it +static double GetCounterFrequency() { - static bool s_counter_initialized = false; // even if this races, it should still result in the same value.. if (!s_counter_initialized) { @@ -24,32 +25,25 @@ Timer::Value Timer::GetValue(void) s_counter_frequency = static_cast(Freq.QuadPart) / 1000000000.0; s_counter_initialized = true; } + return s_counter_frequency; +} + +Timer::Value Timer::GetValue(void) +{ + GetCounterFrequency(); Timer::Value ReturnValue; QueryPerformanceCounter(reinterpret_cast(&ReturnValue)); return ReturnValue; } -double Timer::ConvertValueToNanoseconds(Timer::Value value) -{ - return (static_cast(value) / s_counter_frequency); -} - -double Timer::ConvertValueToMilliseconds(Timer::Value value) -{ - return ((static_cast(value) / s_counter_frequency) / 1000000.0); -} - -double Timer::ConvertValueToSeconds(Timer::Value value) -{ - return ((static_cast(value) / s_counter_frequency) / 1000000000.0); -} - Timer::Value Timer::ConvertSecondsToValue(double s) { - return static_cast((s * 1000000000.0) * s_counter_frequency); + return static_cast((s * 1000000000.0) * GetCounterFrequency()); } + #else + Timer::Value Timer::GetValue(void) { struct timespec tv; @@ -57,45 +51,11 @@ Timer::Value Timer::GetValue(void) return ((Value)tv.tv_nsec + (Value)tv.tv_sec * 1000000000); } -double Timer::ConvertValueToNanoseconds(Timer::Value value) -{ - return static_cast(value); -} - -double Timer::ConvertValueToMilliseconds(Timer::Value value) -{ - return (static_cast(value) / 1000000.0); -} - -double Timer::ConvertValueToSeconds(Timer::Value value) -{ - return (static_cast(value) / 1000000000.0); -} - Timer::Value Timer::ConvertSecondsToValue(double s) { return static_cast(s * 1000000000.0); } + #endif -Timer::Timer() -{ - Reset(); -} - -void Timer::Reset() -{ - m_tvStartValue = GetValue(); -} - -double Timer::GetTimeSeconds() const -{ - return ConvertValueToSeconds(GetValue() - m_tvStartValue); -} - -double Timer::GetTimeMilliseconds() const -{ - return ConvertValueToMilliseconds(GetValue() - m_tvStartValue); -} - } // namespace Common diff --git a/src/common/timer.h b/src/common/timer.h index f1da4744..4d692bfe 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -3,26 +3,17 @@ namespace Common { +// Minimal monotonic timer used only for host-side throttling/sleep decisions +// (never for emulation state - that would break determinism). Time is kept in +// integer Value units; thresholds are produced with ConvertSecondsToValue and +// compared directly, so no time is ever stored in floating point. class Timer { public: using Value = std::uint64_t; - Timer(); - static Value GetValue(void); - static double ConvertValueToSeconds(Value value); - static double ConvertValueToMilliseconds(Value value); - static double ConvertValueToNanoseconds(Value value); static Value ConvertSecondsToValue(double s); - - void Reset(void); - - double GetTimeSeconds(void) const; - double GetTimeMilliseconds(void) const; - -private: - Value m_tvStartValue; }; } // namespace Common diff --git a/src/core/texture_replacements.cpp b/src/core/texture_replacements.cpp index 4cee4358..2afd38ab 100644 --- a/src/core/texture_replacements.cpp +++ b/src/core/texture_replacements.cpp @@ -223,18 +223,17 @@ const TextureReplacementTexture* TextureReplacements::LoadTexture(const std::str void TextureReplacements::PreloadTextures() { - static constexpr float UPDATE_INTERVAL = 1.0f; - - Common::Timer last_update_time; + const Common::Timer::Value update_interval = Common::Timer::ConvertSecondsToValue(1.0); + Common::Timer::Value last_update_time = Common::Timer::GetValue(); uint32_t num_textures_loaded = 0; const uint32_t total_textures = static_cast(m_vram_write_replacements.size()); #define UPDATE_PROGRESS() \ - if (last_update_time.GetTimeSeconds() >= UPDATE_INTERVAL) \ + if ((Common::Timer::GetValue() - last_update_time) >= update_interval) \ { \ g_host_interface->DisplayLoadingScreen("Preloading replacement textures...", 0, static_cast(total_textures), \ static_cast(num_textures_loaded)); \ - last_update_time.Reset(); \ + last_update_time = Common::Timer::GetValue(); \ } for (const auto& it : m_vram_write_replacements)