Shaders: round half add and subtract toward zero once #2915

Open
andrewpozdnakov7-jpg wants to merge 1 commit from upstream-pr/2917 into main
andrewpozdnakov7-jpg commented 2026-11-25 23:55:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T23:55:04Z.


Summary

Round v_add_f16, v_sub_f16, v_subrev_f16, and v_pk_add_f16 toward zero when the program sets MODE.FP_ROUND_16_64. For example, 4096 - 2^-14 rounds to 0x6c00 in f32. The correct f16 result toward zero is 0x6bff.

Reuse the compensated sum already used by v_fma_f16. When f32 rounded the sum away from zero, move one f32 step toward zero before the existing f16 conversion. An f16 boundary can only fall at an f32 point, so the step preserves the correct side for truncation. The previous emitted arithmetic is used in nearest-even mode. See AMD's RDNA 2 ISA, VOP2 and packed math.

Verification

  • The same shader fixture on 63bcca4 and the patch covers 44 instruction/input combinations on app-local WARP SM 6.6 with DXIL validation. Mismatches drop from 10 to 0. In the packed case ec006c00 + 04008400, the toward-zero result changes from ec006c00 to ebff6bff. The nearest-even control stays ec006c00.
  • The fixture checks both signs, plain and reverse subtraction, both packed halves, exact cancellation, signed zeros, subnormals, overflow, and infinities.
  • A standalone numerical check of 56,377,344 signed finite add/subtract pairs against exact f64 sums gives 1,445,676 baseline mismatches and zero for the corrected rounding.
  • WARP suites pass with 35 semantics tests (1 ignored), 35 VALU tests, and 33 lowerer tests. Strict shader Clippy passes in default and all-features configurations.

#2825 keeps its toward-zero arithmetic on the f32 path and currently filters the affected inputs from its native-path comparison. This change addresses that fallback.

Staged asset policy passed (192 files). The exact #2825 and #2891 heads merge textually cleanly. #2884 wraps the adjacent packed-f16 block. If both land, keep its FMA branch and use half_add_sub in the packed add fallback.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5501276018 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T23:55:04Z. --- ### Summary Round `v_add_f16`, `v_sub_f16`, `v_subrev_f16`, and `v_pk_add_f16` toward zero when the program sets `MODE.FP_ROUND_16_64`. For example, `4096 - 2^-14` rounds to `0x6c00` in f32. The correct f16 result toward zero is `0x6bff`. Reuse the compensated sum already used by `v_fma_f16`. When f32 rounded the sum away from zero, move one f32 step toward zero before the existing f16 conversion. An f16 boundary can only fall at an f32 point, so the step preserves the correct side for truncation. The previous emitted arithmetic is used in nearest-even mode. See AMD's [RDNA 2 ISA, VOP2 and packed math](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf). ### Verification - The same shader fixture on `63bcca4` and the patch covers 44 instruction/input combinations on app-local WARP SM 6.6 with DXIL validation. Mismatches drop from 10 to 0. In the packed case `ec006c00 + 04008400`, the toward-zero result changes from `ec006c00` to `ebff6bff`. The nearest-even control stays `ec006c00`. - The fixture checks both signs, plain and reverse subtraction, both packed halves, exact cancellation, signed zeros, subnormals, overflow, and infinities. - A standalone numerical check of 56,377,344 signed finite add/subtract pairs against exact f64 sums gives 1,445,676 baseline mismatches and zero for the corrected rounding. - WARP suites pass with 35 semantics tests (1 ignored), 35 VALU tests, and 33 lowerer tests. Strict shader Clippy passes in default and all-features configurations. #2825 keeps its toward-zero arithmetic on the f32 path and currently filters the affected inputs from its native-path comparison. This change addresses that fallback. Staged asset policy passed (192 files). The exact #2825 and #2891 heads merge textually cleanly. #2884 wraps the adjacent packed-f16 block. If both land, keep its FMA branch and use half_add_sub in the packed add fallback.
fuzeofficlal commented 2026-11-26 00:06:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T00:06:15Z.

