Shaders: VICTORY_F16_NEAREST rounds every f16 narrowing, not only 16-bit ALU results #155

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

@⁠DovisLe on GitHub · 2026-11-24 21:56 UTC · original
DovisLe:shader/f16-nearest-everywhere → main · +144 −30 · 2 files · 1 commits · open

Refs #136, first row ("f16 math on AMD is rounded toward zero").

Problem. The fork already has an exact f16_nearest behind VICTORY_F16_NEAREST=1, but only the VOP1/VOP2/VOP3 16-bit float results (and the wave sum) go through it. The other f32-to-f16 narrowings still call the host's legacyF32ToF16, which rounds toward zero on AMD, NVIDIA and WARP, so they stay one ulp low even with the switch on:

  • v_cvt_f16_f32 (its own arm)
  • v_fma_mixlo_f16, v_fma_mixhi_f16
  • the packed v_pk_*_f16 ops
  • f16 interpolation (v_interp_*_f16)
  • image and buffer d16 loads

Change. A new f16_narrow applies the switch, and all seven sites call it. f16_toward_zero keeps the raw host conversion it builds on. The default is unchanged (off), so nothing moves unless VICTORY_F16_NEAREST=1 is set.

Test. f16_nearest_switch_rounds_every_narrowing turns the switch on with the existing TEST_F16_NEAREST hook. It runs v_cvt_f16_f32, v_pk_add_f16, v_pk_fma_f16 and v_fma_mixlo/mixhi_f16 (checking the destination's other half is kept) on the hardware adapter and WARP, against nearest even computed on the CPU. Every case is one where toward-zero and nearest differ, and the fma inputs keep the f32 intermediate exact.

Results on a Radeon 860M (RDNA 3.5) and WARP:

packed and mix ops with the switch on
main (4e17478) wrong on both. The 860M gives #136's example v_pk_add_f16(0xf8b6, 0x0001) = 0xf8b5; v_pk_fma_f16(0x3c01, 0x3c01, 0x3c01) = 0x4001 (should be 0x4002); v_fma_mixlo_f16(1 - 2^-24) = 0x3bff (should be 0x3c00).
this PR exact on both
  • cargo test -p victory-shader --lib: 211 passed, 0 failed, 2 ignored.
  • cargo clippy -p victory-shader --all-targets [--all-features] --locked -- -D warnings: clean.
  • No game files were used. All inputs are synthetic.

This doesn't decide the default. Whether VICTORY_F16_NEAREST should be on by default is the maintainers' call, since it adds about a dozen instructions per narrowing. This makes the switch mean what it says, so turning it on fixes the whole of #136's first row.

AI-assisted (Claude).

🤖 Generated with Claude Code

**@⁠DovisLe** on GitHub · 2026-11-24 21:56 UTC · [original](https://github.com/fxceless/victory-fork/pull/155) `DovisLe:shader/f16-nearest-everywhere` → `main` · +144 −30 · 2 files · 1 commits · open Refs #136, first row ("f16 math on AMD is rounded toward zero"). **Problem.** The fork already has an exact `f16_nearest` behind `VICTORY_F16_NEAREST=1`, but only the VOP1/VOP2/VOP3 16-bit float results (and the wave sum) go through it. The other f32-to-f16 narrowings still call the host's `legacyF32ToF16`, which rounds toward zero on AMD, NVIDIA and WARP, so they stay one ulp low **even with the switch on**: - `v_cvt_f16_f32` (its own arm) - `v_fma_mixlo_f16`, `v_fma_mixhi_f16` - the packed `v_pk_*_f16` ops - f16 interpolation (`v_interp_*_f16`) - image and buffer d16 loads **Change.** A new `f16_narrow` applies the switch, and all seven sites call it. `f16_toward_zero` keeps the raw host conversion it builds on. The default is unchanged (off), so nothing moves unless `VICTORY_F16_NEAREST=1` is set. **Test.** `f16_nearest_switch_rounds_every_narrowing` turns the switch on with the existing `TEST_F16_NEAREST` hook. It runs `v_cvt_f16_f32`, `v_pk_add_f16`, `v_pk_fma_f16` and `v_fma_mixlo/mixhi_f16` (checking the destination's other half is kept) on the hardware adapter and WARP, against nearest even computed on the CPU. Every case is one where toward-zero and nearest differ, and the `fma` inputs keep the f32 intermediate exact. Results on a **Radeon 860M (RDNA 3.5)** and WARP: | | packed and mix ops with the switch on | |---|---| | `main` (`4e17478`) | wrong on both. The 860M gives #136's example `v_pk_add_f16(0xf8b6, 0x0001) = 0xf8b5`; `v_pk_fma_f16(0x3c01, 0x3c01, 0x3c01) = 0x4001` (should be `0x4002`); `v_fma_mixlo_f16(1 - 2^-24) = 0x3bff` (should be `0x3c00`). | | this PR | exact on both | - `cargo test -p victory-shader --lib`: 211 passed, 0 failed, 2 ignored. - `cargo clippy -p victory-shader --all-targets [--all-features] --locked -- -D warnings`: clean. - No game files were used. All inputs are synthetic. This doesn't decide the default. Whether `VICTORY_F16_NEAREST` should be on by default is the maintainers' call, since it adds about a dozen instructions per narrowing. This makes the switch mean what it says, so turning it on fixes the whole of #136's first row. AI-assisted (Claude). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
With VICTORY_F16_NEAREST=1 only the VOP1/VOP2/VOP3 16-bit results (and the
wave sum) went through f16_nearest. The other narrowings still called the
host's f32tof16, which rounds toward zero on AMD, NVIDIA and WARP:
v_cvt_f16_f32's own arm, v_fma_mixlo/mixhi_f16, the packed v_pk_*_f16 ops,
f16 interpolation, and image and buffer d16 loads. So v_pk_add_f16(0xf8b6,
0x0001) still gave 0xf8b5 on AMD with the switch on (#136).

f16_narrow applies the switch at every one of them; f16_toward_zero keeps
the host conversion it builds on. The default is unchanged.

f16_nearest_switch_rounds_every_narrowing turns the switch on and checks
v_cvt_f16_f32, v_pk_add_f16, v_pk_fma_f16 and v_fma_mixlo/mixhi_f16 (the
other half kept) against nearest even on the hardware adapter and WARP. On
the old routing it fails for the packed and mix ops on both (Radeon 860M).

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

@⁠avt0m4t48 on GitHub · 2026-11-24 22:00 UTC · original

Independent check of 158d93ee0fbc: I ran a source-built fixture for packed add and FMA, both mixed FMA destination halves, and the VOP3 form of v_cvt_f16_f32. It covers positive and negative ties, and checks that conversion and mixed FMA preserve the other destination half. The packed tie cases read back 0xbc023c02. The VOP3 conversion read back 0xaabb3c02. Fixture

With the switch on, this PR emitted byte-for-byte the same five shaders as the independently WARP-tested ALU patch. All five modules from this PR passed Linux dxv. With the switch off, all five matched the original 4e17478 bytes. The WARP run also confirmed that the test fails on the original lowering, where packed add ignores the switch. Windows positive and negative controls

This check covered the five ALU forms. Interpolation, d16 loads and physical AMD hardware were outside this independent run. AI-assisted using Codex.

**@⁠avt0m4t48** on GitHub · 2026-11-24 22:00 UTC · [original](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7145670227) Independent check of `158d93ee0fbc`: I ran a source-built fixture for packed add and FMA, both mixed FMA destination halves, and the VOP3 form of `v_cvt_f16_f32`. It covers positive and negative ties, and checks that conversion and mixed FMA preserve the other destination half. The packed tie cases read back `0xbc023c02`. The VOP3 conversion read back `0xaabb3c02`. [Fixture](https://github.com/avt0m4t48/victory-fork/blob/c7be3908f3488444829be44acc58d245259d1668/crates/victory-shader/src/emit/valu_tests.rs#L311-L359) With the switch on, this PR emitted byte-for-byte the same five shaders as the independently WARP-tested ALU patch. All five modules from this PR passed Linux `dxv`. With the switch off, all five matched the original `4e17478` bytes. The WARP run also confirmed that the test fails on the original lowering, where packed add ignores the switch. [Windows positive and negative controls](https://github.com/avt0m4t48/victory-fork/actions/runs/45496135644) This check covered the five ALU forms. Interpolation, d16 loads and physical AMD hardware were outside this independent run. AI-assisted using Codex.
Author
Owner

@⁠bawhen on GitHub · 2026-11-24 22:18 UTC · original

Reproduced and fixed on a desktop RDNA3 card (RX 7700, driver 32.0.32029.2010).

cargo test -p victory-shader --locked --lib f16_nearest_switch_rounds_every_narrowing -- --nocapture

build result
PR head 158d93e passes on hardware and WARP
PR test, lower.rs from main 14 wrong results across v_pk_add_f16, v_pk_fma_f16, v_fma_mixlo_f16 and v_fma_mixhi_f16, on both the RX 7700 and WARP

The one place the two devices differ is #136's example in the low lane of v_pk_add_f16:

hardware: v_pk_add_f16([3c01f8b6, 10010001, ...]) = 0x3c01f8b5, want 0x3c02f8b6
warp:     v_pk_add_f16([3c01f8b6, 10010001, ...]) = 0x3c01f8b6, want 0x3c02f8b6

The RX 7700 returns 0xf8b5 where WARP returns 0xf8b6. That fits the reading in #136 that AMD's compiler folds the chain into a native f16 add, so RDNA3 desktop behaves like the RX 9070 and gfx1036. The high lane and the fma/mix cases are wrong identically on both (the toward-zero conversion).

Setup: AMD Radeon RX 7700 (RDNA3, 16 GB), driver 32.0.32029.2010, Ryzen 7 8700F, 16 GB RAM, Windows 11 (build 26300), Rust 1.98.1 from rust-toolchain.toml. Baseline main at 4e17478: cargo test --workspace --locked gives 496 passed, 0 failed, 7 ignored. The "without the fix" run checks out main's version of the PR's non-test files and keeps the PR's new test. No game files used.

AI use: Claude Code (Claude Opus 5.5) ran the tests and drafted this comment; posted at my request.

**@⁠bawhen** on GitHub · 2026-11-24 22:18 UTC · [original](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7146066015) Reproduced and fixed on a desktop RDNA3 card (RX 7700, driver 32.0.32029.2010). `cargo test -p victory-shader --locked --lib f16_nearest_switch_rounds_every_narrowing -- --nocapture` | build | result | |---|---| | PR head `158d93e` | passes on hardware and WARP | | PR test, `lower.rs` from `main` | 14 wrong results across `v_pk_add_f16`, `v_pk_fma_f16`, `v_fma_mixlo_f16` and `v_fma_mixhi_f16`, on both the RX 7700 and WARP | The one place the two devices differ is #136's example in the low lane of `v_pk_add_f16`: ``` hardware: v_pk_add_f16([3c01f8b6, 10010001, ...]) = 0x3c01f8b5, want 0x3c02f8b6 warp: v_pk_add_f16([3c01f8b6, 10010001, ...]) = 0x3c01f8b6, want 0x3c02f8b6 ``` The RX 7700 returns `0xf8b5` where WARP returns `0xf8b6`. That fits the reading in #136 that AMD's compiler folds the chain into a native f16 add, so RDNA3 desktop behaves like the RX 9070 and gfx1036. The high lane and the `fma`/`mix` cases are wrong identically on both (the toward-zero conversion). Setup: AMD Radeon RX 7700 (RDNA3, 16 GB), driver 32.0.32029.2010, Ryzen 7 8700F, 16 GB RAM, Windows 11 (build 26300), Rust 1.98.1 from `rust-toolchain.toml`. Baseline `main` at `4e17478`: `cargo test --workspace --locked` gives 496 passed, 0 failed, 7 ignored. The "without the fix" run checks out `main`'s version of the PR's non-test files and keeps the PR's new test. No game files used. *AI use: Claude Code (Claude Opus 5.5) ran the tests and drafted this comment; posted at my request.*
Author
Owner

@⁠DovisLe on GitHub · 2026-11-24 23:20 UTC · original

@⁠alexdeliu, taking you up on the offer in #136: could you run this PR (158d93e) on the RX 9070 and the gfx1036?

cargo test -p victory-shader --locked --lib f16_nearest_switch_rounds_every_narrowing -- --nocapture

The test turns VICTORY_F16_NEAREST on by itself, so no environment variable is needed. So far it passes on a Radeon 860M (RDNA 3.5), on bawhen's RX 7700 (RDNA3) and on WARP. RDNA4 and RDNA2 would cover the rest of the AMD range.

AI-assisted (Claude).

**@⁠DovisLe** on GitHub · 2026-11-24 23:20 UTC · [original](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7147291505) @⁠alexdeliu, taking you up on the offer in #136: could you run this PR (`158d93e`) on the RX 9070 and the gfx1036? ``` cargo test -p victory-shader --locked --lib f16_nearest_switch_rounds_every_narrowing -- --nocapture ``` The test turns `VICTORY_F16_NEAREST` on by itself, so no environment variable is needed. So far it passes on a Radeon 860M (RDNA 3.5), on bawhen's RX 7700 (RDNA3) and on WARP. RDNA4 and RDNA2 would cover the rest of the AMD range. AI-assisted (Claude).
Author
Owner

@⁠alexdeliu on GitHub · 2026-11-24 23:59 UTC · original

@⁠DovisLe 158d93e passes on both: f16_nearest_switch_rounds_every_narrowing is ok on the AMD Radeon RX 9070 (RDNA4) and on the Ryzen 7700's iGPU, gfx1036 (RDNA2), on Windows 11. I selected the iGPU with a local, uncommitted adapter-index override in Host::new, and the log confirms which device each run used. With your runs, that covers RDNA2, RDNA3, RDNA3.5 and RDNA4.

**@⁠alexdeliu** on GitHub · 2026-11-24 23:59 UTC · [original](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7148009548) @⁠DovisLe `158d93e` passes on both: `f16_nearest_switch_rounds_every_narrowing` is ok on the AMD Radeon RX 9070 (RDNA4) and on the Ryzen 7700's iGPU, gfx1036 (RDNA2), on Windows 11. I selected the iGPU with a local, uncommitted adapter-index override in `Host::new`, and the log confirms which device each run used. With your runs, that covers RDNA2, RDNA3, RDNA3.5 and RDNA4.
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
  • crates/victory-shader/src/emit/valu_tests.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

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

Merge

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