Shaders: f16 results round to nearest even, and min/max/med3 read denormal operands as zeros #135

Open
github-bot wants to merge 1 commit from github-bot/gh-135 into main AGit
Owner

@⁠alexdeliu on GitHub · 2026-11-25 16:00 UTC · original
alexdeliu:fix/f16-round-nearest → main · +236 −25 · 2 files · 1 commits · open

What

  • f16 results are narrowed on the bits (narrow16) instead of by DXIL legacyF32ToF16, whose rounding is left to the host. The rounding is to nearest even, the console's default, or toward zero where the program's MODE says so. The round mode is tracked per block through s_round_mode and s_setreg (round16).
    • This applies to v_cvt_f16_f32, v_fma_mix{lo,hi}_f16, the 16-bit VOP results, packed f16 math, this fork's v_pk_fmac_f16, and the f16 interpolation channels.
  • Denormal operands of v_min/max/min3/max3/med3_f32 read as zeros of their sign (ftz, max_min_flushed), as the console's default float mode does, whatever the host's own denormal mode is.

Ported from upstream Marvisak/VIctory 77053a0. Only these parts are taken: not its discard/helper-lane, FMA-probe, v_cvt_*_i32 or cndmask changes.

Why

On main, the RX 9070, the RDNA2 iGPU and WARP all round f16 results toward zero: v_cvt_f16_f32(1 + 0.75 ulp) gives 0x3C00, where the console gives 0x3C01. Every f16 result is off by up to one ulp, and some results differed between AMD generations.

Tested

One machine: RX 9070 (RDNA4, 32.0.32029.2010), Ryzen 7 7700 iGPU (gfx1036, RDNA2, 32.0.21045.29003) and WARP. Windows 11 Pro 26200; VICTORY_DXIL = dxil.dll 1.8.2502.11. No game data.

New GPU tests in valu_tests.rs:

test 01aadd1 this PR
v_cvt_f16_f32_rounds_to_nearest_even (ties to even, both signs) fails on all three: 0x3C00, want 0x3C01 passes on all three
max_and_min_read_denormal_operands_as_zeros fails on all three: -0, want +0 passes on all three

cargo test -p victory-shader --lib -- --test-threads=1:

