Shaders: v_cvt_u16_f16 and v_cvt_i16_f16 saturate at 16 bits #384

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

@⁠LouisRiverstone on GitHub · 2026-11-24 02:49 UTC · original
LouisRiverstone:shader-cvt16-saturate → main · +53 −6 · 2 files · 1 commits · merged 2026-11-24 08:10 UTC

Removes the shortcut: on v_cvt_u16_f16 / v_cvt_i16_f16 in lower.rs ("converted through 32 bits, so out-of-range values wrap instead of saturating at 16 bits").

What was wrong. An f16 can reach ±65504, past the i16 range. v_cvt_i16_f16 of 32768 gave 0x8000 (-32768) instead of 32767. Infinities and NaN went through FpToSi / FpToUi, which leave them undefined.

What it does now. The source is clamped in f32, where every f16 is exact, to [0, 65535] or [-32768, 32767], then converted toward zero. NaN gives 0. That is the ISA's rule for its float-to-integer conversions: out-of-range values, infinity included, saturate, and NaN converts to 0.

Test: v_cvt_u16_and_i16_f16_saturate in valu_tests.rs. It runs both ops on the host GPU and WARP with 16 inputs:

  • ±0, ±1.5 (rounding toward zero), a plain value;
  • the edges of the i16 range (32752, ±32768, -32800);
  • ±65504, ±inf, ±NaN and a subnormal.

Results:

  • Windows 11, RTX 4080 and WARP: passes. DXIL validation with dxil.dll 10.0.26100 passes, and so do the rest of the victory-shader tests (70).
  • With main's lower.rs it fails: hardware: v_cvt_i16_f16(0x7800) = 0x00008000, want 0x7fff.
  • Clippy is clean.

No game files.

AI-assisted (Claude Code): I reviewed the change and ran the tests above.

