WIP: perf(host): seven of the physics' small float routines on host code, bit for bit (HOS_MATH, on by default) #1898

Draft
snaildev99 wants to merge 14 commits from snaildev99/Longshot:perf/native-math into main
Contributor

Summary

Seven small float routines of the game's physics and actor loops run on host code with the game's exact results, behind a new switch, HOS_MATH, on by default (=0 gives the lifted code). They are ported from Hookshot's native engine, which came from the HeyListen fork; see Credit below. The routines:

  • 0x81a040bc (box): a body's world box from its oriented box.
  • 0x80ba6d80 (affine): a centre and half-extents box under a 3x4 matrix.
  • 0x81746440 (verts): the box of a convex vertex set under a precise transform.
  • 0x800a8400 (sphere): a model's bounding sphere over its bones' spheres.
  • 0x817d9d80 (body): the per-body box update of the solver's last substep.
  • 0x817036a0 (relxf): the placement of one precise transform relative to another, in the physics' narrow phase.
  • 0x817472e0 (support): the support point of a convex vertex set along a direction, in the narrow phase.

Together their functions hold 3.7 % of the game's code time in a Market profile and 2.1 % on a Kakariko and Death Mountain Trail route. Of that, relxf has 0.11 % / 0.04 % and support 0.19 % / 0.41 %.

The goal is less CPU work for the same frames. A call the host cannot prove exact runs the lifted routine and nothing is written. A call is answered natively only in the following state:

  • The float state is the lift's own open one: FPCR at its default except FZ, and FPSR.IXC already set. FPSR is never written.
  • No input is a NaN, an infinity or a subnormal.
  • No sum, product or narrowing lands at or below the smallest normal or overflows.
  • No fused multiply-add falls on a binary32 midpoint.
  • relxf only: the result's zero word does not land on an input. The game stores that word before it reads its inputs.

Every byte the host writes is marked for the GPU mirror (HOS_WRITTEN).

How: nx2/oot_patches.py (MATH_GATES, patch_math_gates; the supported build only) puts a gate first in each lifted function:

if (__builtin_expect(hos_math_on_<addr>, 0) && hos_math_<addr>(c)) return;

hos_math_on_<addr> is 1 until the first call has read the switches (after longshot_env.txt), then 0 unless the unit is on. So with the switch off the gate is one load and an untaken branch. host/hos_math.c reads each routine's arguments from the guest registers and memory. It computes into a scratch copy and writes the outputs back marked. Registers and FPSR are left as the caller may see them: the routines are void, and what the game's code leaves in caller-saved registers is not read after the call. The arithmetic is in host/hos_math_kernels.c, plain C operation for operation. The hook declarations take A64_NOESC, as the lifted code's own calls do.

Read first:

  • host/hos_math.c: run, run_n, run_body, run_support, the check compare / compare_n, FP_OPEN.
  • host/hos_math_kernels.c: HmGuard and the hm_* operations.
  • nx2/oot_patches.py: patch_math_gates.
  • tests/test_native_math.py.
  • The four HOS_MATH* rows in gpu/vk/switches.def.
  • host/PROVENANCE.md.

Credit. This is ported code.

  • The first five units come from Hookshot's native engine: Hookshot commit 60a53da7 and its merge 7229f15b, committed by NotRexed. That engine comes from HeyListen (Sacrappy's GPL-3.0-or-later fork of Hookshot).
  • relxf and support were written by Sacrappy in HeyListen 7412a10f. support's two fixes are 1bc4239a and 4bb9c91d. NotRexed merged them into Hookshot in 732c2bd1.
  • All seven were translated from C++20 to C, operation for operation.
  • Changed from upstream:
    • every native write is marked;
    • the native path needs FPSR.IXC set and never writes FPSR (upstream sets IXC);
    • counters are kept only when logged;
    • the switches are read at the first call;
    • the unit Hookshot answered through a pointer lookup that its runtime never calls (0x81a040bc) is gated like the others;
    • relxf declines a result whose zero word lands on an input.
  • The commits and host/PROVENANCE.md name the sources file by file.

Testing

Tested: Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X. This code (before the rebase onto current main and the A64_NOESC declarations) ran in the game in two test trees on main 068fb8eb:

  • 2026-11-19, test/native-math-ports @ 596563cc: HOS_MATH_CHECK=1 with all seven units. Every call was answered natively into a copy and compared with the lifted routine's result. 0 mismatches.
  • 2026-11-20, test/rewrites (nine rewrite branches merged, named under Measurements): check mode again, the Market and Hyrule Field, about five minutes. The seven units compared 0.3 to 8.6 million calls each, with 0 mismatches. FPCR.FZ was set in every call on the worker threads.
  • Then HOS_MATH=1 was played with nothing visibly different.
  • Then the CPU measurement below: test/rewrites @ e614ef49, HOS_MATH=1 with every other rewrite on, all 15 runs held 60 fps.

This head is not built or played.

Exactness offline. A harness runs the game's own routine in Unicorn, on the game's executable. It runs the real host/hos_math.c and host/hos_math_kernels.c through their gates, compiled with the build's host flags, on the same memory and registers. Every byte either side changed must be equal, under FPCR 0 and under FPCR.FZ. A declining call must have written nothing. The input modes:

  • realistic values;
  • short mantissas;
  • signed zeros;
  • NaNs with payloads, infinities, subnormals and the extreme normals;
  • random bits, tiny values and huge values;
  • binary32 midpoints planted in fused multiply-adds;
  • for relxf, near contacts and the result over an input;
  • for support, ties within and across lanes, a zero direction, counts 1 to 600, the padding pointer redirected, and NaNs past the last vertex (a read past the end would show).

Every rounding mode, DN, FZ16 and AHP was tried, and FPSR with IXC clear or other flags set. When the units were ported:

  • The five: 200,000 cases a unit, twice. 2.2 million guest runs, 0 differing bytes.
  • relxf: 1,000,000 cases (599,911 answered, 1,199,822 guest runs), 0 differing bytes.
  • support: 4,000,000 cases (2,328,083 answered), 0 differing bytes.
  • No declining call wrote. No native write was left unmarked.

Deliberately broken versions are caught (1 to 8,746 mismatching cases each):

  • a fused multiply-add done as a multiply and an add;
  • a dropped quaternion conjugate;
  • Arm's FMIN signed-zero rule replaced by x86's;
  • the rows' or the translation's sums in another order;
  • a wrong lane;
  • support's pre-fix start at the fourth vertex;
  • a tie taken by the wrong lane;
  • the zero-word rule taken out.

What the game's code does that the host's does not: it raises sticky FPSR flags (IOC, OFC, UFC, IDC, QC) from lanes it computes and does not use. FPSR is read in the whole game only by the sdk's four float-environment functions.

Run again on this branch, the host files built from this head with seed 41, FPCR 0 and FZ for every answered case, 0 differing bytes everywhere:

unit cases answered guest runs
box 20,000 11,326 22,652
affine 20,000 11,495 22,990
verts 20,000 11,024 22,048
sphere 20,000 11,135 22,270
body 20,000 8,491 16,982
relxf 100,000 59,841 119,682
support 100,000 58,565 117,130

The host files are byte for byte the ones checked in the game. The A64_NOESC change is in the lifted caller's declaration only, which this harness does not compile.

Game-free checks at this head:

  • tests/test_native_math.py passes. It covers:
    • the kernels against references, 450,020 checks, then relxf 200,156 and support 29,872;
    • the seven gates, each first in its function, once, on the supported build only;
    • the off, on, units and check runs;
    • nine broken copies, which fail.
  • tests/test_switch_table.py, tests/test_patch_hooks_defined.py, test_log_levels, test_utf8_paths, test_doc_anchors, test_check_third_party and test_env_on pass.
  • 36 of the other 41 tests that read nx2/oot_patches.py or nx2/entry_hooks.py pass. The other five fail here and on main a104182b alike: test_dev_packs, test_high_fps_fixes, test_libc_hooks, test_lift_cache, test_song_note_cue.
  • tests/run_all.py was not run.

Still to test:

  • Linux and an AMD GPU: the child Market and Hyrule Field by day, a few minutes each. Compare this branch with HOS_MATH=0 against unset (on), and main against this branch with the switch unset. Measure the game's CPU seconds with the frame rate capped at 60, standing and turning, and the frame times uncapped. Expect less CPU time with the switch on, the same frame rate or better, no cost with it =0, and the picture and physics as without it (@Beanstalk linux, amd).
  • This head on Windows: HOS_MATH_CHECK=1 HOS_MATH_LOG=1 in the Market and Hyrule Field. Expect every math: <unit>: ... mismatches 0 line and no MISMATCH line. Then HOS_MATH=1: bodies resting on and bumping into each other look as before (by hand).

Measurements

Verdict: less CPU work, measured together with the other rewrite branches, not yet alone. In a test tree that merges nine rewrite branches (named in the baseline below; the cloth and particle effect rewrites and some later pieces are not among them), at a 60 fps cap in the Market:

  • Those nine on used 2.7 % less CPU standing and 2.4 % less turning (the whole process) than main.
  • With the float fallback change of another branch on top: -3.7 % / -3.0 %.
  • The hooks in with every switch off cost nothing measurable: -0.3 % / -0.2 %.

How much of that is these seven units is not separated. The Test Farm request asks for this branch's own number.

