gpu/recomp: exact RNE rounding and NaN handling for V_CVT_(PK)NORM_*_F16 #80

Manually merged
github merged 1 commit from gh-pr/80/head into gh-pr/80/base 2026-11-26 04:08:36 +00:00
Owner

@⁠certified-retart on GitHub · 2026-11-23 01:45 UTC · original
certified-retart:fix/recomp-cvt-norm-f16 → main · +126 −19 · 2 files · 1 commits · merged 2026-11-23 10:36 UTC

Summary

Fixes two accuracy bugs in V_CVT_NORM_U16_F16, V_CVT_NORM_I16_F16, V_CVT_PKNORM_U16_F16, and V_CVT_PKNORM_I16_F16 in the RDNA2 → HLSL shader recompiler:

  1. Binary32 double-rounding: Previously, all four instructions promoted f16 via f16tof32(x), multiplied by 65535.0f / 32767.0f in fp32, and called round(...). Near $2^{14}$–2^{15}, fp32 ULP exceeds 2^{-10}, causing exact products just below .5 to round up to .5 and then tie-to-even to the wrong integer. For example, 0x3801 (\frac{1025}{2048}):
    • \frac{1025}{2048} \times 65535 = 32799.49951171875 \rightarrow 32799 (0x801F), previously 32800 (0x8020)
    • \frac{1025}{2048} \times 32767 = 16399.49951171875 \rightarrow 16399 (0x400F), previously 16400 (0x4010)
  2. NaN clamping: HLSL clamp(x, -1.0f, 1.0f) lowers to min/max in DXIL, suppressing NaN to ±1.0f (±32767) instead of returning 0.

Changes

  • crates/xcl/src/gpu/recomp/emit.rs:
    • Added pure 32-bit uint HLSL helpers cvt_norm_f16(uint h, uint scale), cvt_norm_u16_f16(uint h), and cvt_norm_i16_f16(uint h) operating directly on raw IEEE-754 binary16 bit patterns (no fp64 dependency; exact round-to-nearest-even across all 65,536 binary16 inputs, NaN → 0, ±Inf / |x| ≥ 1 → ±scale, exact subnormal handling).
    • Switched v_cvt_norm_u16_f16, v_cvt_norm_i16_f16, v_cvt_pknorm_u16_f16, and v_cvt_pknorm_i16_f16 to apply VOP3 abs/neg modifiers directly on bit 15 before calling the helpers.
  • crates/xcl/src/gpu/recomp/tests.rs:
    • Updated cvt_norm_f16_instructions for the new helper emission.
    • Added gpu_cvt_norm_f16_exact_rounding_and_edge_cases testing all four instructions across 0x3801/0xB801, ±0.5, +qNaN/-sNaN, ±Inf, ±0.0, subnormals (0x0080, 0x0081, 0x0101), 0x3BFF, ±1.0, and VOP3 ABS, NEG, and combined ABS+NEG modifier order.

Verification

  • Exhaustive reference check across all 65,536 binary16 bit patterns: 0 mismatches for both unsigned and signed wrappers.
  • Live wave64 GPU verification on Windows 10 Pro + AMD Radeon(TM) Graphics (Ryzen 7 9800X3D RDNA2 iGPU, XCL_ADAPTER=1, XCL_REQUIRE_GPU=wave64, D3D12 Agility SDK 1.620.2):
    • cargo test -p xcl --lib cvt_norm_f16 -- --test-threads=1 — passed (2 passed; 0 failed).
    • cargo test -p xcl --lib gpu_more_atomics_sub_dword_scratch_valu -- --test-threads=1 — passed (1 passed; 0 failed).
