tombox-xshader: bit-exact RDNA2 f16 transcendentals (rcp, rsq, sqrt, log, exp, sin, cos), measured on gfx1036 #150
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
library
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/omonkeyz--xbox-emulator!150
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/150/head"
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?
@SchmashinPrunes on GitHub · 2026-11-22 16:04 UTC · original
SchmashinPrunes:rdna2-f16-trans→main· +1061 −44 · 13 files · 1 commits · closed without mergingThe f16 transcendental VALU ops now return exactly what RDNA2 hardware returns, on every one of the 65,536 inputs, independent of the host GPU. This is the first part of work towards bit-exact RDNA2 transcendentals: a full f32 implementation (
v_rcp/rsq/sqrt/log/exp/sin/cos_f32, then the f64 approximations) is in progress, and the f32 findings so far are below.Library / area: tombox-xshader:
v_rcp_f16,v_rsq_f16,v_sqrt_f16,v_log_f16,v_exp_f16,v_sin_f16,v_cos_f16Exports changed (exports.txt rows): none
Source of the behaviour: results recorded on RDNA2 hardware, the gfx1036 iGPU of a Ryzen 5 9600X (ROCm/HIP 5.7.1), with the new
tools/rdna2-hwprobe/trans_probe.pykernels: every f16 input, both denormal modes, each sweep run twice and kept only when both runs were identical. gfx1036 is RDNA2, not the console's gfx1013. No game or console files are involved.Before
Main lowered these ops to host math (
1.0 / x,rsqrt,log2,exp2,sin(x * 6.28318530718)), so results depended on the host GPU and driver, and the ISA sweep accepted them within 1 ulp (sin/cos within 2e-3).What RDNA2 does (gfx1036, denormals preserved)
rcp,rsq,sqrt,log,exp: correctly rounded on every input.sin,cos(argument in revolutions): correctly rounded except 14 and 4 inputs, each within 2e-4 ulp of a rounding midpoint.expinput lands on an exact f32 tie and rounds up in f16, so f16 rounds the unit's internal value).0xfe00: sqrt/rsq/log of a negative, sin/cos of ±inf.log(±0) = -inf;rsq(-0) = -inf; every exact zero from sin/cos is +0 exceptsin(-0) = -0.Change
crate::trans16(new, public): an integer-only model of the 7 ops and the HLSL that implements the same algorithm (tb_t16(op, bits)):rcp,sqrt,rsq: exact integer division / square root with a sticky bit, then one round-to-nearest-even into f16 (denormal and overflow results included);log: exponent + a 1024-entry Q32 table oflog2(1 + j/1024)(the f16 mantissa has only 1024 values);exp: the input as an exact Q24 fixed-point number, a 1024-entry Q32 table of2^(j/1024)and a second-order correction for the low 14 bits;sin/cos: exact Q24 range reduction to a quarter turn and a Q32 Taylor polynomial to degree 13;The HLSL uses only integer arithmetic (
uint64_t/int64_t, which the d16 MTBUF helpers already need), so every host gives the same bits.src/hlsl.rs: the 7 mnemonics go throughtb_t16(operand bits with VOP3 abs/neg/opsel applied as bit operations, so NaN payloads survive); the host-math arms are removed. With a VOP3 output modifier or clamp, the exact result is passed through the existinghrespath.tools/rdna2-hwprobe:trans_probe.py+trans_runner.cpp(the probe),trans16_gen.py(regeneratestrans16/tables.rs, the exceptions and the hardware digests intrans16/rdna2_hw.rsfrom a probe directory), and a README.txt section with the commands and results.Tests (Windows 11 Pro, WARP, DXC 1.9.2609, on
65ca6dd)trans16::tests::f16_transcendentals_match_rdna2_on_every_input(CPU): the model reproduces FNV-1a-64 digests of the recorded hardware results for all 65,536 inputs of each op.exec_mem::trans16::f16_transcendentals_exact_on_every_input(WARP): the translated shader runs all 65,536 inputs of each op (1024 threads × 64 inputs, folded into one hash per thread) and matches the model; it reruns with runtime inputs.Cmp::X) against the model, denormal inputs included; the oldsin16reference is gone.exppath (>> 33to>> 34) fails the WARP test onv_exp_f16and leaves the other 6 passing.tests/opcodes.rspins forv_log/exp/sin/cos_f16now expecttb_t16(...).Towards f32 (in progress, not in this PR)
Recorded on the same GPU, every sweep run twice:
f32
rcp,rsq,sqrtare faithful but not correctly rounded (rcp: 89.3% correctly rounded, |error| < 0.89 ulp; depends on the mantissa only;v_rcp_iflag_f32equalsv_rcp_f32on normal inputs).expis about 96% correctly rounded,log59-77%.rcpandrsqfit a 32-segment cubic in the low 18 mantissa bits (sqrt: 16 segments), with the square and cube terms taken from the mantissa without its lowest bit. The best such model still leaves about 0.8% of outputs off by one, from about ±0.015 ulp of internal quantization that is not modelled yet (likely a truncated multiplier). The aim is the same result as here: an integer model matching the recorded outputs on every input, then exact HLSL.cargo test -p tombox-xshaderpassesNo game or console files and no binaries included
No fake success
🤖 Generated with Claude Code
v_{rcp,rsq,sqrt,log,exp,sin,cos}_f16 were lowered to host math (1.0/x, rsqrt, log2, exp2, sin, cos) and the ISA sweep checked them within 1 ulp. They now follow results recorded on RDNA2 hardware (gfx1036) for every one of the 65536 inputs. Measured: rcp, rsq, sqrt, log and exp are correctly rounded on every input (denormals preserved); sin and cos are correctly rounded except for 14 and 4 inputs that sit within 2e-4 ulp of a midpoint. Every f16 result equals the hardware's own f32 result rounded to f16. NaN inputs come back unchanged, invalid operations give 0xfe00, and every exact zero from sin/cos is +0 except sin(-0). crate::trans16 holds an integer-only model (exact division and square root; 1024-entry Q32 tables for log2/exp2; a Q32 sine polynomial; 20 exception entries: where this algorithm and the hardware differ, all near-midpoint sin/cos inputs) and emits the same algorithm as HLSL (tb_t16), so the result no longer depends on the host GPU's transcendental unit. trans16::tests checks the model against FNV-1a digests of the recorded hardware results; exec_mem::trans16 runs the translated shaders over all 65536 inputs of all 7 ops on WARP; the ISA sweep compares these mnemonics exactly. tools/rdna2-hwprobe/trans16_gen.py regenerates the tables, exceptions and digests from a probe directory. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01Xsrj8HzdWhzzYYavNNpHjk@MiguelMatMar on GitHub · 2026-11-22 16:15 UTC · original
Independent run on a non-AMD host, PR head
df3c26f:f16_transcendentals_exact_on_every_input: 7 of 7 mnemonics verified on all 65,536 inputs, on WARP and on hardware (TOMBOX_EXEC_HW=1, NVIDIA Ada / RTX 40-series). That backs the "independent of the host GPU" claim on a second vendor.isa_sweep: WARP 530 pass, 0 fail (11 known WARP deviations); hardware 541/541 pass.opcodes: 23/23 pass.@omonkeyz on GitHub · 2026-11-22 16:53 UTC · original
Applied in
afccebe(merged incd5817b) with your authorship kept, thanks, and thanks to MiguelMatMar for the independent check on NVIDIA! Bit-exact is now the default. I addedd93b78con top:TOMBOX_XS_F16_NATIVE=1brings back the old native approximations for perf A/B. It's part of the translation-cache key, like the other XS knobs. The WARP suites pass after the rebase onto current main (lib 76, opcodes 23, isa_sweep 2, exec 93).Pull request closed