Shaders: v_mul_legacy_f32 treats denormal operands as zero #682

Manually merged
github merged 3 commits from gh-pr/682/head into gh-pr/682/base 2026-11-26 04:12:52 +00:00
Owner

@⁠senchsus on GitHub · 2026-11-24 06:45 UTC · original
senchsus:shaders/legacy-mul-denormals → main · +48 −5 · 2 files · 3 commits · merged 2026-11-25 21:49 UTC

RDNA2 treats denormal operands of the legacy multiplies (v_mul_legacy_f32, v_mac_legacy_f32, v_mad_legacy_f32) as zero, so 0 × anything = 0 also holds for them. The lowering only compared each operand with 0.0, so a denormal times inf/NaN gave NaN instead of 0, and the result depended on the host's denormal mode.

legacy_mul now checks each operand's exponent bits (bits & 0x7F800000 == 0), which covers ±0 and denormals and does not depend on how the host handles denormals. Follow-up to the lower-priority item in #2.

Tests

  • New v_mul_legacy_f32_treats_denormals_as_zero in valu_tests.rs: zero/denormal × inf/NaN/large gives +0, the smallest normal is not flushed, and ordinary products still match.
  • cargo test -p victory-shader, clippy and cargo check --tests --target x86_64-pc-windows-msvc pass; cargo xtask policy passes.
  • Not yet run on a GPU or WARP. The new test only runs on Windows, and I couldn't run it there. It would be great if someone could run it (@⁠MiguelMatMar offered GPU testing in #2).
**@⁠senchsus** on GitHub · 2026-11-24 06:45 UTC · [original](https://github.com/Marvisak/VIctory/pull/682) `senchsus:shaders/legacy-mul-denormals` → `main` · +48 −5 · 2 files · 3 commits · merged 2026-11-25 21:49 UTC RDNA2 treats denormal operands of the legacy multiplies (`v_mul_legacy_f32`, `v_mac_legacy_f32`, `v_mad_legacy_f32`) as zero, so `0 × anything = 0` also holds for them. The lowering only compared each operand with `0.0`, so a denormal times inf/NaN gave NaN instead of 0, and the result depended on the host's denormal mode. `legacy_mul` now checks each operand's exponent bits (`bits & 0x7F800000 == 0`), which covers ±0 and denormals and does not depend on how the host handles denormals. Follow-up to the lower-priority item in #2. ### Tests - New `v_mul_legacy_f32_treats_denormals_as_zero` in `valu_tests.rs`: zero/denormal × inf/NaN/large gives +0, the smallest normal is not flushed, and ordinary products still match. - `cargo test -p victory-shader`, `clippy` and `cargo check --tests --target x86_64-pc-windows-msvc` pass; `cargo xtask policy` passes. - **Not yet run on a GPU or WARP.** The new test only runs on Windows, and I couldn't run it there. It would be great if someone could run it (@⁠MiguelMatMar offered GPU testing in #2).
RDNA2 flushes denormal operands of the legacy multiplies to zero, so
0 x anything = 0 also holds for them. The lowering only tested for an exact
float zero, whose result also depends on the host's denormal mode. Test the
exponent bits instead. Follow-up to #2.
# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:12:51 +00:00
github manually merged commit 32b4f8e6ae into gh-pr/682/base 2026-11-26 04:12:52 +00:00
Author
Owner

@⁠lameloball2813-ui on GitHub · 2026-11-24 07:40 UTC · original

Ran it on a GPU and WARP: Windows 11, RTX 5060 Ti (the only adapter), NVIDIA driver 32.0.16.1088, Rust 1.98.1, debug.

Code v_mul_legacy_f32_treats_denormals_as_zero
this PR 3d73ea9 passes (hardware + WARP; neither was skipped)
main d2a4bae with only this PR's valu_tests.rs also passes

All 8 valu_tests on 3d73ea9 pass with --test-threads=1.

So on this NVIDIA card and on WARP the old lowering already gives +0 for denormal × inf/NaN. Most likely those hosts flush the denormal operand, so the old == 0.0 compare is true, which fits the PR's note that the old result "depended on the host's denormal mode". The fix makes it independent of that, but on these two hosts the test can't tell the old code from the new. A host that keeps fp32 denormals (an AMD GPU, maybe) would be needed to see it fail on main; I don't have one.

**@⁠lameloball2813-ui** on GitHub · 2026-11-24 07:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7125242689) Ran it on a GPU and WARP: Windows 11, **RTX 5060 Ti** (the only adapter), NVIDIA driver 32.0.16.1088, Rust 1.98.1, debug. | Code | `v_mul_legacy_f32_treats_denormals_as_zero` | |---|---| | this PR `3d73ea9` | passes (hardware + WARP; neither was skipped) | | `main` `d2a4bae` with only this PR's `valu_tests.rs` | **also passes** | All 8 `valu_tests` on `3d73ea9` pass with `--test-threads=1`. So on this NVIDIA card and on WARP the old lowering already gives +0 for denormal × inf/NaN. Most likely those hosts flush the denormal operand, so the old `== 0.0` compare is true, which fits the PR's note that the old result "depended on the host's denormal mode". The fix makes it independent of that, but on these two hosts the test can't tell the old code from the new. A host that keeps fp32 denormals (an AMD GPU, maybe) would be needed to see it fail on `main`; I don't have one.
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 10:16 UTC · original

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, v_mul_legacy_f32_treats_denormals_as_zero passes on both AMD GPUs.
  • Negative control: it also passes without the fix, on both AMD GPUs and on the WARP pass of run_cases. Those hosts already flush the denormal operands (D3D's default f32 denormal mode), so main's lowering gives +0 there anyway.
  • So the test cannot catch a regression on these hosts. The fix only matters on a host that keeps f32 denormals. A case only such a host would fail, or a check that the DXIL now flushes explicitly, would make the test useful.
**@⁠slint6614-dev** on GitHub · 2026-11-24 10:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7128562914) AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of `run_cases` (local adapter-selection patch, not part of this PR); `cargo test -p victory-shader --release valu_tests` at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept. - With this PR, `v_mul_legacy_f32_treats_denormals_as_zero` passes on both AMD GPUs. - Negative control: it **also passes without the fix**, on both AMD GPUs and on the WARP pass of `run_cases`. Those hosts already flush the denormal operands (D3D's default f32 denormal mode), so main's lowering gives +0 there anyway. - So the test cannot catch a regression on these hosts. The fix only matters on a host that keeps f32 denormals. A case only such a host would fail, or a check that the DXIL now flushes explicitly, would make the test useful.
Author
Owner

@⁠ErRusso on GitHub · 2026-11-24 11:19 UTC · original

Audit: ✅ Looks good — verified & tested

Reviewed this PR as part of an audit of mergeable PRs.

  • Scope: crates/victory-shader/src/emit/lower.rs + valu_tests.rs (+48/-5). v_mul_legacy_f32 now treats denormal operands as zero via bit-level exponent check (0x7F80_0000 mask) instead of a float compare, whose result would depend on the host's denormal mode. Matches RDNA2 semantics.
  • Merges cleanly into main.
  • Tests: cargo test -p victory-shader -- emit::lower:: → 5/5 passed; the new GPU diff test v_mul_legacy_f32_treats_denormals_as_zero compiles cleanly (needs a Windows/D3D12 host to execute — this environment is Linux).
  • Test coverage is thorough: zero×∞, -0×NaN, NaN×0, denorm×∞, NaN×neg-denorm, denorm×2^100, smallest-normal×2, and ordinary products — all asserted as exact bit patterns.

No issues found. The exponent-bit probe is the right approach for denormal-as-zero on RDNA2.

**@⁠ErRusso** on GitHub · 2026-11-24 11:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7129946106) ## Audit: ✅ Looks good — verified & tested Reviewed this PR as part of an audit of mergeable PRs. - **Scope:** `crates/victory-shader/src/emit/lower.rs` + `valu_tests.rs` (+48/-5). `v_mul_legacy_f32` now treats denormal operands as zero via bit-level exponent check (`0x7F80_0000` mask) instead of a float compare, whose result would depend on the host's denormal mode. Matches RDNA2 semantics. - **Merges cleanly** into `main`. - **Tests:** `cargo test -p victory-shader -- emit::lower::` → 5/5 passed; the new GPU diff test `v_mul_legacy_f32_treats_denormals_as_zero` compiles cleanly (needs a Windows/D3D12 host to execute — this environment is Linux). - **Test coverage is thorough:** zero×∞, -0×NaN, NaN×0, denorm×∞, NaN×neg-denorm, denorm×2^100, smallest-normal×2, and ordinary products — all asserted as exact bit patterns. No issues found. The exponent-bit probe is the right approach for denormal-as-zero on RDNA2.
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 14:14 UTC · original

A cross-check from AnyPS5's hardware-matched model; behaviour only, no code. It narrows when this PR's rule holds.

  • AnyPS5's legacy multiply counts an operand as zero for 0 × anything = 0 only when it is exactly ±0 ((bits & 0x7fffffff) == 0) after the operand has gone through the f32 input-denormal handling of the current mode (FP_DENORM).
  • Its legacy MAD/MAC model matched an RDNA1 card (gfx1010) on 45 504 oracle rows in all four f32 denormal modes, with the 144 edge combinations among them (boykopovar/AnyPS5#26098).
  • So in a mode that flushes f32 input denormals (the D3D default, and what every host above ran), a denormal operand is zero and both models give +0 for denormal × inf/NaN. That is why the negative controls on NVIDIA, WARP and AMD all passed without the fix.
  • In a mode that keeps f32 denormals, that model does not count the denormal as zero, and denormal × inf goes through the normal product (inf). This PR's exponent test returns +0 there.

This only matters if VIctory ever runs a program in a mode that keeps f32 denormals (s_setreg of MODE, or a title setting FLOAT_MODE). If it always assumes the flushing default, the exponent test is the host-independent way to get the hardware answer, and this PR is right for that. Derived from AnyPS5's sources and its measured rows; I have not run RDNA2 hardware in a preserving mode myself.

AI-assisted (Claude Code).

**@⁠upipibi** on GitHub · 2026-11-24 14:14 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7134047278) A cross-check from AnyPS5's hardware-matched model; behaviour only, no code. It narrows when this PR's rule holds. - AnyPS5's legacy multiply counts an operand as zero for `0 × anything = 0` only when it is exactly ±0 (`(bits & 0x7fffffff) == 0`) *after* the operand has gone through the f32 input-denormal handling of the current mode (`FP_DENORM`). - Its legacy MAD/MAC model matched an RDNA1 card (gfx1010) on 45 504 oracle rows in all four f32 denormal modes, with the 144 edge combinations among them (boykopovar/AnyPS5#26098). - So in a mode that **flushes** f32 input denormals (the D3D default, and what every host above ran), a denormal operand is zero and both models give +0 for denormal × inf/NaN. That is why the negative controls on NVIDIA, WARP and AMD all passed without the fix. - In a mode that **keeps** f32 denormals, that model does not count the denormal as zero, and denormal × inf goes through the normal product (inf). This PR's exponent test returns +0 there. This only matters if VIctory ever runs a program in a mode that keeps f32 denormals (`s_setreg` of MODE, or a title setting FLOAT_MODE). If it always assumes the flushing default, the exponent test is the host-independent way to get the hardware answer, and this PR is right for that. Derived from AnyPS5's sources and its measured rows; I have not run RDNA2 hardware in a preserving mode myself. AI-assisted (Claude Code).
Author
Owner

