tombox-xshader: bit-exact RDNA2 f16 transcendentals (rcp, rsq, sqrt, log, exp, sin, cos), measured on gfx1036 #150

Closed
github-bot wants to merge 1 commit from gh-pr/150/head into gh-pr/150/base
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-22 16:04 UTC · original
SchmashinPrunes:rdna2-f16-trans → main · +1061 −44 · 13 files · 1 commits · closed without merging

The 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_f16

Exports 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.py kernels: 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.
  • Each f16 result equals the hardware's own f32 result for the same input, rounded to f16 (every normal input: 61,440 for rcp/exp/sin/cos, the 30,720 positive ones for rsq/sqrt/log; one exp input lands on an exact f32 tie and rounds up in f16, so f16 rounds the unit's internal value).
  • NaN inputs are returned unchanged (IEEE mode 0, signalling NaNs included). Invalid operations give 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 except sin(-0) = -0.
  • Denormal mode 0 is mode 3 with denormal inputs and outputs flushed to signed zero (all 7 ops, 0 mismatches). The translator does not model the MODE register, so this PR implements mode 3, as the existing f16 code does.

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 of log2(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 of 2^(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;
    • 20 exception entries (10 sin, 10 cos): the inputs where this algorithm and the hardware differ, all near-midpoint cases. rcp, rsq, sqrt, log and exp need none.
      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 through tb_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 existing hres path.
  • tools/rdna2-hwprobe: trans_probe.py + trans_runner.cpp (the probe), trans16_gen.py (regenerates trans16/tables.rs, the exceptions and the hardware digests in trans16/rdna2_hw.rs from 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.
  • ISA sweep: these 7 mnemonics are now compared exactly (Cmp::X) against the model, denormal inputs included; the old sin16 reference is gone.
  • Mutation check: changing one shift in the HLSL exp path (>> 33 to >> 34) fails the WARP test on v_exp_f16 and leaves the other 6 passing.
cargo check --locked -p tombox-pe -p tombox-archive -p tombox-rpf -p tombox-disasm -p tombox-xshader --all-targets    ok
cargo test -p tombox-xshader --release --lib trans16
test trans16::tests::f16_transcendental_specials ... ok
test trans16::tests::f16_transcendentals_match_rdna2_on_every_input ... ok
cargo test -p tombox-xshader --release --test exec trans16
trans16_all_inputs: 7 mnemonics verified, 0 failing, 7 cases, 7 rerun with runtime inputs
cargo test -p tombox-xshader --release --no-fail-fast
every target passes (exec 89 passed / 2 ignored, isa_sweep 2 passed, opcodes 23 passed)

tests/opcodes.rs pins for v_log/exp/sin/cos_f16 now expect tb_t16(...).

Towards f32 (in progress, not in this PR)

Recorded on the same GPU, every sweep run twice:

  • f32 rcp, rsq, sqrt are faithful but not correctly rounded (rcp: 89.3% correctly rounded, |error| < 0.89 ulp; depends on the mantissa only; v_rcp_iflag_f32 equals v_rcp_f32 on normal inputs). exp is about 96% correctly rounded, log 59-77%.

  • rcp and rsq fit 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-xshader passes

  • No game or console files and no binaries included

  • No fake success

🤖 Generated with Claude Code

**@⁠SchmashinPrunes** on GitHub · 2026-11-22 16:04 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/150) `SchmashinPrunes:rdna2-f16-trans` → `main` · +1061 −44 · 13 files · 1 commits · closed without merging The 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_f16` **Exports 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.py` kernels: 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. - Each f16 result equals the hardware's own f32 result for the same input, rounded to f16 (every normal input: 61,440 for rcp/exp/sin/cos, the 30,720 positive ones for rsq/sqrt/log; one `exp` input lands on an exact f32 tie and rounds up in f16, so f16 rounds the unit's internal value). - NaN inputs are returned unchanged (IEEE mode 0, signalling NaNs included). Invalid operations give `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 except `sin(-0) = -0`. - Denormal mode 0 is mode 3 with denormal inputs and outputs flushed to signed zero (all 7 ops, 0 mismatches). The translator does not model the MODE register, so this PR implements mode 3, as the existing f16 code does. ### 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 of `log2(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 of `2^(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; - 20 exception entries (10 sin, 10 cos): the inputs where this algorithm and the hardware differ, all near-midpoint cases. rcp, rsq, sqrt, log and exp need none. 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 through `tb_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 existing `hres` path. - `tools/rdna2-hwprobe`: `trans_probe.py` + `trans_runner.cpp` (the probe), `trans16_gen.py` (regenerates `trans16/tables.rs`, the exceptions and the hardware digests in `trans16/rdna2_hw.rs` from 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. - ISA sweep: these 7 mnemonics are now compared exactly (`Cmp::X`) against the model, denormal inputs included; the old `sin16` reference is gone. - Mutation check: changing one shift in the HLSL `exp` path (`>> 33` to `>> 34`) fails the WARP test on `v_exp_f16` and leaves the other 6 passing. ``` cargo check --locked -p tombox-pe -p tombox-archive -p tombox-rpf -p tombox-disasm -p tombox-xshader --all-targets ok cargo test -p tombox-xshader --release --lib trans16 test trans16::tests::f16_transcendental_specials ... ok test trans16::tests::f16_transcendentals_match_rdna2_on_every_input ... ok cargo test -p tombox-xshader --release --test exec trans16 trans16_all_inputs: 7 mnemonics verified, 0 failing, 7 cases, 7 rerun with runtime inputs cargo test -p tombox-xshader --release --no-fail-fast every target passes (exec 89 passed / 2 ignored, isa_sweep 2 passed, opcodes 23 passed) ``` `tests/opcodes.rs` pins for `v_log/exp/sin/cos_f16` now expect `tb_t16(...)`. ### Towards f32 (in progress, not in this PR) Recorded on the same GPU, every sweep run twice: - f32 `rcp`, `rsq`, `sqrt` are faithful but not correctly rounded (`rcp`: 89.3% correctly rounded, |error| < 0.89 ulp; depends on the mantissa only; `v_rcp_iflag_f32` equals `v_rcp_f32` on normal inputs). `exp` is about 96% correctly rounded, `log` 59-77%. - `rcp` and `rsq` fit 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. - [x] `cargo test -p tombox-xshader` passes - [x] No game or console files and no binaries included - [x] No fake success 🤖 Generated with [Claude Code](https://claude.com/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
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:11:51 +00:00
github-bot closed this pull request 2026-11-26 06:11:51 +00:00
Author
Owner

@⁠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.
**@⁠MiguelMatMar** on GitHub · 2026-11-22 16:15 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/150#issuecomment-7081929267) 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.
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-22 16:53 UTC · original

Applied in afccebe (merged in cd5817b) with your authorship kept, thanks, and thanks to MiguelMatMar for the independent check on NVIDIA! Bit-exact is now the default. I added d93b78c on top: TOMBOX_XS_F16_NATIVE=1 brings 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).

**@⁠omonkeyz** on GitHub · 2026-11-22 16:53 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/150#issuecomment-7082425065) Applied in afccebe (merged in cd5817b) with your authorship kept, thanks, and thanks to MiguelMatMar for the independent check on NVIDIA! Bit-exact is now the default. I added d93b78c on top: `TOMBOX_XS_F16_NATIVE=1` brings 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

This discussion has been locked. Commenting is limited to contributors.
No description provided.