Round v_fma_f16 once in DXIL lowering #1387

Merged
DoubleD17 merged 1 commit from upstream-pr/1387 into main 2026-11-25 21:50:43 +00:00
DoubleD17 commented 2026-11-24 17:41:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DoubleD17 on 2026-11-24T17:41:00Z.


The DXIL lowering of v_fma_f16 can round twice when the exact result lands just above or below a binary16 midpoint. For 1.5 * 4108 + 2^-14, the binary32 intermediate loses the addend and the baseline produces 0x6e04 (6160). The correctly rounded binary16 result is 0x6e05 (6164).

Use the residual of the binary32 sum to determine which side of the midpoint the exact result lies on. Keep ties-to-even for exact midpoints and preserve the destination register's other half.

Independent Windows x64 QA checked nine finite operand triples against an integer reference and compared both complete output DWORDs on a GeForce RTX 5070 (32-lane waves). The targeted regression failed on the baseline and passed on the candidate. Both synthetic Wave32 shaders passed the Windows SDK 10.0.26100.0 DXIL validator and created and dispatched their D3D12 compute pipelines. The shader capability flags matched, and the victory package test passed. Validation covered one adapter. Wave64, NaNs, and game scenes were outside the test scope.

AI-assisted.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6587833184 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387). Originally posted by [DoubleD17](https://github.com/DoubleD17) on 2026-11-24T17:41:00Z. --- The DXIL lowering of `v_fma_f16` can round twice when the exact result lands just above or below a binary16 midpoint. For `1.5 * 4108 + 2^-14`, the binary32 intermediate loses the addend and the baseline produces `0x6e04` (6160). The correctly rounded binary16 result is `0x6e05` (6164). Use the residual of the binary32 sum to determine which side of the midpoint the exact result lies on. Keep ties-to-even for exact midpoints and preserve the destination register's other half. Independent Windows x64 QA checked nine finite operand triples against an integer reference and compared both complete output DWORDs on a GeForce RTX 5070 (32-lane waves). The targeted regression failed on the baseline and passed on the candidate. Both synthetic Wave32 shaders passed the Windows SDK 10.0.26100.0 DXIL validator and created and dispatched their D3D12 compute pipelines. The shader capability flags matched, and the `victory` package test passed. Validation covered one adapter. Wave64, NaNs, and game scenes were outside the test scope. AI-assisted.
catminou4 commented 2026-11-24 18:05:10 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T17:52:31Z.

Review: APPROVED


v_fma_f16 now rounds once: TwoSum recovers the f32 addition error (exact, since the f16 product is exact in f32), then a manual round-to-nearest-even f16 pack handles subnormals, overflow-to-inf at the 65520 midpoint, and falls back to the legacy op for NaN. The sign trick (XOR error bits with the sum sign so 'away from zero' is uniform in magnitude space) is correct, and the 9 hardware cases include exact ties, subnormal ties, and the overflow midpoint. Two caveats for the Windows run: (1) this expands to ~50 DXIL ops per fma — fine for correctness, but check shader compile times on fma-heavy titles; (2) the tiny -> 0 flush relies on the 2^-25 boundary, please confirm the halfway cases on hardware. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, cargo test -p victory-shader --locked. Shader execution tests (valu_tests/host/diff_tests) are #[cfg(all(test, windows))], so macOS covers lowering/decode/unit tests only. 96 passed, 0 failed on macOS; the valu test is Windows-gated.

<!-- greyfinch-sync:Marvisak/VIctory:review:6044527342 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387#pullrequestreview-6044527342). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-24T17:52:31Z. **Review:** APPROVED --- v_fma_f16 now rounds once: TwoSum recovers the f32 addition error (exact, since the f16 product is exact in f32), then a manual round-to-nearest-even f16 pack handles subnormals, overflow-to-inf at the 65520 midpoint, and falls back to the legacy op for NaN. The sign trick (XOR error bits with the sum sign so 'away from zero' is uniform in magnitude space) is correct, and the 9 hardware cases include exact ties, subnormal ties, and the overflow midpoint. Two caveats for the Windows run: (1) this expands to ~50 DXIL ops per fma — fine for correctness, but check shader compile times on fma-heavy titles; (2) the `tiny -> 0` flush relies on the 2^-25 boundary, please confirm the halfway cases on hardware. Local verification: macOS arm64, rustc 1.98.1, PR head merged into main ffe310e, `cargo test -p victory-shader --locked`. Shader execution tests (valu_tests/host/diff_tests) are `#[cfg(all(test, windows))]`, so macOS covers lowering/decode/unit tests only. 96 passed, 0 failed on macOS; the valu test is Windows-gated.
nullmarsh commented 2026-11-24 18:06:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by nullmarsh on 2026-11-24T18:00:57Z.


Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head c77b253 on main ffe310e.

  • v_fma_f16_rounds_once_and_preserves_other_half passes; the test reports the RADV adapter and DXIL validated. All 9 cases match with the other half kept, including the exact ties, the negative midpoint, the two subnormal ties and the overflow to 0x7C00.
  • victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, stub_pipeline_is_created, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR).
  • No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement.

Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the test fails without the fix on this host.

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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140033422 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387#issuecomment-7140033422). Originally posted by [nullmarsh](https://github.com/nullmarsh) on 2026-11-24T18:00:57Z. --- Ran on RDNA2 hardware through vkd3d-proton on a Steam Deck (not native Windows): AMD Van Gogh, RADV 26.1.99, SteamOS 3.8.28, Proton 11.0-3 (wine-11.0) with its vkd3d-proton 212991fc. Windows MSVC test binary (rustc 1.100.0) built from head `c77b253` on main `ffe310e`. - `v_fma_f16_rounds_once_and_preserves_other_half` passes; the test reports the RADV adapter and `DXIL validated`. All 9 cases match with the other half kept, including the exact ties, the negative midpoint, the two subnormal ties and the overflow to 0x7C00. - victory-shader lib tests: 120 passed, 1 failed, 1 ignored (main on the same host: 119/1/1). The one failure, `stub_pipeline_is_created`, fails on main too (likely vkd3d-proton accepting the zero DXIL digest the test expects refused; environment, not this PR). - No hangs. A pass means the translated DXIL gives these values on RDNA2 through vkd3d-proton/RADV; it isn't a console measurement. Not tested: native Windows AMD drivers; WARP (this setup has none, so the WARP leg ran on RADV too); whether the test fails without the fix on this host. AI-assisted (Claude Code, Claude Opus 5.5); no game data.
AnoHunted commented 2026-11-24 19:04:19 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by AnoHunted on 2026-11-24T18:38:09Z.