Review: COMMENTED


Verified head 1d1b066f cleanly on origin/main (63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0 dxil.dll).

Code & Numeric Review

  • Using Knuth TwoSum (f32_sum_error in the new emit/lower/half_round.rs, also shared by fma_f16) to step sum_bits down by 1 ULP toward zero when (error ^ signbit(sum)) < 0.0 (pred::OLT) cleanly eliminates double-rounding under mode16 == Some(3) (RTZ) while preserving exact ±0.0, signed Inf, and NaN (where OLT(NaN, 0.0) is false).
  • File size & #2915 compatibility: Net shrinks crates/victory-shader/src/emit/lower.rs by 226 B (520,563 B -> 520,337 B, 3,951 B under the 512 KiB cap) and composes cleanly with #2915's float_result omod change without merge conflict.

Local Verification

  • cargo test --locked -p victory-shader --lib -- emit::lower::tests emit::semantics_tests on RTX 5090 + WARP + dxil.dll: 68/68 passed (1 pre-existing ignored), including f16_add_sub_round_to_zero_once.
  • cargo clippy --locked -p victory-shader --tests -- -D warnings (default and --all-features) & cargo xtask policy (192 files ok): clean.

Looks clean and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066746226 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#pullrequestreview-6066746226). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:06:15Z. **Review:** COMMENTED --- Verified head `1d1b066f` cleanly on `origin/main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0 `dxil.dll`). ### Code & Numeric Review - Using Knuth TwoSum (`f32_sum_error` in the new `emit/lower/half_round.rs`, also shared by `fma_f16`) to step `sum_bits` down by 1 ULP toward zero when `(error ^ signbit(sum)) < 0.0` (`pred::OLT`) cleanly eliminates double-rounding under `mode16 == Some(3)` (RTZ) while preserving exact `±0.0`, signed `Inf`, and `NaN` (where `OLT(NaN, 0.0)` is false). - **File size & `#2915` compatibility**: Net shrinks `crates/victory-shader/src/emit/lower.rs` by **226 B** (`520,563 B` -> `520,337 B`, `3,951 B` under the 512 KiB cap) and composes cleanly with `#2915`'s `float_result` `omod` change without merge conflict. ### Local Verification - `cargo test --locked -p victory-shader --lib -- emit::lower::tests emit::semantics_tests` on RTX 5090 + WARP + `dxil.dll`: **68/68 passed** (1 pre-existing ignored), including `f16_add_sub_round_to_zero_once`. - `cargo clippy --locked -p victory-shader --tests -- -D warnings` (default and `--all-features`) & `cargo xtask policy` (`192 files ok`): clean. Looks clean and ready to merge.
kotekompotek commented 2026-11-26 00:26:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-26T00:26:32Z.

Review: COMMENTED


Verified: the extracted f32_sum_error is the verbatim Fast2Sum tail from the FMA path (no behavior change there), and applying it to f16 add/sub is the right fix — a product of halves is exact in f32 but a sum isn't always representable as f32-rounded, so the error term is needed before the single round toward zero. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066897182 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#pullrequestreview-6066897182). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T00:26:32Z. **Review:** COMMENTED --- Verified: the extracted `f32_sum_error` is the verbatim Fast2Sum tail from the FMA path (no behavior change there), and applying it to f16 add/sub is the right fix — a product of halves is exact in f32 but a sum isn't always representable as f32-rounded, so the error term is needed before the single round toward zero. LGTM.
Kajanos commented 2026-11-26 01:03:56 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Kajanos on 2026-11-26T00:07:02Z.


Windows 11 / NVIDIA test run, head 1d1b066f74 merged onto main 63bcca43c6.

Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (dxil.dll from the SDK).

  • cargo test --locked -p victory-shader: 303 passed, 0 failed, 3 ignored (main: 302 passed, so the added test passes).

Not run: the retail title, clippy, and other crates.