**@⁠LouisRiverstone** on GitHub · 2026-11-24 02:49 UTC · [original](https://github.com/Marvisak/VIctory/pull/384) `LouisRiverstone:shader-cvt16-saturate` → `main` · +53 −6 · 2 files · 1 commits · merged 2026-11-24 08:10 UTC Removes the `shortcut:` on `v_cvt_u16_f16` / `v_cvt_i16_f16` in `lower.rs` ("converted through 32 bits, so out-of-range values wrap instead of saturating at 16 bits"). **What was wrong.** An f16 can reach ±65504, past the i16 range. `v_cvt_i16_f16` of 32768 gave `0x8000` (-32768) instead of 32767. Infinities and NaN went through `FpToSi` / `FpToUi`, which leave them undefined. **What it does now.** The source is clamped in f32, where every f16 is exact, to [0, 65535] or [-32768, 32767], then converted toward zero. NaN gives 0. That is the ISA's rule for its float-to-integer conversions: out-of-range values, infinity included, saturate, and NaN converts to 0. Test: `v_cvt_u16_and_i16_f16_saturate` in `valu_tests.rs`. It runs both ops on the host GPU and WARP with 16 inputs: - ±0, ±1.5 (rounding toward zero), a plain value; - the edges of the i16 range (32752, ±32768, -32800); - ±65504, ±inf, ±NaN and a subnormal. Results: - Windows 11, RTX 4080 and WARP: passes. DXIL validation with `dxil.dll` 10.0.26100 passes, and so do the rest of the `victory-shader` tests (70). - With main's `lower.rs` it fails: `hardware: v_cvt_i16_f16(0x7800) = 0x00008000, want 0x7fff`. - Clippy is clean. No game files. AI-assisted (Claude Code): I reviewed the change and ran the tests above.
They went through a 32-bit conversion, so an f16 past the i16 range
(32768 up to 65504) wrapped, and infinities and NaN were undefined. The
value is now clamped in f32 to the 16-bit range first (every f16 is exact
there) and NaN gives 0, as the hardware's conversions do.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:02:01 +00:00
github manually merged commit 2af66ff1d8 into gh-pr/384/base 2026-11-26 04:02:02 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:22 UTC · original

Thanks @⁠LouisRiverstone for fixing the 16-bit saturation behavior. I tested head 2af66ff1d8dbd7ccd7d87488a57e2caf37d065a1 with:

cargo test --locked -p victory-shader --lib v_cvt_u16_and_i16_f16_saturate -- --test-threads=1

Result: 1 passed, 0 failed (69 filtered out). The test exercises signed and unsigned conversion with boundary values, infinities, NaNs, zeros, a subnormal, and truncation cases. These are synthetic conversion checks; this run does not measure console hardware behavior.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/384#issuecomment-7121961887) Thanks @⁠LouisRiverstone for fixing the 16-bit saturation behavior. I tested head `2af66ff1d8dbd7ccd7d87488a57e2caf37d065a1` with: ``` cargo test --locked -p victory-shader --lib v_cvt_u16_and_i16_f16_saturate -- --test-threads=1 ``` Result: 1 passed, 0 failed (69 filtered out). The test exercises signed and unsigned conversion with boundary values, infinities, NaNs, zeros, a subnormal, and truncation cases. These are synthetic conversion checks; this run does not measure console hardware behavior.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:02 UTC · original

RDNA2 hardware check of the saturation rule: matches, and the high half of the destination is kept.

AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs v_cvt_u16_f16 and v_cvt_i16_f16 on 22 halves with junk (0xabcd) in the unread high half of the source and 0xdead0000 preset in the destination. The halves are ±0, ±1.5, 15.0, 32752, ±32768, ±32800, ±65504, ±inf, ±NaN, an sNaN, ±(smallest subnormal), ±(just under 1) and 255.875. RDNA2 desktop silicon, not the console's. No game data.

  • Toward zero, clamped to [0, 65535] / [-32768, 32767], infinities saturating and every NaN giving 0: 22/22 for both ops. For example v_cvt_i16_f16(32768) = 0x7fff and v_cvt_i16_f16(-65504) = 0x8000.
  • The destination's high half stays 0xdead on every row, for both ops.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/384#issuecomment-7123514241) RDNA2 hardware check of the saturation rule: **matches**, and the high half of the destination is kept. AMD Radeon iGPU of a Ryzen 7 9800X3D, **gfx1036 (RDNA2)**, AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs `v_cvt_u16_f16` and `v_cvt_i16_f16` on 22 halves with junk (`0xabcd`) in the unread high half of the source and `0xdead0000` preset in the destination. The halves are ±0, ±1.5, 15.0, 32752, ±32768, ±32800, ±65504, ±inf, ±NaN, an sNaN, ±(smallest subnormal), ±(just under 1) and 255.875. RDNA2 desktop silicon, not the console's. No game data. - Toward zero, clamped to `[0, 65535]` / `[-32768, 32767]`, infinities saturating and every NaN giving 0: **22/22** for both ops. For example `v_cvt_i16_f16(32768)` = `0x7fff` and `v_cvt_i16_f16(-65504)` = `0x8000`. - The destination's high half stays `0xdead` on every row, for both ops. AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 06:59 UTC · original

Follow-up from a differential check of the translator against the gfx1036 hardware (121 VALU opcodes as raw VOP3 words, 220 inputs, translator on the Radeon iGPU as host): v_cvt_i16_f16 differs from the hardware in 7 of 220 cases on main, for example -inf → 0 where the hardware gives 0x8000, and a half of -59744 that wraps to 0x16a0 where the hardware saturates to 0x8000. With this PR it is 0 of 220, and no other opcode changes.

AI use: Claude Code (Claude Opus 5.5) ran the check and drafted this comment.

**@⁠Lyrnc** on GitHub · 2026-11-24 06:59 UTC · [original](https://github.com/Marvisak/VIctory/pull/384#issuecomment-7124500135) Follow-up from a differential check of the translator against the gfx1036 hardware (121 VALU opcodes as raw VOP3 words, 220 inputs, translator on the Radeon iGPU as host): `v_cvt_i16_f16` differs from the hardware in 7 of 220 cases on `main`, for example -inf → `0` where the hardware gives `0x8000`, and a half of -59744 that wraps to `0x16a0` where the hardware saturates to `0x8000`. **With this PR it is 0 of 220**, and no other opcode changes. AI use: Claude Code (Claude Opus 5.5) ran the check and drafted this comment.
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!384
No description provided.