Skip to content

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

Open
andrewpozdnakov7-jpg wants to merge 1 commit into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/packed-fma-nearest-even
Open

andrewpozdnakov7-jpg wants to merge 1 commit into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/packed-fma-nearest-even

Conversation

@andrewpozdnakov7-jpg

@andrewpozdnakov7-jpg andrewpozdnakov7-jpg commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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 <andrewpozdnakov7-jpg@users.noreply.github.com>.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Owner

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

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@andrewpozdnakov7-jpg andrewpozdnakov7-jpg changed the title Shaders: round packed half-precision fused multiply-add once Shaders: round packed FP16 fused multiply-add once in nearest-even mode Nov 26, 2026
@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants