Recompiler: round f16 conversions and products to nearest-even by default #166

Manually merged
github merged 1 commit from gh-pr/166/head into gh-pr/166/base 2026-11-26 04:13:19 +00:00
Owner

@⁠ebarr24 on GitHub · 2026-11-23 18:23 UTC · original
ebarr24:fix/recomp-default-f16-rounding → main · +65 −5 · 2 files · 1 commits · merged 2026-11-23 20:51 UTC

The default RDNA binary16 rounding mode is round-to-nearest-even. XCL has used legacy HLSL f32tof16 for that path, which narrows toward zero. On the RTX 3080, converting 3075.0 gives 0x6A01 instead of 0x6A02, and converting 65520.0 gives 0x7BFF instead of infinity. Direct3D conversion rules, sections 3.2.2 and 22.13.2

Use the existing f32_to_f16_rm helper for v_cvt_f16_f32, v_mul_f16, and v_pk_mul_f16, with a constant nearest-even mode when the shader leaves the default in place. The finite product of two binary16 inputs fits exactly in binary32, so one narrowing is sufficient. The regression covers midpoint and adjacent values, negative input, overflow, a subnormal and scalar and packed multiplication with midpoint products.

Issue #111 tracks the exact product and sum needed for a single-rounding FMA. Rounding issue #111 · CPU reproduction

Verification

Windows 11 25H2 (26200.9711), Rust 1.100.0 x86_64-pc-windows-gnu, RTX 3080 (driver 616.92), DXC 1.9.2609.5.

  • The conversion and scalar product regression reports six wrong outputs when added to db5dc26.
  • On deccf2b the expanded conversion, scalar product and packed product regression passes. The full workspace run on 10f25f4, with #157, #158 and #160 applied alongside this patch, reported 571 passes, zero failures and 39 ignored. Two native-wave64 tests and the unconfigured retail-sample tests returned early.
  • Git confirmed a clean merge against current main at 956a476.

AI-assisted patch preparation and verification.

