perf(runtime): a correctly rounded FMA ahead of suyu's exact bodies on builds without FMA3 #1195
No reviewers
Labels
No labels
area/accessibility
area/audio
area/input
area/install
area/menu
area/renderer
area/saves
area/tooling
area/upscaling
beanstalk: approved
beanstalk: failed
beanstalk: needs approval
beanstalk: running
beanstalk: tested
ci: approved
good first issue
gpu/amd
gpu/igpu
gpu/intel
gpu/nvidia
help wanted
kind
bug
kind
chore
kind
docs
kind
feature
kind
performance
kind
question
needs measurements
platform/linux
platform/macos
platform/steam-deck
platform/windows
preview
priority
critical
priority
high
priority
low
regression
risk
high
risk
low
risk
medium
status
blocked
status
confirmed
status
duplicate
status
invalid
status
needs-info
status
wontfix
ci: approved
No milestone
No project
No assignees
3 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1195
Loading…
Reference in a new issue
No description provided.
Delete branch "brhode/Longshot:perf/fma-soft"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Type: perf · Risk: medium · Tested on: Linux (Ubuntu 24.04), RTX 3060, i7-3770 (no AVX2) · Needs: Windows, any CPU without AVX2
Why
A CPU without AVX2 (Ivy Bridge and older Intel, AMD before Excavator) gets an x86-64-v2 build, which has no hardware fused multiply-add. Every guest FMA there runs suyu's exact multi-word integer routine (
recomp_fpe_fma64, about 180 ns a call, two 1 KB arrays zeroed on each), and on an i7-3770 that was 13.7 % of one core (4.5 ms of CPU a frame) in a sampled profile of rc.5. With this change the common case takes a correctly rounded fast path of a few nanoseconds with the same result and the same FPSR, and the game runs about 5 % faster on that CPU. Nothing changes on a CPU with AVX2 (the code is not compiled in).What changed
The exact bodies stay as they are; a fast path in front of them handles the common case and hands everything else on. It exists only in a build without FMA3, so players on newer CPUs are not touched.
nx2/fma_soft.h(new):recomp_fma_soft64/recomp_fma_soft32: a correctly rounded FMA (Veltkamp split and Dekker's exact product, TwoSum, rounding to odd, then one rounding to nearest; binary32 through binary64). It returns 0, meaning "take the exact body", for any FPCR control bit but FZ, a zero, subnormal, infinite or NaN operand, an exponent range where it could not prove the result, and a result near the range limits. It raises the inexact flag when the result is inexact. Compiled only whereRECOMP_FPX_FMA64is 0 on an x86-64 SSE2 host.nx2/suyu_rt_fixes.py: wrapsa64_rtgen.runtime_cthe way it already wrapsruntime_h: the header's text goes in after#include "a64.h"and an early exit at the top ofrecomp_fpe_fma32/recomp_fpe_fma64, each substitution required to match exactly once.third_party/is untouched.tests/test_fma_soft.py,tests/fma_soft_test.c(new): the generated runtime built the way the lift builds it; the exact body (early exit off) against the early exit on the same operands and the sameGuestContext, comparing the result and every byte of the context.nx2/make_game.py:HOS_FMA_FASTinLIFT_ENV_NOT_OUTPUT(the switch is read by the generated C at play, asHOS_NO_FASTFPis), whichtests/test_lift_cache.pyrequires of every variable a lift source names.docs/development.md: theHOS_FMA_FAST=0switch.docs/tested/: two run files for the measurements below (separate commit).rc.5's own float regions (#38) and
a64_fpx_fma32_fma32already handle most single-precision FMAs without FMA3 (in a double-precision product and sum with a midpoint check), so on current main this change acts mostly on the double-precision FMA:recomp_fpe_fma64went from 13.7 % to 2.9 % of a core andrecomp_fpe_fma32from 2.6 % to 1.3 % (sampled profiles, three passes each).Measurements
Verdict: about 5 % more frames a second on an i7-3770: the game's own fps counter went from a median of 29.55 to 31.0 (+4.9 %; +5.1 % by the mean) with the fast path on, in 4 of 4 alternated pairs of the same binary, every run on above every run off. The presented frames a second (RX_VK_FRAME_LOG, which counts frames frame generation shows too) rose by 1.9 % (+0.76 ± 0.27 fps, 95 %): the CPU saving shows in the frames the game computes, less in the frames the GPU adds. An earlier run of 3 pairs agreed in direction (+5.3 %, its 95 % interval just holds 0), and on rc.3 two independent same-binary comparisons gave +3.1 % and +4.5 % (6 of 6 pairs positive).
Hardware: Ubuntu 24.04.5 (kernel 6.8.0-149) in a Proxmox VM, RTX 3060 12 GB (driver 595.99.02), i7-3770 (4 cores, 8 threads; 7 vCPUs given to the VM), 28 GB DDR3, X11 at 1920x1080.
Scene: the Deku Tree autosave, a scripted walk of about 3 minutes, 1920x1080,
HOS_FPS_MAX=240(cap off),HOS_VCORES=0,RX_VK_WB_MAX_KB=1024; shaders prepared first.Method: the game's "game fps avg" per run (the bench's reading of
longshot_log.txt) and the per-present frame times (RX_VK_FRAME_LOG, the first 30 s of each run dropped); 4 runs of each variant alternated ABBA-style (off, on, on, off, off, on, on, off), the median; the 95 % intervals are Student's t over the runs. The executable is built from source withnx2/build.py --standalone(x86-64-v2) against the game's lifted code of v3.0.0-rc.5, so "off" is this PR's binary withHOS_FMA_FAST=0, the same code as main's path.How to reproduce: build the game with
make_game.py(a CPU without AVX2), then build the executable of this branch and of main against that folder's work folder and run the scene with and withoutHOS_FMA_FAST=0; the lab scripts and the raw lines are in docs/tested/2026-11-16-fma-soft-brhode-run01.md and run02.md.Baseline: the "Before" column is v3.0.0-rc.5 as
make_game.pybuilt it (maine4d035e9, nine commits before3e4e7cab; those commits are docs, the website, the launcher, the 30 fps cap (host/nn_vi.c, the menu),nx2/make_game.py, the job queue scripts and tests: nothing in the float runtime); the cleaner comparison for the same code is the "off" column. The two "After" columns pool the 3 pairs of the first job and the 4 pairs of the second (7 runs each).e4d035e9(3 runs)HOS_FMA_FAST=0: this PR @ 6027b7a7 (7 runs)recomp_fpe_fma64(sampled)Flags and settings
One switch is added.
HOS_FMA_FAST00is for comparisonsTesting
Tested:
python -I -B tests/test_fma_soft.py: 10.3 M random and 8.2 M edge-sweep calls, 0 mismatches in the result and in the wholeGuestContext, in both widths (the early exit is taken on about a quarter of the random calls, and the guard edges are drawn on purpose); the test fails when the inexact flag, the rounding to odd or the FPCR gate is taken out;python -I -B tests/run_all.pyon main71b1edd8and on this branch: 25 failures on main (the Linux build-tool and game-file tests of a VM without the toolchain and the NUP cache) and the same 25 here, plustests/test_lift_cache.pyon the first run of this branch, which namedHOS_FMA_FASTas a variable a lift source reads: it is inLIFT_ENV_NOT_OUTPUTnow and that test passes (rerun alone; the full run was not repeated after that one line);tests/test_fpx_sticky.pyandtests/test_fastfp_override.pypass with the new runtime;tests/test_tested_configurations.pypasses. The measurements below are of this change on main3e4e7cab, before the rebase onto71b1edd8(the 18 commits between are lift speed, the menu, the release scripts and renderer fixes, none in the float runtime), so the commit named in them is the unrebased one; the rebased tests above are the ones run on71b1edd8. The branch was then rebased once more onto main55e8fbd5(6 commits: renderer changes, a lift memory change and run files;docs/development.mdanddocs/tested/merged without a conflict), andtests/test_fma_soft.py,tests/test_lift_cache.pyandtests/test_tested_configurations.pywere run again: they pass; the full run was not repeated. In the game: the scene above with the fast path on and off, 14 runs of this binary on rc.5 (7 on, 7 off), no crash; on rc.3 an earlier build of the same change ran a soak of 8 runs with no crash.Not tested: Windows (the C is the same and builds with the same flags; no run there); an x86-64-v3 machine running the test (the v3 build compiles with the early exit out, and the test's run is skipped on a CPU with AVX2); any scene other than the one measured. The operand exponent limits and the power-of-two exclusion are margins the test cannot show (the test's docstring says so).
Must be tested:
HOS_FMA_FAST=0, expecting the same game and no crash (by hand: a tester with such a CPU, or the Test Farm if it has a runner without AVX2: ask in a comment on the PR)Risks
What could break: a wrong early-exit result would be a wrong float in the game's physics or animation, on CPUs without AVX2 only. It was tested bit for bit against the exact body, FPSR included, and everything outside the proven range falls through to the exact body. The rounding mode other than nearest, and every special value, take the exact body.
Turning it off:
HOS_FMA_FAST=0, or revert the commit.Compatibility: Nothing changes for a player's saves, options file or shader caches. The first update to a build with this change translates the game again on every PC, whatever the CPU: the generated runtime (
a64_suyu.c) is written by the lift, and the lift key hashesnx2/suyu_rt_fixes.pyand the newnx2/fma_soft.h(lift_key,lift_sources). I have not timed that re-translation on this PC. The executable is built again too.Checklist
Everyone
type(scope): description) of the status line's type.can't be run by hand is in Testing's Not testable by hand, with why.
in Risks. Not ticked yet: Risks says the first-update re-translation on every PC was not timed.
main.python -I -B tests/run_all.pypasses (any failure that also fails onmainis named).third_party/untouched (a new folder with aPROVENANCE.mdinstead).When it applies
Assisted-by:/Co-Authored-By:trailers.docs/tested/for each new measured run (format indocs/tested-configurations.md).mainand passing here.tests/that reads changed source updated in this PR, not weakened (tests/test_lift_cache.pypasses withHOS_FMA_FASTadded to its list; no check was loosened).Looking for a tester on Windows, or on any PC whose CPU has no AVX2 (Intel before Haswell, such as Ivy Bridge or Sandy Bridge; AMD before Excavator). That is the one item this PR could not cover: it has only been run on Linux, on an i7-3770.
What to do: build the game with
make_game.pyas usual, start a new file and walk around the Deku Tree for about a minute, once as it is and once withHOS_FMA_FAST=0set. Expected: the same game both times and no crash; the game's own fps counter should read about 5 % higher without the switch (29.55 against 31.0 on the i7-3770).If the Test Farm has a runner without AVX2, a pointer to the right tag would help; I don't know whether it has one.
3590744e5584a2d5996aThe fast path sits in front of the exact FMA bodies and only on a build without FMA3. I read head
84a2d599. It applies onto current main. This is not a merge yet.A normal product and addend in the middle of the exponent range is rounded once, to nearest even, and the inexact flag is set only when the result differs. A zero, a subnormal, an infinity, a NaN, a result near the ends of the range, and any FPCR bit other than flush-to-zero stay on the exact body. The signs of a subtract or a negating multiply are already on the operands before that body is called, so the early exit sees the same bits. A multiply is the same body with a zero addend, and a zero addend is allowed through.
HOS_FMA_FAST=0turns it off. An AVX2 build does not compile it in.The check compares the early exit with the exact body on the same operands and the same context. On the i7-3770 the game's own counter rose in every paired run.
Must be tested is still open: Windows, on a CPU without AVX2, a minute in the Deku Tree with the fast path and with
HOS_FMA_FAST=0, the same game and no crash.Brought up to the ready-for-review standard (#1454): the checklist's Everyone list has two new boxes, added here from what this PR's description and comments show (please check they match what you know).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.