tombox-xshader: bit-exact RDNA2 f32 and f64 transcendentals (rcp, rsq, sqrt, log, exp, sin, cos; f64 rcp, rsq, sqrt) #180
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!180
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/180/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 19:39 UTC · original
SchmashinPrunes:rdna2-f32-trans→main· +1832 −61 · 18 files · 3 commits · closed without mergingFollow-up to #150 (f16): the RDNA2 f32 and f64 transcendental VALU ops (
v_rcp_f32,v_rcp_iflag_f32,v_rsq_f32,v_sqrt_f32,v_log_f32,v_exp_f32,v_sin_f32,v_cos_f32,v_rcp_f64,v_rsq_f64,v_sqrt_f64) now returns exactly what RDNA2 hardware returns, independent of the host GPU. Three commits: rcp/rsq/sqrt f32 from a datapath model derived here, log/exp/sin/cos ported from AMD's rocjitsu models (verified against RDNA2), and the f64 forms, which turn out to be the same unit's unrounded internal value.Library / area: tombox-xshader:
v_rcp_f32,v_rcp_iflag_f32,v_rsq_f32,v_sqrt_f32,v_log_f32,v_exp_f32,v_sin_f32,v_cos_f32,v_rcp_f64,v_rsq_f64,v_sqrt_f64Exports 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
tools/rdna2-hwprobe/trans_probe.py: every mantissa of [1, 2) and [2, 4) for rcp/rsq/sqrt, 25 more complete 2^23-input ranges for log/exp/sin/cos, 723,517,440 f64 results (below), and a 2^20-input sweep over the whole 32-bit range (x = i * 4099) in both denormal modes. Every sweep was 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 to host math (
1.0 / x,rsqrt,sqrt,exp2,tb_log,sin/cos(2*pi*x)), so results depended on the host GPU and driver, and the ISA sweep accepted them within 1-4 ulp. RDNA2's results are faithful but not correctly rounded (v_rcp_f32matches the correctly rounded result on 89.3% of inputs, |error| < 0.89 ulp), so no host function reproduces them.rcp, rsq, sqrt: how the hardware computes them
v_rcp_f64,v_rsq_f64andv_sqrt_f64return the transcendental unit's internal value with no final rounding (34 bits for rcp, 35 for rsq/sqrt). The f32 result is that value minus 2^-27, rounded to nearest even at 24 bits (the offset is 0 in the one table entry whose results reach 1.0). That relation holds on all 8,388,608 mantissas of each op and parity, so the f64 outputs show the datapath directly, and it could be fitted exactly. Per table segment (rcp/rsq: top 5 mantissa bits; sqrt: exponent parity and top 4 bits), withtthe remaining mantissa bits:c0 + c1*t, with the productc1*theld with a 24-bit significand and the sum rounded to 2^-29;S * HwithS = ((t >> k)^2) >> 10(k = 1, or 2 for sqrt),H = c2 + 2 * rne(c3 * (4t + 1) / 4Dn)(the cubic coefficient evaluated a quarter step into t and rounded to even), and the product held with a 24-bit significand;32 rows (rcp), 2 x 32 (rsq) and 2 x 16 (sqrt) of 6 integers reproduce every recorded f64 and f32 output. The rest:
0xffc00000;sqrt(-0) = -0,rsq(-0) = -inf,rcp(+-0) = +-inf,rcp(+-inf) = +-0.v_rcp_iflag_f32equalsv_rcp_f32on every recorded input.log, exp, sin, cos: AMD's models
AMD's rocjitsu emulator (ROCm/rocm-systems,
emulation/rocjitsu/lib/util/include/util/amdgpu_*.h, MIT, added in ROCm/rocm-systems#12510) has integer models of all of these, fitted to RDNA3/4 (gfx1100, gfx1201) captures. Compiled unchanged against the gfx1036 recordings (withquiet_snan = false), its log, exp, sin and cos reproduce every recorded result: 25 complete 2^23-input ranges picked to reach every branch (log just below and above 1, the small-input sine path, large arguments, both signs, the denormal-adjacent binade) and the whole-range sweep in both denormal modes. Denormal mode 0 is mode 3 with denormal inputs and results flushed, which the translator's existing MODE wrapper already does. So these four are ported as they are (trans32/amd.rs, with AMD's notice intrans32/LICENSE-rocjitsu.txt).Its rcp, rsq and sqrt also match RDNA2, but they are a fitted cubic plus sparse one-ulp correction tables (about 2,800 entries for rcp), described there as "an empirical mapping, not a claim about the hardware implementation". The datapath model above needs no corrections, so this PR uses it; the cubic coefficients agree with rocjitsu's where they overlap (e.g. 1925, 1705 for the first rcp segments). AnyPS5 used rocjitsu's reciprocal on RDNA2 (gfx1031).
f64 rcp, rsq, sqrt
v_rcp_f64,v_rsq_f64andv_sqrt_f64are not double-precision operations on RDNA2: they run the same unit and write its ~35-bit internal value into the double without rounding (main computed them with host1.0 / xandsqrtand accepted 2^29 ulp). The unit reads the top 29 mantissa bits (truncated), so the datapath above sees a 24-bitt: the linear term uses all 24 bits (rsq table entry 31 ignores the top two of the six extra bits), the cubic coefficient the top 20 (rne(c3 * (TH + 1) / 4Dn); that same form is what makes f32 exact, so the tables lost the per-row phi of the old floor form), and the square term the same 17 bits as f32. The rest, from whole-range sweeps in both denormal modes:v_sqrt_f64(4.0) = 0x4000000004000000on the hardware);0xfff8000000000000.Recorded and reproduced, 723,517,440 results with 0 mismatches: the whole-range sweep (
x = (i * 4099) << 32 | lo, threelopatterns, both modes), every mantissa of [1, 2) and [2, 4) for each of the 32 combinations of the low-word bits that reach the datapath (two patterns of the linear-only bits), the same at a 1-in-4096 sample for all 64 extra-bit values, and all 2^23 low-bit patterns at both mantissa corners. rocjitsu computes these three with host doubles.Change
crate::trans32(new, public): the integer models and the HLSL that implements the same algorithms (tb_t32(op, bits)andtb_t64(op, lo, hi), 64-bit integers only): the rcp/rsq/sqrt datapath from the tables for both widths (eval,eval64), andamd.rsfor log/exp/sin/cos.src/hlsl.rs: the eight f32 mnemonics go throughtb_t32and the three f64 ones throughtb_t64(abs/neg as bit operations, omod/clamp through the existing f64 result path;TOMBOX_XS_F64_NATIVE=1restores the host math). For the f32 ones: (VOP3 abs/neg applied as bit operations, so NaN payloads survive; with an output modifier or clamp the exact result goes through the existingfrespath).TOMBOX_XS_F32_NATIVE=1restores the host math for perf A/B, mirroringTOMBOX_XS_F16_NATIVE.tests/exec.rsvector_f64:v_sqrt_f64of 4, 0.25, 16 and 1 expects the hardware's value (one 2^-26 step above the exact root).tests/exec_vop/basic.rs:rsq(-1),sqrt(-4)andlog(-1)expect0xffc00000(the hardware's NaN) instead of0x7fc00000.tools/rdna2-hwprobe:trans32_solve.cpp(derives the tables from the f64 outputs; re-running it regenerates the committed tables byte for byte),trans32_gen.py(writestrans32/tables.rsand the hardware digests intrans32/rdna2_hw.rs), and README.txt sections with the commands and the f64 rules.Tests (Windows 11 Pro, WARP, DXC 1.9.2609, on
27122f7)trans32::tests(CPU): the models reproduce FNV-1a-64 digests of 41 recorded f32 ranges, the f32 whole-range sweep of all eight ops in both denormal modes, and 78 f64 records (whole-range sweeps in both modes, both mantissa corners, 64 exhaustive [1, 4) mantissa sets); plus special values. 39 s in a debug build, 8 s in release.exec_mem::trans32(WARP):f32_transcendentals_exact_on_every_mantissa(136 blocks of 2^20 inputs, as before) andf64_rcp_rsq_sqrt_exact_across_mantissas_and_corners(45 cases: every mantissa of [1, 2) and [2, 4) with six low-word patterns, and the whole-range sweep with three), each thread hashing 1024 results; both rerun with runtime inputs.Cmp::X, both dwords for f64).2 * fr + 1to+ 3(17), exp's final rounding inputsumtosum + 1(17), and intb_t64the rsq entry-31 mask& 15to& 31together with the rcp low-bit corner nudge flipped (rcp 2 cases, rsq 8; sqrt still verifies).rust-workspace-checkfails on main too (missingcrates/compat/wldp/data/*.bin), unrelated to this PR.Not covered
f64 denormal results are exact wherever the hardware produces one (rcp results only reach 2^-1024), so no rounding rule for lost bits was needed or measured. All recordings are from gfx1036; the console's gfx1013 was not available.
cargo test -p tombox-xshaderpasses🤖 Generated with Claude Code
@omonkeyz on GitHub · 2026-11-22 23:03 UTC · original
Applied in
233d56f,13ae0c6and227e258(merged inf850f50) with your authorship kept, thanks. Your f64 rcp/rsq/sqrt commit is included. I added a THIRD_PARTY/CREDITS line for the rocjitsu models (05c1caf), and the licence file stays with the module. The full tombox-xshader suite passes on WARP, including exec at 103/0. It ships after the lead's current Trackmania baseline run.Pull request closed