Shaders: lower v_div_scale_f32 and v_div_fmas_f32 #1747

Open
github-bot wants to merge 2 commits from github-bot/gh-1747 into main AGit
Owner

@⁠ESAT586 on GitHub · 2026-11-24 21:54 UTC · original
ESAT586:shader/div-scale-and-fmas-f32 → main · +629 −2 · 4 files · 2 commits · open

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

**@⁠ESAT586** on GitHub · 2026-11-24 21:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/1747) `ESAT586:shader/div-scale-and-fmas-f32` → `main` · +629 −2 · 4 files · 2 commits · open > **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
Lowers RDNA2's IEEE-754 f32 division pre-scale and post-scale FMA helpers
(_div_scale_f32 VOP3B 0x16d and _div_fmas_f32 VOP3A 0x16f) in pure
32-bit DXIL (no INT64/F64 flags) with FTZ-immune exponent scaling, and
wires implicit VCC tracking for _div_fmas_f32 through IR and lane-mask
structure analysis.

AI-assisted with Gemini 4 Neon (Extra High Mode); synthetic tests only,
zero game data used.
Addresses review feedback on #1747:
- Flushes f32 inputs via `Self::ftz` and flushes subnormal outputs to signed zero
  in `scale_f32_pow2`, `div_scale_f32`, and `v_div_fmas_f32`, matching RDNA2's
  default f32 denormal mode (`FLOAT_MODE = 0xc0`, mode 0) and the rest of
  `emit::lower`.
- Eliminates double rounding on denormal quotients (subnormal scaled outputs flush
  to signed zero; normal quotients match IEEE-754 at 0 ULP) and removes the
  `max_ulp = 1` allowance from `rdna2_fdiv32_pipeline_with_div_scale_and_div_fmas`.
- Documents the existing `shortcut:` for DXIL `FMad` (op 46) in `v_div_fmas_f32`.
- Updates tests to verify FTZ behavior on subnormal numerator/denominator inputs
  and quotients scaling into the subnormal range.

Clean-room synthetic tests only; zero game data used.
AI-assisted with Gemini 4 Neon (Extra High Mode).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:43:32 +00:00
Author
Owner

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

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.

**@⁠upipibi** on GitHub · 2026-11-24 22:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7146079776) 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.
Author
Owner

@⁠rminzao on GitHub · 2026-11-24 23:09 UTC · original

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.

**@⁠rminzao** on GitHub · 2026-11-24 23:09 UTC · [original](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7147074926) **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._
Author
Owner

@⁠ESAT586 on GitHub · 2026-11-25 00:17 UTC · original

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

**@⁠ESAT586** on GitHub · 2026-11-25 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7148351160) 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).)*
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 01:35 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-25 01:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/1747#issuecomment-7149727454) <!-- 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)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/valu_tests.rs
  • crates/victory-shader/src/structure.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1747/head:github-bot/gh-1747'
git switch 'github-bot/gh-1747'

Merge

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