**@⁠ebarr24** on GitHub · 2026-11-23 18:23 UTC · [original](https://github.com/xcl-dev/xcl/pull/166) `ebarr24:fix/recomp-default-f16-rounding` → `main` · +65 −5 · 2 files · 1 commits · merged 2026-11-23 20:51 UTC The default RDNA binary16 rounding mode is round-to-nearest-even. XCL has used legacy HLSL `f32tof16` for that path, which narrows toward zero. On the RTX 3080, converting `3075.0` gives `0x6A01` instead of `0x6A02`, and converting `65520.0` gives `0x7BFF` instead of infinity. [Direct3D conversion rules, sections 3.2.2 and 22.13.2](https://microsoft.github.io/DirectX-Specs/d3d/archive/D3D11_3_FunctionalSpec.htm) Use the existing `f32_to_f16_rm` helper for `v_cvt_f16_f32`, `v_mul_f16`, and `v_pk_mul_f16`, with a constant nearest-even mode when the shader leaves the default in place. The finite product of two binary16 inputs fits exactly in binary32, so one narrowing is sufficient. The regression covers midpoint and adjacent values, negative input, overflow, a subnormal and scalar and packed multiplication with midpoint products. Issue #111 tracks the exact product and sum needed for a single-rounding FMA. [Rounding issue #111](https://github.com/ivall/xcl/issues/111) · [CPU reproduction](https://github.com/ivall/xcl/pull/128#issuecomment-7107953004) ## Verification Windows 11 25H2 (26200.9711), Rust 1.100.0 `x86_64-pc-windows-gnu`, RTX 3080 (driver 616.92), DXC 1.9.2609.5. - The conversion and scalar product regression reports six wrong outputs when added to `db5dc26`. - On `deccf2b` the expanded conversion, scalar product and packed product regression passes. The full workspace run on `10f25f4`, with #157, #158 and #160 applied alongside this patch, reported 571 passes, zero failures and 39 ignored. Two native-wave64 tests and the unconfigured retail-sample tests returned early. - Git confirmed a clean merge against current `main` at `956a476`. AI-assisted patch preparation and verification.
Use the existing rounding helper for v_cvt_f16_f32 and scalar and packed half multiplication. A finite product of two half operands fits in binary32, so the final binary16 conversion rounds the exact product.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:13:18 +00:00
github manually merged commit deccf2ba3c into gh-pr/166/base 2026-11-26 04:13:19 +00:00
Author
Owner

@⁠WarmMilkCodes on GitHub · 2026-11-23 18:55 UTC · original

Tested #166 deccf2b merged onto main 956a476 on AMD Radeon RX 6600 XT (RDNA2), driver 32.0.21045.29003, Windows 11 23H2 (22631.6199): pass

cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_* data:

main this
passed 568 569
failed 1 1
GPU tests skipped 0 0

No new failures vs main. The PR's new test passes on RDNA2. The one failure on both, gpu_wave64_ps_whole_wave_reductions, is the 32-lane PS case on this card (#155).

**@⁠WarmMilkCodes** on GitHub · 2026-11-23 18:55 UTC · [original](https://github.com/xcl-dev/xcl/pull/166#issuecomment-7110849126) Tested #166 `deccf2b` merged onto main `956a476` on AMD Radeon RX 6600 XT (RDNA2), driver 32.0.21045.29003, Windows 11 23H2 (22631.6199): pass `cargo test --workspace --no-fail-fast -- --test-threads=4`, no `XCL_SAMPLE_*` data: | | main | this | |---|---|---| | passed | 568 | 569 | | failed | 1 | 1 | | GPU tests skipped | 0 | 0 | No new failures vs main. The PR's new test passes on RDNA2. The one failure on both, `gpu_wave64_ps_whole_wave_reductions`, is the 32-lane PS case on this card (#155).
Author
Owner

@⁠TheHoracio on GitHub · 2026-11-23 19:28 UTC · original

Tested on AMD RDNA3: Radeon RX 7800 XT (16 GB, wave 32–64), driver 32.0.32015.7029, Windows 11 Pro 10.0.26300, Rust 1.100.0 x86_64-pc-windows-msvc, DXC 1.9.2602.17. The PR head deccf2b was tested as is, with no other PRs applied.

cargo test --workspace --no-fail-fast: 568 passed, 1 failed, 39 ignored, 0 SKIPPED.

  • gpu_f16_defaults_to_round_to_nearest_even passes on AMD as well. The existing f16 rounding tests also pass: gpu_s_round_mode_controls_f16_rounding_across_modes, gpu_mad_nf_f16_rounds_intermediate_in_binary16, gpu_cvt_norm_f16_exact_rounding_and_edge_cases, gpu_cmp_class_f16_preserves_snan.
  • The one failure is gpu_wave64_ps_whole_wave_reductions. It fails the same way on main 956a476 on this GPU and is fixed by #155 (see my comment there). It is unrelated to this PR.
**@⁠TheHoracio** on GitHub · 2026-11-23 19:28 UTC · [original](https://github.com/xcl-dev/xcl/pull/166#issuecomment-7111649101) Tested on **AMD RDNA3: Radeon RX 7800 XT** (16 GB, wave 32–64), driver 32.0.32015.7029, Windows 11 Pro 10.0.26300, Rust 1.100.0 `x86_64-pc-windows-msvc`, DXC 1.9.2602.17. The PR head `deccf2b` was tested as is, with no other PRs applied. `cargo test --workspace --no-fail-fast`: **568 passed, 1 failed, 39 ignored**, 0 `SKIPPED`. - `gpu_f16_defaults_to_round_to_nearest_even` passes on AMD as well. The existing f16 rounding tests also pass: `gpu_s_round_mode_controls_f16_rounding_across_modes`, `gpu_mad_nf_f16_rounds_intermediate_in_binary16`, `gpu_cvt_norm_f16_exact_rounding_and_edge_cases`, `gpu_cmp_class_f16_preserves_snan`. - The one failure is `gpu_wave64_ps_whole_wave_reductions`. It fails the same way on `main` 956a476 on this GPU and is fixed by #155 (see my comment there). It is unrelated to this PR.
Author
Owner

@⁠Kalmarv on GitHub · 2026-11-23 20:34 UTC · original

Additional default-mode check on the RTX 5070 Ti (driver 610.74, Windows x64 MSVC, Rust 1.99.0): I tested PR head deccf2ba3c0f09023bc9ccf2f49cb5af8aae798f directly, and carried its test plus a 14-lane synthetic conversion probe to main 956a476bf738a098f39df4f4c8acef682e43030f.

Binary32 input to V_CVT_F16_F32 main, binary16 bits PR head
387fe000 (midpoint between the largest subnormal and smallest normal) 03ff 0400
3f801000, 3f803000 (successive midpoints) 3c00, 3c01 3c00, 3c02
33000000, 33000001, b3000001 0000, 0000, 8000 0000, 0001, 8001
Both signs of zero and infinity Signs preserved Signs preserved
Quiet and signaling NaN inputs Both quiet NaNs Both quiet NaNs

The tie results agree with half-precision round mode 0. The NaN assertion checked the quiet-NaN class only. AMD RDNA 2 ISA, §6.4 and V_CVT_F16_F32

Of seven recompiler tests matching f16, five passed on main and all seven passed on the head. The failures were the verbatim PR regression (including scalar and packed products) and the added conversion probe. Each dispatched synthetic shaders. No retail shaders, merge build, or explicit denorm-mode sweep. AI-assisted review.

**@⁠Kalmarv** on GitHub · 2026-11-23 20:34 UTC · [original](https://github.com/xcl-dev/xcl/pull/166#issuecomment-7113252979) Additional default-mode check on the RTX 5070 Ti (driver 610.74, Windows x64 MSVC, Rust 1.99.0): I tested PR head `deccf2ba3c0f09023bc9ccf2f49cb5af8aae798f` directly, and carried its test plus a 14-lane synthetic conversion probe to main `956a476bf738a098f39df4f4c8acef682e43030f`. | Binary32 input to V_CVT_F16_F32 | main, binary16 bits | PR head | |---|---:|---:| | `387fe000` (midpoint between the largest subnormal and smallest normal) | `03ff` | `0400` | | `3f801000`, `3f803000` (successive midpoints) | `3c00`, `3c01` | `3c00`, `3c02` | | `33000000`, `33000001`, `b3000001` | `0000`, `0000`, `8000` | `0000`, `0001`, `8001` | | Both signs of zero and infinity | Signs preserved | Signs preserved | | Quiet and signaling NaN inputs | Both quiet NaNs | Both quiet NaNs | The tie results agree with half-precision round mode 0. The NaN assertion checked the quiet-NaN class only. [AMD RDNA 2 ISA, §6.4 and V_CVT_F16_F32](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) Of seven recompiler tests matching `f16`, five passed on main and all seven passed on the head. The failures were the verbatim PR regression (including scalar and packed products) and the added conversion probe. Each dispatched synthetic shaders. No retail shaders, merge build, or explicit denorm-mode sweep. AI-assisted review.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 20:52 UTC · original

Merged (main 2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main 38b05b7: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.

**@⁠ivall** on GitHub · 2026-11-23 20:52 UTC · [original](https://github.com/xcl-dev/xcl/pull/166#issuecomment-7113683492) Merged (main 2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main 38b05b7: `cargo build --workspace && cargo test --workspace -- --test-threads=4` - 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.
This discussion has been locked. Commenting is limited to contributors.
No description provided.