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.
This commit is contained in:
libretroadmin 2026-06-26 23:53:49 +00:00 • committed by libretroadmin
commit 04b76b00a7
3 changed files with 21 additions and 71 deletions

View file

@ -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<double>(Freq.QuadPart) / 1000000000.0;
s_counter_initialized = true;
}
return s_counter_frequency;
}
Timer::Value Timer::GetValue(void)
{
GetCounterFrequency();
Timer::Value ReturnValue;
QueryPerformanceCounter(reinterpret_cast<LARGE_INTEGER*>(&ReturnValue));
return ReturnValue;
}
double Timer::ConvertValueToNanoseconds(Timer::Value value)
{
return (static_cast<double>(value) / s_counter_frequency);
}
double Timer::ConvertValueToMilliseconds(Timer::Value value)
{
return ((static_cast<double>(value) / s_counter_frequency) / 1000000.0);
}
double Timer::ConvertValueToSeconds(Timer::Value value)
{
return ((static_cast<double>(value) / s_counter_frequency) / 1000000000.0);
}
Timer::Value Timer::ConvertSecondsToValue(double s)
{
return static_cast<Value>((s * 1000000000.0) * s_counter_frequency);
return static_cast<Value>((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<double>(value);
}
double Timer::ConvertValueToMilliseconds(Timer::Value value)
{
return (static_cast<double>(value) / 1000000.0);
}
double Timer::ConvertValueToSeconds(Timer::Value value)
{
return (static_cast<double>(value) / 1000000000.0);
}
Timer::Value Timer::ConvertSecondsToValue(double s)
{
return static_cast<Value>(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

View file

@ -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

View file

@ -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<uint32_t>(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<int>(total_textures), \
static_cast<int>(num_textures_loaded)); \
last_update_time.Reset(); \
last_update_time = Common::Timer::GetValue(); \
}
for (const auto& it : m_vram_write_replacements)