WIP: perf(host): twelve of the game's maths functions on host code, bit for bit, from Arm optimized-routines and musl (HOS_NATIVE_LIBM, on by default) #1904
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
4 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
MagicBeanDev/Longshot!1904
Loading…
Reference in a new issue
No description provided.
Delete branch "snaildev99/Longshot:perf/native-libm"
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?
Summary
Twelve of the game's maths library functions run on host code with the game's exact results, behind a new switch, on by default (
HOS_NATIVE_LIBM=0gives the lifted code):powf,expf,exp2f,sinf,cosf,sincosf,logf,log2f,log10f,atan2f,acosfandacos. Together the game's maths exports hold 2.03 % of its code time in the Market profile (powfalone 0.94 %, the cloth damping calls it twice a particle). The host's versions are vendored, not written here: Arm optimized-routines 23.01 (sinf,cosf,sincosf,powf,expf,exp2f,logf,log2f, MIT OR Apache-2.0 WITH LLVM-exception) and musl 1.2.5 (log10f,atan2fwith theatanfit calls,acosf,acos,fmodf, MIT), with their licence files andhost/libm/PROVENANCE.md; on every input the hook answers they give the game's bits (below). Two changes are marked in the sources:acosfreturns the game's pi/2 and pi for its two shortcut cases, and the Armroundtoint/converttointare an inline exact round instead of a call into the C runtime (same values; re-proven).fmodfis compiled and proven but not hooked: it cannot beat the lifted routine by more than the hook costs.The goal is less CPU work for the same results. A call the host cannot prove exact returns 0 and the game's own routine runs, nothing written. The host answers only on a CPU with FMA and AVX2; with FPCR 0 or only FZ; with FPSR.IXC set (the lift's own fast-path condition); with no NaN in or out; under FZ with no denormal input and no result below twice the smallest normal (Arm flushes before rounding, x86 after); and, when FPSR lacks any other sticky flag, only for results that raise none (no infinity from finite inputs, no result below 2^-125). Inputs for which the game's routine returns at once (
powfwith |y| = 1, the logs of 1,acosfandacosof ±1,atan2fwith y = 0) are left to it. FPSR and NZCV are left as the caller had them, which is what the game's routine leaves under these rules; MXCSR is set to the proof's environment only when its mode bits differ, and put back.How:
host/oot_libm.cdefines each hook withENTRY_HOOK_SYM_FN, whichnx2/entry_hooks.pysplices at the SDK export's entry, the way main's memcpy hook is (no guest address in the host code); the splice passes the lifted function, so check mode can run it, and declares it in the line, becausenx2/split_giants.pymay put the splice in a piece written beforef_<addr>. With the switch unset each hook returns at its first line (a load and a branch).nx2/build.pycompileshost/libmwith the flags the proof used (-O2 -march=x86-64-v3 -ffp-contract=on -fno-builtin, musl's own), the header bytes in the object key.Read first:
host/oot_libm.c(libm_gate,libm_ixc1,libm_ixc2,libm_ixc64,libm_trivial1,libm_trivial2),host/libm/PROVENANCE.md,nx2/entry_hooks.py(ENTRY_HOOK_SYM_FN), the block innx2/build.py,tests/test_libm_hooks.py,tests/test_libm_pins.py, the two rows ingpu/vk/switches.def.Testing
Tested: Windows 11, RTX 3090 (NVIDIA 610.88), Ryzen 9 5900X. In the game: this branch's code at 12c1bb41 (the gate and counters of this PR, before the rebase) merged with other rewrite branches into a test tree on main
068fb8eb,HOS_NATIVE_LIBM=1in the CPU measurement below (the Market, 15 runs at a 60 fps cap, every run held 60). Its start line (oot: libm: host maths on) is in every log of those runs; no counter was logged (HOS_NATIVE_LIBM_LOGwas not set), so how many calls it answered there is not known. It has not been run in check mode in the game. This head (the rebase onto current main and the upkeep commit) is not built or played.Exactness offline. The game's own routines are the reference: cut from the stock lift with every function they call and compiled as the game's hot code is, and in Unicorn on the executable for the saved bins. The vendored functions are compiled with the build's flags.
sinf,cosf,sincosf,expf,exp2f,logf,log2f,log10f,acosf) on every one of the 4,294,967,296 float inputs, with FPCR 0 and with FZ, twice: with the caller's FPSR at 0x9F, and with FPSR at 0x10 (IXC alone) under the rules above. 0 results differ on inputs the hook answers and 0 calls leave FPSR other than the game's (for examplesinf: 4,244,635,650 inputs answered with FPSR 0x10 in nearest).powf,atan2fandacos(two arguments, or a double: not sweepable whole): an edge set (signed zeros, denormals, the smallest normal and twice it, integers -150 to 150, powers of two, half-integers, exponents near overflow and the bottom of the normal range: 211,600 pairs; 18 doubles foracos) and a 600,000,000-input sample a mode (an eighth any bit pattern, an eighth any finite pair, the rest targeted:powfwith y·log2(x) near 128, -125, -126, -149, -150, negative x with whole y, x near 1;atan2fnear the bottom of the normal range and huge over tiny;acoswithin a million ulps of ±1 and tiny): 0 differences, 0 FPSR changes (powf270,947,109 sampled inputs answered in nearest).fmodf(not hooked): the same edge set and sample, 0 differences.prove.py).tests/libm_pins.txtfreezes 4,160 of the game's results (256 nearest and 64 flush-to-zero a function), so a compiler that contracts differently failstests/test_libm_pins.py: 4,160 pins, 0 bad at this head.Game-free checks at this head:
tests/test_libm_hooks.py(the splice and its declaration, the switch, the declines: a NaN in or out, FPCR other than 0 or FZ, FPSR without IXC, a denormal under FZ, a tiny result, an infinity from finite inputs under IXC alone and answered with 0x9F, the trivial inputs; MXCSR left alone or set and put back; check mode and re-entry; the counters' line;sincosf's guest writes;fmodfnot hooked),tests/test_libm_pins.py,tests/test_switch_table.py(731 switches) andtests/test_patch_hooks_defined.pypass, and so do 36 of the other 41 tests that readnx2/oot_patches.pyornx2/entry_hooks.py. The other five fail here and on maina104182balike:test_dev_packs,test_high_fps_fixes,test_libc_hooks(test_the_lift_splices_themlooks forL.discover(innx2/lift.py, the same error on main),test_lift_cache,test_song_note_cue.tests/run_all.pywas not run.Still to test:
HOS_NATIVE_LIBM=0against unset (on) (withHOS_NATIVE_LIBM_LOG=1on that side), and main against this branch with it unset; the game's CPU seconds with the frame rate capped at 60, and the frame times uncapped. Expecting less CPU time with the switch on, the same frame rate or better, no cost with it=0, the picture as without it, andoot: libm: <function>: calls ... answered ...lines showing calls answered (@Beanstalk linux).HOS_NATIVE_LIBM=check HOS_NATIVE_LIBM_LOG=1in the Market and Hyrule Field, expectingansweredcounts above 0 (calls arrive with IXC set) andmismatches 0on every function's line and noresult differsline (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 are not among them), at a 60 fps cap in the Market, the nine on used 2.7 % less CPU standing and 2.4 % less turning (the whole process) than main, and the hooks in with every switch off cost nothing measurable (-0.3 % / -0.2 %). How much of that is this branch is not separated (the maths functions and another branch's cache of repeated
powfresults share the same calls); the Test Farm request asks for this branch's own number.Baseline: main
068fb8eb(#1802) against the test treetest/rewrites@ e614ef49 (main068fb8ebwith nine rewrite branches: the seven maths units, the maths library (this branch at 12c1bb41), animation, bounds, crowd, visibility, glue, the effect triggers with the six-channel reverb, and the solver; the cloth and particle effect rewrites, the two- and four-channel reverb and later pieces of other branches are not among them), lifted withA64_FPXKEEP=0so that the lift is main's apart from the hooks. Not this PR alone: the run-time switches were all on together. Main has moved 28 commits since068fb8eb; 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. 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).
068fb8ebThe off figures are inside the runs' spread. The process includes the renderer's threads (about 1.4 cores standing), which the nine do not touch; on the executor threads the nine on were -4.1 % / -4.0 %.
Offline (CPU time a call, not the game; the thread's own CPU time, best of 9, Ryzen 9 5900X with other jobs running: take ±10 %; nearest, caller FPSR 0x10). An entry hook puts its function in the old context form; without the hook main gives these functions its register locals. No lift of main's form with these functions was at hand, so "main's form" is an approximation made offline from the stock lift (six registers as C locals), at most as fast as main's real form; for these float functions it measures the same as the old form within the noise.
powf, x 0.01..4, y -8..8sinf, -3.2..3.2cosf, -3.2..3.2sincosf, -3.2..3.2expf, -10..10exp2f, -10..10logf, 0.01..100log2f, 0.01..100log10f, 0.01..100atan2f, -100..100acosf, -1..1acos, -1..1The hook's cost: with
HOS_NATIVE_LIBM=00.5 to 1.7 ns a call; a trivial input (powfwith y = 1, as the cloth damping calls it at exactly 60 fps) about 1 ns over the game's own early return (4.8 against 3.8 to 4.0 ns); a call declined before computing about 4 to 5 ns before the lifted routine runs; withHOS_NATIVE_LIBM_LOG=1about 4 to 5 ns more a call. Under FZ the hook writes MXCSR and puts it back, +1 to 2.5 ns. Estimated from the Market profile's samples and these factors (an estimate, not measured in the game): about 1.5 % of the game's code time,powfand the exponentials 0.83 points of it, sine and cosine 0.48, the inverse cosines andatan2f0.20, the logarithms 0.02; it holds if the game's calls look like these inputs and arrive with IXC set.The switch
HOS_NATIVE_LIBM(new, run time,kill, on by default):0off (the game's own maths); unset or1(any value not starting with c) the host answers where it is exact; a value starting withcruns the host answer and the lifted function, keeps the lifted result and logs the first difference per function (oot: libm: <function> result differs: native ... lifted ..., orFPSR or NZCV differ and the result matched).HOS_NATIVE_LIBM_LOG=1logs one line per function every 10 s and at exit:oot: libm: powf: calls N answered N (with IXC only N) declined off N cpu N fpcr N fpsr N guest N nan-in N denormal-in N trivial N nan-out N tiny-out N flag-out N, in check mode with; compared N mismatches N fpsr-differs N; with the switch off it counts every call as declined "off", which gives the call rates. 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_NATIVE_LIBM=0gives 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 result would move whatever the game computes with it (animation, cloth, camera, sound angles). Every answered input of the nine one-argument functions is proven, and a 600,000,000-input sample a mode of the other three. Not claimed: registers other than the result (the game's routine changes caller-saved registers the hook leaves alone); FPSR states between 0x10 and 0x9F other than those two are argued from the lift's design (its bodies only OR flags), not swept. A CPU without FMA and AVX2 declines every call. Open: in the CPU measurement the renderer's
mkw_vk capture copythread used 10 to 18 % more CPU (0.009 to 0.017 cores) in both variants with the nine on and in neither without; which rewrite causes it is not determined (sincosf's two results are this branch's only guest writes, marked withHOS_WRITTEN).Turning it off:
HOS_NATIVE_LIBM=0.After updating: the routines run on the host by default, with the game's exact results; saves,
longshot_options.iniand the caches do not change. The build compiles 20 more small objects (host/libm).Checklist
Ready for review
main; the title is a Conventional Commit.mise run testpasses (any failure that also fails onmainis named). (The 40 tests that read the changed files pass, the five failures are main's; the whole suite not run.)main, or Measurements says why there is nothing to measure. (Measured with nine rewrite branches merged; this branch alone not yet.)third_party/untouched; nothing GPL-incompatible. (The vendored maths are MIT and MIT OR Apache-2.0 WITH LLVM-exception, inhost/libmwith their licence files andPROVENANCE.md.)When it applies
gpu/vk/switches.def. (The rows are there; on by default,=0gives main's lifted code; this PR's own measurement can still turn it off.)tests/that reads changed source is updated, not weakened. (tests/test_libm_hooks.pyandtests/test_libm_pins.py, new;tests/test_libc_hooks.pyreads the new hook form.)Assisted-by:/Co-Authored-By:trailers. (Every commit but027fa913, the first round, which has no trailer.)Ready to merge
main, measured again or a comment says why the numbers still hold.🤖 Generated with Claude Code
Tested in game: In a test tree of nine merged rewrite branches on main
068fb8eb(Windows, RTX 3090), HOS_NATIVE_LIBM=1 ran in a 15-run CPU measurement in the Market at a 60 fps cap (every run held 60, the start line in every log) with no counter logged, so how many calls it answered there is not known; it has not been run in check mode in the game, and this head itself is not built or played.@Beanstalk linux: this branch with HOS_NATIVE_LIBM unset against HOS_NATIVE_LIBM=1 (the same build, with HOS_NATIVE_LIBM_LOG=1 on the =1 side), 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 it unset against main, the picture as without it, and "oot: libm: : calls ... answered ..." lines in the log every 10 s; please report the last line of each function (they say how many calls the host answered).
@snaildev99 only members of
BigBeanscan start the farm.One of them can approve this request by reacting 👍 to it or to this comment.This request was for936118d, and the PR has changed since (f8c82c1), so a 👍 no longer approves it. Comment@Beanstalkagain to ask for the new commit.Possibly related, for context:
Recommendation: Request changes. The new tests depend on a developer-specific compiler path, the CPU gate does not cover all instructions permitted by the build flags, and the committed reference results are derived from the game. The change’s own performance benefit and default cost are also unmeasured.
This adds optional native implementations of twelve game maths functions, with fallback to the lifted routines and a comparison mode. The offline results are promising, but the additional hooks and objects add complexity and an unmeasured cost for all players, even with the optimization off.
Does it help: Unproven. The reported 2.7% and 2.4% reductions in whole-process CPU use came from nine changes enabled together on an older base, with no count of calls answered by this change. The per-call microbenchmarks do not establish an in-game gain or frame-time effect for this PR alone.
Other systems: The hooks can affect game calculations across animation, camera, cloth and audio regardless of GPU. Linux, Steam Deck, AMD and the current head have no reported game run; the renderer capture thread’s 11–14% CPU increase in the combined run is unexplained. The additional compilation and switch-off hook costs have not been isolated on the current base.
Switches: HOS_NATIVE_LIBM defaults off and offers on and check modes; HOS_NATIVE_LIBM_LOG defaults off. Both have table rows. The performance switch’s default was chosen because it is unmeasured, contrary to the project’s switch rule; measurements are needed to choose the default.
Reviewed:
936118d415againsta104182b40: Read AGENTS.md, CONTRIBUTING.md, the pull request template, the relevant standards, commit log, build and hook diffs, entry_hooks.py, provenance, pin test and hook test. Inspected the main hook implementation and its CPU gate; skimmed the pin data. The vendored maths files, licences, full host header and full build script were not read in full, so this review is incomplete. The referenced sibling Tooling AGENTS.md and docs/review-checklist.md were absent.; nothing run (a code review).Checked and fine:
Blocking (to change before merge)
Suggestions (the author may take or leave)
Testing: No code, tests, builds or project scripts were run, as requested. The author reports offline comparisons and selected tests, but not a complete suite or an in-game run of this head.
Overlaps and merge order: PR #1898 also optimizes host maths and may share call paths; its effect and integration order should be checked when measuring this change.
Automated code review by BeanBot (gpt-6.1-terra), on the pull request's opening. Nothing was run; a maintainer decides.
@ -104,6 +104,8 @@ SW(HOS_MOVIE_PACE, kill, bool, "on: a 60 FPS cap is raised for a movie's pictureSW(HOS_MUTEX_TRACE, diag, int, "0: off", "Logs mutex waits longer than <ms>, with the thread that held the mutex.")SW(HOS_MUTEX_WATCH, diag, string, "off", "Logs every operation on the mutex at that guest address (hex) and its condition variables' signals.")SW(HOS_MW_WAKE, kill, enum, "only changes to objects linked into a multi-wait wake WaitAny", "=all: every event, semaphore or queue change wakes every WaitAny, as before.")SW(HOS_NATIVE_LIBM, attempt, enum, "off: the game's own maths", "1 answers the proven sin, cos, pow and the rest on the host; a value starting with c runs both and reports a difference.")The stated reason for the off default is that this has not been measured alone. The switch standard requires a default chosen from measurements even in WIP; please measure the branch and choose the default from those results.
@ -0,0 +179,4 @@/* CPUID.1:ECX — FMA bit 12, OSXSAVE bit 27, AVX bit 28. CPUID.7:EBX — AVX2 bit 5. */if (__get_cpuid(1, &a, &b, &c, &d) &&(c & ((1u << 12) | (1u << 27) | (1u << 28))) == ((1u << 12) | (1u << 27) | (1u << 28)) &&__get_cpuid_count(7, 0, &a, &b, &c, &d) && (b & (1u << 5))) {AVX2 and FMA are only part of x86-64-v3. A VM or CPU missing another permitted feature, such as BMI2, can pass this gate and fault in a v3 object. Please check the full target feature set or narrow the compilation target.
@ -0,0 +297,4 @@LIBM_BASE static void libm_diff(int id, const char *name, uint64_t native, uint64_t lifted) {LIBM_ADD(&libm_count[id].mismatches);if (libm_said_diff & (1u << id)) return;Concurrent check-mode mismatches can read and update this mask at the same time, creating a data race; the flag mask below has the same issue. Please use atomic bit operations.
@ -0,0 +1,4163 @@# Frozen game results for tests/test_libm_pins.py.# Bits are the saved Unicorn outputs, sampled where host/oot_libm.c would answer.These saved Unicorn outputs are derived from the game executable. CONTRIBUTING.md prohibits committing anything made from game files; please remove them and use an independently sourced or locally generated reference check.
@ -0,0 +489,4 @@if bad["ret"] != "0" or bad["writes"] != "0" or bad["calls"] != "0":fail("sincos bad pointer: " + str(bad))scc = run_case(exe, "sincos-check", {"HOS_NATIVE_LIBM": "c"})if scc["ret"] != "1" or scc["s0"] != "11" or scc["writes"] != "0" or "sincosf result differs" not in scc["log"]:Without CC, this test uses a developer-specific Windows path, so the normal suite fails on other machines. Please use the project’s portable compiler selection.
@ -0,0 +12,4 @@ROOT = Path(__file__).resolve().parents[1]OO = Path(r"C:\Repos\OoT-Modding")LIBM = ROOT / "host" / "libm"CC = OO / "build-hookshot" / "builder" / "llvm-mingw" / "bin" / "x86_64-w64-mingw32-clang.exe"This hard-coded Windows compiler path makes the discovered test fail on Linux and on Windows checkouts without this directory. Please use portable project compiler discovery and the actual platform build flags.
@fix
I changed this pull request by bringing it up to date: the new head is
f8c82c16bd. I rebased it ontomain(a764653726).Conflicts resolved
Left for the author
Nothing was built or run: check the change. It will be reviewed again.
Automated fix by BeanBot (gpt-6.1-terra), asked for by HumanBean. Nothing was run; a maintainer decides.
936118d415f8c82c16bdRecommendation: Request changes. The checked-out head still contains game-derived fixtures, nonportable tests, and a CPU gate that can admit unsupported instructions. The in-game gain and the cost with the switch off remain unproven for this PR alone.
Adds optional host implementations for twelve SDK math functions, with lifted fallback and a comparison mode. The offline comparisons make the approach worth testing in game. The cost is twenty extra build objects and twelve entry hooks that force the lifted functions into context form even when the optimization is off.
Does it help: Unproven. Nine rewrites together reduced median whole-process CPU use by 2.7% standing and 2.4% turning in three interleaved runs per variant in the Market at a 60 FPS cap on an older Windows/NVIDIA base. The all-off differences of 0.3% and 0.2% were within the run spread. Offline answering calls were 2.4–6.0× faster than an approximation of main's lifted code. The combined game run recorded no per-function answer counts and provides no isolated gain, current-base switch-off comparison, or uncapped frame-time result.
Other systems: The SDK hooks can affect animation, cloth, camera and audio math across GPU vendors. The CPU gate and compiler output matter on Windows, Linux and Steam Deck. Builder packaging includes the vendored sources, and every standalone build queues the extra objects.
sincosfmarks guest writes for capture and mirrors. The combined run saw the capture-copy thread use 0.009–0.017 more cores, with attribution still open. No save format, options format, renderer API or shader change is present.Switches:
HOS_NATIVE_LIBMdefaults off, accepts enabled values, and treats a value beginning withcas comparison mode. It has anattemptrow, but the default needs the isolated measurements required by the project.HOS_NATIVE_LIBM_LOGdefaults off and has adiagrow. Off is appropriate for counters that add hot-path work and periodic logging.Reviewed:
f8c82c16bdagainsta764653726: Reviewedf8c82c16bdagainsta764653726. Read AGENTS.md, CONTRIBUTING.md, the Forgejo template, developer standards, the commits, the supplied comments and review, the host hook, hook generator, new tests and vendored math implementation. Read the changed sections of build.py, hos.h, switches.def and test_libc_hooks.py and inspected the lift key, guest-write helpers, test compiler selection, and Builder packaging. Skimmed pin data, license texts and reported runs. Unrelated sections of the build and host files were outside the changed paths reviewed in detail. docs/review-checklist.md and the sibling Tooling checkout were absent. The offline proof tools were unavailable in this checkout.; nothing run (a code review).Checked and fine:
Blocking (to change before merge)
Waiting on the author (a run, a measurement or a decision; no edit does it)
Suggestions (the author may take or leave)
Testing: Read-only review. The author reports zero differences in offline comparisons and selected game-free passes, with five failures also seen on an older main. The selected passes do not establish a portable full-suite result. The only reported game run used an older tree with nine rewrites, and the supplied Test Farm thread contains no run result for this head.
Overlaps and merge order: #1898 was flagged as related host math work. The combined benchmark also included a rewrite that caches repeated powf results and shares this PR's call sites. No merge-order dependency was established from the supplied checkout. Measure this PR in isolation before using combined results to assess either change.
Automated code review by BeanBot (gpt-6.2-sol), asked for by BeanBot, after its fix. Nothing was run; a maintainer decides.
@ -105,6 +105,8 @@ SW(HOS_MOVIE_PACE, kill, bool, "on: a 60 FPS cap is raised for a movie's pictureSW(HOS_MUTEX_TRACE, diag, int, "0: off", "Logs mutex waits longer than <ms>, with the thread that held the mutex.")SW(HOS_MUTEX_WATCH, diag, string, "off", "Logs every operation on the mutex at that guest address (hex) and its condition variables' signals.")SW(HOS_MW_WAKE, kill, enum, "only changes to objects linked into a multi-wait wake WaitAny", "=all: every event, semaphore or queue change wakes every WaitAny, as before.")SW(HOS_NATIVE_LIBM, attempt, enum, "off: the game's own maths", "1 answers the proven sin, cos, pow and the rest on the host; a value starting with c runs both and reports a difference.")Choose the default from isolated runs on the current base. Collect answer counts separately from the timing runs.
@ -0,0 +177,4 @@unsigned a, b, c, d;int pass = 0;/* CPUID.1:ECX — FMA bit 12, OSXSAVE bit 27, AVX bit 28. CPUID.7:EBX — AVX2 bit 5. */if (__get_cpuid(1, &a, &b, &c, &d) &&Match the gate to the full x86-64-v3 target. A VM can expose AVX2 and FMA while masking BMI2 or F16C.
@ -0,0 +297,4 @@LIBM_BASE static void libm_diff(int id, const char *name, uint64_t native, uint64_t lifted) {LIBM_ADD(&libm_count[id].mismatches);if (libm_said_diff & (1u << id)) return;Use atomic fetch-or for both mismatch masks so concurrent check calls can claim each diagnostic safely.
@ -0,0 +158,4 @@int unset = -1;m = !env_on("HOS_NATIVE_LIBM") ? LIBM_OFF : (v && (v[0] | 0x20) == 'c') ? LIBM_CHECK : LIBM_ON;const int log = env_on("HOS_NATIVE_LIBM_LOG");if (log) __atomic_store_n(&libm_log_on, 1, __ATOMIC_RELAXED);Publish logging configuration safely and use atomic reads. A concurrent first call can still store while another thread reads this flag.
@ -0,0 +485,4 @@libm_inside = 1;fn(c);libm_inside = 0;uint32_t gs, gc;Equal output pointers make the check read the last store twice. Decline overlapping ranges or compare the expected final bytes in lifted store order.
@ -53,9 +65,15 @@ def hooked_symbols(host=HOST):return sorted(s if not n else "%s>=%d" % (s, n) for s, n in hooks(host).items())Include the callback form in the lift fingerprint so a host-only form change cannot reuse a splice with the wrong argument list.
@ -85,3 +103,3 @@else:table[addr] = line(sym, least)table[addr] = line(sym, least, addr if sym in fn_form else None)lifter.no_local.add(addr)Measure switch-off play against current main. This still forces the twelve lifted bodies into context form.
@ -0,0 +1,4163 @@# Frozen game results for tests/test_libm_pins.py.# Bits are the saved Unicorn outputs, sampled where host/oot_libm.c would answer.These expected outputs are game-derived. Keep this comparison local and replace the committed fixture with independently sourced checks.
@ -0,0 +346,4 @@def run_case(exe, name, env):e = os.environ.copy()e.pop("HOS_NATIVE_LIBM", None)Clear HOS_NATIVE_LIBM_LOG as well. An inherited value makes the quiet case fail.
@ -0,0 +497,4 @@def main():splice()import tempfilecc = os.environ.get("CC") or r"C:\Repos\OoT-Modding\build-hookshot\builder\llvm-mingw\bin\x86_64-w64-mingw32-clang.exe"Use the existing compiler discovery pattern and make the wrapper fixture independent of the runner's actual CPU features.
@ -0,0 +12,4 @@ROOT = Path(__file__).resolve().parents[1]OO = Path(r"C:\Repos\OoT-Modding")LIBM = ROOT / "host" / "libm"CC = OO / "build-hookshot" / "builder" / "llvm-mingw" / "bin" / "x86_64-w64-mingw32-clang.exe"Use project compiler discovery and platform link flags. This path prevents the test from starting on other Windows and Linux setups.
WIP: perf(host): twelve of the game's maths functions on host code, bit for bit, from Arm optimized-routines and musl (HOS_NATIVE_LIBM, off by default)to WIP: perf(host): twelve of the game's maths functions on host code, bit for bit, from Arm optimized-routines and musl (HOS_NATIVE_LIBM, on by default)HOS_NATIVE_LIBMis now on by default (c412ab37), as docs/dev/standards.md asks: a switch ships at its value from the first push, not off because it is new.HOS_NATIVE_LIBM=0gives main's lifted code. The row's kind iskill, unset reads as on (env_on_or), and the test runs it unset and=1(on),=0and=off(off) andcheck; it fails when the old read is put back. Title and description updated to match.Recommendation: Request changes.
tests/libm_pins.txtis still the game's own outputs, run from the executable under Unicorn, and CONTRIBUTING.md bans committing anything generated from the dump. Both pin and wrapper tests also still need one developer's Windows compiler path. The host code itself looks sound and exact by its offline proof. Its own gain is still unmeasured alone.For players, it answers twelve of the SDK's maths functions (
powf,sinf/cosf/sincosf, the exps and logs,atan2f,acosf,acos) on host code that gives the game's bits. Offline that is 2.4-6x faster a call, and it falls back to the lifted routine whenever exactness isn't proven. The code makes things better if the gain holds. The cost is 12 hooked functions that lose their register locals even with the switch off, and 20 small objects in every build.Does it help: Unproven. The only in-game number is nine rewrite branches together on old main
068fb8eb: -2.7 % / -2.4 % whole-process CPU at a 60 fps cap in the Market (3 runs each, Windows, RTX 3090, 5900X), with the hooks-in-but-off variant at -0.3 % / -0.2 % (noise). This branch's share isn't separated, and no answered-call count was logged, so it isn't known how many calls arrive with IXC set and get answered at all. The per-call figures are against an offline approximation of main's form. Needed: this head against main, the switch unset against=0, CPU seconds capped plus uncapped frame times, and oneHOS_NATIVE_LIBM_LOG=1run in its own session for the answer counts.Other systems: Everything that computes with these functions: animation, cloth, camera, sound angles. Exactness on the answered inputs is shown by the author's offline sweeps. The proof tool (
longshot-dev/libm-rewrite-tools/prove.py) isn't in the repository, so a reviewer can't reproduce it. Linux has not been built:-include prefix_or.hahead of glibc'smath.hrenames itssinf/powfdeclarations through__MATHCALL's token pasting, which should be fine but is untested. On a CPU below x86-64-v3 the objects are still compiled for v3 and only the run-time gate keeps them from running, which makes the gate's completeness load-bearing (blocking 3). GPU vendor and driver are untouched. The capture-copy thread's +10-18 % in the combined run is still unattributed.sincosf's two guest writes are this PR's only stores, marked after each store.Switches:
HOS_NATIVE_LIBM(kill, on by default,=0the lifted maths,c...check mode): on is defensible, because exactness is proven offline, every unproven state falls back, and=0is the way back. Its gain alone is unmeasured, so the default stays as it is until the numbers above exist.HOS_NATIVE_LIBM_LOG(diag, off): right. Both have rows ingpu/vk/switches.def.Reviewed:
c412ab377againsta764653726(its fork point; it rebases onto mainb62932a96with no conflicts and an empty interdiff). Read in full:host/oot_libm.c,nx2/entry_hooks.py, thenx2/build.pyblocks,host/hos.h,tests/test_libm_hooks.py,tests/test_libm_pins.py,tests/test_libc_hooks.py's change,host/libm/PROVENANCE.md, the prefix and shim headers, and the(longshot)changes inor/math_config.handmusl/acosf.c. Skimmed: the rest of the vendored sources against their upstream headers,tests/libm_pins.txt, the licence files, andtests/test_mark_bitmap_host.py. Nothing run (a code review). Compared against BeanBot's reviews at936118d4andf8c82c16.Fixed in this review: nothing pushed: the push to
snaildev99/Longshotwas refused here. The three fixes I made are below as suggestions to apply.Checked and fine:
host/libm/musl/acosf.c:44-46,53-55swaps two shortcut constants for the documented values (pi and pi/2, as facts in the PR's own words).host/libm/or/math_config.h:103-123is a generic exactround().host/oot_libm.cis original host C: the gate, the counters and the splices. There is no disassembly or instruction-by-instruction body anywhere in it (unlike #1911's kernels).oot_libm.c:355-368) are behaviour stated as facts.host/libmsits outsidethird_party/with its own provenance note. standards.md asks for that when a copy is modified.libm_unary/binary/acos/sincosf: NaN in or out, FZ denormals, tiny results under FZ, and the IXC-only flag rules all decline before any write.vreg[0][1]is zeroed as a scalar write does.sincosfmarks each destination after its store (8a881003).f_<addr>in its block, for split_giants' pieces.env_on_orgives unset = on and0/off= off.Blocking (to change before merge)
tests/libm_pins.txt:2: "the saved Unicorn outputs", the game executable's own results. That is generated from the dump, which CONTRIBUTING.md's rule and its Licensing section both forbid, however small each value is. It needs to come out of the branch's history, not only the head, since071e1c5fadds it. Keep the game comparison local, and pin results from an independent source if a contraction check is wanted (correctly rounded values from mpmath/MPFR where the vendored functions are correctly rounded, or the vendored build's own output frozen once). (BeanBot raised it twice. The @fix comment says it was removed, but it is still at this head.)tests/test_libm_pins.py:13-15: the compiler isC:\Repos\OoT-Modding\build-hookshot\...with noCCfallback, so the check cannot start on Linux or on any other Windows PC. It also restatesnx2/build.py's flags instead of reading them, so they can drift apart. Usetests/compiler_command.py(or the toolchain discoverytests/test_fpx_sticky.pyuses) and skip without a compiler. This has to be reworked together with point 1 anyway. (BeanBot, unresolved.)host/oot_libm.c:181-184: the gate checks AVX, AVX2, FMA and OSXSAVE, but the objects are built-march=x86-64-v3, which also allows BMI1/2, LZCNT, MOVBE and F16C.nx2/host_cpu.pybuilds the game for v2 on a Linux PC or VM that hides BMI2 or MOVBE. The libm objects are still v3 there, this gate passes, and the firstshlx/andnfaults. The fix is suggestion A. (BeanBot, unresolved. The @fix comment says it was completed, but it isn't at this head.)host/oot_libm.c:302-303,310-311:libm_said_diff/libm_said_flagare a plain read-modify-write from every guest thread in check mode, a data race. Alsolibm_log_onis stored atomically but read plainly (:124,270,280). Suggestion A fixes both. (BeanBot, unresolved.)nx2/entry_hooks.py:65:hooked_symbols()feeds the lift key (nx2/make_game.py:2332), but it givespowffor both a plain and an_FNhook. Changing only a hook's form would reuse lifted objects whose splice passes the wrong arguments, and check mode would then call a garbagefn. Suggestion B appends:fn. That costs no extra cache invalidation, since this PR changes the key anyway.tests/test_libm_hooks.py:396,551: the cases inheritHOS_NATIVE_LIBM_LOG, so the quiet case fails with it set, and the compiler falls back to the same developer path. Suggestion B fixes both. (BeanBot, unresolved.)Suggestions (the author may take or leave)
host/oot_libm.c:488: withx0 == x1, check mode reads the same final word twice and reports a mismatch whenever sin != cos. Native mode also assumes sine-then-cosine store order (the test pins that order). Unlikely from the game, but declining overlapping ranges in check mode would be simpler than reasoning about it.host/libm/PROVENANCE.md: add each upstream's URL, tag and commit, plus a hash of the copied files, asthird_party/notes have, so a refresh or an audit can check the copy.fmodf's object and counter slot. It isn't hooked, and the PR's own benchmark says it can't be.powf(host/vfx_powf.c). Whichever merges second should calllibm_or_powfinstead.host/oot_libm.c:181("—"). Suggestion A rewrites it.Suggestion A: fix(libm): the CPU check asks for all of x86-64-v3, and the check-mode masks and the log flag are atomic
Suggestion B: fix(libm): the lift key tells an ENTRY_HOOK_SYM_FN hook from a plain one, and the wrapper test finds its compiler
Testing: The author's offline proofs (every float for the nine one-argument functions, 600 M samples a mode for the rest) and one combined-tree Windows CPU run with no counters. Missing: this head built and played, Linux built at all, check mode in the game with answer counts and
mismatches 0, and this branch's own measurement. All of that holds the merge.Overlaps and merge order:
powfcalls, which shrinks this PR's biggest call source. Measure each alone.powf(above).powf's FMADD, which this PR bypasses when it answers.gpu/vk/switches.def, and6e4dbb48(the MARK_BITMAP host flag andtests/test_mark_bitmap_host.py) is shared with them. Whichever merges first takes that commit, and the rest rebase.No order is forced. This one rebases onto main cleanly.
@ -0,0 +179,4 @@unsigned a, b, c, d;int pass = 0;/* CPUID.1:ECX — FMA bit 12, OSXSAVE bit 27, AVX bit 28. CPUID.7:EBX — AVX2 bit 5. */if (__get_cpuid(1, &a, &b, &c, &d) &&-march=x86-64-v3 also allows BMI1/2, LZCNT, MOVBE and F16C. A VM that hides one of them passes this gate and faults in a v3 object. Check every v3 feature (suggestion A in the summary).
@ -0,0 +299,4 @@LIBM_BASE static void libm_diff(int id, const char *name, uint64_t native, uint64_t lifted) {LIBM_ADD(&libm_count[id].mismatches);if (libm_said_diff & (1u << id)) return;Plain read-modify-write from every thread in check mode. Use __atomic_fetch_or and test the bit it returns. Same for libm_said_flag at 310.
@ -0,0 +121,4 @@int64_t calls, answered, answered_ixc, compared, mismatches, flag_differs, declined[LD_N];} LibmCount;static LibmCount libm_count[LM_N];#define LIBM_ADD(p) do { if (__builtin_expect(libm_log_on, 0)) __atomic_fetch_add((p), 1, __ATOMIC_RELAXED); } while (0)libm_log_on is stored atomically but read plainly here and at 270/280. Use __atomic_load_n relaxed (it's still a plain mov on x86-64).
@ -53,9 +65,15 @@ def hooked_symbols(host=HOST):return sorted(s if not n else "%s>=%d" % (s, n) for s, n in hooks(host).items())The lift key reads this, but a plain hook and an _FN hook give the same text. Append ':fn' for fn_form so a form-only change re-lifts (suggestion B).
@ -0,0 +1,4163 @@# Frozen game results for tests/test_libm_pins.py.# Bits are the saved Unicorn outputs, sampled where host/oot_libm.c would answer.These are the game executable's own outputs (Unicorn): generated from the dump, which CONTRIBUTING.md bans. They need to come out of the history (added in
071e1c5f). Pin independent values instead.@ -0,0 +393,4 @@def run_case(exe, name, env):e = os.environ.copy()e.pop("HOS_NATIVE_LIBM", None)Also pop HOS_NATIVE_LIBM_LOG: an inherited value makes the quiet case fail.
@ -0,0 +548,4 @@def main():splice()import tempfilecc = os.environ.get("CC") or r"C:\Repos\OoT-Modding\build-hookshot\builder\llvm-mingw\bin\x86_64-w64-mingw32-clang.exe"The same developer path: use compiler_command('CC', 'clang', 'gcc') and skip without one.
@ -0,0 +12,4 @@ROOT = Path(__file__).resolve().parents[1]OO = Path(r"C:\Repos\OoT-Modding")LIBM = ROOT / "host" / "libm"CC = OO / "build-hookshot" / "builder" / "llvm-mingw" / "bin" / "x86_64-w64-mingw32-clang.exe"One developer's compiler path with no CC fallback: this can't start anywhere else. Use tests/compiler_command.py and skip without a compiler, and read the flags from nx2/build.py.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.