From 37daa253fb7bb46923b450487ee75a514bedb06c Mon Sep 17 00:00:00 2001 From: Arzaroth Date: Tue, 24 Nov 2026 09:55:33 +0100 Subject: [PATCH 1/3] [perf/fight-frames] refactor(vu0): field-by-field FMAC paths as named functions add, sub, mul, madd and msub keep their SIMD shortcut and hand the other cases to addExact, subExact, mulExact, maddExact and msubExact. Co-Authored-By: Claude Opus 5.5 --- runtime/include/bozo/ops_vu0.h | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/runtime/include/bozo/ops_vu0.h b/runtime/include/bozo/ops_vu0.h index c1abc19..f98a776 100644 --- a/runtime/include/bozo/ops_vu0.h +++ b/runtime/include/bozo/ops_vu0.h @@ -225,28 +225,30 @@ namespace bozo::vu0 #else #define BOZO_VU0_FAST(op, acc) #endif + inline void addExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, addf); } + inline void subExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, subf); } + inline void mulExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, mulf); } inline void add(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { BOZO_VU0_FAST(Add, s) - fmac(ctx, d, s, t, dest, addf); + addExact(ctx, d, s, t, dest); } inline void sub(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { BOZO_VU0_FAST(Sub, s) - fmac(ctx, d, s, t, dest, subf); + subExact(ctx, d, s, t, dest); } inline void mul(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { BOZO_VU0_FAST(Mul, s) - fmac(ctx, d, s, t, dest, mulf); + mulExact(ctx, d, s, t, dest); } // max / mini do not change the flags. inline void max(Vec &d, Vec s, Vec t, uint32_t dest) { apply(d, s, t, dest, ps2f::max); } inline void mini(Vec &d, Vec s, Vec t, uint32_t dest) { apply(d, s, t, dest, ps2f::min); } // madd: d = acc + s * t (the product rounded first, then the sum: Q6). msub: d = acc - s * t. - inline void madd(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + inline void maddExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) { - BOZO_VU0_FAST(Madd, acc) uint32_t r[4] = {}, fl[4] = {}; for (int i = 0; i < 4; ++i) if (fieldOn(dest, i)) @@ -259,9 +261,13 @@ namespace bozo::vu0 d.u[i] = r[i]; setFlags(ctx, dest, r, fl); } - inline void msub(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + inline void madd(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + { + BOZO_VU0_FAST(Madd, acc) + maddExact(ctx, d, acc, s, t, dest); + } + inline void msubExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) { - BOZO_VU0_FAST(Msub, acc) uint32_t r[4] = {}, fl[4] = {}; for (int i = 0; i < 4; ++i) if (fieldOn(dest, i)) @@ -274,6 +280,11 @@ namespace bozo::vu0 d.u[i] = r[i]; setFlags(ctx, dest, r, fl); } + inline void msub(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + { + BOZO_VU0_FAST(Msub, acc) + msubExact(ctx, d, acc, s, t, dest); + } // The outer product pair: opmula acc.xyz = (s.y*t.z, s.z*t.x, s.x*t.y); opmsub d.xyz = acc.xyz - (s.y*t.z, ...). // The instruction's dest is xyz by definition (w untouched). #undef BOZO_VU0_FAST From bba3f6dc0361162c7b2abca12e8402aee1dd07bd Mon Sep 17 00:00:00 2001 From: Arzaroth Date: Tue, 24 Nov 2026 09:59:45 +0100 Subject: [PATCH 2/3] [perf/fight-frames] perf(vu0): inline the four-field FMAC shortcut at every call add, sub, mul, madd and msub were real calls from the generated code (built at -O1) and from the port's microprograms. They are now always_inline with their sources by reference, and the field-by-field paths stay out of line, so only the rare exact case pays for a call. Tales of Souls fight (PAL60, Valentine Mansion), headless game-thread work: 7.42 -> 6.30 ms a frame. The US route's record is unchanged. Co-Authored-By: Claude Opus 5.5 --- runtime/include/bozo/ops_mem.h | 2 ++ runtime/include/bozo/ops_vu0.h | 49 +++++++++++++++++----------------- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/runtime/include/bozo/ops_mem.h b/runtime/include/bozo/ops_mem.h index 4f16c29..ce56d84 100644 --- a/runtime/include/bozo/ops_mem.h +++ b/runtime/include/bozo/ops_mem.h @@ -17,10 +17,12 @@ #define BOZO_LIKELY(x) __builtin_expect(!!(x), 1) #define BOZO_UNLIKELY(x) __builtin_expect(!!(x), 0) #define BOZO_INLINE inline __attribute__((always_inline)) +#define BOZO_NOINLINE inline __attribute__((noinline)) #else #define BOZO_LIKELY(x) (x) #define BOZO_UNLIKELY(x) (x) #define BOZO_INLINE inline +#define BOZO_NOINLINE inline #endif namespace bozo diff --git a/runtime/include/bozo/ops_vu0.h b/runtime/include/bozo/ops_vu0.h index f98a776..a144fbf 100644 --- a/runtime/include/bozo/ops_vu0.h +++ b/runtime/include/bozo/ops_vu0.h @@ -8,6 +8,7 @@ #include "bozo/context.h" #include "bozo/ops_fpu.h" +#include "bozo/ops_mem.h" #include "bozo/runtime_api.h" #include @@ -23,13 +24,13 @@ namespace bozo::vu0 inline bool fieldOn(uint32_t dest, int i) { return (dest >> (3 - i)) & 1u; } // The second operand of an FMAC instruction: a vector, one field broadcast, Q or I. - inline Vec broadcast(uint32_t bits) + BOZO_INLINE Vec broadcast(uint32_t bits) { Vec v; v.u[0] = v.u[1] = v.u[2] = v.u[3] = bits; return v; } - inline Vec bc(const Vec &t, uint32_t field) { return broadcast(t.u[field]); } + BOZO_INLINE Vec bc(const Vec &t, uint32_t field) { return broadcast(t.u[field]); } // ---- Arithmetic of one field ---- // VU0's adder rounds to nearest even (unlike the FPU's, ops_fpu.h); its multiplier is the FPU's. @@ -123,24 +124,24 @@ namespace bozo::vu0 Madd, Msub }; - inline __m128i load(const Vec &v) { return _mm_loadu_si128(reinterpret_cast(v.u)); } - inline __m128i exponent(__m128i x) { return _mm_and_si128(_mm_srli_epi32(x, 23), _mm_set1_epi32(0xFF)); } - inline __m128i zero(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_setzero_si128()); } // or denormal - inline __m128i top(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(0xFF)); } - inline __m128i abnormal(__m128i x) { return _mm_or_si128(zero(x), top(x)); } + BOZO_INLINE __m128i load(const Vec &v) { return _mm_loadu_si128(reinterpret_cast(v.u)); } + BOZO_INLINE __m128i exponent(__m128i x) { return _mm_and_si128(_mm_srli_epi32(x, 23), _mm_set1_epi32(0xFF)); } + BOZO_INLINE __m128i zero(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_setzero_si128()); } // or denormal + BOZO_INLINE __m128i top(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(0xFF)); } + BOZO_INLINE __m128i abnormal(__m128i x) { return _mm_or_si128(zero(x), top(x)); } // Exponent 1, the smallest normals: a product just below 2^-126 rounds up to them on the host, where the PS2 // flushes it to zero with the underflow flags (ps2f::mul's shortcut leaves them to the exact path too). - inline __m128i bottom(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(1)); } - inline __m128i select(__m128i mask, __m128i a, __m128i b) { return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b)); } - inline __m128i signBits() { return _mm_set1_epi32(static_cast(0x80000000u)); } + BOZO_INLINE __m128i bottom(__m128i x) { return _mm_cmpeq_epi32(exponent(x), _mm_set1_epi32(1)); } + BOZO_INLINE __m128i select(__m128i mask, __m128i a, __m128i b) { return _mm_or_si128(_mm_and_si128(mask, a), _mm_andnot_si128(mask, b)); } + BOZO_INLINE __m128i signBits() { return _mm_set1_epi32(static_cast(0x80000000u)); } // Lane i on when dest enables field i (x = dest bit 3 = lane 0). - inline __m128i lanes(uint32_t dest) + BOZO_INLINE __m128i lanes(uint32_t dest) { return _mm_set_epi32(-static_cast(dest & 1u), -static_cast((dest >> 1) & 1u), -static_cast((dest >> 2) & 1u), -static_cast((dest >> 3) & 1u)); } // s * t; `bad` collects the lanes outside the cases above. - inline __m128i mul(__m128i s, __m128i t, __m128i &bad) + BOZO_INLINE __m128i mul(__m128i s, __m128i t, __m128i &bad) { const __m128i z = _mm_or_si128(zero(s), zero(t)); const __m128i p = _mm_castps_si128(_mm_mul_ps(_mm_castsi128_ps(s), _mm_castsi128_ps(t))); @@ -149,7 +150,7 @@ namespace bozo::vu0 return select(z, _mm_and_si128(_mm_xor_si128(s, t), signBits()), p); } // a + b, likewise. - inline __m128i add(__m128i a, __m128i b, __m128i &bad) + BOZO_INLINE __m128i add(__m128i a, __m128i b, __m128i &bad) { const __m128i za = zero(a), zb = zero(b); const __m128i sum = _mm_castps_si128(_mm_add_ps(_mm_castsi128_ps(a), _mm_castsi128_ps(b))); @@ -163,7 +164,7 @@ namespace bozo::vu0 // d (op) on the enabled fields with the flags, when every enabled field is a case above; false (nothing // written) otherwise. template - inline bool fmac(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest) + BOZO_INLINE bool fmac(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest) { const __m128i S = load(s), T = load(t), on = lanes(dest); __m128i bad = _mm_setzero_si128(), R; @@ -225,20 +226,20 @@ namespace bozo::vu0 #else #define BOZO_VU0_FAST(op, acc) #endif - inline void addExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, addf); } - inline void subExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, subf); } - inline void mulExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, mulf); } - inline void add(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) + BOZO_NOINLINE void addExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, addf); } + BOZO_NOINLINE void subExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, subf); } + BOZO_NOINLINE void mulExact(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) { fmac(ctx, d, s, t, dest, mulf); } + BOZO_INLINE void add(R5900Context *ctx, Vec &d, const Vec &s, const Vec &t, uint32_t dest) { BOZO_VU0_FAST(Add, s) addExact(ctx, d, s, t, dest); } - inline void sub(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) + BOZO_INLINE void sub(R5900Context *ctx, Vec &d, const Vec &s, const Vec &t, uint32_t dest) { BOZO_VU0_FAST(Sub, s) subExact(ctx, d, s, t, dest); } - inline void mul(R5900Context *ctx, Vec &d, Vec s, Vec t, uint32_t dest) + BOZO_INLINE void mul(R5900Context *ctx, Vec &d, const Vec &s, const Vec &t, uint32_t dest) { BOZO_VU0_FAST(Mul, s) mulExact(ctx, d, s, t, dest); @@ -247,7 +248,7 @@ namespace bozo::vu0 inline void max(Vec &d, Vec s, Vec t, uint32_t dest) { apply(d, s, t, dest, ps2f::max); } inline void mini(Vec &d, Vec s, Vec t, uint32_t dest) { apply(d, s, t, dest, ps2f::min); } // madd: d = acc + s * t (the product rounded first, then the sum: Q6). msub: d = acc - s * t. - inline void maddExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + BOZO_NOINLINE void maddExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) { uint32_t r[4] = {}, fl[4] = {}; for (int i = 0; i < 4; ++i) @@ -261,12 +262,12 @@ namespace bozo::vu0 d.u[i] = r[i]; setFlags(ctx, dest, r, fl); } - inline void madd(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + BOZO_INLINE void madd(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest) { BOZO_VU0_FAST(Madd, acc) maddExact(ctx, d, acc, s, t, dest); } - inline void msubExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + BOZO_NOINLINE void msubExact(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) { uint32_t r[4] = {}, fl[4] = {}; for (int i = 0; i < 4; ++i) @@ -280,7 +281,7 @@ namespace bozo::vu0 d.u[i] = r[i]; setFlags(ctx, dest, r, fl); } - inline void msub(R5900Context *ctx, Vec &d, Vec acc, Vec s, Vec t, uint32_t dest) + BOZO_INLINE void msub(R5900Context *ctx, Vec &d, const Vec &acc, const Vec &s, const Vec &t, uint32_t dest) { BOZO_VU0_FAST(Msub, acc) msubExact(ctx, d, acc, s, t, dest); From eca63e515f784751dba804f70a01d2fb53f683b2 Mon Sep 17 00:00:00 2001 From: Arzaroth Date: Tue, 24 Nov 2026 10:43:36 +0100 Subject: [PATCH 3/3] [perf/fight-frames] fix(app): sleep through a waitable timer on Windows FramePacer::frameEnd and the window's own present schedule slept with std::this_thread::sleep_until, which MinGW turns into nanosleep. Under Proton Experimental with fsync that oversleeps by up to 13 ms, so a game with 1 ms of work a frame ran its menus at 55 frames a second while the presents held 60. bozo::app::sleepUntil waits on a high-resolution waitable timer instead (a plain one where that kind is missing, then sleep_until); other systems keep sleep_until. PAL sc3.exe under Proton Experimental with fsync, menus: 58.6 -> 59.95 game frames a second (a standalone pacer loop: 55 -> 60.00). Co-Authored-By: Claude Opus 5.5 --- runtime/CMakeLists.txt | 1 + runtime/app/window.cpp | 2 +- runtime/core/precise_sleep.cpp | 60 ++++++++++++++++++++++++ runtime/include/bozo/app/frame_pacer.h | 3 +- runtime/include/bozo/app/precise_sleep.h | 11 +++++ 5 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 runtime/core/precise_sleep.cpp create mode 100644 runtime/include/bozo/app/precise_sleep.h diff --git a/runtime/CMakeLists.txt b/runtime/CMakeLists.txt index e138030..2d3dd58 100644 --- a/runtime/CMakeLists.txt +++ b/runtime/CMakeLists.txt @@ -2,6 +2,7 @@ # recompiled code calls, routed to a RuntimeServices implementation; the EE kernel and the dispatcher (bozo/ee/); the # per-frame reference record (bozo/reference.h); the native libraries (bozo/libs/). add_library(bozo_runtime_core STATIC + core/precise_sleep.cpp core/runtime_core.cpp core/state.cpp ee/kernel.cpp diff --git a/runtime/app/window.cpp b/runtime/app/window.cpp index f285080..70d5d17 100644 --- a/runtime/app/window.cpp +++ b/runtime/app/window.cpp @@ -594,7 +594,7 @@ namespace bozo::app if (t > nextPresent + period) nextPresent = t; if (nextPresent - t > std::chrono::milliseconds(2)) - std::this_thread::sleep_until(nextPresent - std::chrono::milliseconds(1)); + sleepUntil(nextPresent - std::chrono::milliseconds(1)); while (Clock::now() < nextPresent) std::this_thread::yield(); } diff --git a/runtime/core/precise_sleep.cpp b/runtime/core/precise_sleep.cpp new file mode 100644 index 0000000..3b5263c --- /dev/null +++ b/runtime/core/precise_sleep.cpp @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root). +#include "bozo/app/precise_sleep.h" + +#include + +#if defined(_WIN32) +#ifndef NOMINMAX +#define NOMINMAX +#endif +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#ifndef CREATE_WAITABLE_TIMER_HIGH_RESOLUTION +#define CREATE_WAITABLE_TIMER_HIGH_RESOLUTION 0x00000002 +#endif +#endif + +namespace bozo::app +{ +#if defined(_WIN32) +namespace +{ +struct Timer +{ + HANDLE handle = nullptr; + Timer() + { + handle = CreateWaitableTimerExW(nullptr, nullptr, CREATE_WAITABLE_TIMER_HIGH_RESOLUTION, TIMER_ALL_ACCESS); + if (!handle) + handle = CreateWaitableTimerW(nullptr, FALSE, nullptr); + } + ~Timer() + { + if (handle) + CloseHandle(handle); + } + Timer(const Timer &) = delete; + Timer &operator=(const Timer &) = delete; +}; +} + +void sleepUntil(std::chrono::steady_clock::time_point until) +{ + const auto left = until - std::chrono::steady_clock::now(); + if (left <= std::chrono::steady_clock::duration::zero()) + return; + thread_local Timer timer; + LARGE_INTEGER due; + due.QuadPart = -static_cast(std::chrono::duration_cast(left).count() / 100); + if (timer.handle && due.QuadPart < 0 && SetWaitableTimer(timer.handle, &due, 0, nullptr, nullptr, FALSE) && + WaitForSingleObject(timer.handle, INFINITE) == WAIT_OBJECT_0) + return; + std::this_thread::sleep_until(until); +} +#else +void sleepUntil(std::chrono::steady_clock::time_point until) { std::this_thread::sleep_until(until); } +#endif +} diff --git a/runtime/include/bozo/app/frame_pacer.h b/runtime/include/bozo/app/frame_pacer.h index 36c4f10..5ecf31e 100644 --- a/runtime/include/bozo/app/frame_pacer.h +++ b/runtime/include/bozo/app/frame_pacer.h @@ -14,6 +14,7 @@ #include #include "bozo/app/determinism.h" +#include "bozo/app/precise_sleep.h" // Native frame pacing: a game that opts in runs its own loop on real time instead of on the PS2's vblanks. Its frame // end calls frameEnd(), which holds the frame to the cap (frames per second, 0 = unlimited: only the host's speed @@ -92,7 +93,7 @@ class FramePacer due = m_previous + period; // Sleep most of the wait, then spin the last stretch: Windows sleeps overshoot by up to a millisecond. if (due - now > std::chrono::milliseconds(2)) - std::this_thread::sleep_until(due - std::chrono::milliseconds(1)); + sleepUntil(due - std::chrono::milliseconds(1)); while ((now = Clock::now()) < due) std::this_thread::yield(); } diff --git a/runtime/include/bozo/app/precise_sleep.h b/runtime/include/bozo/app/precise_sleep.h new file mode 100644 index 0000000..db7bc51 --- /dev/null +++ b/runtime/include/bozo/app/precise_sleep.h @@ -0,0 +1,11 @@ +// SPDX-License-Identifier: MIT +// Copyright (c) 2026 The Bozos (boubou666 and Arzaroth). MIT licence (see LICENSE at the repository root). +#pragma once + +#include + +namespace bozo::app +{ +// On Windows a waitable timer: MinGW's sleep_until oversleeps by up to a timer tick under some Wine builds. +void sleepUntil(std::chrono::steady_clock::time_point until); +}