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

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

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 23:55 UTC · original
andrewpozdnakov7-jpg:codex2/f16-add-rtz → main · +110 −12 · 3 files · 1 commits · open

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.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 23:55 UTC · [original](https://github.com/Marvisak/VIctory/pull/2917) `andrewpozdnakov7-jpg:codex2/f16-add-rtz` → `main` · +110 −12 · 3 files · 1 commits · open ### 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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:17:27 +00:00
Author
Owner

@⁠Kajanos on GitHub · 2026-11-26 00:07 UTC · original

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)

**@⁠Kajanos** on GitHub · 2026-11-26 00:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7178412468) **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)
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 00:53 UTC · original

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

**@⁠oni-swr** on GitHub · 2026-11-26 00:53 UTC · [original](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7179213646) 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).
Author
Owner

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

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

**@⁠Marvisak** on GitHub · 2026-11-26 04:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2917#issuecomment-7182640874) <!-- 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)_
This pull request can be merged automatically.
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/2917/head:github-bot/gh-2917'
git switch 'github-bot/gh-2917'

Merge

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