Skip to content

Recompiler: round f16 add / sub to nearest-even by default (#685) - #696

Open
Anon64 wants to merge 1 commit into
xcl-dev:mainfrom
Anon64:recomp/f16-add-rne
Open

Anon64 wants to merge 1 commit into
xcl-dev:mainfrom
Anon64:recomp/f16-add-rne

Conversation

@Anon64

@Anon64 Anon64 commented Nov 24, 2026 •

Copy link
Copy Markdown

In the default mode the f16 add, sub, subrev and packed add results were narrowed with HLSL f32tof16, which rounds toward zero: a sum between two binary16 values came out one ulp toward zero, and an overflow stopped at ±65504 instead of ±inf. deccf2b moved the f16 multiplies to f32_to_f16_rm(…, 0u); this does the same for the sums. The binary32 sum of two binary16 values rounded once more to binary16 is the correctly rounded binary16 sum, because 24 ≥ 2·11 + 2 (Figueroa, 1995). FMA and the directed modes (#111 / #128) are unchanged; under a dynamic mode the code path is the same as before.

Fixes #685

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: v_add_f16, v_sub_f16, v_subrev_f16 and v_pk_add_f16 take the rounding helper the multiplies already use
  • crates/xcl/src/gpu/recomp/tests.rs: gpu_f16_add_sub_round_to_nearest_even: 5 cases for each of the four instructions (rounding up, a tie to even, a negative sum, overflow to +inf, and 1 - 2^-13, which rounding toward zero got wrong)

Tested

  • NVIDIA GeForce RTX 3060 (driver 581.80, wave32), Intel Core i9-14900KF, Windows 11 25H2 26200.9974, Rust 1.90.0, debug
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on 910333e (run on the same tree as an earlier hash of this commit, which differs only in author): 692 passed, 1 failed, 41 ignored
  • without the emit.rs change the new test fails: wave32 lane 0: a 0x3c00, b 0x1200, add 0x3c00 instead of 0x3c01
  • cargo test -p xcl --lib f16: 13 passed (the existing f16 tests are unchanged)
  • SKIPPED on this adapter: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64)
  • AMD, run by @joeypercia on 910333e merged onto main f4ce27c (comment): the new test passes on an AMD RDNA2 iGPU (driver 26.11.2, wave64 and wave32) and on an RTX 4070 Ti; without the emit.rs change it fails on the RDNA2 iGPU at the same first case, so AMD's f32tof16 truncates too

Regression run

main:

xcl-smoke v1
commit: 91f9a5e9f03156ea82dc55a18f7704862da7a5db
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1648)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14684 MB

this PR:

xcl-smoke v1
commit: 910333edb0e1e9e48d2cb484237d1b503e6c2099
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1649)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14679 MB

Not tested

  • RDNA3 / RDNA4 and the regression run on AMD (the AMD run above is the unit test and the suite on an RDNA2 iGPU)
  • whether the retail title's shaders depend on these f16 results; the run is a regression check only
  • cost: the helper is more ALU than f32tof16 in programs with many f16 sums; not measured

AI use

  • Claude Code (Claude Opus 5.5): found the bug, wrote the fix and the test, ran the tests and smoke runs, and wrote this description

