tombox-xshader: decode and translate the f32 IEEE division helpers #272
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!272
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/272/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?
@azerioxfr on GitHub · 2026-11-23 10:28 UTC · original
azerioxfr:xshader-div-f32-helpers→main· +320 −2 · 4 files · 1 commits · closed without mergingLibrary / area: tombox-xshader,
v_div_scale_f32/v_div_fmas_f32/v_div_fixup_f32(hlsl.rsnext to the f64 helpers)Exports changed (exports.txt rows): none
Source of the behaviour: opcodes from
tests/isa_table/gfx1013.txt(VOP3 0x15f, 0x16d, 0x16f). Semantics from the RDNA2 ISA pseudocode, structured exactly like the already-verifiedtb_div_scale64/tb_div_fmas64/tb_div_fixup64with the f32 constants.One topic, +320 / -2: 4 lines in
rdna2/mnem.rs, 49 inhlsl.rs, the rest tests.What was missing
Only the f64 division helpers were decoded. Compilers emit the f32 trio (scale → rcp/fma → fmas → fixup) for every correctly rounded f32 divide, so such a shader failed to translate.
The change
v_div_scale_f32(0x16d, VOP3B)ldexpby ±64; VCC set for the zero and the 1/S1 && S2/S1 denormal casesv_div_fmas_f32(0x16f)ldexpby 64.v_div_fixup_f32(0x15f)tb_d_fmas) and the f32 helpers do not yet, so they are not silently run with default rounding.Tests
The f32 fma reference is computed exactly in f64. There is no
f32::mul_add, which the GNU toolchain rounds wrongly (#126).--release, DXC 1.9.2609)div_scale_and_fixup_f32div_fmas_f32_directdiv_f32_sequence(full scale → rcp/fma → fmas → fixup, against correctly rounded a/b)f32_division_helpers_refuse_a_shader_written_mode+ translation rows (--test opcodes)div_scale_and_fixup_f64,div_f64div_fmas_f64_directfails here as on main: the GNUfmafreference issue (#126)Cases are normal, denormal, near-overflow, zero, inf and NaN per op. Cases whose reference could not be made exact were dropped, not loosened. Only WARP was run, no hardware. A silicon cross-check of the f32 scale constant would be welcome if someone has the probe set up.
x86_64-pc-windows-gnu)missingwith a note@omonkeyz on GitHub · 2026-11-23 14:08 UTC · original
Applied locally on main with your authorship kept (cherry-picked,
ebc3148); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.@QuentinDanblon on GitHub · 2026-11-23 18:37 UTC · original
Ran this PR's exec tests on an NVIDIA RTX 5090 (driver 617.42,
TOMBOX_EXEC_HW=1; the adapter is checked, not WARP).div_scale_and_fixup_f32anddiv_f32_sequencepass;div_fmas_f32_directfails 3 runs out of 3 on one case, while WARP passes:v_div_fmas_f32with a = 0, b = +inf, c = 1, vcc = 1. Expected0x7fc00000, the 5090 returns0x7fffffff.The cause is in the translation, not a driver defect:
tb_fmareturns the canonical NaN, then the vcc = 1 path multiplies it by 2^-64. NVIDIA returns0x7ffffffffor any NaN produced or propagated by float math (a HLSL probe on the 5090:mad(0, inf, 1),qnan * 2^-64,qnan + 1,0 * infall give0x7fffffff; WARP keeps the payload). This is the same host behaviour as the f16 entry "RDNA2 f16 FMA NaN payloads on non-AMD hosts" in BLOCKERS.Fix tested locally: return
ras soon as it is a NaN, before any scaling. On RDNA2 a NaN times 2^±64 is the same NaN, so the result does not change there, and the bits stop depending on the host. With it, the fourdiv_*tests (f32 and f64) pass on the 5090 and on WARP.The other division helpers pass their tests on the 5090, but I did not check that their NaN cases are covered, so the same pattern (scaling or adding to a NaN result) may exist elsewhere. Other PRs replayed on the 5090 with no failure: #247, #249, #251, #258, #263, #264, #305.
@SchmashinPrunes on GitHub · 2026-11-24 12:49 UTC · original
I recorded the three ops on RDNA2 silicon: the gfx1036 iGPU of a Ryzen 5 9600X, ROCm/HIP 5.7.1, raw wave32 kernels. That is RDNA2, not the console's gfx1013.
Setup:
div_f32_sequence, with the same instruction order.I compared the recordings against my own C++ port of this PR's HLSL helpers. I did not run the PR's code here.
Summary (counts are cases where the result bits differ):
v_div_fixup_f32div_f32sequencev_div_fmas_f32v_div_scale_f32v_div_scale_f32. The hardware decides with exponent tests only and never forms the quotient. This rule reproduces all 1,048,576 recorded results and VCC values bit-exactly:
0xffc00000.Here x·2^k is rounded to f32 with denormal results kept, and inf or NaN x is returned unchanged.
Where the PR differs:
1/dandn/dtests where the hardware usesed ≥ 253anddf ≤ −96. For example, x = n =0x9bcdf504, d =0x5a7f8c2fgives0xbbcdf504with VCC 1 on silicon; the PR gives x unchanged with VCC 0.0x8cdfb772, n =0x8c5d532bgives0xacdfb772on silicon.0x7fc00000with VCC 1. Silicon gives0xffc00000with VCC from the exponent rule, so 0/0 gives VCC 0 and 1/0 gives VCC 1.v_div_fmas_f32. Silicon matches a single rounding of the exact a·b + c × 2^(±64), with no f32 range limit on the intermediate. The scale is +64 when VCC is set and exp(c) > 127, and −64 when VCC is set otherwise. That model matches every non-NaN case. The PR's model rounds to f32 first, so it differs when the unscaled result is out of range. Examples, all with VCC 1:
0xe03fffff, PR0xff800000.0x6a533e4f, b =0x5f0439ce, c =0x0bfdc135: silicon0x69da37a7, PR0x7f800000.NaN results, all three ops. These are relevant to the RTX 5090 report above:
0x7f800001stays0x7f800001.0xffc00000, not0x7fc00000. Case 5 ofdiv_fmas_f32_direct(a = 0, b = +inf, c = 1, VCC = 1) gives0xffc00000on gfx1036.0x7fc00000, so its expected value needs to be0xffc00000too.So on RDNA2 neither
0x7fc00000nor the 5090's0x7fffffffis the hardware value. If NaN payloads stay host-dependent, as for the f16 FMA ops, comparing those cases as a NaN class on hardware runs would match what #197 did.Not covered:
0x00800001). I didn't model it, since the PR refuses shader-written MODE.I can turn the recordings into a WARP replay test (
exec_vop, likerdna2_silicon) once this lands, if you want it.(Measured and written with an AI assistant, Claude.)
@omonkeyz on GitHub · 2026-11-25 13:08 UTC · original
Applied on main in
ebc3148(cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.Pull request closed