Repository navigation
Open
Description
Follow-up to #91 / #73, found while testing #91 on an RTX 5070 Ti.
With a directed f16 rounding mode (s_round_mode MODE[3:2] = RTZ, +inf or -inf), f16 add / sub / FMA results are rounded twice: the operation runs in f32 (RNE), and then f32_to_f16_rm (to_f16_expr, emit.rs) rounds that f32 value to f16 in the requested mode. When the exact result isn't representable in f32, the first RNE step can land exactly on an f16 value, and the directed step then has nothing left to round.
Measured on the GPU (same results natively and in the split wave64 form):
| mode | instruction | inputs | XCL | correct |
|---|---|---|---|---|
s_round_mode 0x4 (+inf) |
v_add_f16 |
0x3C00 (1.0) + 0x0001 (2⁻²⁴) |
0x3C00 |
0x3C01 |
s_round_mode 0xC (RTZ) |
v_add_f16 |
0x4000 (2.0) + 0x8001 (−2⁻²⁴) |
0x4000 |
0x3FFF |
s_round_mode 0xC (RTZ) |
v_fma_f16 |
2.0 × 1.0 + (−2⁻²⁴) | 0x4000 |
0x3FFF |
- Affected:
v_add_f16/v_sub_f16/v_subrev_f16,v_fma_f16,v_fmac_f16/v_fmamk_f16/v_fmaak_f16,v_pk_add_f16/v_pk_fma_f16/v_pk_fmac_f16,v_fma_mix*_f16. - Not affected:
v_mul_f16andv_cvt_f16_f32, whose f32 intermediate is exact, and RNE mode, where double rounding through f32 is harmless (24 ≥ 2·11 + 2). - Why it matters: per Floating-point modes: the non-fused MAD/MAC family needs
precise, and thes_round_modeNOOPs are not free #73,0xC(f16/f64 RTZ) is 95% of the retail pack'ss_round_modeuses, so f16 add / FMA under RTZ is the main case recomp: handle s_round_mode/s_setreg* rounding modes and verify non-fused MAD/MAC #91 handles. It's much closer than before (always RNE) but not exact. recomp: handle s_round_mode/s_setreg* rounding modes and verify non-fused MAD/MAC #91's tests cover conversion, multiply and the mix instruction, whose inputs are exact, so they don't catch this. - Possible fix: an f16 sum is always exact in f64 (at most 41 significant bits), so add/sub can be computed in double and rounded once to f16 with the directed mode. FMA needs the exact error term (TwoSum / FMA residual, or round-to-odd in f64) before the directed rounding.
Two smaller notes from the same review:
V_MAD_F16is in neitherrdna2_spec.rsnorrdna2_xbox.rs(encode("V_MAD_F16", …)fails), so thev_mad_f16arm inemit.rscan't be reached.gpu_mad_nf_f16_rounds_intermediate_in_binary16tests the HLSL helper directly.is_f64_rounded_opcounts every_f64name except compares and three conversions, sos_round_mode 0xCfollowed by an exactv_cvt_f64_f32is reported unsupported ("non-default f64 round mode"). The pack still translates 100%, so this has no effect today.
Metadata
Metadata
Assignees
Labels
No labels
Activity
gefydev commentedon Nov 23, 2026
@Foniass are you fixing this one yourself? If not, I can take the add / sub / FMA part: add and sub in f64 and rounded once to f16 in the directed mode (exact, as you say). For FMA the f64 product is exact (22 bits), but product + addend is not always: c = 2^15 with a product of -2^-40 gives exactly c in f64, and RTZ needs c - 1 ulp. So I would take the exact error of that sum (TwoSum) and move the f64 result one ulp towards it when it is not zero, then round once to f16. I would test it on a GTX 1650. I will wait for your answer so we do not do it twice.
Recompiler: f16 add / sub / fma rounded once under a directed mode (i…
HANDOFF: session 6 (the audits xcl-dev#103–xcl-dev#106, xcl-dev#111, …
Recompiler: f16 add / sub / fma rounded once under a directed mode (i…
Recompiler: f16 add / sub / fma rounded once under a directed mode (i…
Recompiler: f16 add / sub / fma rounded once under a directed mode (i…
ic3bear-ai commentedon Nov 23, 2026
Two additions from a run on an RTX 3070. Both concern the default mode. The issue lists it under Not affected: "RNE mode, where double rounding through f32 is harmless (24 ≥ 2·11 + 2)".
1. In the default mode the f16 step is not round-to-nearest in the code as it is. That reasoning needs the second rounding to be RNE too (and, see 2, holds for add / sub only). Without a dynamic mode, though,
to_f16_expremits the legacyf32tof16, notf32_to_f16_rm, and on this GPU that rounds toward zero. The comment atto_f16_rounded_exprsays so as well.Tested on
mainat9ab7d76: RTX 3070 (driver 616.92), Windows 11 build 26200, debug build, DXC 1.8.2502.11. The GPU test is against an exact reference: f16 values as integers in units of 2^-24,a + banda·b + cexact ini128, rounded to nearest even in integer arithmetic. It's IEEE as documented for RDNA2, not a comparison with console hardware. Results before the fix (1600 per form, wave32; the fma forms include the pinned cases from point 2):v_add_f16/ with neg, abs / with omod ×2v_sub_f16/v_subrev_f16/v_sub_f16with abs on bothv_fma_f16/ with neg, abs / clamp / omod ×0.5v_fmac_f16/v_fmamk_f16/v_fmaak_f16v_pk_add_f16lo / hiv_pk_fma_f16lo / hi / with neg lo / neg_hiv_pk_fmac_f16lo / hiv_mul_f16,v_pk_mul_f16lo / hi (control)The 40 failures the test prints are all one ulp toward zero, e.g.
v_add_f16(0x03ff, 0xbc00)(−1 + 6.1e-5) gives0xbbffinstead of0xbc00.One GPU and one driver. I don't know whether other NVIDIA GPUs or AMD's
f32tof16truncate too.2. For fma, RNE double rounding through f32 is not harmless, even with an RNE second step. The 2p + 2 result covers +, −, ×, ÷ and √ of p-bit operands, not a·b + c (the product has 22 bits). Counterexample:
fma(0x3100, 0x7162, 0x0300)= 1722.5 + 4.6e-5. The exact value is just above an f16 tie, so it must round to 1723 (0x66bb); through f32 the addend is lost, and the tie goes to0x66ba.A CPU simulation ("f32 fma, then f16 RNE" against the exact reference) found 710 such cases in 31.8 M. The inputs were biased toward ties, so that says little about the rate in real shaders. So the fma fix is needed in the default mode as well, not only the directed ones.
What I tried locally (not pushed), for #128 or whoever takes this: round-to-odd, as the issue suggests, but in binary32 rather than f64:
f32_to_f16_rm.Results:
XCL_FUZZ_ITERS=5000(160 000 per form);cargo test --workspace --no-fail-fast -- --test-threads=4: 653 passed, 1 failed, 40 ignored, 2 SKIPPED (no wave64). The failure is a separate test of mine for partial-wave reductions (VS / PS whole-wave reductions as wave operations (partial waves, every GPU) #152), which fails onmainby design.That's a sample, not a proof.
Not tested:
preciseTwoSum as written;The test is below, in case it helps check #128 or an AMD GPU.
Comment written with Claude Code (Claude Opus 5.5) from runs on the hardware above; I checked the results.
The test (paste into
crates/xcl/src/gpu/recomp/tests.rs, beforefn hwreg_imm; it uses the file's existingf16_to_f64,Rng,gpu_waves,kernel_wave,prog)Run with
cargo test -p xcl --lib f16_ -- --nocapture;XCL_FUZZ_ITERS=5000for more cases.joeypercia commentedon Nov 24, 2026
Two answers to the open points from @ic3bear-ai's default-mode report above, measured on
maincb868b2, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug, DXC 1.8.2502.11:1. AMD and Ada truncate too.
f32tof16rounds toward zero on an RTX 4070 Ti (616.56) and on an AMD RDNA2 iGPU (Ryzen 7 7800X3D, 26.11.2), wave64 and wave32.2. It's wider than add / sub / fma. Every f16 result that goes through
to_f16_exprtruncates in the default mode, including the unary ones. A GPU test against an exact reference (inexact results only, 100 dispatches, counts as [nearest even, toward zero, neither]):v_add_f16v_sqrt_f16v_rcp_f16v_rsq_f16v_exp_f16v_log_f16v_ldexp_f16v_mul_f16(control, #166)(The 61 nearest-even
v_add_f16results on the 4070 Ti are sums that are inexact in f32 already.)Fix.
to_f16_exprtakesf32_to_f16_rm(e, 0u)in the default mode too, the helper #166 already uses forv_mul_f16:With it, every count above becomes nearest even on both GPUs.
cargo test --workspace --no-fail-fast: 686 passed, 0 failed, 41 ignored on both (the existings_round_modeand fma f16 tests included). The test,gpu_f16_results_round_to_nearest_even_by_default, fails onmainand passes with the fix.Not covered: the fma double rounding from point 2 above, which needs the exact sum (#128's TwoSum work); the directed modes (#128);
v_sin/v_cos_f16and the packed forms; and the otherf32tof16call sites inemit.rs(exports and d16 stores, lines ~1662, 2108, 2333, 5166), which I haven't checked. No regression run, since I have no title.@ic3bear-ai, @gbuyssens: happy for either of you to fold this into your work, or I can open it as a PR with the test if someone with a title can add the
xcl-smokeruns.AI use: Claude Code (Claude Opus 5.5) wrote the test, the fix and this comment; all numbers are from the runs described.
joeypercia commentedon Nov 24, 2026
Correction to my comment above: the
to_f16_exprchange I proposed is not safe on its own. As #655 (@acaracargo) shows, it regressesv_fract_f16:fract_fclamps the f32 result to just below 1.0, and narrowing that to nearest even lands on 1.0 in f16 (x = −2⁻¹⁴ gives0x3c00instead of0x3bff). With truncation the clamp happened to survive. So the default-mode fix also needsv_fract_f16to clamp after the narrowing (to0x3bff), and #655's test should go in alongside it. My measurements above still stand; only the one-line fix as written needs that extra case.AI use: Claude Code (Claude Opus 5.5) wrote this; the regression itself is from #655's run, I haven't re-measured it.
acaracargo commentedon Nov 24, 2026
Thanks for pointing this out. That's exactly why I included the fract edge case in #655. Glad the test was useful! @joeypercia
Anon64 commentedon Nov 24, 2026
@joeypercia if you open the PR for the default-mode
to_f16_exprfix (with thev_fract_f16clamp and #655's test), I can make the twoscripts/smoke.ps1runs on a retail title, the PR's head commit and themainit is based on, and post thexcl-smokeblocks there for Regression run, as for #672. Same machine as then: RTX 3060 12 GB, wave32.It overlaps #696, which sends only add / sub through the same helper: your change covers those too, so if it lands first, #696 can be closed.
Written with Claude Code (Claude Opus 5.5).
joeypercia commentedon Nov 24, 2026
@Anon64 thanks, it's up as #842:
to_f16_exprrounds to nearest even in the default mode,v_fract_f16keeps its result at0x3BFF(the #655 case, with its own test that fails without the clamp), and it covers #696's add / sub. Headfeaeb7b, based onmainbeaa245; whenever you have time for the twoxcl-smokeruns.Written with Claude Code (Claude Opus 5.5).
Recompiler: f16 add / sub / fma rounded once under a directed mode (x…
Tests: float rounding ops and fract's clamp below 1.0 on the GPU
Tests: f16 transcendentals on the GPU against f64 references
Recompiler: round f16 add / sub to nearest-even by default (xcl-dev#685)
Recompiler: f16 results round to nearest even in the default mode (xc…
malzaharhub-ctrl commentedon Nov 24, 2026
I've been working on the default round-to-nearest-even F16 FMA double-rounding issue described in this discussion.
I now have a local implementation covering:
v_fma_f16v_fmac_f16v_fmamk_f16v_fmaak_f16v_pk_fma_f16v_pk_fmac_f16The implementation uses TwoSum error recovery and sticky-aware final binary16 rounding.
Testing performed on an AMD Radeon RX 7900 XTX:
scripts/test.ps1completed successfully (exit code 0).I have noticed the ongoing work in PR #128 and PR #842. My implementation addresses the default-RNE FMA single-rounding gap, rather than replacing the directed-rounding work.
Would you prefer this to be developed as an extension of PR #128, or submitted as a separate PR coordinated with the existing changes?
I haven't committed or pushed the local implementation yet.
AI assistance disclosure: AI assisted with implementation development, test design and test scripts. I applied the changes locally and ran the reported tests on the hardware stated above.