Baseline: main 068fb8eb (#1802) against the test tree test/rewrites @ e614ef49. That tree is main 068fb8eb with nine rewrite branches merged (not every rewrite there is: the cloth and particle effect branches were not in it): these seven units (596563cc, before the A64_NOESC declarations), the maths library, animation, bounds, crowd, visibility, glue, the effect triggers with the reverb, and the solver. It was lifted with A64_FPXKEEP=0, so that the lift is main's apart from the hooks. It is not this PR alone: the run-time switches were all on together. Main has moved since 068fb8eb; this branch was not measured on current main.
Setup: Windows 11, Ryzen 9 5900X, RTX 3090, 3840x2160; the child Market, hands-free; frame rate capped at 60 (all 15 runs held 60). Each run: 30 s standing, then 30 s turning the camera. Five variants, interleaved over three rounds, the PC otherwise idle. The figure: CPU seconds per second of play (cores busy on average) of the whole process, from the processor's cycle counter; the median of three runs (the three in brackets).

Before: main @ 068fb8eb After, every switch off: test/rewrites @ e614ef49 After, the nine branches on: test/rewrites @ e614ef49 Δ off Δ on
Market standing, cores 5.156 [5.144 5.156 5.161] 5.140 [5.303 5.140 5.125] 5.017 [5.007 5.017 5.049] -0.3 % -2.7 %
Market turning, cores 5.639 [5.676 5.627 5.639] 5.630 [5.722 5.630 5.628] 5.504 [5.504 5.498 5.577] -0.2 % -2.4 %

The off figures are inside the runs' spread. The process includes the renderer's threads (about 1.4 cores standing), which the rewrites do not touch. Over the game's own threads, with the float fallback change too: -5.7 % standing and -5.0 % turning. On the executors, where these units mostly run: -4.1 % / -4.0 % with the rewrites on.

Offline: CPU time a call, not the game. On realistic inputs answered natively, against the same functions in the old lift's form (every register in the context; build-hookshot's stock lift, Hookshot's translator), compiled with the hot pieces' flags; one pinned thread, best of nine interleaved rounds:

unit factor
box 3.1x to 3.2x
affine 1.2x
verts 4.8x to 4.9x
sphere 1.6x
body 2.5x

relxf and support were not timed offline. These factors are against the old form, not against main's lift, which keeps registers in C locals where it can: against main's form they are smaller by an amount not measured. A gated function is written in the old form. Its cost with the switch off was measured in the old form only: within the noise for four units, 12 % for sphere's gated copy, and that was before the declarations took A64_NOESC. At the process's scale the game measurement above found no off cost.

The switches

HOS_MATH (new, run time, kill, on by default): 0 off (the lifted routines, main's results); 1 answers the units natively where exact.

HOS_MATH_UNITS names the units: a comma list of box, affine, verts, sphere, body, relxf, support, or all (the default).

HOS_MATH_CHECK=1 runs both on every call. The game keeps the lifted result. A difference logs math: <unit> MISMATCH at out ..., fpcr ...: lifted ... / native ... with the inputs.

HOS_MATH_LOG=1 logs every 10 s and at the end:

math: <unit>: calls N native N declined D (float state F, inputs I) compared C mismatches 0 fpsr-differs X

The start line names each unit's state, for example math: box native; ...; support native; (HOS_MATH, on by default; =0 runs them lifted). It ships on: docs/dev/standards.md has a switch ship at its value from the first push, never off because it is new, and the measurement with the rewrite branches together says on. HOS_MATH=0 gives main's lifted code. This PR's own measurement (the farm request) can still turn it off before it leaves WIP.

Risks

What could break: a wrong box, sphere, placement or support point would show in the physics or in what is culled: bodies passing through or catching on each other, models popping near the screen's edges. Every answered call in the two in-game check runs matched the lifted routine.

Open: in the CPU measurement, the renderer's mkw_vk capture copy thread used 10 to 18 % more CPU (0.009 to 0.017 cores), in both variants with the rewrites on and in neither without. That thread copies guest memory the game marked as written, and the native units mark what they write (HOS_WRITTEN, possibly more coarsely than the lifted stores' own marks). Which rewrite causes it is not determined; this branch is one of several candidates.

Turning it off: HOS_MATH=0; HOS_MATH_UNITS leaves out single units.

After updating: the game is translated again once (nx2/oot_patches.py is in the lift's key). The routines run on the host by default, with the game's exact results; saves, longshot_options.ini and the caches do not change.

Checklist

Ready for review

  • One topic, on current main; the title is a Conventional Commit.
  • Tested on my own PC, in the game for a change the game runs, as Testing says. (The code ran in the game in two test trees on main 068fb8eb, in check mode and on; this head is not built or played.)
  • mise run test passes (any failure that also fails on main is named). (The tests that read the changed files pass, apart from five that fail on main a104182b too; the whole suite not run.)
  • Measured against main, or Measurements says why there is nothing to measure. (Measured with the other rewrite branches merged; this branch alone not yet.)
  • No game files, build output or hand-edited generated files; third_party/ untouched; nothing GPL-incompatible.

When it applies

  • A switch added or changed: it ships at the default the measurements chose, with its row in gpu/vk/switches.def. (The four rows are there; on by default, HOS_MATH=0 gives main's lifted code; this PR's own measurement can still turn it off.)
  • A check under tests/ that reads changed source is updated, not weakened. (tests/test_native_math.py, new; tests/test_switch_table.py reads the four rows.)
  • Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. (Windows: in both test trees. Linux: not built.)
  • AI assistance recorded with Assisted-by: / Co-Authored-By: trailers.

Ready to merge

  • Everything in Still to test has run, its results in Tested.
  • Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers").
  • Approved in review, every comment answered.
  • CI passes on the head; on current main, measured again or a comment says why the numbers still hold.

🤖 Generated with Claude Code

## Summary Seven small float routines of the game's physics and actor loops run on host code with the game's exact results, behind a new switch, `HOS_MATH`, on by default (`=0` gives the lifted code). They are ported from Hookshot's native engine, which came from the HeyListen fork; see Credit below. The routines: - 0x81a040bc (box): a body's world box from its oriented box. - 0x80ba6d80 (affine): a centre and half-extents box under a 3x4 matrix. - 0x81746440 (verts): the box of a convex vertex set under a precise transform. - 0x800a8400 (sphere): a model's bounding sphere over its bones' spheres. - 0x817d9d80 (body): the per-body box update of the solver's last substep. - 0x817036a0 (relxf): the placement of one precise transform relative to another, in the physics' narrow phase. - 0x817472e0 (support): the support point of a convex vertex set along a direction, in the narrow phase. Together their functions hold 3.7 % of the game's code time in a Market profile and 2.1 % on a Kakariko and Death Mountain Trail route. Of that, relxf has 0.11 % / 0.04 % and support 0.19 % / 0.41 %. The goal is less CPU work for the same frames. A call the host cannot prove exact runs the lifted routine and nothing is written. A call is answered natively only in the following state: - The float state is the lift's own open one: FPCR at its default except FZ, and FPSR.IXC already set. FPSR is never written. - No input is a NaN, an infinity or a subnormal. - No sum, product or narrowing lands at or below the smallest normal or overflows. - No fused multiply-add falls on a binary32 midpoint. - relxf only: the result's zero word does not land on an input. The game stores that word before it reads its inputs. Every byte the host writes is marked for the GPU mirror (`HOS_WRITTEN`). How: `nx2/oot_patches.py` (`MATH_GATES`, `patch_math_gates`; the supported build only) puts a gate first in each lifted function: ``` if (__builtin_expect(hos_math_on_<addr>, 0) && hos_math_<addr>(c)) return; ``` `hos_math_on_<addr>` is 1 until the first call has read the switches (after `longshot_env.txt`), then 0 unless the unit is on. So with the switch off the gate is one load and an untaken branch. `host/hos_math.c` reads each routine's arguments from the guest registers and memory. It computes into a scratch copy and writes the outputs back marked. Registers and FPSR are left as the caller may see them: the routines are void, and what the game's code leaves in caller-saved registers is not read after the call. The arithmetic is in `host/hos_math_kernels.c`, plain C operation for operation. The hook declarations take `A64_NOESC`, as the lifted code's own calls do. Read first: - `host/hos_math.c`: `run`, `run_n`, `run_body`, `run_support`, the check `compare` / `compare_n`, `FP_OPEN`. - `host/hos_math_kernels.c`: `HmGuard` and the `hm_*` operations. - `nx2/oot_patches.py`: `patch_math_gates`. - `tests/test_native_math.py`. - The four `HOS_MATH*` rows in `gpu/vk/switches.def`. - `host/PROVENANCE.md`. **Credit.** This is ported code. - The first five units come from Hookshot's native engine: Hookshot commit 60a53da7 and its merge 7229f15b, committed by NotRexed. That engine comes from HeyListen (Sacrappy's GPL-3.0-or-later fork of Hookshot). - relxf and support were written by Sacrappy in HeyListen 7412a10f. support's two fixes are 1bc4239a and 4bb9c91d. NotRexed merged them into Hookshot in 732c2bd1. - All seven were translated from C++20 to C, operation for operation. - Changed from upstream: - every native write is marked; - the native path needs FPSR.IXC set and never writes FPSR (upstream sets IXC); - counters are kept only when logged; - the switches are read at the first call; - the unit Hookshot answered through a pointer lookup that its runtime never calls (0x81a040bc) is gated like the others; - relxf declines a result whose zero word lands on an input. - The commits and `host/PROVENANCE.md` name the sources file by file. ## Testing **Tested:** Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X. This code (before the rebase onto current main and the `A64_NOESC` declarations) ran in the game in two test trees on main 068fb8eb: - 2026-11-19, `test/native-math-ports` @ 596563cc: `HOS_MATH_CHECK=1` with all seven units. Every call was answered natively into a copy and compared with the lifted routine's result. 0 mismatches. - 2026-11-20, `test/rewrites` (nine rewrite branches merged, named under Measurements): check mode again, the Market and Hyrule Field, about five minutes. The seven units compared 0.3 to 8.6 million calls each, with 0 mismatches. FPCR.FZ was set in every call on the worker threads. - Then `HOS_MATH=1` was played with nothing visibly different. - Then the CPU measurement below: `test/rewrites` @ e614ef49, `HOS_MATH=1` with every other rewrite on, all 15 runs held 60 fps. This head is not built or played. Exactness offline. A harness runs the game's own routine in Unicorn, on the game's executable. It runs the real `host/hos_math.c` and `host/hos_math_kernels.c` through their gates, compiled with the build's host flags, on the same memory and registers. Every byte either side changed must be equal, under FPCR 0 and under FPCR.FZ. A declining call must have written nothing. The input modes: - realistic values; - short mantissas; - signed zeros; - NaNs with payloads, infinities, subnormals and the extreme normals; - random bits, tiny values and huge values; - binary32 midpoints planted in fused multiply-adds; - for relxf, near contacts and the result over an input; - for support, ties within and across lanes, a zero direction, counts 1 to 600, the padding pointer redirected, and NaNs past the last vertex (a read past the end would show). Every rounding mode, DN, FZ16 and AHP was tried, and FPSR with IXC clear or other flags set. When the units were ported: - The five: 200,000 cases a unit, twice. 2.2 million guest runs, 0 differing bytes. - relxf: 1,000,000 cases (599,911 answered, 1,199,822 guest runs), 0 differing bytes. - support: 4,000,000 cases (2,328,083 answered), 0 differing bytes. - No declining call wrote. No native write was left unmarked. Deliberately broken versions are caught (1 to 8,746 mismatching cases each): - a fused multiply-add done as a multiply and an add; - a dropped quaternion conjugate; - Arm's FMIN signed-zero rule replaced by x86's; - the rows' or the translation's sums in another order; - a wrong lane; - support's pre-fix start at the fourth vertex; - a tie taken by the wrong lane; - the zero-word rule taken out. What the game's code does that the host's does not: it raises sticky FPSR flags (IOC, OFC, UFC, IDC, QC) from lanes it computes and does not use. FPSR is read in the whole game only by the sdk's four float-environment functions. Run again on this branch, the host files built from this head with seed 41, FPCR 0 and FZ for every answered case, 0 differing bytes everywhere: | unit | cases | answered | guest runs | |---|---|---|---| | box | 20,000 | 11,326 | 22,652 | | affine | 20,000 | 11,495 | 22,990 | | verts | 20,000 | 11,024 | 22,048 | | sphere | 20,000 | 11,135 | 22,270 | | body | 20,000 | 8,491 | 16,982 | | relxf | 100,000 | 59,841 | 119,682 | | support | 100,000 | 58,565 | 117,130 | The host files are byte for byte the ones checked in the game. The `A64_NOESC` change is in the lifted caller's declaration only, which this harness does not compile. Game-free checks at this head: - `tests/test_native_math.py` passes. It covers: - the kernels against references, 450,020 checks, then relxf 200,156 and support 29,872; - the seven gates, each first in its function, once, on the supported build only; - the off, on, units and check runs; - nine broken copies, which fail. - `tests/test_switch_table.py`, `tests/test_patch_hooks_defined.py`, `test_log_levels`, `test_utf8_paths`, `test_doc_anchors`, `test_check_third_party` and `test_env_on` pass. - 36 of the other 41 tests that read `nx2/oot_patches.py` or `nx2/entry_hooks.py` pass. The other five fail here and on main a104182b alike: `test_dev_packs`, `test_high_fps_fixes`, `test_libc_hooks`, `test_lift_cache`, `test_song_note_cue`. - `tests/run_all.py` was not run. **Still to test:** - Linux and an AMD GPU: the child Market and Hyrule Field by day, a few minutes each. Compare this branch with `HOS_MATH=0` against unset (on), and main against this branch with the switch unset. Measure the game's CPU seconds with the frame rate capped at 60, standing and turning, and the frame times uncapped. Expect less CPU time with the switch on, the same frame rate or better, no cost with it `=0`, and the picture and physics as without it (@Beanstalk linux, amd). - This head on Windows: `HOS_MATH_CHECK=1 HOS_MATH_LOG=1` in the Market and Hyrule Field. Expect every `math: <unit>: ... mismatches 0` line and no `MISMATCH` line. Then `HOS_MATH=1`: bodies resting on and bumping into each other look as before (by hand). ## Measurements **Verdict:** less CPU work, measured together with the other rewrite branches, not yet alone. In a test tree that merges nine rewrite branches (named in the baseline below; the cloth and particle effect rewrites and some later pieces are not among them), at a 60 fps cap in the Market: - Those nine on used 2.7 % less CPU standing and 2.4 % less turning (the whole process) than main. - With the float fallback change of another branch on top: -3.7 % / -3.0 %. - The hooks in with every switch off cost nothing measurable: -0.3 % / -0.2 %. How much of that is these seven units is not separated. The Test Farm request asks for this branch's own number. Baseline: main 068fb8eb (#1802) against the test tree `test/rewrites` @ e614ef49. That tree is main 068fb8eb with nine rewrite branches merged (not every rewrite there is: the cloth and particle effect branches were not in it): these seven units (596563cc, before the `A64_NOESC` declarations), the maths library, animation, bounds, crowd, visibility, glue, the effect triggers with the reverb, and the solver. It was lifted with `A64_FPXKEEP=0`, so that the lift is main's apart from the hooks. It is not this PR alone: the run-time switches were all on together. Main has moved since 068fb8eb; this branch was not measured on current main. Setup: Windows 11, Ryzen 9 5900X, RTX 3090, 3840x2160; the child Market, hands-free; frame rate capped at 60 (all 15 runs held 60). Each run: 30 s standing, then 30 s turning the camera. Five variants, interleaved over three rounds, the PC otherwise idle. The figure: CPU seconds per second of play (cores busy on average) of the whole process, from the processor's cycle counter; the median of three runs (the three in brackets). | | Before: main @ 068fb8eb | After, every switch off: test/rewrites @ e614ef49 | After, the nine branches on: test/rewrites @ e614ef49 | Δ off | Δ on | |---|---|---|---|---|---| | Market standing, cores | 5.156 [5.144 5.156 5.161] | 5.140 [5.303 5.140 5.125] | 5.017 [5.007 5.017 5.049] | -0.3 % | -2.7 % | | Market turning, cores | 5.639 [5.676 5.627 5.639] | 5.630 [5.722 5.630 5.628] | 5.504 [5.504 5.498 5.577] | -0.2 % | -2.4 % | The off figures are inside the runs' spread. The process includes the renderer's threads (about 1.4 cores standing), which the rewrites do not touch. Over the game's own threads, with the float fallback change too: -5.7 % standing and -5.0 % turning. On the executors, where these units mostly run: -4.1 % / -4.0 % with the rewrites on. Offline: CPU time a call, not the game. On realistic inputs answered natively, against the same functions in the old lift's form (every register in the context; `build-hookshot`'s stock lift, Hookshot's translator), compiled with the hot pieces' flags; one pinned thread, best of nine interleaved rounds: | unit | factor | |---|---| | box | 3.1x to 3.2x | | affine | 1.2x | | verts | 4.8x to 4.9x | | sphere | 1.6x | | body | 2.5x | relxf and support were not timed offline. These factors are against the old form, not against main's lift, which keeps registers in C locals where it can: against main's form they are smaller by an amount not measured. A gated function is written in the old form. Its cost with the switch off was measured in the old form only: within the noise for four units, 12 % for sphere's gated copy, and that was before the declarations took `A64_NOESC`. At the process's scale the game measurement above found no off cost. ### The switches `HOS_MATH` (new, run time, `kill`, on by default): `0` off (the lifted routines, main's results); `1` answers the units natively where exact. `HOS_MATH_UNITS` names the units: a comma list of `box`, `affine`, `verts`, `sphere`, `body`, `relxf`, `support`, or `all` (the default). `HOS_MATH_CHECK=1` runs both on every call. The game keeps the lifted result. A difference logs `math: <unit> MISMATCH at out ..., fpcr ...: lifted ... / native ...` with the inputs. `HOS_MATH_LOG=1` logs every 10 s and at the end: ``` math: <unit>: calls N native N declined D (float state F, inputs I) compared C mismatches 0 fpsr-differs X ``` The start line names each unit's state, for example `math: box native; ...; support native; (HOS_MATH, on by default; =0 runs them lifted)`. It ships on: docs/dev/standards.md has a switch ship at its value from the first push, never off because it is new, and the measurement with the rewrite branches together says on. `HOS_MATH=0` gives main's lifted code. This PR's own measurement (the farm request) can still turn it off before it leaves WIP. ## Risks **What could break:** a wrong box, sphere, placement or support point would show in the physics or in what is culled: bodies passing through or catching on each other, models popping near the screen's edges. Every answered call in the two in-game check runs matched the lifted routine. Open: in the CPU measurement, the renderer's `mkw_vk capture copy` thread used 10 to 18 % more CPU (0.009 to 0.017 cores), in both variants with the rewrites on and in neither without. That thread copies guest memory the game marked as written, and the native units mark what they write (`HOS_WRITTEN`, possibly more coarsely than the lifted stores' own marks). Which rewrite causes it is not determined; this branch is one of several candidates. **Turning it off:** `HOS_MATH=0`; `HOS_MATH_UNITS` leaves out single units. **After updating:** the game is translated again once (`nx2/oot_patches.py` is in the lift's key). The routines run on the host by default, with the game's exact results; saves, `longshot_options.ini` and the caches do not change. ## Checklist ### Ready for review - [x] One topic, on current `main`; the title is a Conventional Commit. - [ ] Tested on my own PC, in the game for a change the game runs, as Testing says. (The code ran in the game in two test trees on main 068fb8eb, in check mode and on; this head is not built or played.) - [ ] `mise run test` passes (any failure that also fails on `main` is named). (The tests that read the changed files pass, apart from five that fail on main a104182b too; the whole suite not run.) - [ ] Measured against `main`, or Measurements says why there is nothing to measure. (Measured with the other rewrite branches merged; this branch alone not yet.) - [x] No game files, build output or hand-edited generated files; `third_party/` untouched; nothing GPL-incompatible. ### When it applies - [ ] A switch added or changed: it ships at the default the measurements chose, with its row in `gpu/vk/switches.def`. (The four rows are there; on by default, `HOS_MATH=0` gives main's lifted code; this PR's own measurement can still turn it off.) - [x] A check under `tests/` that reads changed source is updated, not weakened. (`tests/test_native_math.py`, new; `tests/test_switch_table.py` reads the four rows.) - [ ] Host code builds on Windows (llvm-mingw) and Linux, with UTF-8 paths. (Windows: in both test trees. Linux: not built.) - [x] AI assistance recorded with `Assisted-by:` / `Co-Authored-By:` trailers. ### Ready to merge - [ ] Everything in Still to test has run, its results in Tested. - [ ] Tier 1 hardware it can affect is tested (docs/dev/standards.md, "Hardware tiers"). - [ ] Approved in review, every comment answered. - [ ] CI passes on the head; on current `main`, measured again or a comment says why the numbers still hold. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Five small float routines of the physics and actor loops answered by native code: a body's box from its oriented box
(0x81a040bc), a centre / half-extents box under a 3x4 matrix (0x80ba6d80), a convex vertex set's box (0x81746440), a
model's bounding sphere (0x800a8400) and the per-body box update of Havok's last solver substep (0x817d9d80). A call goes
native only in the translation's own native float state (FPCR at its default but FZ, FPSR.IXC set) and when every value
is one x86 and the translated Arm code agree on; otherwise the lifted function runs. HOS_MATH=1 turns them on,
HOS_MATH_UNITS picks units, HOS_MATH_CHECK=1 runs both and compares (the game keeps the lifted result), HOS_MATH_LOG=1
logs the counters.

Ported from Hookshot's native engine: Hookshot commit 60a53da7 ("A fork's native engine", merged in 7229f15b), committed
by NotRexed; the engine comes from HeyListen, a GPL-3.0-or-later fork of Hookshot. Its host/hl_math.c,
native/include/hl/math/Exact.h, Bounds.h, BodyBox.h, native/src/math/Bounds.cpp, BodyBox.cpp,
native/src/c/hl_math.cpp and native/include/hl/bridge/MathBridge.h, from C++20 to C operation for operation
(host/PROVENANCE.md). Changed from there: every byte the native path writes is marked for the GPU mirror (Hookshot's
32-byte answers were not); the native path needs FPSR.IXC set and leaves FPSR alone (Hookshot's set IXC); the counters are
kept only when they are logged (six worker threads otherwise add to one cache line on every call); the switches are read
at the first call, after longshot_env.txt; the unit Hookshot answers through a pointer (0x81a040bc, whose lookup hook
nothing in Hookshot's runtime calls) is gated like the others; the subIntegrateLastStep check (nothing native there yet)
is left out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nx2/oot_patches.py MATH_GATES: `if (__builtin_expect(hos_math_on_<addr>, 0) && hos_math_<addr>(c)) return;` first in
f_81a040bc, f_80ba6d80, f_81746440, f_800a8400 and f_817d9d80 (once; again on a patched text: no change), counted in
report(), which warns when one of the five is not found. The switch starts at 1 so that the first call reads HOS_MATH and
leaves it 0 when the unit is off: from then on the gate is one load and a branch not taken. oot_patches.py is in the
lift's key, so the gates come with the next lift; HOS_MATH itself is read at run time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tests/test_native_math.py compiles host/hos_math.c and host/hos_math_kernels.c over a stand-in guest memory: each kernel
against a reference written from what the guest function computes (after Hookshot's native/tests/math_test.cpp), the
fused multiply-add against fmaf on 2 million triples with the midpoint case given up and an exact midpoint answered, the
inputs every kernel gives up on; each gate off (the switch drops to 0, nothing written), on (the arguments read where the
function has them, the answer written and marked, FPSR and registers left; declined in another float state or without
an array, a record, a thread context), HOS_MATH_UNITS, another build, the check (the lifted result kept, a difference
counted, no recursion); the gates nx2/oot_patches.py puts in. Three broken copies must fail.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A sixth HOS_MATH unit, `relxf`: the game's 0x817036a0 (111 instructions, Havok's narrow phase) gives transform b's
placement relative to transform a in 80 bytes: the rotation rows as a's columns times b's rows, the translation
difference in binary64 rotated by a's rows and split into a float and the float's remainder, and a zero word. Same rules
as the other units: native only in the translation's own float state (FPCR at its default but FZ, FPSR.IXC set) and when
every value is one x86 and the translated Arm code agree on; otherwise the lifted function runs. HOS_MATH_UNITS=relxf
picks it; HOS_MATH_CHECK compares all 80 bytes.

Ported from HeyListen's relxf unit, written by Sacrappy (HeyListen commit 7412a10f, "the support point 0x817472e0 and
the relative placement 0x817036a0 native"; checked in the game and turned on there in 064d5ba2), as merged into Hookshot
by NotRexed (Hookshot commit 732c2bd1, "two native narrow-phase math units"): native/src/math/Support.cpp
relative_transform, native/include/hl/math/Support.h, Exact.h's binary64 multiply and host/hl_math.c's relxf unit, from
C++20 to C operation for operation, GPL-3.0-or-later (host/PROVENANCE.md). No later change to the unit in either project.

Changed from there: the game stores the zero word at +76 before it reads its inputs, so a result whose zero word lands on
a's or b's bytes is left to the lifted function (upstream reads all first and would differ there); a result over a or b
otherwise stays native and exact. The 80-byte unit runs through run_n / compare_n (run / compare with the length given)
so that units of other sizes do not edit the 32-byte path.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
nx2/oot_patches.py: MATH_GATES gains 0x817036a0 (a line of its own after the tuple), so patch_math_gates puts the same
one-line gate first in f_817036a0, counted in report() with the others. The gate comes with the next lift (oot_patches.py
is in the lift's key); HOS_MATH is read at run time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
tests/test_native_math.py: the relxf kernel against a reference written from 0x817036a0's instructions on 200,000 random
transform pairs, a at the origin giving b, every used input that is a NaN, an infinity or a subnormal given up (and one
in a lane the game never uses answered), a subnormal remainder given up and a zero remainder answered as +0; its gate
(the arguments where the function has them, all 80 bytes written and marked, FPSR and registers left; the result over a
or b answered; the zero word over an input, another float state left to the lifted function), its switch off,
HOS_MATH_UNITS=relxf, its check over all 80 bytes with no recursion. Three broken copies must fail (the rows' sum in
another order, the zero word's overlap rule taken out, the marks taken out). The gate count checks follow MATH_GATES.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
gpu/vk/switches.def is where switches are documented since #1802, and tests/test_switch_table.py fails on a name the
source reads without a row: the native maths units' four switches get theirs (HOS_MATH and HOS_MATH_UNITS attempt, off /
all units; the check and the log diag).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The support point of a convex shape's vertex array along a direction (Havok's support function, which the narrow
phase's GSK iteration calls through the convex shape's thunk) becomes the sixth unit of HOS_MATH: the same bits as the
game's function, a gate first in the lifted f_817472e0, a check mode that compares the 16-byte answer.

Ported from HeyListen's native engine (github.com/Sacrappy/HeyListen, GPL-3.0-or-later): engine/native/src/math/
Support.cpp, include/hl/math/Support.h and engine/host/hl_math.c, written by Sacrappy in 7412a10f and fixed in 1bc4239a
(the rounds start at the fifth vertex for a count of 0 mod 4 and read nothing past the last) and 4bb9c91d (the module's
minus-infinity padding accepted), read at 62b2cb69; merged into Hookshot by NotRexed in 732c2bd1 (the same code at
Hookshot's main 29619a1c), where HL_MATH_CHECK compared 1.8 M calls in three scenes without a mismatch. C++20 to C,
operation for operation.

- host/hos_math_kernels.c, .h: hos_math_support_point and its tables (the jump bytes, the padding, the lane indices and
  step, the three-vertex mask table), read from guest memory at each call as upstream does.
- host/hos_math.c: the unit support (support_native, run_support, compare_n: the answer is 16 bytes, run() and compare()
  take 32); the answer marked for the GPU mirror; answered only in the translation's open float state (FP_OPEN) and while
  the lanes' pointer holds the block the function's first lanes read directly.
- nx2/oot_patches.py: 817472e0 added to MATH_GATES.
- tests/test_native_math.py: the kernel against a reference written from the guest's lanes (counts 1 to 299, ties, a zero
  direction, NaNs past the last vertex), its give-ups, the gate, its declines and check; three broken copies must fail.
- host/PROVENANCE.md: where it comes from.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
fix(lift): the native maths gates declare their context A64_NOESC
Some checks failed
Website / Website on Cloudflare (pull_request_target) Has been skipped
beanstalk/approval waiting for a BigBean's 👍 on the request
PR description / Description follows the template (pull_request_target) Successful in 19s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
2b4874087e
The lifted functions' own context parameter is A64_NOESC; the gates' hos_math_<addr> declarations were
`extern int hos_math_<addr>(Ctx *c)`, so the compiler had to take the context for escaped at the gate's call, in the
function's code whether the switch is on or not. They are now `extern int hos_math_<addr>(Ctx *A64_NOESC c)`, as the
lifted code's own calls are. The host definitions are unchanged. The gates pass no f_<addr>, so they need no forward
declaration for nx2/split_giants.py's pieces.

tests/test_native_math.py looks for the new declaration.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Author
Contributor

Tested in game: Ran in the game in two test trees on main 068fb8eb (Windows, RTX 3090): HOS_MATH_CHECK with all seven units twice, 0 mismatches (0.3 to 8.6 million calls a unit in the second run, the Market and Hyrule Field), then HOS_MATH=1 with nothing visibly different, and on in a 15-run CPU measurement at a 60 fps cap with the other rewrite branches (every run held 60); this head itself is not built or played.

Tested in game: Ran in the game in two test trees on main 068fb8eb (Windows, RTX 3090): HOS_MATH_CHECK with all seven units twice, 0 mismatches (0.3 to 8.6 million calls a unit in the second run, the Market and Hyrule Field), then HOS_MATH=1 with nothing visibly different, and on in a 15-run CPU measurement at a 60 fps cap with the other rewrite branches (every run held 60); this head itself is not built or played.
Author
Contributor

@Beanstalk linux, and amd: this branch with HOS_MATH unset against HOS_MATH=1 (the same build), and main against this branch with the switch unset, each in the child Market for a few minutes and in Hyrule Field by day for a few minutes. Measure the game's CPU seconds with the frame rate capped at 60 (on a PC that holds 60 there), standing and turning the camera, and the frame times uncapped. Expect less CPU time with the switch at 1, the same frame rate or better, no extra CPU time with the switch unset against main, the picture and physics as without the switch, a "math: box native; ... support native;" start line with the switch set, and no "MISMATCH" in the log.

@Beanstalk linux, and amd: this branch with HOS_MATH unset against HOS_MATH=1 (the same build), and main against this branch with the switch unset, each in the child Market for a few minutes and in Hyrule Field by day for a few minutes. Measure the game's CPU seconds with the frame rate capped at 60 (on a PC that holds 60 there), standing and turning the camera, and the frame times uncapped. Expect less CPU time with the switch at 1, the same frame rate or better, no extra CPU time with the switch unset against main, the picture and physics as without the switch, a "math: box native; ... support native;" start line with the switch set, and no "MISMATCH" in the log.
Member

@snaildev99 only members of BigBeans can start the farm.

One of them can approve this request by reacting 👍 to it or to this comment. This request was for 2b48740, and the PR has changed since (92962a4), so a 👍 no longer approves it. Comment @Beanstalk again to ask for the new commit.

@snaildev99 only members of `BigBeans` can start the farm. ~~One of them can approve this request by reacting 👍 to it or to this comment.~~ This request was for `2b48740`, and the PR has changed since (`92962a4`), so a 👍 no longer approves it. Comment `@Beanstalk` again to ask for the new commit.
Member

Possibly related, for context:

  • PR #1880 WIP: perf(lift): fast float regions span guest stores and vector moves, so more of the game's float maths runs as plain SSE — Overlapping host-side float performance work in physics/lifted code
Possibly related, for context: - PR #1880 WIP: perf(lift): fast float regions span guest stores and vector moves, so more of the game's float maths runs as plain SSE — Overlapping host-side float performance work in physics/lifted code
BeanBot left a comment

Recommendation: Request changes. The gates and diagnostic counters have cross-thread data races, and native calls can leave guest floating-point flags different from the lifted routines. The PR also has not shown that its own gain outweighs its default-off and GPU-mirror costs.

The change can replace seven physics and actor maths routines with guarded host implementations, with differential checking and fallback to lifted code. It adds substantial arithmetic and integration code; the reported earlier-tree checks are encouraging, but the current head’s performance and behavior remain unverified.

Does it help: Unproven. The PR reports 2.7% less whole-process CPU standing and 2.4% less turning with all rewrite branches enabled on an older base, not this PR alone. Its isolated offline timings use an older lift, and the current head has no game measurement against its base. Average FPS, 1% lows, frame times, build time, and the suspected mirror-copy cost are also missing.
Other systems: The gates execute even with the switch off and affect lifted-code compilation on Windows and Linux. Native writes can affect GPU mirror traffic on every GPU; the combined run saw the copy thread use 0.010–0.014 more cores, with the responsible rewrite unknown. Linux, AMD, Steam Deck and this head on Windows remain untested. Guest floating-point environment readers can observe flags omitted by the native path.
Switches: HOS_MATH defaults off; HOS_MATH_UNITS defaults to all; HOS_MATH_CHECK and HOS_MATH_LOG default off. All four have table rows. The off default is conservative but was chosen because the change is unmeasured, contrary to the project’s measurement-based switch rule; there is not yet evidence to choose a performance default.

Reviewed: 2b4874087e against a104182b40: Read the repository rules, PR template, standards, commits, gate diff, provenance, host integration and arithmetic excerpts. Skimmed portions of the kernels, header and test fixture; the tool output for those files was truncated, so a full code review remains outstanding. docs/review-checklist.md and the adjacent Tooling checkout were absent. No generated files or translations changed.; nothing run (a code review).
Checked and fine:

  • The new switches have entries in switches.def, and the source provenance identifies the upstream projects and commits.
  • The reported differential runs retained lifted results in check mode and found no output-byte mismatches on the tested earlier trees.
  • The changed files do not include third_party or generated assets.

Blocking (to change before merge)

  1. nx2/oot_patches.py:393: The generated gate reads a volatile int while start() writes it from another worker thread. Volatile does not synchronize C threads, so simultaneous first calls create a data race even with HOS_MATH unset. Use atomic loads and stores for every gate, with initialization published safely.
  2. host/hos_math.c:345: report() reads counters and other_fn entries non-atomically while worker threads update them with atomic operations. With HOS_MATH_LOG or HOS_MATH_CHECK enabled, periodic reports race with active calls, making diagnostic behavior undefined. Read shared fields atomically or take a synchronized snapshot.
  3. host/hos_math.c:57: Requiring IXC alone does not preserve other sticky FPSR flags: an answered call can omit IOC, OFC, UFC, IDC or QC that the lifted routine raises, and a later SDK floating-point environment read can observe a different result. Preserve the flags or decline unless all flags the routine could raise are already set; cover this with a guest-state comparison.
  4. nx2/oot_patches.py:393: The gate changes every affected lifted function even at the default, while the only isolated off measurement used the old lift and found a 12% sphere cost. Measure this head against its base with the switch off and on, including frame pacing, build time and GPU-mirror traffic; reduce or remove any material regression and choose defaults from those results.

Suggestions (the author may take or leave)

  • Complete a full read of the changed kernels, header and test fixture before merging.
  • Run the stated head-specific Windows checks and Tier 1 Linux, AMD and Steam Deck coverage; include the full required test suite and CI results.

Testing: No commands from the project, builds or tests were run, as requested. The PR reports game-free checks and earlier-tree in-game comparisons; this head has not been built or played.
Overlaps and merge order: Not established; the network is closed.


Automated code review by BeanBot (gpt-6.1-terra), on the pull request's opening. Nothing was run; a maintainer decides.

**Recommendation: Request changes.** The gates and diagnostic counters have cross-thread data races, and native calls can leave guest floating-point flags different from the lifted routines. The PR also has not shown that its own gain outweighs its default-off and GPU-mirror costs. The change can replace seven physics and actor maths routines with guarded host implementations, with differential checking and fallback to lifted code. It adds substantial arithmetic and integration code; the reported earlier-tree checks are encouraging, but the current head’s performance and behavior remain unverified. **Does it help:** Unproven. The PR reports 2.7% less whole-process CPU standing and 2.4% less turning with all rewrite branches enabled on an older base, not this PR alone. Its isolated offline timings use an older lift, and the current head has no game measurement against its base. Average FPS, 1% lows, frame times, build time, and the suspected mirror-copy cost are also missing. **Other systems:** The gates execute even with the switch off and affect lifted-code compilation on Windows and Linux. Native writes can affect GPU mirror traffic on every GPU; the combined run saw the copy thread use 0.010–0.014 more cores, with the responsible rewrite unknown. Linux, AMD, Steam Deck and this head on Windows remain untested. Guest floating-point environment readers can observe flags omitted by the native path. **Switches:** HOS_MATH defaults off; HOS_MATH_UNITS defaults to all; HOS_MATH_CHECK and HOS_MATH_LOG default off. All four have table rows. The off default is conservative but was chosen because the change is unmeasured, contrary to the project’s measurement-based switch rule; there is not yet evidence to choose a performance default. **Reviewed:** 2b4874087ee7 against a104182b4037: Read the repository rules, PR template, standards, commits, gate diff, provenance, host integration and arithmetic excerpts. Skimmed portions of the kernels, header and test fixture; the tool output for those files was truncated, so a full code review remains outstanding. docs/review-checklist.md and the adjacent Tooling checkout were absent. No generated files or translations changed.; nothing run (a code review). **Checked and fine:** - The new switches have entries in switches.def, and the source provenance identifies the upstream projects and commits. - The reported differential runs retained lifted results in check mode and found no output-byte mismatches on the tested earlier trees. - The changed files do not include third_party or generated assets. **Blocking** (to change before merge) 1. nx2/oot_patches.py:393: The generated gate reads a volatile int while start() writes it from another worker thread. Volatile does not synchronize C threads, so simultaneous first calls create a data race even with HOS_MATH unset. Use atomic loads and stores for every gate, with initialization published safely. 2. host/hos_math.c:345: report() reads counters and other_fn entries non-atomically while worker threads update them with atomic operations. With HOS_MATH_LOG or HOS_MATH_CHECK enabled, periodic reports race with active calls, making diagnostic behavior undefined. Read shared fields atomically or take a synchronized snapshot. 3. host/hos_math.c:57: Requiring IXC alone does not preserve other sticky FPSR flags: an answered call can omit IOC, OFC, UFC, IDC or QC that the lifted routine raises, and a later SDK floating-point environment read can observe a different result. Preserve the flags or decline unless all flags the routine could raise are already set; cover this with a guest-state comparison. 4. nx2/oot_patches.py:393: The gate changes every affected lifted function even at the default, while the only isolated off measurement used the old lift and found a 12% sphere cost. Measure this head against its base with the switch off and on, including frame pacing, build time and GPU-mirror traffic; reduce or remove any material regression and choose defaults from those results. **Suggestions** (the author may take or leave) - Complete a full read of the changed kernels, header and test fixture before merging. - Run the stated head-specific Windows checks and Tier 1 Linux, AMD and Steam Deck coverage; include the full required test suite and CI results. **Testing:** No commands from the project, builds or tests were run, as requested. The PR reports game-free checks and earlier-tree in-game comparisons; this head has not been built or played. **Overlaps and merge order:** Not established; the network is closed. --- <sub>Automated code review by BeanBot (gpt-6.1-terra), on the pull request's opening. Nothing was run; a maintainer decides.</sub>
@ -0,0 +54,4 @@
/* (the translation's own float gate, RECOMP_FPX_OPEN: FPCR at its default but FZ, the host's inhibit bit 32 included, and
FPSR.IXC already set. With IXC set the native path leaves FPSR as it is and is right to: every value it answers for is
one whose only Arm flag would be IXC. Hookshot's answered without IXC and set it, a flag the game had not raised) */
#define FP_OPEN(c) (((c)->fpsr & FPSR_IXC) && !((c)->fpcr & ~FPCR_FZ))
Member

IXC being set does not cover the other sticky flags the lifted routine can raise. A later floating-point environment read can therefore see a different FPSR; please preserve those flags or decline when they could differ, and test guest state.

IXC being set does not cover the other sticky flags the lifted routine can raise. A later floating-point environment read can therefore see a different FPSR; please preserve those flags or decline when they could differ, and test guest state.
@ -0,0 +342,4 @@
return;
for (int i = 0; i < UNITS; i++) {
const Unit *u = &units[i];
if (!u->calls) continue;
Member

Periodic reports read counters while workers update them atomically. These plain reads race with those writes in log and check modes; please take an atomic or otherwise synchronized snapshot.

Periodic reports read counters while workers update them atomically. These plain reads race with those writes in log and check modes; please take an atomic or otherwise synchronized snapshot.
@ -386,0 +390,4 @@
# Each gets a gate first in its lifted function: hos_math_on_<addr> is 1 until the first call has read the switches, then 0
# unless the unit is on (one load and an untaken branch when off); hos_math_<addr>(c) returns 1 when it answered.
MATH_GATES = ("81a040bc", "80ba6d80", "81746440", "800a8400", "817d9d80")
MATH_GATE = (" if (__builtin_expect(hos_math_on_%s, 0) && hos_math_%s(c)) return; "
Member

This volatile load races with start() writing the gate when workers first enter concurrently, including with the switch unset. Please use atomic gate loads and stores and publish initialization safely.

This volatile load races with start() writing the gate when workers first enter concurrently, including with the switch unset. Please use atomic gate loads and stores and publish initialization safely.
Member

This gate also changes the default-off lifted code. The isolated old-lift measurement found a 12% sphere cost, while the combined game run cannot attribute costs to this PR. Please measure this head against its base, including off, frame pacing, build time and mirror-copy traffic.

This gate also changes the default-off lifted code. The isolated old-lift measurement found a 12% sphere cost, while the combined game run cannot attribute costs to this PR. Please measure this head against its base, including off, frame pacing, build time and mirror-copy traffic.
Co-Authored-By: Codex Sol <[email protected]>
(cherry picked from commit 535655babf38b63776c310086e36f425be658652)
Co-Authored-By: Codex Sol <[email protected]>
(cherry picked from commit cffdaa41e4713ec494e8439a9088e8d616cfa82d)
Conflict resolved on this branch: the test's RUNS/BROKEN tables here are built up per unit (relxf, support), so only BROKEN_SUPPORT_MARK was changed to the A64_MARK line; the rest of the source hunk was already applied by the math pick.

Co-Authored-By: Codex Sol <[email protected]>
(cherry picked from commit 7008a2e6f48e17b5f960fa8f85ff6a46dd8f59b1)
fix(build): the host's native hooks test the watch bitmap too (A64_MARK_BITMAP)
Some checks failed
beanstalk/approval changed since the request: ask again
PR description / Description follows the template (pull_request_target) Successful in 6s
CI / Conventional Commit title (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Has been cancelled
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Has been cancelled
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Has been cancelled
CI / Workflow lint (actionlint) (pull_request) Has been cancelled
CI / Website build and checks (pull_request) Has been cancelled
Guide screenshots / Guide images and local UI source review (pull_request) Has been cancelled
Third-party parts / As their notes say (pull_request) Has been cancelled
Third-party parts / The same as upstream (pull_request) Has been cancelled
92962a41fb
Every host file is now compiled with MARK_BITMAP, the lifted code's own setting, instead of none (or, on the crowd
branch, only host/oot_crowd.c). A native hook's A64_MARK then tests the first page's watch bit exactly as the lifted
store it stands for does, and the hooks' #ifdef A64_GPU_PAGE guards (the uniform-bitmap preflights in reverb and
visibility) are compiled in. hos_mark_written / HOS_WRITTEN, the host's own marks, still mark every page.
RX_MARK_BITMAP=0 turns the bitmap off for the lifted code and the hooks alike.

tests/test_mark_bitmap_host.py reads nx2/build.py (no build) and fails if the lifted flags or a host compile job
lack an unconditional MARK_BITMAP.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
perf(host): HOS_MATH is on by default; =0 runs the maths routines lifted
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
beanstalk/approval changed since the request: ask again
PR description / Description follows the template (pull_request_target) Successful in 20s
e690fad06a
docs/dev/standards.md: a switch ships at its value from the branch's first push, never off because it is new.
The row's kind is kill, unset reads as 1 (env_on_or), and the test runs unset as on and 0 / off as off.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
snaildev99 changed title from WIP: perf(host): seven of the physics' small float routines on host code, bit for bit (HOS_MATH, off by default) to WIP: perf(host): seven of the physics' small float routines on host code, bit for bit (HOS_MATH, on by default) 2026-11-21 07:45:58 +00:00
Author
Contributor

HOS_MATH is now on by default (e690fad0), as docs/dev/standards.md asks: a switch ships at its value from the first push, not off because it is new. HOS_MATH=0 gives main's lifted code. The row's kind is kill, unset reads as on (env_on_or), and the test runs it unset and =1 (on), =0 and =off (off) and check; it fails when the old read is put back. Title and description updated to match.

`HOS_MATH` is now on by default (e690fad0), as docs/dev/standards.md asks: a switch ships at its value from the first push, not off because it is new. `HOS_MATH=0` gives main's lifted code. The row's kind is `kill`, unset reads as on (`env_on_or`), and the test runs it unset and `=1` (on), `=0` and `=off` (off) and `check`; it fails when the old read is put back. Title and description updated to match.
HumanBean left a comment

Recommendation: Request changes. By their own description, the kernels are the game's routines transcribed one guest operation per line, in the guest's order. CONTRIBUTING.md's Licensing section rules that out ("no decompiled game code or anything generated from the dump"), and the sibling PR whose kernels were written the same way is blocked for the same reason. Apart from that, the code is careful and its exactness evidence is strong, but this branch's gain hasn't been measured on its own.

For a player, seven of the physics' and actor loops' small float routines run as plain host C with the game's exact results, which means less CPU per frame. The code is clean: the gates fall back to the lifted function, the kernels write nothing when they decline, there is a check mode and the marks match the lifted stores. Against that, it adds about 1,900 lines of host C and about 1,400 lines of test, and its gain isn't separated from eight other branches.

Does it help: Unproven. The only game measurement is the test/rewrites tree, which has nine rewrite branches on together, on main 068fb8eb: −2.7 % process CPU standing and −2.4 % turning (median of 3, Windows, RTX 3090, 60 fps cap). How much of that comes from these seven units isn't known. The offline per-call factors (1.2x to 4.9x) are measured against the old lift's form, not main's. relxf and support weren't timed at all.
Other systems:

  • Every OS, GPU and CPU runs this path (the gates are in the lifted code). Linux and AMD are untested, and this head wasn't built or played anywhere.
  • The GPU mirror: native writes now go through A64_MARK with the lifted store's watched-page filter (nx2/build.py gives every host file MARK_BITMAP; hos_math.c is the only host file that uses A64_MARK today, so nothing else changes). The open "capture copy +10–18 %" observation from the combined run isn't attributed to any branch.
  • FPSR: the native path doesn't raise the sticky IOC/OFC/UFC/IDC flags that the lifted code raises from unused lanes. Main's fast float regions keep those flags exact (nx2/fastfp.py). Only the SDK's fenv functions read FPSR.
  • Build: the patched text is in the lift's key, so there is one re-lift. The build-time cost is small (one line per function), and nothing was measured.

Switches:

  • HOS_MATH (kill, on by default; =0 gives main's lifted code). The on default is consistent with standards.md and with the exactness evidence (0 mismatches over 0.3–8.6 M calls per unit in game, plus the Unicorn differential runs). But it rests on a combined measurement, so it is Unproven alone. I left the default as it is. The farm run should confirm it.
  • HOS_MATH_UNITS (attempt, "all"), HOS_MATH_CHECK and HOS_MATH_LOG (diag, off): the defaults are right, and these are developer switches.
  • All four have rows in gpu/vk/switches.def, and the code reads them the way the rows say (env_on_or("HOS_MATH", 1)).

Reviewed: e690fad06 against its fork point a104182b4. I also rebased it onto main b62932a96: the only conflict was a textual one in nx2/oot_patches.py's report(), where I kept both main's "sprint press" warning and the PR's "math gates" warning, and the interdiff to the PR head is empty.

  • Read in full: host/hos_math.c, host/hos_math_kernels.c, host/hos_math_kernels.h, the nx2/oot_patches.py and nx2/build.py hunks, gpu/vk/switches.def, host/PROVENANCE.md and tests/test_mark_bitmap_host.py.
  • Skimmed: tests/test_native_math.py (its docstring, and what it runs: it compiles and runs the host files in a temp directory and downloads nothing).
  • Nothing run (a code review). I couldn't check the kernels against the game's instructions without the game.

Fixed in this review: nothing pushed. The permission system refused the push to snaildev99's fork, so here are the two changes as suggestions:

  1. Rebase onto main (the report() conflict above: keep both if blocks).
  2. host/PROVENANCE.md:57: the support unit's answer is marked with A64_MARK (with the lifted store's watched-page filter), not HOS_WRITTEN (stale since 845dd761). The description's "Every byte the host writes is marked for the GPU mirror (HOS_WRITTEN)" is stale in the same way.

Checked and fine:

  • Each gate goes first in its function, is inserted once (and not again on a re-patch), on the supported build only, with a count warning in report().
  • start() publishes the unit state with a release store of started. run*() re-reads it with an acquire load, so a thread that saw a stale gate of 1 still sees the unit off.
  • FP_OPEN is stricter than the lift's RECOMP_FPX_OPEN (every FPCR bit but FZ), so it only declines more.
  • Every native path computes into scratch and writes only after the kernel succeeds. A declining kernel writes nothing.
  • The check mode runs the lifted function with bypass set, so nested gates don't recurse, and the game keeps the lifted bytes.
  • relxf declines a zero word that lands on an input. The kernel reads all of a and b before any write.
  • support answers only while the lanes' pointer holds the block the starts read. Its rounds end at n.
  • A64_MARK covers the first and last byte's pages, and every native range is 192 bytes or less, so at most two pages.
  • The 192-byte body struct's offsets are pinned by _Static_asserts.
  • The counters are only touched when they are logged.
  • The switch rows, kinds and read are consistent.
  • There is no duplicate hook with 1899. It hooks f_80ba37a0 and the sphere merge f_800a8000 (unspliced), and its test asserts f_80ba6d80 isn't hooked, while this PR hooks 0x80ba6d80 and 0x800a8400.

Blocking (to change before merge)

  1. host/hos_math_kernels.c:3-4 and host/hos_math_kernels.h:7-8: licensing. The header says "Each line is one guest operation, in the guest's order; the comments name the guest function a sequence is read from". That is a statement-for-instruction transcription of the game's code, and porting it through Hookshot/HeyListen (GPL) doesn't change what it was made from. The clearest cases are:

    • hos_math_body_box_update (:434-738), the game's per-body solver step, transcribed op by op, with the game's own literal constants (HM_BYTE, HM_SPEED, HM_ROUND, HM_CAST at :404-407), its stream-record tags (0x0000000403000010, 0x0000000d04000010 at :701, :714), its block limit (4048, :696) and its lane shuffles (:488-489, "the guest's lane order" at :588);
    • relxf (:757: "The guest (111 instructions ...)") and its lane-shuffled columns (:803-808);
    • support's jump-table cases (:852-890) and the three-vertex select table.

    The affine box (:317) and the vertex-set box (:207) are standard maths (centre ± |M|·e, a min/max over transformed points). Their operation order is arguably a fact needed for bit-exactness, but even those are presented as read from the guest. CONTRIBUTING.md allows host code written from facts about behaviour, described in your own words. It doesn't allow the game's functions re-expressed in C. Fixing it means either rewriting the units from a behavioural spec (what each computes, the order of roundings as a documented fact, and a public source where there is one), or a maintainer ruling that this form is acceptable. Either way, it also applies to the siblings that port the same engine.

Suggestions (the author may take or leave)

  • nx2/oot_patches.py:443 / host/hos_math.c:70: the gate is a plain volatile int that start() writes while other threads read it. That's benign on x86-64 and arm64, but formally a data race. __atomic_load_n(&hos_math_on_X, __ATOMIC_RELAXED) in the generated gate costs the same and settles it (BeanBot's point 1: still there, not blocking).
  • host/hos_math.c:338-366: report() reads counters with plain loads while workers add to them atomically. It's diagnostic only. Relaxed atomic loads would make it clean (BeanBot's point 2: still there, not blocking).
  • 1899 implements f_80ba6d80's box-under-a-matrix arithmetic again (host/bounds_kernels.c) next to hos_math_affine_box. Two kernels of the same routine should become one.

Questions

  • BeanBot's FPSR point (3): in the two in-game check runs, what did fpsr-differs count per unit? Does the game ever call the SDK's fenv functions (fetestexcept and the like) while these units run? If it doesn't, please say so in Risks, and the point is settled. If it does, a unit whose lifted run raised a flag has to decline.

Testing: the in-game evidence (check mode with 0 mismatches, then on) is from older test trees on main 068fb8eb, Windows/NVIDIA only. This head and main b62932a96 are neither built nor played. Still missing:

  • this branch's own measurement (HOS_MATH unset against =0, and against main);
  • Linux and AMD;
  • the head on Windows in check mode.

The Beanstalk request was made for 2b48740 and has lapsed (it needs a BigBean's 👍 on a new request). CI is pending on e690fad0 (blocked by required conditions). The rule from the Wait still applies once the licensing point is settled: on its own measurement and out of WIP.
Overlaps and merge order: 1899 adds the identical nx2/build.py MARK_BITMAP hunk and tests/test_mark_bitmap_host.py (the same blob), so whichever merges second will merge cleanly or need a trivial rebase. 1900, 1902, 1903, 1904, 1909, 1910, 1911, 2001 and 2036 add rows to switches.def and hooks in nx2/oot_patches.py next to these. They're independent hooks, and each needs only a textual rebase after the other. Settle the licensing question for the whole family once.

**Recommendation: Request changes.** By their own description, the kernels are the game's routines transcribed one guest operation per line, in the guest's order. CONTRIBUTING.md's Licensing section rules that out ("no decompiled game code or anything generated from the dump"), and the sibling PR whose kernels were written the same way is blocked for the same reason. Apart from that, the code is careful and its exactness evidence is strong, but this branch's gain hasn't been measured on its own. For a player, seven of the physics' and actor loops' small float routines run as plain host C with the game's exact results, which means less CPU per frame. The code is clean: the gates fall back to the lifted function, the kernels write nothing when they decline, there is a check mode and the marks match the lifted stores. Against that, it adds about 1,900 lines of host C and about 1,400 lines of test, and its gain isn't separated from eight other branches. **Does it help:** Unproven. The only game measurement is the `test/rewrites` tree, which has nine rewrite branches on together, on main 068fb8eb: −2.7 % process CPU standing and −2.4 % turning (median of 3, Windows, RTX 3090, 60 fps cap). How much of that comes from these seven units isn't known. The offline per-call factors (1.2x to 4.9x) are measured against the old lift's form, not main's. relxf and support weren't timed at all. **Other systems:** - Every OS, GPU and CPU runs this path (the gates are in the lifted code). Linux and AMD are untested, and this head wasn't built or played anywhere. - The GPU mirror: native writes now go through `A64_MARK` with the lifted store's watched-page filter (`nx2/build.py` gives every host file `MARK_BITMAP`; `hos_math.c` is the only host file that uses `A64_MARK` today, so nothing else changes). The open "capture copy +10–18 %" observation from the combined run isn't attributed to any branch. - FPSR: the native path doesn't raise the sticky IOC/OFC/UFC/IDC flags that the lifted code raises from unused lanes. Main's fast float regions keep those flags exact (`nx2/fastfp.py`). Only the SDK's fenv functions read FPSR. - Build: the patched text is in the lift's key, so there is one re-lift. The build-time cost is small (one line per function), and nothing was measured. **Switches:** - `HOS_MATH` (kill, on by default; `=0` gives main's lifted code). The on default is consistent with standards.md and with the exactness evidence (0 mismatches over 0.3–8.6 M calls per unit in game, plus the Unicorn differential runs). But it rests on a combined measurement, so it is Unproven alone. I left the default as it is. The farm run should confirm it. - `HOS_MATH_UNITS` (attempt, "all"), `HOS_MATH_CHECK` and `HOS_MATH_LOG` (diag, off): the defaults are right, and these are developer switches. - All four have rows in `gpu/vk/switches.def`, and the code reads them the way the rows say (`env_on_or("HOS_MATH", 1)`). **Reviewed:** e690fad06 against its fork point a104182b4. I also rebased it onto main b62932a96: the only conflict was a textual one in `nx2/oot_patches.py`'s `report()`, where I kept both main's "sprint press" warning and the PR's "math gates" warning, and the interdiff to the PR head is empty. - Read in full: `host/hos_math.c`, `host/hos_math_kernels.c`, `host/hos_math_kernels.h`, the `nx2/oot_patches.py` and `nx2/build.py` hunks, `gpu/vk/switches.def`, `host/PROVENANCE.md` and `tests/test_mark_bitmap_host.py`. - Skimmed: `tests/test_native_math.py` (its docstring, and what it runs: it compiles and runs the host files in a temp directory and downloads nothing). - Nothing run (a code review). I couldn't check the kernels against the game's instructions without the game. **Fixed in this review:** nothing pushed. The permission system refused the push to snaildev99's fork, so here are the two changes as suggestions: 1. Rebase onto main (the `report()` conflict above: keep both `if` blocks). 2. `host/PROVENANCE.md:57`: the support unit's answer is marked with `A64_MARK` (with the lifted store's watched-page filter), not `HOS_WRITTEN` (stale since 845dd761). The description's "Every byte the host writes is marked for the GPU mirror (`HOS_WRITTEN`)" is stale in the same way. **Checked and fine:** - Each gate goes first in its function, is inserted once (and not again on a re-patch), on the supported build only, with a count warning in `report()`. - `start()` publishes the unit state with a release store of `started`. `run*()` re-reads it with an acquire load, so a thread that saw a stale gate of 1 still sees the unit off. - `FP_OPEN` is stricter than the lift's `RECOMP_FPX_OPEN` (every FPCR bit but FZ), so it only declines more. - Every native path computes into scratch and writes only after the kernel succeeds. A declining kernel writes nothing. - The check mode runs the lifted function with `bypass` set, so nested gates don't recurse, and the game keeps the lifted bytes. - relxf declines a zero word that lands on an input. The kernel reads all of a and b before any write. - support answers only while the lanes' pointer holds the block the starts read. Its rounds end at n. - `A64_MARK` covers the first and last byte's pages, and every native range is 192 bytes or less, so at most two pages. - The 192-byte body struct's offsets are pinned by `_Static_assert`s. - The counters are only touched when they are logged. - The switch rows, kinds and read are consistent. - There is no duplicate hook with [1899](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1899). It hooks f_80ba37a0 and the sphere merge f_800a8000 (unspliced), and its test asserts f_80ba6d80 isn't hooked, while this PR hooks 0x80ba6d80 and 0x800a8400. **Blocking** (to change before merge) 1. `host/hos_math_kernels.c:3-4` and `host/hos_math_kernels.h:7-8`: licensing. The header says "Each line is one guest operation, in the guest's order; the comments name the guest function a sequence is read from". That is a statement-for-instruction transcription of the game's code, and porting it through Hookshot/HeyListen (GPL) doesn't change what it was made from. The clearest cases are: - `hos_math_body_box_update` (`:434-738`), the game's per-body solver step, transcribed op by op, with the game's own literal constants (`HM_BYTE`, `HM_SPEED`, `HM_ROUND`, `HM_CAST` at `:404-407`), its stream-record tags (`0x0000000403000010`, `0x0000000d04000010` at `:701`, `:714`), its block limit (4048, `:696`) and its lane shuffles (`:488-489`, "the guest's lane order" at `:588`); - relxf (`:757`: "The guest (111 instructions ...)") and its lane-shuffled columns (`:803-808`); - support's jump-table cases (`:852-890`) and the three-vertex select table. The affine box (`:317`) and the vertex-set box (`:207`) are standard maths (centre ± |M|·e, a min/max over transformed points). Their operation order is arguably a fact needed for bit-exactness, but even those are presented as read from the guest. CONTRIBUTING.md allows host code written from facts about behaviour, described in your own words. It doesn't allow the game's functions re-expressed in C. Fixing it means either rewriting the units from a behavioural spec (what each computes, the order of roundings as a documented fact, and a public source where there is one), or a maintainer ruling that this form is acceptable. Either way, it also applies to the siblings that port the same engine. **Suggestions** (the author may take or leave) - `nx2/oot_patches.py:443` / `host/hos_math.c:70`: the gate is a plain `volatile int` that `start()` writes while other threads read it. That's benign on x86-64 and arm64, but formally a data race. `__atomic_load_n(&hos_math_on_X, __ATOMIC_RELAXED)` in the generated gate costs the same and settles it (BeanBot's point 1: still there, not blocking). - `host/hos_math.c:338-366`: `report()` reads counters with plain loads while workers add to them atomically. It's diagnostic only. Relaxed atomic loads would make it clean (BeanBot's point 2: still there, not blocking). - [1899](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1899) implements f_80ba6d80's box-under-a-matrix arithmetic again (`host/bounds_kernels.c`) next to `hos_math_affine_box`. Two kernels of the same routine should become one. **Questions** - BeanBot's FPSR point (3): in the two in-game check runs, what did `fpsr-differs` count per unit? Does the game ever call the SDK's fenv functions (`fetestexcept` and the like) while these units run? If it doesn't, please say so in Risks, and the point is settled. If it does, a unit whose lifted run raised a flag has to decline. **Testing:** the in-game evidence (check mode with 0 mismatches, then on) is from older test trees on main 068fb8eb, Windows/NVIDIA only. This head and main b62932a96 are neither built nor played. Still missing: - this branch's own measurement (HOS_MATH unset against `=0`, and against main); - Linux and AMD; - the head on Windows in check mode. The Beanstalk request was made for 2b48740 and has lapsed (it needs a BigBean's 👍 on a new request). CI is pending on e690fad0 (blocked by required conditions). The rule from the Wait still applies once the licensing point is settled: on its own measurement and out of WIP. **Overlaps and merge order:** [1899](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1899) adds the identical `nx2/build.py` MARK_BITMAP hunk and `tests/test_mark_bitmap_host.py` (the same blob), so whichever merges second will merge cleanly or need a trivial rebase. [1900](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1900), [1902](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1902), [1903](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1903), [1904](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1904), [1909](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1909), [1910](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1910), [1911](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/1911), [2001](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2001) and [2036](https://git.magicbean.dev/MagicBeanDev/Longshot/pulls/2036) add rows to `switches.def` and hooks in `nx2/oot_patches.py` next to these. They're independent hooks, and each needs only a textual rebase after the other. Settle the licensing question for the whole family once.
@ -36,0 +54,4 @@
`engine/host/hl_math.c`; written by Sacrappy in commit `7412a10f`, fixed in `1bc4239a` and `4bb9c91d`, read at
`62b2cb69`), as merged into Hookshot by NotRexed (commit `732c2bd1`, the same code at Hookshot's `upstream/main`
`29619a1c`), ported from C++20 to C operation for operation. Changed: Longshot's gate, the 16-byte answer marked
(`HOS_WRITTEN`), the float gate `FP_OPEN`, an answer only while the lanes' pointer holds the block the function's
Owner

Nit: the answer is marked with A64_MARK (with the lifted store's watched-page filter) since 845dd761, not HOS_WRITTEN. My push of this fix was refused, so it's a suggestion.

Nit: the answer is marked with `A64_MARK` (with the lifted store's watched-page filter) since 845dd761, not `HOS_WRITTEN`. My push of this fix was refused, so it's a suggestion.
@ -0,0 +1,923 @@
/* (longshot) Bit-exact native kernels (hos_math_kernels.h has the rules and where they come from). Ported from Hookshot's
native engine, Hookshot commit 60a53da7 (NotRexed; the engine from the HeyListen fork of Hookshot, GPL-3.0-or-later):
native/include/hl/math/Exact.h, native/src/math/Bounds.cpp and BodyBox.cpp, C++20 to C, operation for operation. Each
line is one guest operation, in the guest's order; the comments name the guest function a sequence is read from. */
Owner

Licensing (blocking): "each line is one guest operation, in the guest's order" describes a transcription of the game's code. CONTRIBUTING.md forbids that, even when it comes through Hookshot/HeyListen. The units need to be written from a behavioural spec, or a maintainer has to rule on it. Details are in the review summary.

Licensing (blocking): "each line is one guest operation, in the guest's order" describes a transcription of the game's code. CONTRIBUTING.md forbids that, even when it comes through Hookshot/HeyListen. The units need to be written from a behavioural spec, or a maintainer has to rule on it. Details are in the review summary.
@ -0,0 +431,4 @@
r[2][2] = hm_sub32(g, hm_sub32(g, 1.0f, xx), yy);
}
int hos_math_body_box_update(const HosMathBodyIn *in, HosMathBodyOut *out, int *why) {
Owner

The clearest case: the game's per-body solver step, op by op, with its literal constants (:404-407), record tags (:701, :714), block limit (:696) and lane shuffles (:488-489, :588).

The clearest case: the game's per-body solver step, op by op, with its literal constants (:404-407), record tags (:701, :714), block limit (:696) and lane shuffles (:488-489, :588).
@ -0,0 +754,4 @@
/* ---- the relative placement of two precise transforms, 0x817036a0 (relxf) ----
Ported from HeyListen's engine/native/src/math/Support.cpp, relative_transform (commit 7412a10f, by Sacrappy; the same
code in Hookshot since 732c2bd1, merged by NotRexed), and Exact.h's binary64 multiply, C++20 to C operation for
operation. GPL-3.0-or-later. The guest (111 instructions; x1 = a, x2 = b, x0 = the result) stores the zero word first,
Owner

"The guest (111 instructions ...)": relxf too is presented as read from the instructions.

"The guest (111 instructions ...)": relxf too is presented as read from the instructions.
@ -386,0 +415,4 @@
if gate in out: # (made before: a lift patched again)
continue
at = start + len(head)
out = (out[:at] + gate + out[at:]).replace(
Owner

Suggestion: extern volatile int read while start() writes it. It's benign on x86-64/arm64, but __atomic_load_n(&hos_math_on_%s, __ATOMIC_RELAXED) in MATH_GATE costs the same and removes the formal data race.

Suggestion: `extern volatile int` read while `start()` writes it. It's benign on x86-64/arm64, but `__atomic_load_n(&hos_math_on_%s, __ATOMIC_RELAXED)` in MATH_GATE costs the same and removes the formal data race.
Some checks are pending
CI / Conventional Commit title (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 1/2) (pull_request) Blocked by required conditions
CI / Python source checks (ubuntu-24.04, part 2/2) (pull_request) Blocked by required conditions
CI / SPIR-V fixtures (global narrow load/store, direct surface atomics, F2I immediate) (pull_request) Blocked by required conditions
CI / Workflow lint (actionlint) (pull_request) Blocked by required conditions
CI / Website build and checks (pull_request) Blocked by required conditions
Guide screenshots / Guide images and local UI source review (pull_request) Blocked by required conditions
Third-party parts / As their notes say (pull_request) Blocked by required conditions
Third-party parts / The same as upstream (pull_request) Blocked by required conditions
beanstalk/approval changed since the request: ask again
PR description / Description follows the template (pull_request_target) Successful in 20s
This pull request has changes conflicting with the target branch.
  • nx2/oot_patches.py
Some workflows are waiting to be reviewed.
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u 'perf/native-math:snaildev99-perf/native-math'
git switch 'snaildev99-perf/native-math'
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
4 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
MagicBeanDev/Longshot!1898
No description provided.