Shaders: round packed FP16 fused multiply-add once in nearest-even mode #2882
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2884"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T23:02:31Z.
The f32 fallback for
V_PK_FMA_F16andV_PK_FMAC_F16could 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
842b0cband 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 produce0x3e043e02with0x3e053e01expected. 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
3c81083and 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]>.Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:34:39Z.
Review: COMMENTED
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.
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:10:36Z.
Reviewed and verified
64c62d5eon Windows 11 (RTX 5090 + WARP +dxil.dll) againstmain(842b0cb):v_pk_fma_f16/v_pk_fmac_f16throughfma_f16(TwoSum single rounding) under RNE (None | Some(0)) while keeping the directed-roundingfmad + narrow16path forSome(1..=3)eliminates double-rounding while preservingop_sel, per-halfneg, andclamp(and complements#2825's native-16-bit path on software/WARP hosts).semantics_tests35/35 passed (includingpacked_fused_f16_rounds_each_half_onceacross all 20 cases with 0 discrepancies on RTX 5090, WARP, and the reference model),emit::lower::tests33/33 passed,cargo clippy --locked -p victory-shader --all-targets -- -D warnings0 warnings,cargo xtask policy(191 files ok;emit/lower.rs=520,817bytes).Looks clean and ready to merge.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:28:20Z.
Automated triage for the maintainers.
Merge candidate.
v_pk_fma_f16andv_pk_fmac_f16in nearest-even mode now go through the single-roundingfma_f16helper the scalarv_fma_f16already uses (packed16inemit/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_oncechecks against an exact f64 fused result, with midpoints, a subnormal tie, op_sel/neg per half, clamp, and both encodings ofv_pk_fmac_f16.CI: passing.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T09:29:35Z.
Review: APPROVED
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).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T10:32:36Z.
Review: COMMENTED
Routing
v_pk_fma_f16andv_pk_fmac_f16throughfma_f16in nearest-even mode (matches!(self.mode16, None | Some(0))) eliminates double rounding on midpoint and subnormal ties while preservingop_sel, per-halfneg,clamp, and both VOP2/VOP3 FMAC encodings.git merge-treeagainstmain(63bcca43) is clean, andemit/lower.rsis520,817bytes (under the 512 KiB limit).One scope note worth making explicit in the title/summary: directed rounding modes (
Some(1..=3)) still go throughfmad + narrow16, so RTZ still double-rounds cases like(0x4000, 0x3c00, 0x8001)(2 - 2^-24rounds to2.0in f32 ->0x4000instead of0x3fff), 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): cleanpacked_fused_f16_rounds_each_half_onceandsixteen_bit_floats_match_the_hardware(-- --test-threads=1, RTX 5090 andVICTORY_WARP=1): 1 passed, 0 failed each on both[PR #2884] Shaders: round packed half-precision fused multiply-add onceto [PR #2884] Shaders: round packed FP16 fused multiply-add once in nearest-even modeImported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T11:40:45Z.
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
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.