01aadd1 this PR
RX 9070 266 passed, 0 failed 268 passed, 0 failed
gfx1036 264 passed, 2 failed 266 passed, 2 failed (the two #2 wave-size tests, fixed by #110)
WARP 252 passed, 14 failed 254 passed, the same 14 failed

The cross-device fuzzer from #129 (307 opcodes, 160 inputs each, all three adapters):

  • Opcodes that differ between devices: 38 before, 23 after. 15 are fixed and none are new: v_add/sub/subrev_f16, v_pk_add_f16, v_fma_f16, v_fmac_f16, v_pk_fma_f16, v_pk_fmac_f16, v_fma_mixlo/mixhi_f16, v_min/max/min3/max3/med3_f32.
  • Opcodes where RDNA4 and RDNA2 disagree with each other: 8 before, 2 after. The two left, v_add_f64 and v_mul_f64, are the AMD shared-dword bug reported at https://devcommunity.amd.com/t/1518.
  • v_sin_f16, approximate by definition, now differs from WARP on 12 of 160 inputs instead of 9.

Not tested

  • The game: the title also sets toward-zero rounding around its 16-bit interpolation, which this keeps.
  • NVIDIA and Intel.

AI use: Claude Code (Claude Opus 5.5) ported the change from upstream and ran the tests on my machine.

🤖 Generated with Claude Code

**@⁠alexdeliu** on GitHub · 2026-11-25 16:00 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/135) `alexdeliu:fix/f16-round-nearest` → `main` · +236 −25 · 2 files · 1 commits · open ## What - **f16 results are narrowed on the bits** (`narrow16`) instead of by DXIL `legacyF32ToF16`, whose rounding is left to the host. The rounding is to nearest even, the console's default, or toward zero where the program's MODE says so. The round mode is tracked per block through `s_round_mode` and `s_setreg` (`round16`). - This applies to `v_cvt_f16_f32`, `v_fma_mix{lo,hi}_f16`, the 16-bit VOP results, packed f16 math, this fork's `v_pk_fmac_f16`, and the f16 interpolation channels. - **Denormal operands of `v_min/max/min3/max3/med3_f32` read as zeros of their sign** (`ftz`, `max_min_flushed`), as the console's default float mode does, whatever the host's own denormal mode is. Ported from upstream Marvisak/VIctory `77053a0`. Only these parts are taken: not its discard/helper-lane, FMA-probe, `v_cvt_*_i32` or cndmask changes. ## Why On main, the RX 9070, the RDNA2 iGPU and WARP all round f16 results toward zero: `v_cvt_f16_f32(1 + 0.75 ulp)` gives `0x3C00`, where the console gives `0x3C01`. Every f16 result is off by up to one ulp, and some results differed between AMD generations. ## Tested One machine: RX 9070 (RDNA4, 32.0.32029.2010), Ryzen 7 7700 iGPU (gfx1036, RDNA2, 32.0.21045.29003) and WARP. Windows 11 Pro 26200; `VICTORY_DXIL` = dxil.dll 1.8.2502.11. No game data. New GPU tests in `valu_tests.rs`: | test | `01aadd1` | this PR | |---|---|---| | `v_cvt_f16_f32_rounds_to_nearest_even` (ties to even, both signs) | fails on all three: `0x3C00`, want `0x3C01` | passes on all three | | `max_and_min_read_denormal_operands_as_zeros` | fails on all three: `-0`, want `+0` | passes on all three | `cargo test -p victory-shader --lib -- --test-threads=1`: | | `01aadd1` | this PR | |---|---|---| | RX 9070 | 266 passed, 0 failed | 268 passed, 0 failed | | gfx1036 | 264 passed, 2 failed | 266 passed, 2 failed (the two #2 wave-size tests, fixed by #110) | | WARP | 252 passed, 14 failed | 254 passed, the same 14 failed | The cross-device fuzzer from #129 (307 opcodes, 160 inputs each, all three adapters): - Opcodes that differ between devices: **38 before, 23 after.** 15 are fixed and none are new: `v_add/sub/subrev_f16`, `v_pk_add_f16`, `v_fma_f16`, `v_fmac_f16`, `v_pk_fma_f16`, `v_pk_fmac_f16`, `v_fma_mixlo/mixhi_f16`, `v_min/max/min3/max3/med3_f32`. - Opcodes where **RDNA4 and RDNA2 disagree with each other: 8 before, 2 after.** The two left, `v_add_f64` and `v_mul_f64`, are the AMD shared-dword bug reported at https://devcommunity.amd.com/t/1518. - `v_sin_f16`, approximate by definition, now differs from WARP on 12 of 160 inputs instead of 9. ## Not tested - The game: the title also sets toward-zero rounding around its 16-bit interpolation, which this keeps. - NVIDIA and Intel. AI use: Claude Code (Claude Opus 5.5) ported the change from upstream and ran the tests on my machine. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
f16 results went through DXIL's `legacyF32ToF16`, which leaves the rounding
to the host: the RX 9070, the RDNA2 iGPU and WARP all round toward zero
(1 + 0.75 ulp gives 0x3C00, not 0x3C01). They are now narrowed on the bits,
to nearest even, or toward zero where the program's MODE says so (the
round mode is tracked through `s_round_mode` and `s_setreg`). Denormal
operands of `v_min/max_f32` and `v_med3_f32` read as zeros of their sign,
as the console's default float mode does, whatever the host's mode.

Ported from upstream Marvisak/VIctory 77053a0 (narrow16, round16, ftz,
max_min_flushed), without its discard, FMA-probe and conversion changes;
also used for this fork's `v_pk_fmac_f16`.

