Shaders: round packed FP16 fused multiply-add once in nearest-even mode #2884

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

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 23:02 UTC · original
andrewpozdnakov7-jpg:codex2/packed-fma-nearest-even → main · +74 −37 · 2 files · 1 commits · open

The f32 fallback for V_PK_FMA_F16 and V_PK_FMAC_F16 could round the sum to an f16 midpoint before narrowing it. Use the single-rounding helper introduced by #1387 for each nearest-even packed result. Both source-half selectors and the per-half negation and clamp go through the existing packed instruction path.

Scope is round-to-nearest-even (RNE), including the default lowering when the rounding mode is unknown. Directed modes 1–3 still use the existing f32 FMad and narrowing path.

The same synthetic fixture on 842b0cb and this commit checks 20 input and instruction-form pairs on app-local WARP SM 6.6. Eight pairs disagree with the CPU model on main. For example, both packed operations produce 0x3e043e02 with 0x3e053e01 expected. All 20 pairs match with this change, including swapped halves, VOP2/VOP3 accumulators, cancellation and a subnormal tie. The f16 semantics test, 35 VALU tests, DXIL validation, strict shader Clippy and the asset policy passed.

#2825 adds a capability-gated native half path. I resolved the adjacent lowering edit locally with its head 3c81083 and ran the same WARP tests. The capability probe still selects the fallback on WARP and the packed single-rounding fixture passes in that combined build.

RDNA 2 ISA · Coordination claim

Please retain the Git author if cherry-picking or rebasing the commit. If you fold the change into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 23:02 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884) `andrewpozdnakov7-jpg:codex2/packed-fma-nearest-even` → `main` · +74 −37 · 2 files · 1 commits · open The f32 fallback for `V_PK_FMA_F16` and `V_PK_FMAC_F16` could round the sum to an f16 midpoint before narrowing it. Use the single-rounding helper introduced by #1387 for each nearest-even packed result. Both source-half selectors and the per-half negation and clamp go through the existing packed instruction path. Scope is round-to-nearest-even (RNE), including the default lowering when the rounding mode is unknown. Directed modes 1–3 still use the existing f32 FMad and narrowing path. The same synthetic fixture on `842b0cb` and this commit checks 20 input and instruction-form pairs on app-local WARP SM 6.6. Eight pairs disagree with the CPU model on main. For example, both packed operations produce `0x3e043e02` with `0x3e053e01` expected. All 20 pairs match with this change, including swapped halves, VOP2/VOP3 accumulators, cancellation and a subnormal tie. The f16 semantics test, 35 VALU tests, DXIL validation, strict shader Clippy and the asset policy passed. #2825 adds a capability-gated native half path. I resolved the adjacent lowering edit locally with its head `3c81083` and ran the same WARP tests. The capability probe still selects the fallback on WARP and the packed single-rounding fixture passes in that combined build. [RDNA 2 ISA](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) · [Coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633) Please retain the Git author if cherry-picking or rebasing the commit. If you fold the change 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 05:16:41 +00:00
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 23:10 UTC · original

