From 8e15d70fd402db21c469115b3fa27e4755d02c94 Mon Sep 17 00:00:00 2001 From: Valiantsin Dzerakh Date: Tue, 27 Oct 2026 16:22:11 +0300 Subject: [PATCH] Scale the performance counter by a fixed-point multiplier set before main: plat_now_ns and rt_ticks divided a 128-bit product by the counter frequency on every call, and __udivti3 took 5% of the game's running samples Co-Authored-By: Claude Opus 5.5 --- runtime/main.c | 19 +++++++++++-------- runtime/platform/platform.h | 10 ++++++++++ runtime/platform/win32.c | 6 +++++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/runtime/main.c b/runtime/main.c index 00b98fce..620641f7 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -27,6 +27,7 @@ #include "cfg.h" #include "host.h" +#include "platform/platform.h" #define MAX_MODULES 16 @@ -105,17 +106,19 @@ void rt_udf(Cpu *c, uint64_t pc) { ldr_describe(c->x[30], lr, sizeof lr)); } -// CNTPCT_EL0: the Switch counter runs at 19.2 MHz. +// CNTPCT_EL0: the Switch counter runs at 19.2 MHz. The scale is set before main, so no thread reads it half written. +static PlatScale tick_scale; + +__attribute__((constructor)) static void tick_scale_init(void) { + LARGE_INTEGER f; + QueryPerformanceFrequency(&f); + tick_scale = plat_scale(19200000u, (uint64_t)f.QuadPart); +} + uint64_t rt_ticks(void) { - static uint64_t freq; LARGE_INTEGER t; - if (!freq) { - LARGE_INTEGER f; - QueryPerformanceFrequency(&f); - freq = (uint64_t)f.QuadPart; - } QueryPerformanceCounter(&t); - return (uint64_t)((unsigned __int128)(uint64_t)t.QuadPart * 19200000u / freq); + return plat_scaled(tick_scale, (uint64_t)t.QuadPart); } // ---- crash reporter diff --git a/runtime/platform/platform.h b/runtime/platform/platform.h index ae5d5f39..e686eab0 100644 --- a/runtime/platform/platform.h +++ b/runtime/platform/platform.h @@ -23,6 +23,16 @@ uint32_t plat_cpu_count(void); // logical processors // ---- time uint64_t plat_now_ns(void); // monotonic, high resolution, arbitrary epoch +// t * num / den for a fixed num / den without a 128-bit division per call (__udivti3 took 5% of the game's running +// samples through plat_now_ns and rt_ticks): t * q + (t * f >> 64) with q = num / den and f = (num % den) * 2^64 / den. +// Monotonic in t and at most one below the exact quotient while t * (num % den) / den fits 64 bits. +typedef struct { uint64_t q, f; } PlatScale; +static inline PlatScale plat_scale(uint64_t num, uint64_t den) { + return (PlatScale){num / den, (uint64_t)(((unsigned __int128)(num % den) << 64) / den)}; +} +static inline uint64_t plat_scaled(PlatScale s, uint64_t t) { + return t * s.q + (uint64_t)(((unsigned __int128)t * s.f) >> 64); +} void plat_sleep_ns(uint64_t ns); // returns no earlier than ns later; overshoot well under 1 ms // ---- locks (not recursive) and condition variables; zero-initialised storage is a valid unlocked lock and an empty diff --git a/runtime/platform/win32.c b/runtime/platform/win32.c index 7dd424fd..5c51683a 100644 --- a/runtime/platform/win32.c +++ b/runtime/platform/win32.c @@ -78,10 +78,14 @@ static uint64_t qpc_freq(void) { return freq; } +static PlatScale qpc_ns; // set before main, so no thread reads it half written + +__attribute__((constructor)) static void qpc_ns_init(void) { qpc_ns = plat_scale(1000000000u, qpc_freq()); } + uint64_t plat_now_ns(void) { LARGE_INTEGER t; QueryPerformanceCounter(&t); - return (uint64_t)((unsigned __int128)(uint64_t)t.QuadPart * 1000000000u / qpc_freq()); + return plat_scaled(qpc_ns, (uint64_t)t.QuadPart); } // A high-resolution waitable timer per sleeping thread (Windows 10 1803+; one handle per thread, kept until exit)