@⁠senchsus on GitHub · 2026-11-24 18:00 UTC · original

A cross-check from AnyPS5's hardware-matched model; behaviour only, no code. It narrows when this PR's rule holds.

  • AnyPS5's legacy multiply counts an operand as zero for 0 × anything = 0 only when it is exactly ±0 ((bits & 0x7fffffff) == 0) after the operand has gone through the f32 input-denormal handling of the current mode (FP_DENORM).
  • Its legacy MAD/MAC model matched an RDNA1 card (gfx1010) on 45 504 oracle rows in all four f32 denormal modes, with the 144 edge combinations among them (fix(shader): flush the v_mad_legacy_f32 and v_mac_legacy_f32 product by its tininess after rounding boykopovar/AnyPS5#26098).
  • So in a mode that flushes f32 input denormals (the D3D default, and what every host above ran), a denormal operand is zero and both models give +0 for denormal × inf/NaN. That is why the negative controls on NVIDIA, WARP and AMD all passed without the fix.
  • In a mode that keeps f32 denormals, that model does not count the denormal as zero, and denormal × inf goes through the normal product (inf). This PR's exponent test returns +0 there.

This only matters if VIctory ever runs a program in a mode that keeps f32 denormals (s_setreg of MODE, or a title setting FLOAT_MODE). If it always assumes the flushing default, the exponent test is the host-independent way to get the hardware answer, and this PR is right for that. Derived from AnyPS5's sources and its measured rows; I have not run RDNA2 hardware in a preserving mode myself.

AI-assisted (Claude Code).

Thanks, this is a fair point and I should have qualified the RDNA2 claim, which I didn't verify on hardware. VIctory doesn't set float mode anywhere (s_denorm_mode is a no-op in ir.rs), so the fix assumes the flushing default. I'll state that assumption in the PR description. If a title ever switches to preserving denormals, this rule would be wrong for denormal × inf, so it would need to read the mode first.

**@⁠senchsus** on GitHub · 2026-11-24 18:00 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7140011495) > A cross-check from AnyPS5's hardware-matched model; behaviour only, no code. It narrows when this PR's rule holds. > > * AnyPS5's legacy multiply counts an operand as zero for `0 × anything = 0` only when it is exactly ±0 (`(bits & 0x7fffffff) == 0`) _after_ the operand has gone through the f32 input-denormal handling of the current mode (`FP_DENORM`). > * Its legacy MAD/MAC model matched an RDNA1 card (gfx1010) on 45 504 oracle rows in all four f32 denormal modes, with the 144 edge combinations among them ([fix(shader): flush the v_mad_legacy_f32 and v_mac_legacy_f32 product by its tininess after rounding boykopovar/AnyPS5#26098](https://github.com/boykopovar/AnyPS5/pull/26098)). > * So in a mode that **flushes** f32 input denormals (the D3D default, and what every host above ran), a denormal operand is zero and both models give +0 for denormal × inf/NaN. That is why the negative controls on NVIDIA, WARP and AMD all passed without the fix. > * In a mode that **keeps** f32 denormals, that model does not count the denormal as zero, and denormal × inf goes through the normal product (inf). This PR's exponent test returns +0 there. > > This only matters if VIctory ever runs a program in a mode that keeps f32 denormals (`s_setreg` of MODE, or a title setting FLOAT_MODE). If it always assumes the flushing default, the exponent test is the host-independent way to get the hardware answer, and this PR is right for that. Derived from AnyPS5's sources and its measured rows; I have not run RDNA2 hardware in a preserving mode myself. > > AI-assisted (Claude Code). Thanks, this is a fair point and I should have qualified the RDNA2 claim, which I didn't verify on hardware. VIctory doesn't set float mode anywhere (s_denorm_mode is a no-op in ir.rs), so the fix assumes the flushing default. I'll state that assumption in the PR description. If a title ever switches to preserving denormals, this rule would be wrong for denormal × inf, so it would need to read the mode first.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:34 UTC · original · edited 2026-11-25 09:16 UTC