In the default mode the f16 add, sub, subrev and packed add results were
narrowed with HLSL f32tof16, which rounds toward zero: a sum between two
binary16 values came out one ulp toward zero, and an overflow stopped at
±65504 instead of ±inf. deccf2b moved the f16 multiplies to f32_to_f16_rm;
do the same for the sums. The binary32 sum of two binary16 values rounded
once more to binary16 is the correctly rounded binary16 sum, since
24 >= 2·11 + 2 (Figueroa). FMA and the directed modes (xcl-dev#111) are unchanged.

Fixes xcl-dev#685

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@joeypercia

Copy link
Copy Markdown

AMD run, the Not tested item: 910333e merged onto main f4ce27c (clean merge).

Tested

  • AMD Radeon iGPU (Ryzen 7 7800X3D, RDNA2, native wave64), driver 26.11.2 (32.0.21045.29003); RTX 4070 Ti (616.56, wave32); Windows 11 Pro 26200; Rust 1.100.0 MSVC; DXC 1.8.2502.11; debug
  • gpu_f16_add_sub_round_to_nearest_even: passes on the RDNA2 iGPU (wave64 and wave32) and on the RTX 4070 Ti
  • AMD's f32tof16 truncates too: with only emit.rs back to main, the test fails on the RDNA2 iGPU at wave64 lane 0: a 0x3c00, b 0x1200 (add, sub, subrev, pk_add), the same first case as on the RTX 3060
  • cargo test --workspace --no-fail-fast -- --test-threads=4 on the RDNA2 iGPU: 699 passed, 3 failed, 41 ignored; the 3 are placed_title_textures_share_vram_and_preserve_pixels and its two lock-poison followers (D3D12.X: back kept-in-step title textures by shared VRAM pages (#248) #288 / Tests: check cubemap tile reuse independently of idle page reclamation #447), under load, and pass alone on this merge (2 runs of 2). SKIPPED: gpu_geometry_mode_es_gs_strip, gpu_mesh_mode_two_passes (no mesh shaders)

For a follow-up (out of this PR's scope, which is right): the unary f16 results go through the same f32tof16 in the default mode, and on both GPUs every inexact one comes out toward zero: v_sqrt_f16, v_rcp_f16, v_rsq_f16, v_exp_f16, v_log_f16, v_ldexp_f16 (counts in #111). Switching them needs v_fract_f16's clamp kept below 1.0 after the narrowing (#655).

Run and report by Claude Code (Claude Opus 5.5).

@SlopTart

Copy link
Copy Markdown

2|
3|RDNA3 + AMD regression datapoint — the axis your row lists as not tested (yours was an RDNA2 iGPU).
4|
5|Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Wine = Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust 1.100.0 cross x86_64-pc-windows-gnu, release profile, XCL_NETWORK=0, native wave64 (no split). Same rig as my Linux rows in #42 / #718.
6|
7|Merged state: head 910333edb onto main beaa245, clean merge.
8|
9|Full suite, cargo test --workspace --release --no-fail-fast -- --test-threads=4: 707 passed, 7 failed, 41 ignored (17 suites) — the 7 failures are exactly this rig's known Wine-lane set (my #42 row), nothing new. Reference run on plain main beaa245 this same day: 702 / 11 / 41 (same 7 plus the #718 trio firing and, for the first time I have seen, a lone gpu::recomp::tests::gpu_geometry_wave32_prefix_count_over_a_mask failure — flagging that as an observation, it has not repeated). Your f16 tests pass on RDNA3: cvt_norm_f16_instructions, gpu_cmp_class_f16_preserves_snan, gpu_cvt_norm_f16_exact_rounding_and_edge_cases and the rest, ok.
10|
11|Not tested: retail titles (none here), RDNA4, Windows.
12|Counts only, no game data.
13|AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.

@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown
Author

Superseded by #842 (@joeypercia): it sends every default-mode f16 result through the same helper, add / sub included, with the v_fract_f16 clamp. Its regression runs are posted there. I will close this PR once #842 is merged; if #842 is not taken, this one still stands on its own.

Thanks @SlopTart for the RDNA3 data point above.

Written with Claude Code (Claude Opus 5.5).

@Anon64 Anon64 mentioned this pull request Nov 24, 2026
CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
…l-dev#111)

In the default mode every f16 result that is narrowed from binary32 went
through HLSL f32tof16, which rounds toward zero: on an RTX 4070 Ti and an
RDNA2 iGPU every inexact v_add/sub/sqrt/rcp/rsq/exp/log/ldexp_f16 result
came out one binary16 step low, and an overflow stopped at 65504 instead
of inf. Native RDNA2 rounds to nearest even (luckyteagirl's measurements on
xcl-dev#672). to_f16_expr now narrows with f32_to_f16_rm(e, 0u) in the default
mode, the helper the multiplies already use (xcl-dev#166); under a dynamic mode
nothing changes.

v_fract_f16 clamps its binary32 result below 1.0, and 1 - 2^-24 narrows to
1.0 by nearest even, so its binary16 result is kept at 0x3BFF (xcl-dev#655).

Supersedes xcl-dev#696, whose add / sub cases this covers.

Assisted-by: Claude Code (Claude Opus 5.5)
@JJAutoCompare

Copy link
Copy Markdown

Empirical test report (910333e)

Hardware / environment

  • Intel Arc A770 selected with XCL_ADAPTER=A770, on an AMD CPU (family 23, model 113) and Windows x64 build 26200. The GPU driver version was not recorded.
  • rustc 1.100.0 (0ef1bc386 2026-11-09), DXC 1.9.2609.5 (01b62ad47), and an unoptimized test profile with debug info and incremental compilation disabled.

Commands and results

  • cargo test -p xcl --lib --no-run completed with exit code 0 outside the Arc test lock.
  • With RUST_TEST_NOCAPTURE=1, ran the library test executable produced by Cargo under Invoke-ArcTest.ps1:
& $testExe gpu::recomp::tests::gpu_f16_add_sub_round_to_nearest_even --exact --nocapture --test-threads=1
  • Focused result was 1 passed, 0 failed, 0 ignored, 0 skipped, and 673 filtered out. The fixture completed in 0.18 s. The captured output contained no SKIPPED lines.
  • Exact revision tested was 910333edb0e1e9e48d2cb484237d1b503e6c2099 in a clean worktree.

Observed behavior

The recompiled D3D12 compute fixture matched the expected binary16 bits for five operand pairs across the active lanes. The assertions covered scalar add, subtract, and reverse subtract, plus both halves of packed add. The cases exercised nearest rounding, halfway ties to even, negative operands, and positive overflow to infinity.

Not tested

The full GPU workspace suite and game smoke runs were omitted. This focused run used one adapter and did not include a comparison run on the parent commit.

AI use

Verified with T3 Code / Codex (gpt-6.2-sol, reasoningEffort: high). Coordinator #2 (T3 Code / Antigravity, gemini-4-neon) verified counts against pr696-focused.log and published.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Recompiler: f16 add / sub round toward zero in the default mode (f32tof16), not to nearest even

4 participants