Shaders: lower v_div_scale_f32 and v_div_fmas_f32 #1746

Open
ESAT586 wants to merge 2 commits from upstream-pr/1747 into main
ESAT586 commented 2026-11-24 21:54:12 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-24T21:54:12Z.


AI Authorship Disclosure: Written and verified with Gemini 4 Neon (Extra High Mode). Strictly clean-room: zero game files or title data were used; all tests are synthetic RDNA2 compute shaders validated with dxil.dll and executed on real hardware and WARP.

Summary

Lowers RDNA2's f32 IEEE-754 division pre-scale and post-scale instructions — ** _div_scale_f32** (VOP3B

<!-- greyfinch-sync:Marvisak/VIctory:issue:6592168192 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1747). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-24T21:54:12Z. --- > **AI Authorship Disclosure:** Written and verified with **Gemini 4 Neon (Extra High Mode)**. Strictly clean-room: **zero game files or title data** were used; all tests are synthetic RDNA2 compute shaders validated with dxil.dll and executed on real hardware and WARP. ## Summary Lowers RDNA2's f32 IEEE-754 division pre-scale and post-scale instructions — ** _div_scale_f32** (VOP3B
upipibi commented 2026-11-24 23:05:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T22:19:02Z.


A note on denormals, from reading the diff (not run).

scale_f32_pow2 keeps f32 denormals exactly in both directions. It scales a subnormal input up through FirstbitHi, and a value scaled down into the subnormal range gets its rounded subnormal. That is RDNA2's behaviour only when the shader's f32 denormal mode keeps denormals.

  • VIctory reads f32 denormals as the console's float mode does elsewhere: ftz on main (lower.rs around 2067) makes them a zero of their sign.
  • AnyPS5 measured v_div_scale_f32 on RDNA2 hardware (gfx1036, 2,304 operand rows, every f32 denormal mode, IEEE 0 and 1; recorded in their docs/dev/debt/shader.md). In mode 0, which the common FLOAT_MODE 0xc0 selects, and in mode 2, it flushes its inputs. In mode 1 it keeps the inputs and flushes a denormal result to a zero of its sign. Only mode 3 keeps both.

So for the usual float mode, passing the operands through ftz and flushing a subnormal result to a signed zero would match the hardware and the rest of the lowering. The current form matches mode 3. A test with a subnormal numerator, and one with a quotient that scales into the subnormal range, would show the difference.

