Shaders: honor directed rounding in V_CVT_F16_F32 #3157
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/3159"
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 andrewpozdnakov7-jpg on 2026-11-26T10:33:54Z.
V_CVT_F16_F32uses the half-precision rounding mode. On main the bitwise narrowing helper treats both directed modes as nearest-even. Make the direct conversion use the two directed modes in the bitwise narrowing helper. Arithmetic callers keep their existing rounding behavior. Increment the magnitude only when the discarded bits are nonzero and the sign matches the rounding direction. Finite overflow chooses signed infinity or signed max finite by the same direction. AMD RDNA 2 ISA, section 6.4On main, converting the smallest normal f32 in mode 1 returns 0x0000 instead of 0x0001. With the patch, the same WARP fixture passes 136 cases across all four modes and both signs, including subnormals, exact values, ties, overflow, infinity, and NaN. All 36 VALU tests passed on WARP, including a control checking that V_FRACT_F16 still returns 0x3bff for -3 * 2^-13 with mode 1 selected. Strict shader Clippy and the asset policy passed.
AI-assisted with Codex. Please retain the Git author when cherry-picking or rebasing. If folded into another commit, please add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T10:50:17Z.
Review: APPROVED
Verified on RTX 5090 & WARP against
origin/main(63bcca4):cargo test -p victory-shader --lib v_cvt_f16_f32_obeys_all_four_rounding_modes -- --test-threads=1: passed on RTX 5090 and WARP.cargo clippy -p victory-shader --all-targets -- -D warnings: clean.cargo xtask policy: 191 files ok.Cleanly honors all four IEEE 754 directed rounding modes in
V_CVT_F16_F32.Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T10:41:20Z.
Updated to
872b1baso the directed narrowing path applies toV_CVT_F16_F32. The broader first commit madeV_FRACT_F16(-3 * 2^-13)return0x3c00with mode 1 selected. The added control reads the expected0x3bffon this head, and all 136 conversion cases and 36 WARP VALU tests pass. Strict shader Clippy and the asset policy pass.Imported from Marvisak/VIctory. Originally posted by Lyrnc on 2026-11-26T17:21:34Z.
Checked
872b1baagainst RDNA2 hardware, with the same instruction words on both sides.s_denorm_mode, thens_round_mode, then threev_cvt_f16_f32per lane. The destinations are preset, so the high half is checked too. There are 330 f32 inputs:build_valu_csharness (a local test, not part of this PR). I ran it onmain63bcca4and on the PR merged onto it, on the RTX 5080 (610.88) and on the same Radeon (picked with a local adapter-name filter inhardware_adapter).v_cvt_f16_f32in all four f16 round modes, with f16 denormals kept (s_denorm_mode15 and 12): 2,640 results.0x3f800001gives0x3c00where the hardware gives0x3c01, and0x477fe001gives0x7bffwhere the hardware gives0x7c00.s_denorm_mode 15).0x0001for0x00000001,0x00400000and0x007fffff. In mode 2 it gives0x8001for their negatives.narrow16_in_modereads f32 denormals as zero.s_denorm_mode 12), the hardware also gives ±0, and the PR matches all 1,320 directed-mode results.v_fract_f16control (0x8e00in mode 1) is0x3bffon the hardware too.Two more things I saw, outside this PR. Both are the same on main and on the PR, so neither is a regression:
s_denorm_mode3 or 0), the hardware flushes a subnormal f16 result ofv_cvt_f16_f32to a signed zero, in every round mode:0x33800000gives0x0000. The translator keeps0x0001. This affects 592 of the 2,640 results in those two modes.v_fract_f16andv_add_f16round as MODE says on the hardware. In modes 1 and 2 they differ by 1 ulp in 21 and 130 of 600 results, plus 6v_add_f16results in toward-zero mode. As your description says, the arithmetic paths are left as they were.No game data was used.
AI use: Claude Code (Claude Opus 5.5) wrote the probe and the comparison and ran them on the GPUs above; the numbers come from those runs.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T00:35:26Z.
@Lyrnc Thanks for the RDNA2 comparison. I reproduced the remaining input-denormal case at the exact PR head
872b1baon WARP: with DENORM mode 13 and rounding toward positive infinity, f320x00000001converted to0x0000.The follow-up commit follows MODE bit 4 for direct conversions with directed rounding. The fixture covers the four f32 denormal settings with f16 denormals kept, two partial MODE writes, all four rounding modes and both signs. All 240 distinct cases matched on WARP in each of three processes, including the destination's high half. The 38 VALU tests, the existing conversion and
v_fract_f16controls, strict clippy in both configurations, and asset policy passed. The six cases from your hardware table are in the new test.Can you recheck the input-denormal rows on gfx1036 when convenient? This commit covers input denormals. The output-denormal cases you listed remain a separate change. Please retain the Git author on cherry-pick or add
Co-authored-by: andrewpozdnakov7-jpgif folding the follow-up.View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.