Second, independent NVIDIA confirmation. No game files were used.

Machine: Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1, DXC dxil.dll 1.9.2609 (SDK 10.0.26100) validating.

cargo test -p victory-shader --features host -- --test-threads=1 with this PR:

  • 121 passed, 0 failed, 1 ignored (main: 120 / 0 / 1).
  • v_fma_f16_rounds_once_and_preserves_other_half dispatches on the RTX 5080 and passes all 9 cases: the single-rounding result matches and the other half of the destination register is preserved, ties-to-even included.

Not tested: the in-game effect; AMD.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7140922122 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387#issuecomment-7140922122). Originally posted by [AnoHunted](https://github.com/AnoHunted) on 2026-11-24T18:38:09Z. --- Second, independent NVIDIA confirmation. No game files were used. Machine: Windows 11, NVIDIA GeForce RTX 5080, Rust 1.98.1, DXC `dxil.dll` 1.9.2609 (SDK 10.0.26100) validating. `cargo test -p victory-shader --features host -- --test-threads=1` with this PR: - **121 passed, 0 failed, 1 ignored** (main: 120 / 0 / 1). - `v_fma_f16_rounds_once_and_preserves_other_half` dispatches on the RTX 5080 and passes all 9 cases: the single-rounding result matches and the other half of the destination register is preserved, ties-to-even included. Not tested: the in-game effect; AMD.
DovisLe commented 2026-11-24 20:03:08 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by DovisLe on 2026-11-24T19:23:56Z.