Reviewed and verified 64c62d5e on Windows 11 (RTX 5090 + WARP + dxil.dll) against main (842b0cb):

  • Routing v_pk_fma_f16/v_pk_fmac_f16 through fma_f16 (TwoSum single rounding) under RNE (None | Some(0)) while keeping the directed-rounding fmad + narrow16 path for Some(1..=3) eliminates double-rounding while preserving op_sel, per-half neg, and clamp (and complements #2825's native-16-bit path on software/WARP hosts).
  • Local checks: semantics_tests 35/35 passed (including packed_fused_f16_rounds_each_half_once across all 20 cases with 0 discrepancies on RTX 5090, WARP, and the reference model), emit::lower::tests 33/33 passed, cargo clippy --locked -p victory-shader --all-targets -- -D warnings 0 warnings, cargo xtask policy (191 files ok; emit/lower.rs = 520,817 bytes).

Looks clean and ready to merge.

**@⁠fuzeofficlal** on GitHub · 2026-11-25 23:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884#issuecomment-7177231222) Reviewed and verified `64c62d5e` on Windows 11 (RTX 5090 + WARP + `dxil.dll`) against `main` (`842b0cb`): - Routing `v_pk_fma_f16`/`v_pk_fmac_f16` through `fma_f16` (TwoSum single rounding) under RNE (`None | Some(0)`) while keeping the directed-rounding `fmad + narrow16` path for `Some(1..=3)` eliminates double-rounding while preserving `op_sel`, per-half `neg`, and `clamp` (and complements `#2825`'s native-16-bit path on software/WARP hosts). - Local checks: `semantics_tests` 35/35 passed (including `packed_fused_f16_rounds_each_half_once` across all 20 cases with 0 discrepancies on RTX 5090, WARP, and the reference model), `emit::lower::tests` 33/33 passed, `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` 0 warnings, `cargo xtask policy` (`191 files ok`; `emit/lower.rs` = `520,817` bytes). Looks clean and ready to merge.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:28 UTC · original

Automated triage for the maintainers.

Merge candidate. v_pk_fma_f16 and v_pk_fmac_f16 in nearest-even mode now go through the single-rounding fma_f16 helper the scalar v_fma_f16 already uses (packed16 in emit/lower.rs); directed modes keep the f32 FMad. This matches the RDNA2 ISA's definition of these as fused. packed_fused_f16_rounds_each_half_once checks against an exact f64 fused result, with midpoints, a subnormal tie, op_sel/neg per half, clamp, and both encodings of v_pk_fmac_f16.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 04:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884#issuecomment-7182624705) <!-- victory-triage v1 head=64c62d5e1749d3414e5b7f66e9edce621a9cc00e verdict=merge-candidate at=2026-11-26T04:28:14Z --> Automated triage for the maintainers. **Merge candidate.** `v_pk_fma_f16` and `v_pk_fmac_f16` in nearest-even mode now go through the single-rounding `fma_f16` helper the scalar `v_fma_f16` already uses (`packed16` in `emit/lower.rs`); directed modes keep the f32 FMad. This matches the RDNA2 ISA's definition of these as fused. `packed_fused_f16_rounds_each_half_once` checks against an exact f64 fused result, with midpoints, a subnormal tie, op_sel/neg per half, clamp, and both encodings of `v_pk_fmac_f16`. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 23:34 UTC · original · reviewed

Right fix for double rounding: a fused fma rounds once, so computing in f32 then narrowing to f16 misrounds exactly the tie cases (exact product-sum needing >f32 mantissa). Keeping the f32 FMad only for directed modes (which need the explicit rounding anyway) is the correct split, and the test was updated to match the new semantics rather than keeping the known-wrong expectation. LGTM.

**@⁠kotekompotek** on GitHub · 2026-11-25 23:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884#pullrequestreview-6066474446) · **reviewed** Right fix for double rounding: a fused fma rounds once, so computing in f32 then narrowing to f16 misrounds exactly the tie cases (exact product-sum needing >f32 mantissa). Keeping the f32 FMad only for directed modes (which need the explicit rounding anyway) is the correct split, and the test was updated to match the new semantics rather than keeping the known-wrong expectation. LGTM.
Author
Owner

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

Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-shader --lib packed_fused_f16_rounds_each_half_once -- --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 ensures packed f16 FMA rounds each half once rather than double-rounding.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

**@⁠Dukestar69** on GitHub · 2026-11-26 09:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884#pullrequestreview-6071565186) · **approved these changes** Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib packed_fused_f16_rounds_each_half_once -- --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 ensures packed f16 FMA rounds each half once rather than double-rounding. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 10:32 UTC · original · reviewed

Routing v_pk_fma_f16 and v_pk_fmac_f16 through fma_f16 in nearest-even mode (matches!(self.mode16, None | Some(0))) eliminates double rounding on midpoint and subnormal ties while preserving op_sel, per-half neg, clamp, and both VOP2/VOP3 FMAC encodings. git merge-tree against main (63bcca43) is clean, and emit/lower.rs is 520,817 bytes (under the 512 KiB limit).

One scope note worth making explicit in the title/summary: directed rounding modes (Some(1..=3)) still go through fmad + narrow16, so RTZ still double-rounds cases like (0x4000, 0x3c00, 0x8001) (2 - 2^-24 rounds to 2.0 in f32 -> 0x4000 instead of 0x3fff), and modes 1 and 2 narrow as nearest-even.

Verified locally on Windows 11 x64 (RTX 5090 + WARP + dxil.dll, 64c62d5e):

  • cargo run --locked -p xtask -- policy: passed (191 files ok)
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default and --all-features): clean
  • packed_fused_f16_rounds_each_half_once and sixteen_bit_floats_match_the_hardware (-- --test-threads=1, RTX 5090 and VICTORY_WARP=1): 1 passed, 0 failed each on both
**@⁠fuzeofficlal** on GitHub · 2026-11-26 10:32 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884#pullrequestreview-6072499585) · **reviewed** Routing `v_pk_fma_f16` and `v_pk_fmac_f16` through `fma_f16` in nearest-even mode (`matches!(self.mode16, None | Some(0))`) eliminates double rounding on midpoint and subnormal ties while preserving `op_sel`, per-half `neg`, `clamp`, and both VOP2/VOP3 FMAC encodings. `git merge-tree` against `main` (`63bcca43`) is clean, and `emit/lower.rs` is `520,817` bytes (under the 512 KiB limit). One scope note worth making explicit in the title/summary: directed rounding modes (`Some(1..=3)`) still go through `fmad + narrow16`, so RTZ still double-rounds cases like `(0x4000, 0x3c00, 0x8001)` (`2 - 2^-24` rounds to `2.0` in f32 -> `0x4000` instead of `0x3fff`), and modes 1 and 2 narrow as nearest-even. Verified locally on Windows 11 x64 (RTX 5090 + WARP + `dxil.dll`, `64c62d5e`): - `cargo run --locked -p xtask -- policy`: passed (`191 files ok`) - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default and `--all-features`): clean - `packed_fused_f16_rounds_each_half_once` and `sixteen_bit_floats_match_the_hardware` (`-- --test-threads=1`, RTX 5090 and `VICTORY_WARP=1`): 1 passed, 0 failed each on both
github-bot changed title from Shaders: round packed half-precision fused multiply-add once to Shaders: round packed FP16 fused multiply-add once in nearest-even mode 2026-11-26 11:45:39 +00:00
Author
Owner

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

Thanks, @⁠fuzeofficlal. I clarified the title and summary. The single-rounding path is for round-to-nearest-even, and modes 1–3 continue through the existing f32 FMad and narrowing path. Your toward-zero example remains outside this PR's scope. Review note

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 11:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/2884#issuecomment-7191031339) Thanks, @⁠fuzeofficlal. I clarified the title and summary. The single-rounding path is for round-to-nearest-even, and modes 1–3 continue through the existing f32 FMad and narrowing path. Your toward-zero example remains outside this PR's scope. [Review note](https://git.gtahub.st/github/Marvisak--VIctory/pulls/2884)
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/2884/head:github-bot/gh-2884'
git switch 'github-bot/gh-2884'

Merge

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