xshader: guard f32 sub and f16 add/sub/mul against AMD host folding (fixes three #333 failures) #375
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!375
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/375/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-24 18:44 UTC · original
SchmashinPrunes:amd-host-fold-guards→main· +80 −11 · 5 files · 2 commits · closed without mergingLibrary / area: tombox-xshader.
hlsl.rsgets thetb_fsubguard and newtb_hadd/tb_hsub/tb_hmulhelpers;tests/exec.rsgets the UAV view fix inrun_res_psv.Fixes the three #333 failures that reproduce on our AMD host, a Radeon RX 9060 XT with
TOMBOX_EXEC_HW=1. They pass on WARP, and per #333 on an RTX 3070.What was wrong
vop2_basicruntime inputs #642/643/707/708v_sub_f32/v_subrev_f32with ±inf − ±inf gives0, want0x7fc000000x7f800000 | v200), so DXC's CSE merges them. The DXIL isfsub float %16, %16, and thepreciseintb_fsubkeeps fast-math flags off it, but AMD's driver still foldsx - xto 0.f16_omod_follows_moderuntime inputs #4v_add_f16(-0, -0)gives+0f16tof32(x) + f16tof32(x)tofmul fast x, 2.0, and AMD drops the zero sign. Compiling with-Gisdoes not help.scalar_smem_loads#39–46s_load_dword*descriptor probes read word 3 of the slot instead of word 9 (e.g.a5002323vsa5002929)run_res_psvonly recognisedRWStructuredBuffer<TbElem_ubuf0>. These probes useubuf1/ubuf2/ubuf6, which were therefore given typedR32_UINTviews under a structured declaration.The change
tb_fsubreturns the quiet NaN from an integer test when either input is NaN or both are the same infinity. Otherwise it returns theprecisedifference. Finite inputs never produce NaN, so the oldtb_qnon the result is no longer needed.v_add_f16,v_sub_f16,v_subrev_f16andv_mul_f16now go throughtb_hadd/tb_hsub/tb_hmul. When the f32 result is ±0, these OR in the IEEE round-to-nearest zero sign with integer ops:An f16×f16 or f16±f16 result can't underflow to an f32 zero, so a zero result is always exact.
The harness picks the structured view stride from any
TbElem_ubufNdeclaration, not justubuf0.Validation
TOMBOX_EXEC_HW=1 cargo test --locked -p tombox-xshader --release --test exec: 128 passed, 0 failed. Onmainthe same run failsscalar_smem_loads,vop2_basicandf16_omod_follows_mode.cargo test --locked -p tombox-xshader --release --no-fail-fast: all pass.isa_sweepon the RX 9060 XT: the first commit fixes thev_mul_f16signed-zero case. The remaining sweep failures are handled in the second commit (below).Second commit: the remaining AMD sweep failures
This comes from the RX 9060 XT
isa_sweeprun (TOMBOX_EXEC_HW=1). Failing mnemonics go from 11 to 3.v_trunc/floor/ceil/fract_f64with VOP3neg: trunc(−1.0) = +1.0, floor(−1.0) = 0(double)(int64_t)x. The DXIL is correct (fsub fast -0.0, xthenfptosi), but AMD's driver drops the folded negation in the f64→i64 conversion.tb_trunc64clears the fraction bits with integer masks, with no int64 conversion. Checked bit for bit againstmath.truncon 400k doubles: ±0, denormals, the 2^52 boundary, infinities.v_fmaak/v_madak_f32andv_fmaak_f16with K = 0: −0·0 + 0 gives −0x + 0.0tox, the same class as thex − xfold above.tb_pzforces an exact zero result to +0. It's also applied tov_add_f32with an inline/literal +0 and tov_fma/v_mad_f32/v_fma_f16with a +0 addend, before denorm flushing and output modifiers. For the fused f32 forms,tb_pzfapplies it only when an operand is zero, since a product that underflows must still round to a signed zero.v_ldexp_f16(−0, e)gives +0ldexp.It also fixes a bug in this PR's first commit:
tb_hzOR-ed the zero sign in, so it could never turn a folded −0 back into +0. It now sets the sign.Tests.
amd_host_zero_sign_and_f64_neg: 48 cases with constant and runtime inputs, including controls where the guard must not fire (aneg-modified constant, K = −0, a fused underflow).hlsl.rschanges, 23 runtime-input cases fail. With them, the test passes there and on WARP.v_ceil/floor/trunc/fract_f64,v_madak_f32andv_fmaak_f16, so they're removed fromWARP_KNOWNinisa_sweep.rsand CI guards them.Still failing on AMD, not addressed:
v_add/sub/mul_f16with SDWA byte select plusabson src0. The operand reduces toand i32 %x, 255beforelegacyF16ToF32, so it is provably a positive f16 denormal, and AMD returns 0 for it in the sweep's compute shader. The same pattern in the exec harness's pixel shader passes, thenegvariant passes, and forcing-denorm preserveor-denorm anymakes no difference. A real compiler wouldn't emit SDWA byte select on an f16 operand, so I've left it.Notes
mtbuf_d16_store_formats, passes on the RX 9060 XT, so this PR doesn't touch it.ifline inrun_res_psv. It's a one-line textual conflict, and I'll rebase whichever lands second. #347 and #368 merge cleanly with this branch.🤖 Generated with Claude Code
@omonkeyz on GitHub · 2026-11-25 13:50 UTC · original
Applied on main in
d0fabcb(cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.Pull request closed