Shaders: v_cvt_u16_f16 and v_cvt_i16_f16 saturate at 16 bits #384
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/384"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by LouisRiverstone on 2026-11-24T02:49:04Z.
Removes the
shortcut:onv_cvt_u16_f16/v_cvt_i16_f16inlower.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_f16of 32768 gave0x8000(-32768) instead of 32767. Infinities and NaN went throughFpToSi/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_saturateinvalu_tests.rs. It runs both ops on the host GPU and WARP with 16 inputs:Results:
dxil.dll10.0.26100 passes, and so do the rest of thevictory-shadertests (70).lower.rsit fails:hardware: v_cvt_i16_f16(0x7800) = 0x00008000, want 0x7fff.No game files.
AI-assisted (Claude Code): I reviewed the change and ran the tests above.
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
2af66ff1d8dbd7ccd7d87488a57e2caf37d065a1with: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.
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_f16andv_cvt_i16_f16on 22 halves with junk (0xabcd) in the unread high half of the source and0xdead0000preset 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.[0, 65535]/[-32768, 32767], infinities saturating and every NaN giving 0: 22/22 for both ops. For examplev_cvt_i16_f16(32768)=0x7fffandv_cvt_i16_f16(-65504)=0x8000.0xdeadon 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.
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_f16differs from the hardware in 7 of 220 cases onmain, for example -inf →0where the hardware gives0x8000, and a half of -59744 that wraps to0x16a0where the hardware saturates to0x8000. 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.