Recompiler: f16 add / sub / fma rounded once under a directed mode (#… #128
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!128
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-128"
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?
@gbuyssens on GitHub · 2026-11-23 13:40 UTC · original
gbuyssens:pr/f16-directed-rounding→main· +342 −23 · 2 files · 1 commits · open…111)
Under s_round_mode MODE[3:2] != RNE the f16 add, sub, fma (also fmac / fmamk / fmaak, the packed forms and v_fma_mix*_f16) computed in f32 with RNE and then converted to binary16 in the requested mode: when the exact result does not fit f32 (1.0 + 2^-24 needs 25 bits) the first rounding could land exactly on a binary16 value and the directed rounding had nothing left to do (1.0 + 2^-24 under +inf gave 1.0; the console gives 1 + 2^-10).
@ivall on GitHub · 2026-11-23 14:14 UTC · original
Holding this one: its own GPU test fails on an RX 9070 (RDNA4), in the PR as it is (without merging anything else):
lane 0 mode 0x4: v_sub_f16– left813710337(0x30804001), right15361(0x3C01) (gpu_f16_add_and_fma_round_once_under_directed_modes, tests.rs:5791). Note both halves are off, the high half too. Which GPU did you run it on? (#111 stays open; @gefydev offered to take it as well.)@Foniass on GitHub · 2026-11-23 14:45 UTC · original
Tested on an NVIDIA RTX 5070 Ti (32-lane waves, driver 621.14), merged onto
origin/main(95bda11, fast-forward).NVIDIA and denormals: this is fine. With DXC's default
-denorm anythis GPU does flush f32 results below the normal range (precise2⁻⁷⁰ × 2⁻⁷⁰ → 0). But the f32 TwoSum here only handles multiples of 2⁻⁴⁸ (2⁻⁴⁹ after OMOD ×0.5), so the flush never reaches it, and f32↔f64 conversions keep denormals. Over 21 M random f16 add vectors × 4 modes, the GPU matches an exact CPU reference (big-integer value, rounded once), apart from the sign of zero (see 5).Verified on the GPU
Regression under RNE: a result just below 65536 rounds to 65504 instead of inf. In
f32_to_f16_rm_stickythe overflow branch's tie case compares with0x47800000(65536). An f32 result of exactly 65536 with a negative error is really in (65520, 65536) and must round to inf under RNE.maingives0x7C00for all of these:v_fma_f160x5C00 × 0x5C00 + 0x8001(256·256 − 2⁻²⁴)0x7BFF0x7C00v_fmac_f160x7BFF0x7C00v_pk_fma_f160x7BFF7BFF0x7C007C00v_add_f16OMOD ×20x7800 + 0x80010x7BFF0x7C00v_fma_mixlo_f161.0f × 65536.0f + (−2⁻³⁰)0x7BFF0x7C00s_round_mode. A random run found 674 add+OMOD, 76 fma and 7 514 fma_mix cases, all of this kind, negative values included.if (rm == 0u) return sign | 0x7C00u;. The probe at0x477FF000takes the normal path, so it never reaches this branch.v_fract_f64is listed as exact, but it isn't. RDNA2 defines it as x − floor(x), which rounds for negative x.fract(0xBFD0000000000001)under RTZ gives0x3FE8000000000000; RTZ is0x3FE7FFFFFFFFFFFF.mainreporteds_round_mode 0xC+v_fract_f64as unsupported; with this PR it translates and computes in RNE. The retail pack doesn't hit it, butv_fract_f64should probably leave the exact list.gpu_s_round_mode_controls_f16_rounding_across_modesfails here (lane 17):v_fma_mixlo_f16(−0.0, 1.0, +0.0)under RNE gives0x0000, and the test expects0x8000. The PR is right: by IEEE fma(−0, 1, +0) is +0. The test reuses the conversion table, which expects −0.mainprobably passed because DXC simplifiedmad(x, 1, 0)tox. With lane 17's v22 / v25 expected as0x0000, it passes. If it passes on AMD, that may mean theprecise+0 is being dropped there.Also present on
main(not regressions)fma_mix_f16_rm, f32 range limits: the product is exact in a double, but its f32 rounding can overflow or underflow.0x7C00instead of0x7BFF(84 862 random cases).if (ax == 0u) return sign;drops the error: 2⁻¹⁰⁰f × 2⁻¹⁰⁰f under +inf gives0x0000instead of0x0001(59 211 cases). The f32-denormal case (2⁻⁷⁰ × 2⁻⁷⁰ under +inf →0x0001) is fixed by this PR.v_add_f16 0x3C00 + 0xBC00).Other checks
f32_to_f16_rm/mad_nf_f16leave the shared prelude. Behaviour is the same, but the text changes, so every program's DXIL cache entry is rebuilt once.XCL_SAMPLE_RPF,maingives 517 passed / 0 failed in the lib tests, and this PR 517 passed / 1 failed (3 above). Every other test binary has the same counts, andXCL_SPLIT_WAVE64=1gives the same result. The PR's new GPU tests run on this card; they are not skipped.@tronLives111 on GitHub · 2026-11-23 16:58 UTC · original
A follow-up case for #111: default round-to-nearest-even can also double-round a fused half-precision multiply-add. On
49ee291,emit.rsstill usesmadfollowed byf32tof16when the shader never writes a non-defaultMODE[3:2](see lines 456, 534, and 2854). That looks like pre-existing behavior outside this PR’s directed-mode fix.I checked these binary16 bit patterns using native AArch64 half-precision
FMADDand, separately, binary32fmaffollowed by a binary16 conversion on Apple Silicon. All nonzero inputs are normal.abc0x6A000x3C010x84000x6A010x6A020x6A000x3C030x04000x6A050x6A04The exact sums sit just below and above their respective half-precision midpoints. The binary32 intermediate lands exactly on each midpoint, so ties-to-even picks the wrong neighbor. With
c = 0, both paths agree on the even neighbor in each case. Av_fma_f16fixture with nos_round_modewould exercise the default emitter path. CPU arithmetic and source inspection only, no XCL GPU run or game data. AI-assisted review.@ivall on GitHub · 2026-11-23 21:09 UTC · original
Not merged yet: with this PR on current main,
gpu_s_round_mode_controls_f16_rounding_across_modesfails on an RX 9070 (RDNA4):lane 18 (0x7f800000) v_fma_mixlo_f16 RTZ: left 31743, right 31744- an infinite f32 input under RTZ comes out as 0x7BFF instead of 0x7C00 (infinity stays infinity in every mode). Could you look at the fma path for an infinite input (the TwoSum error is NaN there) and rebase on main?@Kalmarv on GitHub · 2026-11-23 21:38 UTC · original
The RX 9070 failure reproduces on NVIDIA Turing through vkd3d-proton / dxil-spirv (
lane 18 (0x7f800000) v_fma_mixlo_f16 RTZ, left31743, right31744), so it's the HLSL, not RDNA4. The test is right: an infinite operand is an exact infinity in every mode. The clamp @RemingtonWilcox pointed at infma_mix_f16_rmis the cause. Clamping only finite sums fixes it here (false for ±inf and NaN; HLSL has noisinfon a double):Tested
master30b0eb9.x86_64-pc-windows-gnurelease build of49ee291merged ontomain60565c4(clean).f16filter: 12 passed / 1 failed as merged (gpu_s_round_mode_controls_f16_rounding_across_modes, the case above); 13 / 0 with the line.gpu_f16_add_and_fma_round_once_under_directed_modespasses in both.gpu::recomp: 176 / 7 as merged, 177 / 6 with the line. The other 6 fail the same with and without it (the memory-window and UMA-check tests that read zeros on this Wine build, #175).Not tested
🤖 Claude Opus 5.5
@srcamarda on GitHub · 2026-11-23 21:52 UTC · original
AMD run, as asked: RDNA3 Radeon RX 7800 XT, driver 32.0.32029.2010, Windows 11 26300, native 64-lane waves, DXC 1.8.2502.11, debug build.
49ee291merged ontomain60565c4(clean merge).f16filtercargo test -p xcl --libXCL_SPLIT_WAVE64=1gpu_s_round_mode_controls_f16_rounding_across_modes,lane 18 (0x7f800000) v_fma_mixlo_f16 RTZ, left31743, right31744. So it shows on RDNA3 as on RDNA4 and Turing. Clamping only finite sums infma_mix_f16_rmfixes it here as well (same line as Kalmarv's, with a comment; diff below).gpu_f16_add_and_fma_round_once_under_directed_modes(lane 0 mode 0x4: v_sub_f16) passes on the RX 7800 XT, as it does on Turing.fma(-0, 1, +0)→ +0) passes here, so AMD keeps theprecise+0.v_fract_f64off the exact list) are in49ee291already.Not covered: RDNA4 with the line (whether the RX 9070's
v_sub_f16failure is still there on the current head), and the open points that predate this PR (@tronLives111's default-mode double rounding of fused f16 multiply-adds, and Foniass's 4 and 5).@WarmMilkCodes on GitHub · 2026-11-23 22:09 UTC · original
Tested #128
49ee291merged onto main9ab7d76on AMD Radeon RX 6600 XT (RDNA2), driver 32.0.21045.29003, Windows 11 23H2 (22631.6199): fail – one test on main now failscargo test --workspace --no-fail-fast -- --test-threads=4: main 651 passed / 0 failed; this 651 passed / 1 failed:That test came to main with
93072dc("handle s_round_mode/s_setreg*"), after this branch's base (129 commits behind), so it won't show on the branch itself. Lane 18 is +inf: under RTZ,v_fma_mixlo_f16now gives0x7BFF(max finite) instead of0x7C00(+inf), which the test's table expects for +inf in every mode. Looks like the directed-mode path for the fma_mix forms clamps overflow to the largest finite value without letting an infinite input through – RTZ should only clamp finite results that overflow. Probably not GPU-specific; it should reproduce anywhere after a rebase.@PHeitor1 on GitHub · 2026-11-23 22:34 UTC · original
Tested
49ee291merged ontomain9ab7d76on RTX 5060 Ti 8 GB (Blackwell, 32-lane waves), NVIDIA 621.24, Ryzen 5 5600X, Windows 11 Pro 26300 (native), rustc 1.100.0 MSVC, debug,cargo test --workspace --no-fail-fast -- --test-threads=4, noXCL_SAMPLE_*. Baselinemain9ab7d76: 651 passed, 0 failed, 40 ignored.Fail, same as on RDNA2 above (so not GPU-specific): 651 passed, 1 failed ?
gpu_s_round_mode_controls_f16_rounding_across_modes, lane 18 (0x7f800000)v_fma_mixlo_f16 RTZ: 31743 (0x7BFF) vs 31744 (0x7C00).Cause: in
fma_mix_f16_rman infinite sum reaches the out-of-range clamp (if (sa > 131072.0) s = ?131072.0) and becomes a finite overflow, which RTZ rounds to 65504. A guard before the clamp fixes it:Note the check is on the f32 sources: my first try,
isinf(s)on the double, broke this PR's ownfma_mix_f16_rm(2^100, 2^100, 0, false, 3u)case (expects0x7BFF) because HLSL'sisinfconverts the double 2^200 to a float first.With the guard on the same merge:
cargo test -p xcl --lib f16: 13 passed, 0 failed; whole suite 652 passed, 0 failed, 40 ignored. GPU skips: the same 2 as onmain(no wave64).Not tested: AMD with the guard; no game.
Run and written with Claude Code (Claude Opus 5.5).
@ic3bear-ai on GitHub · 2026-11-24 00:00 UTC · original
A note for this PR from #111 (the details and numbers are there): on an RTX 3070 the default mode is wrong for these instructions too, so this PR may want to cover it as well.
to_f16_expremits the legacyf32tof16, which truncates on that GPU: about 30 % of randomv_add/sub_f16andv_pk_add_f16results are one ulp off, and the fma forms too.What fixed both locally: round-to-odd in binary32 (TwoSum, then the sticky lowest bit), then
f32_to_f16_rm, one helper for every mode. I haven't checked it against the infinite-input RTZ case this PR is held on.I'd rather not open a PR that conflicts with this one. The exact-reference test is in #111, if it helps.
Comment written with Claude Code (Claude Opus 5.5); I checked the results.
@vladbondarenko97 on GitHub · 2026-11-24 19:15 UTC · original
A data point for this PR from NVIDIA, where the default (round-to-nearest-even) case is wrong on
maintoday, not only directed modes: on an RTX 3080 Ti (wave32, Windows 11, DXC 1.9.2602.17), the HLSL path's f16 add/sub/fma/fmac/ldexp andv_pk_add_f16/v_pk_fma_f16results go throughto_f16_expr→f32tof16, which rounds toward zero on this driver. Examples from GPU tests against Rust-computed RDNA2 results:v_add_f16(0x3C01, 2^-11)(a tie) gives0x3C01, expected0x3C02;v_pk_add_f16high half lane 0 gives0xbfff, expected0xc000;v_ldexp_f16into denormals lane 15 gives0x207, expected0x208.v_mul_f16/v_pk_mul_f16are right (they usef32_to_f16_rm). AMD drivers likely round to nearest already, so an RDNA4 run may not show this. The test programs are onvladbondarenko97/xclrecomp/f16-tests(1a08c39); I have not run this PR's head yet.AI use: Claude Code (Claude Opus 5.5) wrote and ran the GPU tests and this note.
@joeypercia on GitHub · 2026-11-24 19:18 UTC · original
#842 fixes this default-mode case:
to_f16_exprrounds to nearest even withf32_to_f16_rm(e, 0u), the helper the multiplies already use, so add/sub/fma/ldexp and the packed ops round to nearest even. On AMD it isn't different: the RDNA2 iGPU of a 7800X3D truncates the same way onmain(#842's Tested has both GPUs). #842 also coversv_interp_p2_f16andv_fma_mixlo/mixhi_f16.AI use: Claude Code (Claude Opus 5.5) drafted this note.
@malzaharhub-ctrl on GitHub · 2026-11-24 21:27 UTC · original
Independent AMD GPU validation — RX 7900 XTX
Tested PR #128 at commit
2bc03fa2767727ca2a52c9a8d53c475c1fd7a346.Hardware and environment
32.0.32029.2010(18 November 2026)1.100.01.9.2602.17Targeted f16 GPU tests
Both tests passed with
XCL_REQUIRE_GPU=wave64:gpu_s_round_mode_controls_f16_rounding_across_modes: 1 passed.gpu_f16_add_and_fma_round_once_under_directed_modes: 1 passed.F16-filtered library tests
cargo test --locked -p xcl --lib f16 -- --test-threads=1 --nocaptureResult: 13 passed, 0 failed.
Note: this name filter also includes four UTF-16-related tests.
Full GPU recompiler test group
cargo test --locked -p xcl --lib gpu::recomp -- --test-threads=1 --show-outputResult:
Run with
XCL_REQUIRE_GPU=wave64.Full workspace validation
Ran
scripts/test.ps1.Result: exit code 0.
One optional GPU test was skipped because the adapter does not support heap windows.
The repository working tree remained clean. No source changes were made.
I have not tested any game files.
AI assistance disclosure: ChatGPT assisted with interpreting test results and drafting this report. I independently executed the commands and tests on my hardware and verified the results.
@Toons29 on GitHub · 2026-11-25 02:52 UTC · original
Regression run of
2bc03fa2on the retail title, for this PR's Regression run section. Both blocks are fromscripts/smoke.ps1as printed, not edited:mainon4178e888, this PR on its head commit with no uncommitted changes, same title and time.Against the limits in CONTRIBUTING.md: 101 % of
main's frames, 100 % of its peak commit, no crash, no device removal.AMD Radeon RX 9070 (RDNA4, driver 32.0.32029.2010), Ryzen 7 7800X3D, 32 GB, Windows 11 build 26300, Rust 1.100.0,
cargo build --release -p xcl, 60 s,XCL_NETWORK=0. This is a regression check only: it does not show whether the run reaches the code this PR changes.AI use: Claude Code (Claude Opus 5.5) built both trees and ran
scripts/smoke.ps1; I checked the blocks before posting.@JJAutoCompare on GitHub · 2026-11-25 03:00 UTC · original
Tested
I ran both checked-in f16 rounding fixtures from a clean checkout of
2bc03fa2767727ca2a52c9a8d53c475c1fd7a346. Afterward GitHub still showed PR #128 open at that exact head.Intel Arc A770 (driver
32.0.101.9175), AMD Ryzen 7 3800X, Windows 11 Enterprise Evaluation26200.9974, Rust and Cargo1.100.0(x86_64-pc-windows-msvc), and loaded DXC/DXIL1.9.2609.5. Cargo test profile was unoptimized with debug info disabled.SKIPPEDlines were emitted.cs_6_6with DXC and then failed atCreateComputePipelineStatewithThe parameter is incorrect. (0x80070057). The helper failed creating the pipeline for its first probe kernel (tests.rs:6979), and the instruction fixture failed creating the pipeline for its kernel (tests.rs:7061). No GPU dispatch or result comparisons completed.Not tested
The cause of the D3D12 pipeline rejection, other adapters or DXC versions, direct DXIL, a
mainbaseline, the full workspace suite, and retail-title smoke runs.AI use
T3 Code / Codex (
gpt-6.2-sol) ran the fixtures, checked the exact head and assertion counts, and wrote this report.@DavidM1911 on GitHub · 2026-11-25 15:22 UTC · original
Verification on RTX 3060 Ti (Windows 11): merge conflict with current
main— needs a rebase before it can be tested.View command line instructions
Manual merge helper
Use this merge commit message when completing the merge manually.
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.