mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-20 12:45:57 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
acd550e822
commit
8e15d70fd4
3 files changed
+26
-9
No files matched your search
+11
-8
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user