Shaders: saturate clamped 24-bit multiply and multiply-add #2924

Open
andrewpozdnakov7-jpg wants to merge 1 commit from upstream-pr/2926 into main
andrewpozdnakov7-jpg commented 2026-11-26 00:16:15 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-26T00:16:15Z.


Summary

Lower the clamp forms of v_mul_i32_i24, v_mul_u32_u24, v_mad_i32_i24, and v_mad_u32_u24. Saturate the complete product or multiply-add to the signed or unsigned 32-bit range. The unclamped forms still wrap.

Only the clamped path computes a high word. Splitting each 24-bit operand into 16 low bits and 8 high bits keeps each partial product and intermediate sum in i32. For MAD, the carry and sign extension from the addend complete the high word before saturation. This needs only 32-bit DXIL integer operations. The forms are listed with clamp in the GFX1030 assembly reference, and integer saturation is described in section 6.5 of the RDNA 2 ISA.

Verification

  • On 63bcca4, the same fixture is refused at v_mul_i32_i24 with integer output modifiers. On the patch, all 96 signed/unsigned MUL/MAD input and mode combinations match an exact wide-integer reference on app-local WARP SM 6.6 with DXIL validation.
  • The controls cover high operand bits, positive and negative saturation, a MAD addend bringing a wide signed product back into range, unsigned carry out, and unclamped wrapping.
  • A separate arithmetic cross-check of 1,061,572 edge and fixed-seed pairs matched the exact high word and saturated result.
  • 36 VALU, 34 semantics (1 ignored), and 33 lowerer tests passed on WARP. Strict shader Clippy in both feature configurations and the staged asset policy (192 files) passed.
<!-- greyfinch-sync:Marvisak/VIctory:issue:6616518924 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2926). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-26T00:16:15Z. --- ### Summary Lower the `clamp` forms of `v_mul_i32_i24`, `v_mul_u32_u24`, `v_mad_i32_i24`, and `v_mad_u32_u24`. Saturate the complete product or multiply-add to the signed or unsigned 32-bit range. The unclamped forms still wrap. Only the clamped path computes a high word. Splitting each 24-bit operand into 16 low bits and 8 high bits keeps each partial product and intermediate sum in i32. For MAD, the carry and sign extension from the addend complete the high word before saturation. This needs only 32-bit DXIL integer operations. The forms are listed with `clamp` in the [GFX1030 assembly reference](https://rocmdocs.amd.com/projects/llvm-project/en/latest/LLVM/llvm/html/AMDGPU/AMDGPUAsmGFX1030.html), and integer saturation is described in section 6.5 of the [RDNA 2 ISA](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf). ### Verification - On `63bcca4`, the same fixture is refused at `v_mul_i32_i24` with `integer output modifiers`. On the patch, all 96 signed/unsigned MUL/MAD input and mode combinations match an exact wide-integer reference on app-local WARP SM 6.6 with DXIL validation. - The controls cover high operand bits, positive and negative saturation, a MAD addend bringing a wide signed product back into range, unsigned carry out, and unclamped wrapping. - A separate arithmetic cross-check of 1,061,572 edge and fixed-seed pairs matched the exact high word and saturated result. - 36 VALU, 34 semantics (1 ignored), and 33 lowerer tests passed on WARP. Strict shader Clippy in both feature configurations and the staged asset policy (192 files) passed.
fuzeofficlal commented 2026-11-26 01:03:48 +00:00 (Migrated from github.com)

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

Review: COMMENTED


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

Numeric & File-Size Review

  • Exact 48-bit overflow-free arithmetic in pure 32-bit DXIL (emit/lower/mul24.rs):
    • 16+8 splitting keeps every intermediate safely within 32-bit range (low_product <= 65535^2 < u32::MAX, middle in [-16,776,960, 33,488,384], high in [-16,513, 65,535] even after MAD unsigned carry and signed x >> 31 extension).
    • Saturation checks are exact for both unsigned (high == 0, else u32::MAX) and signed (high == (sum >>_arith 31), else i32::MIN / i32::MAX by high <_s 0), properly handling intermediate 48-bit products pulled back into 32-bit range by MAD.
  • File size: Extracting emit/lower/mul24.rs net shrinks emit/lower.rs to 519,362 B (4,926 B below the 512 KiB policy limit).

Local Verification (VICTORY_D3D12_DEBUG=1, dxil.dll)

  • RTX 5090 & WARP: mul_and_mad_i24 + emit::lower::tests + emit::semantics_tests: 68/68 passed (1 pre-existing ignored) on both adapters.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features) & cargo xtask policy (192 files ok): clean.