Automated triage for the maintainers.

Merge candidate. The branch now merges cleanly. legacy_mul in crates/victory-shader/src/emit/lower.rs decides "operand is zero" on the exponent bits instead of a float == 0.0. That covers ±0 and f32 denormals, and the result no longer depends on the host's denormal mode. The change reaches v_mul_legacy_f32, v_mac_legacy_f32 and v_mad_legacy_f32. It matches main's existing convention that f32 denormals are read as zero (ftz, max_min), and VIctory does not model a mode that keeps denormals (s_denorm_mode is a no-op in ir.rs). The author acknowledged that limit after the cross-check in the thread. Recommending a look, not approving.

Notes (not blocking):

  • The new v_mul_legacy_f32_treats_denormals_as_zero test cannot tell old code from new on the hosts reported in this thread (NVIDIA, AMD RDNA2/RDNA4, WARP), because they all flush denormals by default. It documents the intended result but is not a regression guard there.
  • The bit test could reuse the existing ftz helper (e.g. compare ftz(a) with 0) instead of repeating the mask.
  • The same legacy_mul change is also part of the larger bundle in #1330; this PR is the focused version.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7148654554) · edited 2026-11-25 09:16 UTC <!-- victory-triage v1 head=32b4f8e6ae2798109843d27141fa5bab870ad88f verdict=merge-candidate at=2026-11-25T09:16:15Z --> Automated triage for the maintainers. **Merge candidate.** The branch now merges cleanly. `legacy_mul` in `crates/victory-shader/src/emit/lower.rs` decides "operand is zero" on the exponent bits instead of a float `== 0.0`. That covers ±0 and f32 denormals, and the result no longer depends on the host's denormal mode. The change reaches `v_mul_legacy_f32`, `v_mac_legacy_f32` and `v_mad_legacy_f32`. It matches main's existing convention that f32 denormals are read as zero (`ftz`, `max_min`), and VIctory does not model a mode that keeps denormals (`s_denorm_mode` is a no-op in `ir.rs`). The author acknowledged that limit after the cross-check in the thread. Recommending a look, not approving. Notes (not blocking): - The new `v_mul_legacy_f32_treats_denormals_as_zero` test cannot tell old code from new on the hosts reported in this thread (NVIDIA, AMD RDNA2/RDNA4, WARP), because they all flush denormals by default. It documents the intended result but is not a regression guard there. - The bit test could reuse the existing `ftz` helper (e.g. compare `ftz(a)` with 0) instead of repeating the mask. - The same `legacy_mul` change is also part of the larger bundle in #1330; this PR is the focused version. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠senchsus on GitHub · 2026-11-25 08:26 UTC · original

Automated triage for the maintainers.

Needs rebase. This PR does not merge cleanly into current main (ac2525d). Conflicting files: crates/victory-shader/src/emit/valu_tests.rs.

The content has not been reviewed yet; it will be once the branch is rebased onto main.

Generated by Claude Code

Merged main into the branch, the conflict in valu_tests.rs is resolved (kept both the new tests and this one). It merges cleanly now.

**@⁠senchsus** on GitHub · 2026-11-25 08:26 UTC · [original](https://github.com/Marvisak/VIctory/pull/682#issuecomment-7157035282) > Automated triage for the maintainers. > > **Needs rebase.** This PR does not merge cleanly into current `main` ([ac2525d](https://github.com/Marvisak/VIctory/commit/ac2525dce10e98d16da9b4fd4bf7ea0e4876f4ff)). Conflicting files: `crates/victory-shader/src/emit/valu_tests.rs`. > > The content has not been reviewed yet; it will be once the branch is rebased onto `main`. > > _Generated by [Claude Code](https://claude.ai/code)_ Merged main into the branch, the conflict in valu_tests.rs is resolved (kept both the new tests and this one). It merges cleanly now.
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!682
No description provided.