AI-assisted (Claude Code)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7178412468 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7178412468). Originally posted by [Kajanos](https://github.com/Kajanos) on 2026-11-26T00:07:02Z. --- **Windows 11 / NVIDIA test run**, head `1d1b066f74` merged onto `main` `63bcca43c6`. Setup: Windows 11 Home build 26300, RTX 5080, NVIDIA 621.40, rustc 1.98.1 MSVC, VS 2022 Build Tools, Windows SDK 10.0.26100 (`dxil.dll` from the SDK). - `cargo test --locked -p victory-shader`: 303 passed, 0 failed, 3 ignored (`main`: 302 passed, so the added test passes). Not run: the retail title, clippy, and other crates. AI-assisted (Claude Code)
oni-swr commented 2026-11-26 01:04:17 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-26T00:53:16Z.


Independent Windows WARP verification on PR head 1d1b066f74ae81a2de74df67e89429f9c3537d4f against main 63bcca43c6a00a758510398fa43096ce7e4f7df0.

Synthetic fixture Main mismatches PR head mismatches
Exact-integer reference (1,560 cases) 448 0
PR regression fixture (44 cases) 10 0

All 458 baseline mismatches were in round-toward-zero outputs. Every nearest-even control matched. Both revisions ran with required DXIL validation and confirmed the Microsoft Basic Render Driver software adapter at shader model 6.6. The harness blocked physical adapter fallback.

Verified with Codex (gpt-6.2-sol).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7179213646 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7179213646). Originally posted by [oni-swr](https://github.com/oni-swr) on 2026-11-26T00:53:16Z. --- Independent Windows WARP verification on PR head `1d1b066f74ae81a2de74df67e89429f9c3537d4f` against main `63bcca43c6a00a758510398fa43096ce7e4f7df0`. | Synthetic fixture | Main mismatches | PR head mismatches | |---|---:|---:| | Exact-integer reference (1,560 cases) | 448 | 0 | | PR regression fixture (44 cases) | 10 | 0 | All 458 baseline mismatches were in round-toward-zero outputs. Every nearest-even control matched. Both revisions ran with required DXIL validation and confirmed the Microsoft Basic Render Driver software adapter at shader model 6.6. The harness blocked physical adapter fallback. Verified with Codex (gpt-6.2-sol).
Marvisak commented 2026-11-26 05:04:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:29:29Z.


Automated triage for the maintainers.

Merge candidate. This makes v_add_f16, v_sub_f16, v_subrev_f16 and v_pk_add_f16 round toward zero correctly when MODE.FP_ROUND_16_64 is 3, stepping the f32 sum back one ulp before narrow16 truncates when it rounded away from zero (half_add_sub, f32_sum_error in emit/lower/half_round.rs). The step is sound since every f16 boundary is on the f32 grid and the TwoSum error of a sum of halves is a normal f32; the v_fma_f16 refactor is equivalent. f16_add_sub_round_to_zero_once compares against exact f64 sums with a nearest-even control. If #2884 lands too, the packed-add fallback needs the merge note from the description.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182640874 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7182640874). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:29:29Z. --- <!-- victory-triage v1 head=1d1b066f74ae81a2de74df67e89429f9c3537d4f verdict=merge-candidate at=2026-11-26T04:29:24Z --> Automated triage for the maintainers. **Merge candidate.** This makes `v_add_f16`, `v_sub_f16`, `v_subrev_f16` and `v_pk_add_f16` round toward zero correctly when MODE.FP_ROUND_16_64 is 3, stepping the f32 sum back one ulp before `narrow16` truncates when it rounded away from zero (`half_add_sub`, `f32_sum_error` in `emit/lower/half_round.rs`). The step is sound since every f16 boundary is on the f32 grid and the TwoSum error of a sum of halves is a normal f32; the `v_fma_f16` refactor is equivalent. `f16_add_sub_round_to_zero_once` compares against exact f64 sums with a nearest-even control. If #2884 lands too, the packed-add fallback needs the merge note from the description. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 05:35:21 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T05:35:21Z.

Review: APPROVED


Tested locally merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP):

  • Diff review: Extracting half_add_sub and f32_sum_error into emit/lower/half_round.rs corrects double-rounding on v_add_f16 / v_sub_f16 / v_subrev_f16 / v_pk_add_f16 when rounding toward zero while keeping lower.rs within the file-size policy limit.
  • cargo test -p victory-shader f16_add_sub_round_to_zero_once -- --test-threads=1 (RTX 5090 & WARP): 1 passed, 0 failed on both.
  • cargo test -p victory-shader f16_arithmetic_rounds_once -- --test-threads=1 (RTX 5090 & WARP): 1 passed, 0 failed on both.
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

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