**@⁠certified-retart** on GitHub · 2026-11-23 01:45 UTC · [original](https://github.com/xcl-dev/xcl/pull/80) `certified-retart:fix/recomp-cvt-norm-f16` → `main` · +126 −19 · 2 files · 1 commits · merged 2026-11-23 10:36 UTC ## Summary Fixes two accuracy bugs in `V_CVT_NORM_U16_F16`, `V_CVT_NORM_I16_F16`, `V_CVT_PKNORM_U16_F16`, and `V_CVT_PKNORM_I16_F16` in the RDNA2 → HLSL shader recompiler: 1. **Binary32 double-rounding**: Previously, all four instructions promoted `f16` via `f16tof32(x)`, multiplied by `65535.0f` / `32767.0f` in `fp32`, and called `round(...)`. Near $2^{14}$–$2^{15}$, `fp32` ULP exceeds $2^{-10}$, causing exact products just below `.5` to round up to `.5` and then tie-to-even to the wrong integer. For example, `0x3801` ($\frac{1025}{2048}$): - $\frac{1025}{2048} \times 65535 = 32799.49951171875 \rightarrow 32799$ (`0x801F`), previously `32800` (`0x8020`) - $\frac{1025}{2048} \times 32767 = 16399.49951171875 \rightarrow 16399$ (`0x400F`), previously `16400` (`0x4010`) 2. **NaN clamping**: HLSL `clamp(x, -1.0f, 1.0f)` lowers to `min`/`max` in DXIL, suppressing NaN to `±1.0f` (`±32767`) instead of returning `0`. ### Changes - **`crates/xcl/src/gpu/recomp/emit.rs`**: - Added pure 32-bit `uint` HLSL helpers `cvt_norm_f16(uint h, uint scale)`, `cvt_norm_u16_f16(uint h)`, and `cvt_norm_i16_f16(uint h)` operating directly on raw IEEE-754 binary16 bit patterns (no `fp64` dependency; exact round-to-nearest-even across all 65,536 binary16 inputs, `NaN → 0`, `±Inf / |x| ≥ 1 → ±scale`, exact subnormal handling). - Switched `v_cvt_norm_u16_f16`, `v_cvt_norm_i16_f16`, `v_cvt_pknorm_u16_f16`, and `v_cvt_pknorm_i16_f16` to apply VOP3 `abs`/`neg` modifiers directly on bit 15 before calling the helpers. - **`crates/xcl/src/gpu/recomp/tests.rs`**: - Updated `cvt_norm_f16_instructions` for the new helper emission. - Added `gpu_cvt_norm_f16_exact_rounding_and_edge_cases` testing all four instructions across `0x3801`/`0xB801`, `±0.5`, `+qNaN`/`-sNaN`, `±Inf`, `±0.0`, subnormals (`0x0080`, `0x0081`, `0x0101`), `0x3BFF`, `±1.0`, and VOP3 `ABS`, `NEG`, and combined `ABS+NEG` modifier order. ### Verification - Exhaustive reference check across all 65,536 binary16 bit patterns: 0 mismatches for both unsigned and signed wrappers. - Live wave64 GPU verification on Windows 10 Pro + AMD Radeon(TM) Graphics (Ryzen 7 9800X3D RDNA2 iGPU, `XCL_ADAPTER=1`, `XCL_REQUIRE_GPU=wave64`, D3D12 Agility SDK 1.620.2): - `cargo test -p xcl --lib cvt_norm_f16 -- --test-threads=1` — passed (`2 passed; 0 failed`). - `cargo test -p xcl --lib gpu_more_atomics_sub_dword_scratch_valu -- --test-threads=1` — passed (`1 passed; 0 failed`).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:08:34 +00:00
github manually merged commit 527fe982fd into gh-pr/80/base 2026-11-26 04:08:36 +00:00
Author
Owner

@⁠Lum7nt on GitHub · 2026-11-23 05:57 UTC · original

Tested locally on Windows — both cvt_norm_f16_instructions and gpu_cvt_norm_f16_exact_rounding_and_edge_cases pass cleanly.

Key improvements verified:

  1. Exact integer arithmetic: Operating directly on IEEE-754 binary16 bits via 32-bit integer arithmetic avoids fp32 double-rounding near $2^{14}$–2^{15}, guaranteeing exact round-to-nearest-even conversions across all edge cases (such as 0x3801 rounding to 0x801F/0x400F instead of 0x8020/0x4010).
  2. IEEE-754 compliance: Proper handling of subnormals, infinities, and exact NaN -> 0 mapping without relying on clamp/min/max suppression.
  3. Bit-level modifier handling: Applying VOP3 abs/neg directly to bit 15 before conversion is clean and preserves sign semantics.

Great precision fix for shader math.

**@⁠Lum7nt** on GitHub · 2026-11-23 05:57 UTC · [original](https://github.com/xcl-dev/xcl/pull/80#issuecomment-7092886171) Tested locally on Windows — both `cvt_norm_f16_instructions` and `gpu_cvt_norm_f16_exact_rounding_and_edge_cases` pass cleanly. Key improvements verified: 1. **Exact integer arithmetic**: Operating directly on IEEE-754 binary16 bits via 32-bit integer arithmetic avoids fp32 double-rounding near $2^{14}$–$2^{15}$, guaranteeing exact round-to-nearest-even conversions across all edge cases (such as `0x3801` rounding to `0x801F`/`0x400F` instead of `0x8020`/`0x4010`). 2. **IEEE-754 compliance**: Proper handling of subnormals, infinities, and exact `NaN -> 0` mapping without relying on clamp/min/max suppression. 3. **Bit-level modifier handling**: Applying VOP3 `abs`/`neg` directly to bit 15 before conversion is clean and preserves sign semantics. Great precision fix for shader math.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:36 UTC · original

Merged. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord, Minecraft and Fortnite run with this round merged. Thanks!

**@⁠ivall** on GitHub · 2026-11-23 10:36 UTC · [original](https://github.com/xcl-dev/xcl/pull/80#issuecomment-7098217043) Merged. Tested on Windows 11 with an RX 9070 (RDNA4): `cargo test --workspace` green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with `XCL_SPLIT_WAVE64=1`; Unicorn Overlord, Minecraft and Fortnite run with this round merged. Thanks!
This discussion has been locked. Commenting is limited to contributors.
No description provided.