Tested on a Radeon 860M (RDNA 3.5) and WARP, at c77b253. v_fma_f16_rounds_once_and_preserves_other_half passes, and the lib suite passes. An f16 sweep (#1575) shows v_fma_f16 is now correct. v_pk_fma_f16 still differs from the exact result on both backends, for example (0x3c01, 0x3c01, 0x3c01) gives 0x4001 instead of 0x4002, though most of that is the f16 narrowing #422 fixes. This conflicts with #422 in lower.rs; both add a helper after f16_toward_zero.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7142039101 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387#issuecomment-7142039101). Originally posted by [DovisLe](https://github.com/DovisLe) on 2026-11-24T19:23:56Z. --- Tested on a Radeon 860M (RDNA 3.5) and WARP, at `c77b253`. `v_fma_f16_rounds_once_and_preserves_other_half` passes, and the lib suite passes. An f16 sweep (#1575) shows `v_fma_f16` is now correct. `v_pk_fma_f16` still differs from the exact result on both backends, for example `(0x3c01, 0x3c01, 0x3c01)` gives `0x4001` instead of `0x4002`, though most of that is the f16 narrowing #422 fixes. This conflicts with #422 in `lower.rs`; both add a helper after `f16_toward_zero`.
Marvisak commented 2026-11-25 02:06:00 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:24:53Z.


Automated triage for the maintainers.

Merge candidate. It now merges cleanly with main. emit/lower.rs gets a new Lower::fma_f16, so v_fma_f16 rounds once. The product of two halves is exact in f32. TwoSum recovers the error of the f32 addition, and the f16 is packed by hand with round-to-nearest-even, using the sign of that error to settle f32 sums that land exactly on an f16 midpoint. This replaces the double-rounding fmad shortcut. I walked the packing through the normal, subnormal (exponent 102 to 112), underflow, overflow (a carry out of 0x7BFF reaches 0x7C00) and NaN branches. omod and clamp are handled the same way as main's float_result, and the round-toward-zero mode (mode16 == Some(3)) keeps the old path. Single rounding is what a fused multiply-add is documented to do. The test checks nine cases against an integer reference, and an RDNA2 run on the thread matches. v_fmac_f16 still takes the old path, which could be a follow-up. #1598 touches the neighbouring v_mad_f16 lines.

CI: passing (check).


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149520785 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387#issuecomment-7149520785). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:24:53Z. --- <!-- victory-triage v1 head=db78277d9f62d811fad5bf26f1eb1031392b0694 verdict=merge-candidate at=2026-11-25T06:27:00Z --> Automated triage for the maintainers. **Merge candidate.** It now merges cleanly with `main`. `emit/lower.rs` gets a new `Lower::fma_f16`, so `v_fma_f16` rounds once. The product of two halves is exact in f32. TwoSum recovers the error of the f32 addition, and the f16 is packed by hand with round-to-nearest-even, using the sign of that error to settle f32 sums that land exactly on an f16 midpoint. This replaces the double-rounding `fmad` shortcut. I walked the packing through the normal, subnormal (exponent 102 to 112), underflow, overflow (a carry out of `0x7BFF` reaches `0x7C00`) and NaN branches. `omod` and `clamp` are handled the same way as main's `float_result`, and the round-toward-zero mode (`mode16 == Some(3)`) keeps the old path. Single rounding is what a fused multiply-add is documented to do. The test checks nine cases against an integer reference, and an RDNA2 run on the thread matches. `v_fmac_f16` still takes the old path, which could be a follow-up. #1598 touches the neighbouring `v_mad_f16` lines. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
sinan-cmyk commented 2026-11-25 02:06:23 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by sinan-cmyk on 2026-11-25T01:48:17Z.


Offline measurement on a retail dump, and the shader tests on Ada

Result: no count change against main.

Offline. I merged head c77b253 onto main ffe310e (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 21,723 on main, 21,723 with this PR.

No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on main. The count shows that the change rejects nothing. It does not show whether the output is right.

Tests: with the same merge, cargo test --locked -p victory-shader --lib passed 121, failed 0. The bitcode benchmark is ignored, as on main, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with VICTORY_WARP unset.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149975702 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1387#issuecomment-7149975702). Originally posted by [sinan-cmyk](https://github.com/sinan-cmyk) on 2026-11-25T01:48:17Z. --- ### Offline measurement on a retail dump, and the shader tests on Ada **Result: no count change against `main`.** **Offline.** I merged head c77b253 onto `main` ffe310e (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 21,723 on `main`, 21,723 with this PR. No program changes its failure reason either. That is the expected result for a fix whose instructions sit behind the earlier failures on `main`. The count shows that the change rejects nothing. It does not show whether the output is right. **Tests:** with the same merge, `cargo test --locked -p victory-shader --lib` passed 121, failed 0. The bitcode benchmark is ignored, as on `main`, where 120 pass. The run was on an RTX 4070 SUPER (Ada, 32-lane waves, driver 32.0.16.1088), the default adapter, with `VICTORY_WARP` unset. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
upstream-sync closed this pull request 2026-11-25 22:02:58 +00:00
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
Marvisak/VIctory!1387
No description provided.