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

Open
DovisLe wants to merge 1 commit from upstream-pr/155 into main
DovisLe commented 2026-11-24 21:56:45 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by DovisLe on 2026-11-24T21:56:45Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:issue:6592208075 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/155). Originally posted by [DovisLe](https://github.com/DovisLe) on 2026-11-24T21:56:45Z. --- 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)
avt0m4t48 commented 2026-11-24 22:07:40 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by avt0m4t48 on 2026-11-24T22:00:08Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7145670227 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7145670227). Originally posted by [avt0m4t48](https://github.com/avt0m4t48) on 2026-11-24T22:00:08Z. --- 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.
bawhen commented 2026-11-24 23:09:33 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by bawhen on 2026-11-24T22:18:22Z.


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.

<!-- greyfinch-sync:fxceless/victory-fork:comment:7146066015 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7146066015). Originally posted by [bawhen](https://github.com/bawhen) on 2026-11-24T22:18:22Z. --- 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.*
DovisLe commented 2026-11-25 00:10:05 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by DovisLe on 2026-11-24T23:20:34Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:comment:7147291505 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7147291505). Originally posted by [DovisLe](https://github.com/DovisLe) on 2026-11-24T23:20:34Z. --- @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).
alexdeliu commented 2026-11-25 00:10:12 +00:00 (Migrated from github.com)

Imported from fxceless/victory-fork. Originally posted by alexdeliu on 2026-11-24T23:59:07Z.


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

<!-- greyfinch-sync:fxceless/victory-fork:comment:7148009548 --> Imported from [fxceless/victory-fork](https://github.com/fxceless/victory-fork/pull/155#issuecomment-7148009548). Originally posted by [alexdeliu](https://github.com/alexdeliu) on 2026-11-24T23:59:07Z. --- @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 cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/155:upstream-pr/155'
git switch 'upstream-pr/155'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/155'
git switch 'upstream-pr/155'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/155'
git switch 'upstream-pr/155'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/155'
git switch 'main'
git merge --squash 'upstream-pr/155'
git switch 'main'
git merge --ff-only 'upstream-pr/155'
git switch 'main'
git merge 'upstream-pr/155'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
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.

Reference
fxceless/victory-fork!155
No description provided.