<!-- greyfinch-sync:Marvisak/VIctory:review:6069041460 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#pullrequestreview-6069041460). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T05:35:21Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: Extracting `half_add_sub` and `f32_sum_error` into `emit/lower/half_round.rs` corrects double-rounding on `v_add_f16` / `v_sub_f16` / `v_subrev_f16` / `v_pk_add_f16` when rounding toward zero while keeping `lower.rs` within the file-size policy limit. - `cargo test -p victory-shader f16_add_sub_round_to_zero_once -- --test-threads=1` (RTX 5090 & WARP): 1 passed, 0 failed on both. - `cargo test -p victory-shader f16_arithmetic_rounds_once -- --test-threads=1` (RTX 5090 & WARP): 1 passed, 0 failed on both. - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 192 files ok. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
andrewpozdnakov7-jpg commented 2026-11-27 16:44:41 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T16:44:41Z.


I tested #2917 with #2623 and #3124 on 63bcca4. Combined branch 7a43e7a keeps the original histories and shares the TwoSum helper between FMA and add/subtract.

The extended fixture seeds a VGPR with v_add_f16_sdwa WORD_0, UNUSED_SEXT, then runs clamped 4096 * 1 + seed into its low half. With both input orders for -4096 + 2^-14, the seed rounds to -4096 in RNE and -4094 in RTZ. The full outputs are ffff0000 and ffff3c00, respectively. Restoring only the old scalar f32 add returns ffff0000 in RTZ on the same fixture.

On app-local WARP 1.0.22 with DXIL validation, all 12 cases in the expanded fixture passed. All 43 VALU tests and 35 semantics tests passed, with one semantics test ignored. The SDWA encoding test, strict shader Clippy and asset policy also passed.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7223619480 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7223619480). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-27T16:44:41Z. --- I tested #2917 with #2623 and #3124 on `63bcca4`. [Combined branch `7a43e7a`](https://github.com/andrewpozdnakov7-jpg/VIctory/tree/review/fp16-add-fma-and-sdwa) keeps the original histories and shares the TwoSum helper between FMA and add/subtract. The extended fixture seeds a VGPR with `v_add_f16_sdwa WORD_0, UNUSED_SEXT`, then runs clamped `4096 * 1 + seed` into its low half. With both input orders for `-4096 + 2^-14`, the seed rounds to -4096 in RNE and -4094 in RTZ. The full outputs are `ffff0000` and `ffff3c00`, respectively. Restoring only the old scalar f32 add returns `ffff0000` in RTZ on the same fixture. On app-local WARP 1.0.22 with DXIL validation, all 12 cases in the expanded fixture passed. All 43 VALU tests and 35 semantics tests passed, with one semantics test ignored. The SDWA encoding test, strict shader Clippy and asset policy also passed.
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/2917:upstream-pr/2917'
git switch 'upstream-pr/2917'

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/2917'
git switch 'upstream-pr/2917'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2917'
git switch 'upstream-pr/2917'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2917'
git switch 'main'
git merge --squash 'upstream-pr/2917'
git switch 'main'
git merge --ff-only 'upstream-pr/2917'
git switch 'main'
git merge 'upstream-pr/2917'
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
Marvisak/VIctory!2915
No description provided.