Shaders: v_cvt_u16_f16 and v_cvt_i16_f16 saturate at 16 bits #384

Merged
LouisRiverstone merged 1 commit from upstream-pr/384 into main 2026-11-24 08:10:02 +00:00
LouisRiverstone commented 2026-11-24 02:49:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by LouisRiverstone on 2026-11-24T02:49:04Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6573491755 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/384). Originally posted by [LouisRiverstone](https://github.com/LouisRiverstone) on 2026-11-24T02:49:04Z. --- 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.
jeremygold02 commented 2026-11-24 06:55:43 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by jeremygold02 on 2026-11-24T04:22:39Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7121961887 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/384#issuecomment-7121961887). Originally posted by [jeremygold02](https://github.com/jeremygold02) on 2026-11-24T04:22:39Z. --- 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.
Lyrnc commented 2026-11-24 06:56:14 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:02:21Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7123514241 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/384#issuecomment-7123514241). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T06:02:21Z. --- 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 commented 2026-11-24 07:01:18 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-24T06:59:34Z.


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.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7124500135 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/384#issuecomment-7124500135). Originally posted by [Lyrnc](https://github.com/Lyrnc) on 2026-11-24T06:59:34Z. --- 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.
upstream-sync closed this pull request 2026-11-24 09:05:27 +00:00
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
Marvisak/VIctory!384
No description provided.