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

Closed
Anon64 wants to merge 1 commit from upstream-pr/696 into main
Anon64 commented 2026-11-24 03:37:54 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Anon64 on 2026-11-24T03:37:54Z.


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
    • the failure is hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory, which fails the same way on main on this machine (#650 / #656)
  • 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
<!-- greyfinch-sync:ivall/xcl:issue:6574053738 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/696). Originally posted by [Anon64](https://github.com/Anon64) on 2026-11-24T03:37:54Z. --- 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 - the failure is `hle::kernelx::tests::xmem_virtual_query_marks_cpu_readable_memory`, which fails the same way on `main` on this machine (#650 / #656) - 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](https://github.com/ivall/xcl/pull/696#issuecomment-7121668599)): 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
joeypercia commented 2026-11-24 07:06:05 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by joeypercia on 2026-11-24T04:03:35Z.


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 (#288 / #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).

<!-- greyfinch-sync:ivall/xcl:comment:7121668599 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/696#issuecomment-7121668599). Originally posted by [joeypercia](https://github.com/joeypercia) on 2026-11-24T04:03:35Z. --- **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 (#288 / #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 commented 2026-11-24 07:06:50 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-24T06:40:03Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7124168215 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/696#issuecomment-7124168215). Originally posted by [SlopTart](https://github.com/SlopTart) on 2026-11-24T06:40:03Z. --- 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 commented 2026-11-24 07:06:53 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Anon64 on 2026-11-24T06:51:31Z.


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).

<!-- greyfinch-sync:ivall/xcl:comment:7124362720 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/696#issuecomment-7124362720). Originally posted by [Anon64](https://github.com/Anon64) on 2026-11-24T06:51:31Z. --- 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).
JJAutoCompare commented 2026-11-25 07:05:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by JJAutoCompare on 2026-11-25T06:10:31Z.


Empirical test report (910333ed)

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.

<!-- greyfinch-sync:ivall/xcl:comment:7154518750 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/696#issuecomment-7154518750). Originally posted by [JJAutoCompare](https://github.com/JJAutoCompare) on 2026-11-25T06:10:31Z. --- ### Empirical test report (910333ed) **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`: ```powershell & $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.
DavidM1911 commented 2026-11-25 12:06:30 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T11:54:30Z.


Verification on RTX 3060 Ti (Windows 11), branch merged with current main:

  • cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)

Thanks!

<!-- greyfinch-sync:ivall/xcl:comment:7161664084 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/696#issuecomment-7161664084). Originally posted by [DavidM1911](https://github.com/DavidM1911) on 2026-11-25T11:54:30Z. --- Verification on RTX 3060 Ti (Windows 11), branch merged with current `main`: - `cargo test -p xcl --lib`: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.) Thanks!
upstream-sync closed this pull request 2026-11-25 23:06:53 +00:00
CalvyPZ commented 2026-11-25 23:09:11 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by CalvyPZ on 2026-11-25T22:34:57Z.


This PR has been marked as a duplicate and closed after multiple agreeing checks in #1607. If you believe this is incorrect please re-open with why.

<!-- greyfinch-sync:ivall/xcl:comment:7176540990 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/696#issuecomment-7176540990). Originally posted by [CalvyPZ](https://github.com/CalvyPZ) on 2026-11-25T22:34:57Z. --- This PR has been marked as a duplicate and closed after multiple agreeing checks in #1607. If you believe this is incorrect please re-open with why.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
xcl-dev/xcl!691
No description provided.