The cross-device fuzzer (#129) goes from 38 opcodes that differ to 23,
and from 8 that differ between RDNA4 and RDNA2 to 2 (the AMD f64 bug).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 03:36:28 +00:00
Author
Owner

@⁠KernelSmoker on GitHub · 2026-11-25 19:51 UTC · original

Independent A/B verification on NVIDIA GeForce RTX 3090 (Ampere, 32-lane warp, Bazzite Linux 45 x86_64, driver 620.73.04, GE-Proton 10-34 / Wine 10.0 / vkd3d-proton 3.0.0 bd3f5e3d245c3ad, Rust 1.98.1) on PR head 8807970:

Test (emit::valu_tests) Base 01aadd1 (lower.rs reverted) on RTX 3090 PR #135 (8807970) on RTX 3090
v_cvt_f16_f32_rounds_to_nearest_even PASSED (NVIDIA already rounds legacyF32ToF16 nearest-even) PASSED (preserved)
max_and_min_read_denormal_operands_as_zeros FAILED (op 0x110 [1, 80000000, 0, 0]: got 1 / denormal preserved, expected 0) PASSED

Full emit::valu_tests on RTX 3090 (--test-threads=1, vkd3d-proton 3.0.0): 25 passed, 4 failed (the 4 failures are the pre-existing 64-lane/wave32 tests loop_exit_mask_counted_after_the_loop, mask_grown_inside_a_branch_counted_after_it, scalar_counted_in_a_divergent_loop_read_after_it, and scalar_counted_in_nested_divergent_loops, unrelated to this PR; note EnumWarpAdapter in DXVK/vkd3d-proton falls back to the RTX 3090 rather than Windows WARP).

Additional checks on 8807970:

  • Linux cargo test --locked -p victory-shader: 185 passed, 0 failed, 6 ignored (lib) + 3 passed, 0 failed, 1 ignored (tests/resource_tracking.rs).
  • Windows GNU cargo clippy -p victory-shader --all-targets --locked --target x86_64-pc-windows-gnu: 0 new warnings (10 pre-existing warnings).
**@⁠KernelSmoker** on GitHub · 2026-11-25 19:51 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/135#issuecomment-7172849433) Independent A/B verification on **NVIDIA GeForce RTX 3090** (Ampere, 32-lane warp, Bazzite Linux 45 x86_64, driver 620.73.04, GE-Proton 10-34 / Wine 10.0 / vkd3d-proton 3.0.0 `bd3f5e3d245c3ad`, Rust 1.98.1) on PR head `8807970`: | Test (`emit::valu_tests`) | Base `01aadd1` (`lower.rs` reverted) on RTX 3090 | PR #135 (`8807970`) on RTX 3090 | |---|---|---| | `v_cvt_f16_f32_rounds_to_nearest_even` | **PASSED** (NVIDIA already rounds `legacyF32ToF16` nearest-even) | **PASSED** (preserved) | | `max_and_min_read_denormal_operands_as_zeros` | **FAILED** (`op 0x110 [1, 80000000, 0, 0]`: got `1` / denormal preserved, expected `0`) | **PASSED** | Full `emit::valu_tests` on RTX 3090 (`--test-threads=1`, vkd3d-proton 3.0.0): **25 passed, 4 failed** (the 4 failures are the pre-existing 64-lane/wave32 tests `loop_exit_mask_counted_after_the_loop`, `mask_grown_inside_a_branch_counted_after_it`, `scalar_counted_in_a_divergent_loop_read_after_it`, and `scalar_counted_in_nested_divergent_loops`, unrelated to this PR; note `EnumWarpAdapter` in DXVK/vkd3d-proton falls back to the RTX 3090 rather than Windows WARP). Additional checks on `8807970`: - Linux `cargo test --locked -p victory-shader`: **185 passed, 0 failed, 6 ignored** (lib) + **3 passed, 0 failed, 1 ignored** (`tests/resource_tracking.rs`). - Windows GNU `cargo clippy -p victory-shader --all-targets --locked --target x86_64-pc-windows-gnu`: **0 new warnings** (10 pre-existing warnings).
Author
Owner

@⁠adrianmjim on GitHub · 2026-11-25 19:52 UTC · original

Corpus data from the game's shaders.rpf on an RX 7900 XT (Windows 11 build 26300, dxil.dll 10.0.26100.0), head 8807970 on 01aadd1, victory translate --validate --list --host-lanes 32-64, comparing a hash of each program's DXIL with 01aadd1:

  • 19,318 of the 25,623 emitted programs change (9,633 pixel, 6,507 mesh, 2,103 compute, 1,075 vertex), and all of them validate. The counts are unchanged (VS/PS 14,247/14,247, CS 2,682/2,682, MS 8,694 of 8,765).
  • cargo test -p victory-shader --lib -- --test-threads=1: 268 passed, 0 failed, 6 ignored.

So nearly every host shader is retranslated with this change, and every cached pipeline is rebuilt on first run. Pipelines were not created and the game was not run here.

**@⁠adrianmjim** on GitHub · 2026-11-25 19:52 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/135#issuecomment-7172891496) Corpus data from the game's `shaders.rpf` on an RX 7900 XT (Windows 11 build 26300, dxil.dll 10.0.26100.0), head `8807970` on `01aadd1`, `victory translate --validate --list --host-lanes 32-64`, comparing a hash of each program's DXIL with `01aadd1`: - 19,318 of the 25,623 emitted programs change (9,633 pixel, 6,507 mesh, 2,103 compute, 1,075 vertex), and all of them validate. The counts are unchanged (VS/PS 14,247/14,247, CS 2,682/2,682, MS 8,694 of 8,765). - `cargo test -p victory-shader --lib -- --test-threads=1`: 268 passed, 0 failed, 6 ignored. So nearly every host shader is retranslated with this change, and every cached pipeline is rebuilt on first run. Pipelines were not created and the game was not run here.
Author
Owner

@⁠MamaLeVrai on GitHub · 2026-11-26 05:00 UTC · original

RDNA2 data point. This PR (8807970) merged on main 4f62c2a, cargo test --release -p victory-shader --lib on an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1):

  • main: 275 passed, 2 failed.
  • This PR: 277 passed, 2 failed. The 2 new tests pass.