AI-assisted (Claude Code). No game files.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7146079776 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7146079776). Originally posted by [upipibi](https://github.com/upipibi) on 2026-11-24T22:19:02Z. --- A note on denormals, from reading the diff (not run). `scale_f32_pow2` keeps f32 denormals exactly in both directions. It scales a subnormal input up through `FirstbitHi`, and a value scaled down into the subnormal range gets its rounded subnormal. That is RDNA2's behaviour only when the shader's f32 denormal mode keeps denormals. - VIctory reads f32 denormals as the console's float mode does elsewhere: `ftz` on `main` (`lower.rs` around 2067) makes them a zero of their sign. - AnyPS5 measured `v_div_scale_f32` on RDNA2 hardware (gfx1036, 2,304 operand rows, every f32 denormal mode, IEEE 0 and 1; recorded in their `docs/dev/debt/shader.md`). In mode 0, which the common `FLOAT_MODE` 0xc0 selects, and in mode 2, it flushes its inputs. In mode 1 it keeps the inputs and flushes a denormal result to a zero of its sign. Only mode 3 keeps both. So for the usual float mode, passing the operands through `ftz` and flushing a subnormal result to a signed zero would match the hardware and the rest of the lowering. The current form matches mode 3. A test with a subnormal numerator, and one with a quotient that scales into the subnormal range, would show the difference. AI-assisted (Claude Code). No game files.
rminzao commented 2026-11-25 00:06:42 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by rminzao on 2026-11-24T23:09:15Z.


Double rounding on denormal quotients

The denormal-mode point is already raised above. One more: in v_div_fmas_f32 (lower.rs ~6084/6094) the 2^±64 scale is applied after an already-rounded fmad. When the quotient ends up denormal it is rounded twice, so it can be 1 ULP off the hardware's single rounding. valu_tests.rs ~1792 allows max_ulp = 1 for denormal results, which would hide exactly this.

Related: this uses FMad (DXIL op 46), which isn't guaranteed to be fused. That's the existing v_fma_f32 shortcut, but here the final correction step relies on the fused result, so it might deserve a shortcut: note like the others.

Static review (AI-assisted), read against main at 70564ae; nothing was run on a GPU.

<!-- greyfinch-sync:Marvisak/VIctory:comment:7147074926 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7147074926). Originally posted by [rminzao](https://github.com/rminzao) on 2026-11-24T23:09:15Z. --- **Double rounding on denormal quotients** The denormal-mode point is already raised above. One more: in `v_div_fmas_f32` (`lower.rs` ~6084/6094) the 2^±64 scale is applied after an already-rounded `fmad`. When the quotient ends up denormal it is rounded twice, so it can be 1 ULP off the hardware's single rounding. `valu_tests.rs` ~1792 allows `max_ulp = 1` for denormal results, which would hide exactly this. Related: this uses FMad (DXIL op 46), which isn't guaranteed to be fused. That's the existing `v_fma_f32` shortcut, but here the final correction step relies on the fused result, so it might deserve a `shortcut:` note like the others. _Static review (AI-assisted), read against `main` at 70564ae; nothing was run on a GPU._
ESAT586 commented 2026-11-25 01:07:38 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by ESAT586 on 2026-11-25T00:17:16Z.


Thanks @upipibi and @rminzao for the sharp RDNA2 denormal-mode and rounding feedback!

Updated this branch to match VIctory's default f32 FTZ mode (FLOAT_MODE = 0xc0, mode 0) and address both notes:

  • div_scale_f32 & v_div_fmas_f32: flush inputs with Self::ftz and flush subnormal outputs (exp <= 64 under 2^-64, or unscaled subnormal FMad) to a zero of their sign in scale_f32_pow2, matching lower.rs's ftz behavior and RDNA2 mode 0.
  • No double-rounding tolerance: because scaled subnormal quotients flush to signed zero and normal results scale by an exact power of two, rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas now asserts exact bit equality (0 ULP) across all cases (removing max_ulp = 1).
  • shortcut: note: documented that v_div_fmas_f32 uses DXIL FMad (op 46), matching v_fma_f32.
  • Tests: added explicit FTZ test cases for subnormal numerator/denominator inputs (qNaN, flag = 0), negative/positive subnormal outputs in v_div_fmas_f32 (±0.0), and subnormal-range quotients in the full Newton-Raphson pipeline (±0.0).

Re-verified on Windows 11 with NVIDIA GeForce RTX 3060 Ti + Microsoft D3D12 WARP + dxil.dll:

  • v_div_scale_f32_matches_rdna2, v_div_fmas_f32_matches_rdna2, rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas: 3 passed, 0 failed
  • cargo clippy --workspace --all-targets -- -D warnings & --all-features: 0 warnings
  • cargo xtask policy: ok

(Clean-room synthetic tests only, zero game data. AI-assisted with Gemini 4 Neon (Extra High Mode).)

<!-- greyfinch-sync:Marvisak/VIctory:comment:7148351160 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7148351160). Originally posted by [ESAT586](https://github.com/ESAT586) on 2026-11-25T00:17:16Z. --- Thanks @upipibi and @rminzao for the sharp RDNA2 denormal-mode and rounding feedback! Updated this branch to match VIctory's default f32 FTZ mode (`FLOAT_MODE = 0xc0`, mode 0) and address both notes: - **`div_scale_f32` & `v_div_fmas_f32`**: flush inputs with `Self::ftz` and flush subnormal outputs (`exp <= 64` under `2^-64`, or unscaled subnormal `FMad`) to a zero of their sign in `scale_f32_pow2`, matching `lower.rs`'s `ftz` behavior and RDNA2 mode 0. - **No double-rounding tolerance**: because scaled subnormal quotients flush to signed zero and normal results scale by an exact power of two, `rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas` now asserts exact bit equality (`0` ULP) across all cases (removing `max_ulp = 1`). - **`shortcut:` note**: documented that `v_div_fmas_f32` uses DXIL `FMad` (op 46), matching `v_fma_f32`. - **Tests**: added explicit FTZ test cases for subnormal numerator/denominator inputs (`qNaN`, `flag = 0`), negative/positive subnormal outputs in `v_div_fmas_f32` (`±0.0`), and subnormal-range quotients in the full Newton-Raphson pipeline (`±0.0`). Re-verified on Windows 11 with **NVIDIA GeForce RTX 3060 Ti** + **Microsoft D3D12 WARP** + **`dxil.dll`**: - `v_div_scale_f32_matches_rdna2`, `v_div_fmas_f32_matches_rdna2`, `rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas`: **3 passed, 0 failed** - `cargo clippy --workspace --all-targets -- -D warnings` & `--all-features`: **0 warnings** - `cargo xtask policy`: **ok** *(Clean-room synthetic tests only, zero game data. AI-assisted with Gemini 4 Neon (Extra High Mode).)*
Marvisak commented 2026-11-25 02:06:13 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T01:35:38Z.


Automated triage for the maintainers.

Needs rebase. This pull request conflicts with main (conflicting: crates/victory-shader/src/structure.rs). It will be reviewed once it is rebased onto the current main.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7149727454 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7149727454). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T01:35:38Z. --- <!-- victory-triage v1 head=9cd8d74669218b2346cf43c702563825aad4cbdc verdict=needs-rebase at=2026-11-25T01:35:38Z --> Automated triage for the maintainers. **Needs rebase.** This pull request conflicts with `main` (conflicting: `crates/victory-shader/src/structure.rs`). It will be reviewed once it is rebased onto the current `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
catminou4 commented 2026-11-25 04:05:52 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T03:39:22Z.

Review: COMMENTED


Shaders: lower v_div_scale_f32 and v_div_fmas_f32 — merge conflict with main (940d32f69b), verified locally. Also overlaps #200 (same v_div_scale/v_div_fmas lowering, plus v_div_fixup). Rebase needed; will re-test.

<!-- greyfinch-sync:Marvisak/VIctory:review:6051513069 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/1747#pullrequestreview-6051513069). Originally posted by [catminou4](https://github.com/catminou4) on 2026-11-25T03:39:22Z. **Review:** COMMENTED --- Shaders: lower v_div_scale_f32 and v_div_fmas_f32 — merge conflict with main (940d32f69b), verified locally. Also overlaps #200 (same v_div_scale/v_div_fmas lowering, plus v_div_fixup). Rebase needed; will re-test.
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/1747:upstream-pr/1747'
git switch 'upstream-pr/1747'

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