Looks clean and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066882728 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2926#pullrequestreview-6066882728). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-26T00:24:29Z. **Review:** COMMENTED --- Verified head `3b60f1bd` cleanly on `origin/main` (`63bcca43`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + `VICTORY_D3D12_DEBUG=1` + Windows SDK 10.0.26100.0 `dxil.dll`). ### Numeric & File-Size Review - **Exact 48-bit overflow-free arithmetic in pure 32-bit DXIL (`emit/lower/mul24.rs`)**: - 16+8 splitting keeps every intermediate safely within 32-bit range (`low_product <= 65535^2 < u32::MAX`, `middle in [-16,776,960, 33,488,384]`, `high in [-16,513, 65,535]` even after MAD unsigned carry and signed `x >> 31` extension). - Saturation checks are exact for both unsigned (`high == 0`, else `u32::MAX`) and signed (`high == (sum >>_arith 31)`, else `i32::MIN` / `i32::MAX` by `high <_s 0`), properly handling intermediate 48-bit products pulled back into 32-bit range by MAD. - **File size**: Extracting `emit/lower/mul24.rs` net shrinks `emit/lower.rs` to **`519,362 B`** (`4,926 B` below the 512 KiB policy limit). ### Local Verification (`VICTORY_D3D12_DEBUG=1`, `dxil.dll`) - **RTX 5090 & WARP**: `mul_and_mad_i24` + `emit::lower::tests` + `emit::semantics_tests`: **68/68 passed** (1 pre-existing ignored) on both adapters. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`) & `cargo xtask policy` (`192 files ok`): clean. Looks clean and ready to merge.
kotekompotek commented 2026-11-26 01:03:48 +00:00 (Migrated from github.com)

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

Review: COMMENTED


Scoped review: moving the four *_u24/i24 ops into a dedicated module that saturates at the 24-bit boundary is the right shape — the old path sign/zero-extended and multiplied without the console's saturate behavior. The shared shl/shr sign-extension is preserved. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066897400 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2926#pullrequestreview-6066897400). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-26T00:26:34Z. **Review:** COMMENTED --- Scoped review: moving the four *_u24/i24 ops into a dedicated module that saturates at the 24-bit boundary is the right shape — the old path sign/zero-extended and multiplied without the console's saturate behavior. The shared shl/shr sign-extension is preserved. LGTM.
Marvisak commented 2026-11-26 05:04:08 +00:00 (Migrated from github.com)

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


Automated triage for the maintainers.

Merge candidate. Adds the clamp forms of v_mul_{i,u}32_{i,u}24 and v_mad_{i,u}32_{i,u}24, which main refused in write_i, in emit/lower/mul24.rs, leaving the unclamped forms unchanged. clamp_mul24 checks out: the 16/8-bit split keeps partial products within 32 bits, middle_hi floors correctly for signed forms, the MAD carry and addend sign extension give the exact high word, and saturation is right. The test compares against an exact i64 reference, and the RDNA2 ISA integer-clamp rule is cited.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182561689 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2926#issuecomment-7182561689). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:24:05Z. --- <!-- victory-triage v1 head=3b60f1bd966bcb0f6908f294ade763e832a7e27d verdict=merge-candidate at=2026-11-26T04:23:56Z --> Automated triage for the maintainers. **Merge candidate.** Adds the `clamp` forms of `v_mul_{i,u}32_{i,u}24` and `v_mad_{i,u}32_{i,u}24`, which main refused in `write_i`, in `emit/lower/mul24.rs`, leaving the unclamped forms unchanged. `clamp_mul24` checks out: the 16/8-bit split keeps partial products within 32 bits, `middle_hi` floors correctly for signed forms, the MAD carry and addend sign extension give the exact high word, and saturation is right. The test compares against an exact i64 reference, and the RDNA2 ISA integer-clamp rule is cited. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 06:02:50 +00:00 (Migrated from github.com)

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

Review: APPROVED


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

  • Diff review: Extracting 24-bit multiply / multiply-add lowering into emit/lower/mul24.rs and saturating clamped v_mul_{i32_i24,u32_u24} and v_mad_{i32_i24,u32_u24} on the full wide result matches RDNA2 hardware semantics and keeps lower.rs within the file-size policy budget.
  • cargo test -p victory-shader mul_and_mad_i24 -- --test-threads=1 (RTX 5090): 1 passed, 0 failed.
  • VICTORY_WARP=1 cargo test -p victory-shader mul_and_mad_i24 -- --test-threads=1 (WARP): 1 passed, 0 failed.
  • 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:6069011626 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2926#pullrequestreview-6069011626). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T05:31:58Z. **Review:** APPROVED --- Tested locally merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP): - Diff review: Extracting 24-bit multiply / multiply-add lowering into `emit/lower/mul24.rs` and saturating clamped `v_mul_{i32_i24,u32_u24}` and `v_mad_{i32_i24,u32_u24}` on the full wide result matches RDNA2 hardware semantics and keeps `lower.rs` within the file-size policy budget. - `cargo test -p victory-shader mul_and_mad_i24 -- --test-threads=1` (RTX 5090): 1 passed, 0 failed. - `VICTORY_WARP=1 cargo test -p victory-shader mul_and_mad_i24 -- --test-threads=1` (WARP): 1 passed, 0 failed. - `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).
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/2926:upstream-pr/2926'
git switch 'upstream-pr/2926'

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