Shaders: honor directed rounding in V_CVT_F16_F32 #3159

Open
github-bot wants to merge 2 commits from github-bot/gh-3159 into main AGit
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 10:33 UTC · original
andrewpozdnakov7-jpg:fix/directed-half-rounding → main · +112 −13 · 2 files · 2 commits · open

V_CVT_F16_F32 uses 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.4

On 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]>.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 10:33 UTC · [original](https://github.com/Marvisak/VIctory/pull/3159) `andrewpozdnakov7-jpg:fix/directed-half-rounding` → `main` · +112 −13 · 2 files · 2 commits · open `V_CVT_F16_F32` uses 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.4](https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content) On 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]>`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 11:09:01 +00:00
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 10:41 UTC · original

Updated to 872b1ba so the directed narrowing path applies to V_CVT_F16_F32. The broader first commit made V_FRACT_F16(-3 * 2^-13) return 0x3c00 with mode 1 selected. The added control reads the expected 0x3bff on this head, and all 136 conversion cases and 36 WARP VALU tests pass. Strict shader Clippy and the asset policy pass.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 10:41 UTC · [original](https://github.com/Marvisak/VIctory/pull/3159#issuecomment-7189699707) Updated to `872b1ba` so the directed narrowing path applies to `V_CVT_F16_F32`. The broader first commit made `V_FRACT_F16(-3 * 2^-13)` return `0x3c00` with mode 1 selected. The added control reads the expected `0x3bff` on this head, and all 136 conversion cases and 36 WARP VALU tests pass. Strict shader Clippy and the asset policy pass.
Author
Owner

@⁠Dukestar69 on GitHub · 2026-11-26 10:50 UTC · original · approved these changes

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).

**@⁠Dukestar69** on GitHub · 2026-11-26 10:50 UTC · [original](https://github.com/Marvisak/VIctory/pull/3159#pullrequestreview-6072796992) · **approved these changes** 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).
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-26 17:21 UTC · original

Checked 872b1ba against RDNA2 hardware, with the same instruction words on both sides.

  • Hardware: Radeon iGPU (Ryzen 7 9800X3D), gfx1036 / RDNA2, driver 32.0.21042.62. A HIP kernel (assembled by the driver's amd_comgr) runs s_denorm_mode, then s_round_mode, then three v_cvt_f16_f32 per lane. The destinations are preset, so the high half is checked too. There are 330 f32 inputs:
    • your 17 values, with both signs;
    • f32 denormals and NaNs;
    • values around the half overflow, the subnormal-half range and the ties;
    • random values.
  • Translator: the same words through a local copy of the build_valu_cs harness (a local test, not part of this PR). I ran it on main 63bcca4 and on the PR merged onto it, on the RTX 5080 (610.88) and on the same Radeon (picked with a local adapter-name filter in hardware_adapter).

v_cvt_f16_f32 in all four f16 round modes, with f16 denormals kept (s_denorm_mode 15 and 12): 2,640 results.

Host branch matches the hardware bit for bit NaN with a different payload other differences
Radeon gfx1036 main 2,024 0 616
Radeon gfx1036 #3159 2,634 0 6
RTX 5080 main 2,024 0 616
RTX 5080 #3159 2,634 0 6
  • main: all 616 differences are in the two directed modes. Two examples in mode 1: 0x3f800001 gives 0x3c00 where the hardware gives 0x3c01, and 0x477fe001 gives 0x7bff where the hardware gives 0x7c00.
  • This PR: those 616 are fixed. Every value in your test's table matches the hardware, in all four modes and with both signs. Nearest-even and toward-zero results are unchanged.
  • The 6 left: f32 denormal inputs, only when the program keeps f32 denormals (s_denorm_mode 15).
    • In mode 1 the hardware gives 0x0001 for 0x00000001, 0x00400000 and 0x007fffff. In mode 2 it gives 0x8001 for their negatives.
    • The PR gives ±0, because narrow16_in_mode reads f32 denormals as zero.
    • With f32 denormals flushed (s_denorm_mode 12), the hardware also gives ±0, and the PR matches all 1,320 directed-mode results.
    • So this gap only shows when MODE keeps f32 denormals. It could be a follow-up: skip the f32-denormal zeroing in the directed modes when the mode is known to keep them.
  • Your v_fract_f16 control (0x8e00 in mode 1) is 0x3bff on 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:

  • With f16 output denormals flushed (s_denorm_mode 3 or 0), the hardware flushes a subnormal f16 result of v_cvt_f16_f32 to a signed zero, in every round mode: 0x33800000 gives 0x0000. The translator keeps 0x0001. This affects 592 of the 2,640 results in those two modes.
  • v_fract_f16 and v_add_f16 round as MODE says on the hardware. In modes 1 and 2 they differ by 1 ulp in 21 and 130 of 600 results, plus 6 v_add_f16 results 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.

**@⁠Lyrnc** on GitHub · 2026-11-26 17:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/3159#issuecomment-7198881460) Checked `872b1ba` against RDNA2 hardware, with the same instruction words on both sides. - **Hardware:** Radeon iGPU (Ryzen 7 9800X3D), gfx1036 / RDNA2, driver 32.0.21042.62. A HIP kernel (assembled by the driver's amd_comgr) runs `s_denorm_mode`, then `s_round_mode`, then three `v_cvt_f16_f32` per lane. The destinations are preset, so the high half is checked too. There are 330 f32 inputs: - your 17 values, with both signs; - f32 denormals and NaNs; - values around the half overflow, the subnormal-half range and the ties; - random values. - **Translator:** the same words through a local copy of the `build_valu_cs` harness (a local test, not part of this PR). I ran it on `main` 63bcca4 and on the PR merged onto it, on the RTX 5080 (610.88) and on the same Radeon (picked with a local adapter-name filter in `hardware_adapter`). `v_cvt_f16_f32` in all four f16 round modes, with f16 denormals kept (`s_denorm_mode` 15 and 12): 2,640 results. | Host | branch | matches the hardware bit for bit | NaN with a different payload | other differences | |---|---|---|---|---| | Radeon gfx1036 | main | 2,024 | 0 | 616 | | Radeon gfx1036 | #3159 | 2,634 | 0 | **6** | | RTX 5080 | main | 2,024 | 0 | 616 | | RTX 5080 | #3159 | 2,634 | 0 | **6** | - **main:** all 616 differences are in the two directed modes. Two examples in mode 1: `0x3f800001` gives `0x3c00` where the hardware gives `0x3c01`, and `0x477fe001` gives `0x7bff` where the hardware gives `0x7c00`. - **This PR:** those 616 are fixed. Every value in your test's table matches the hardware, in all four modes and with both signs. Nearest-even and toward-zero results are unchanged. - **The 6 left:** f32 denormal inputs, only when the program keeps f32 denormals (`s_denorm_mode 15`). - In mode 1 the hardware gives `0x0001` for `0x00000001`, `0x00400000` and `0x007fffff`. In mode 2 it gives `0x8001` for their negatives. - The PR gives ±0, because `narrow16_in_mode` reads f32 denormals as zero. - With f32 denormals flushed (`s_denorm_mode 12`), the hardware also gives ±0, and the PR matches all 1,320 directed-mode results. - So this gap only shows when MODE keeps f32 denormals. It could be a follow-up: skip the f32-denormal zeroing in the directed modes when the mode is known to keep them. - Your `v_fract_f16` control (`0x8e00` in mode 1) is `0x3bff` on 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: - With f16 output denormals flushed (`s_denorm_mode` 3 or 0), the hardware flushes a subnormal f16 result of `v_cvt_f16_f32` to a signed zero, in every round mode: `0x33800000` gives `0x0000`. The translator keeps `0x0001`. This affects 592 of the 2,640 results in those two modes. - `v_fract_f16` and `v_add_f16` round as MODE says on the hardware. In modes 1 and 2 they differ by 1 ulp in 21 and 130 of 600 results, plus 6 `v_add_f16` results 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.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/3159/head:github-bot/gh-3159'
git switch 'github-bot/gh-3159'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-3159'
git switch 'github-bot/gh-3159'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-3159'
git switch 'github-bot/gh-3159'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-3159'
git switch 'main'
git merge --squash 'github-bot/gh-3159'
git switch 'main'
git merge --ff-only 'github-bot/gh-3159'
git switch 'main'
git merge 'github-bot/gh-3159'
git push origin 'main'
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!3159
No description provided.