The 2 failures are the same as on main: the RDNA2 wave-size tests scalar_counted_in_a_divergent_loop_read_after_it and scalar_counted_in_nested_divergent_loops, fixed by #110 and #204. No regression here. One run ended without a result line, which this test binary does now and then on this machine, on main too. Not run in the game.

harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300

AI-assisted (Claude Code); no game data.

**@⁠MamaLeVrai** on GitHub · 2026-11-26 05:00 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/135#issuecomment-7183094174) RDNA2 data point. This PR (`8807970`) merged on `main` `4f62c2a`, `cargo test --release -p victory-shader --lib` on an RX 6950 XT (Windows 11 build 26300, Rust 1.98.1): - `main`: 275 passed, 2 failed. - This PR: 277 passed, 2 failed. The 2 new tests pass. The 2 failures are the same as on `main`: the RDNA2 wave-size tests `scalar_counted_in_a_divergent_loop_read_after_it` and `scalar_counted_in_nested_divergent_loops`, fixed by #110 and #204. No regression here. One run ended without a result line, which this test binary does now and then on this machine, on `main` too. Not run in the game. harness machine: rx6950xt-w11 | AMD Radeon RX 6950 XT (RDNA2), driver 32.0.21045.29003 | 32 GB RAM | Windows 11 build 26300 AI-assisted (Claude Code); no game data.
@ -3438,0 +3534,4 @@
// shortcut: rounding toward +-infinity, or a mode not known at this
// point (paths that set it differently join), rounds to nearest even.
fn narrow16(&mut self, f: V) -> V {
let rtz = self.mode16 == Some(3);
Author
Owner

@⁠gut0to on GitHub · 2026-11-27 15:03 UTC · original

AI-assisted review: round16_after preserves the MODE value, and the comment above acknowledges rounding toward both infinities, but narrow16 selects only between RTZ (Some(3)) and nearest-even. A shader that sets either directed mode before a half conversion therefore gets a different result. Could the two modes be implemented here and covered with values on both sides of zero that are not halfway ties?

**@⁠gut0to** on GitHub · 2026-11-27 15:03 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/135#discussion_r4720924830) AI-assisted review: `round16_after` preserves the MODE value, and the comment above acknowledges rounding toward both infinities, but `narrow16` selects only between RTZ (`Some(3)`) and nearest-even. A shader that sets either directed mode before a half conversion therefore gets a different result. Could the two modes be implemented here and covered with values on both sides of zero that are not halfway ties?
Author
Owner

@⁠gut0to on GitHub · 2026-11-27 15:03 UTC · original · reviewed

AI-assisted static review; I traced the MODE propagation and the half-conversion path. I did not run the test suite.

**@⁠gut0to** on GitHub · 2026-11-27 15:03 UTC · [original](https://github.com/Belobug/VIctory-Belobug-AMD/pull/135#pullrequestreview-6090070071) · **reviewed** AI-assisted static review; I traced the MODE propagation and the half-conversion path. I did not run the test suite.
This pull request can be merged automatically.
This branch is out-of-date with the base branch
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/135/head:github-bot/gh-135'
git switch 'github-bot/gh-135'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-135'
git switch 'github-bot/gh-135'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-135'
git switch 'github-bot/gh-135'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-135'
git switch 'main'
git merge --squash 'github-bot/gh-135'
git switch 'main'
git merge --ff-only 'github-bot/gh-135'
git switch 'main'
git merge 'github-bot/gh-135'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.