Shaders: lower binary64 reciprocal square root with dwords #2809

Open
andrewpozdnakov7-jpg wants to merge 2 commits from upstream-pr/2811 into main
andrewpozdnakov7-jpg commented 2026-11-25 21:14:05 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T21:14:05Z.


Main stops translation on V_RSQ_F64. This lowering normalizes a binary64 input, computes the square root of its significand to 28 bits, and divides to obtain a 26-bit reciprocal. The 32-bit DXIL arithmetic retains the known rounding direction and input-denormal mode, handles signed zeros and infinities, and quiets NaNs. AMD specifies a 2^29 binary64 ULP bound for this instruction. RDNA 2 ISA reference

Both synthetic shaders stop on unsupported v_rsq_f64 at 13f91ca. The same tests pass all 112 cases on app-local WARP SM 6.6 with the DXIL validator. They cover VOP1 VGPR and VOP3 SGPR sources, all four rounding modes, input-subnormal modes, special values, and the two-dword normalization boundary. Feature-flag checks confirm the 32-bit lowering. An independent integer model stayed within the bound for all 327,520 CPU comparisons across the finite exponent fields.

The paired full shader suite moved from 268 passed, 5 failed and 3 ignored to 270 passed, 3 failed and 3 ignored. The other three failures were the same gather-at-level, lane-permute and edge-flagged-triangle tests in both runs. All 33 patched VALU tests including runtime identity passed. Strict shader Clippy and staged asset policy passed.

The mode analysis follows the same approach as #2743, #2791 and #2799. Coordination claim

Please retain the Git author on cherry-pick or rebase. If you fold this change into another commit, please include Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

<!-- greyfinch-sync:Marvisak/VIctory:issue:5499283615 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2811). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T21:14:05Z. --- Main stops translation on `V_RSQ_F64`. This lowering normalizes a binary64 input, computes the square root of its significand to 28 bits, and divides to obtain a 26-bit reciprocal. The 32-bit DXIL arithmetic retains the known rounding direction and input-denormal mode, handles signed zeros and infinities, and quiets NaNs. AMD specifies a 2^29 binary64 ULP bound for this instruction. [RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) Both synthetic shaders stop on unsupported `v_rsq_f64` at `13f91ca`. The same tests pass all 112 cases on app-local WARP SM 6.6 with the DXIL validator. They cover VOP1 VGPR and VOP3 SGPR sources, all four rounding modes, input-subnormal modes, special values, and the two-dword normalization boundary. Feature-flag checks confirm the 32-bit lowering. An independent integer model stayed within the bound for all 327,520 CPU comparisons across the finite exponent fields. The paired full shader suite moved from 268 passed, 5 failed and 3 ignored to 270 passed, 3 failed and 3 ignored. The other three failures were the same gather-at-level, lane-permute and edge-flagged-triangle tests in both runs. All 33 patched VALU tests including runtime identity passed. Strict shader Clippy and staged asset policy passed. The mode analysis follows the same approach as #2743, #2791 and #2799. [Coordination claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7174605169) Please retain the Git author on cherry-pick or rebase. If you fold this change into another commit, please include `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`.
kotekompotek commented 2026-11-25 21:19:25 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T21:19:25Z.

Review: COMMENTED


Same careful pattern as the rcp64 PR (#2799), shared FpMode16 base — sqrt-of-significand to 28 bits then dword division for a 26-bit quotient, with explicit subnormal normalization, signed-zero/inf handling, NaN quieting, and refusal on modifiers or unknown modes. The 2^29 ULP bound cited from the RDNA2 ISA is the right correctness bar for V_RSQ_F64, and 112 validator-checked cases across rounding modes/denorm modes/specials is solid coverage. LGTM; same merge-order note — whichever of the FpMode16 refactor PRs lands first wins the base.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065130435 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2811#pullrequestreview-6065130435). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T21:19:25Z. **Review:** COMMENTED --- Same careful pattern as the rcp64 PR (#2799), shared FpMode16 base — sqrt-of-significand to 28 bits then dword division for a 26-bit quotient, with explicit subnormal normalization, signed-zero/inf handling, NaN quieting, and refusal on modifiers or unknown modes. The 2^29 ULP bound cited from the RDNA2 ISA is the right correctness bar for V_RSQ_F64, and 112 validator-checked cases across rounding modes/denorm modes/specials is solid coverage. LGTM; same merge-order note — whichever of the FpMode16 refactor PRs lands first wins the base.
fuzeofficlal commented 2026-11-25 22:04:30 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:33:09Z.


Verified e1de9799 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll):

  • cargo xtask policy: 185 files ok; cargo clippy -p victory-shader --all-targets --locked -- -D warnings (default & --features host): 0 warnings.
  • cargo test -p victory-shader --features host --lib --locked -- valu_tests::: 32/32 passed on RTX 5090 + WARP + dxil.dll, including rsqrt_f64_tracks_binary64_range_and_rounding and rsqrt_f64_obeys_the_input_denormal_mode (and SFI0 & 0x8001 == 0 confirms pure 32-bit DXIL with no double/int64 flags).
  • Code review of lower/rsqrt.rs: special values (±0.0 -> ±Inf, negative/-Inf -> qNaN, +Inf -> +0.0, sNaN quieted preserving sign/payload, MODE[7:6] input denormal flush vs normalize via unaryBits.i32 op 33), 28-bit integer sqrt + opposite-direction rounding + 26-bit long division, and modifier rejection (clamp/omod/sdwa/dpp, abs before neg, 32-bit literal as high dword) all check out.
  • Merge notes via git merge-tree: clean against origin/main 13f91ca and #2812; conflicts with #2743/#2791/#2799 in lower.rs on the shared FpMode16 refactor, and shares the same Lower::mode16 -> FpMode16 compile interaction with #2782 (lower.rs:3384 needs self.mode16.round.ok_or(...) if both land).
<!-- greyfinch-sync:Marvisak/VIctory:comment:7175228676 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2811#issuecomment-7175228676). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:33:09Z. --- Verified `e1de9799` on Windows 11 (RTX 5090 + WARP + Windows SDK `10.0.26100.0` `dxil.dll`): - `cargo xtask policy`: 185 files ok; `cargo clippy -p victory-shader --all-targets --locked -- -D warnings` (default & `--features host`): 0 warnings. - `cargo test -p victory-shader --features host --lib --locked -- valu_tests::`: 32/32 passed on RTX 5090 + WARP + `dxil.dll`, including `rsqrt_f64_tracks_binary64_range_and_rounding` and `rsqrt_f64_obeys_the_input_denormal_mode` (and `SFI0 & 0x8001 == 0` confirms pure 32-bit DXIL with no double/int64 flags). - Code review of `lower/rsqrt.rs`: special values (`±0.0 -> ±Inf`, negative/`-Inf -> qNaN`, `+Inf -> +0.0`, sNaN quieted preserving sign/payload, `MODE[7:6]` input denormal flush vs normalize via `unaryBits.i32` op 33), 28-bit integer sqrt + opposite-direction rounding + 26-bit long division, and modifier rejection (`clamp`/`omod`/`sdwa`/`dpp`, `abs` before `neg`, 32-bit literal as high dword) all check out. - Merge notes via `git merge-tree`: clean against `origin/main` `13f91ca` and #2812; conflicts with #2743/#2791/#2799 in `lower.rs` on the shared `FpMode16` refactor, and shares the same `Lower::mode16 -> FpMode16` compile interaction with #2782 (`lower.rs:3384` needs `self.mode16.round.ok_or(...)` if both land).
Dukestar69 commented 2026-11-26 02:32:45 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T02:32:45Z.

Review: APPROVED


Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::valu_tests::rsqrt: 2 passed, 0 failed (rsqrt_f64_obeys_the_input_denormal_mode and rsqrt_f64_tracks_binary64_range_and_rounding pass on both RTX 5090 hardware and WARP).
  • 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:6067785961 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2811#pullrequestreview-6067785961). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T02:32:45Z. **Review:** APPROVED --- Tested merged onto `origin/main` (`63bcca4`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, `dxil.dll` validator active): - `cargo test -p victory-shader --lib emit::valu_tests::rsqrt`: **2 passed, 0 failed** (`rsqrt_f64_obeys_the_input_denormal_mode` and `rsqrt_f64_tracks_binary64_range_and_rounding` pass on both RTX 5090 hardware and WARP). - `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).
Marvisak commented 2026-11-26 03:05:07 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T02:19:48Z.


Automated triage for the maintainers.

This lowers v_rsq_f64, which translation refuses today, in 32-bit integer DXIL (emit/lower/rsqrt.rs): it normalizes the input, takes a 28-bit integer square root of the significand, then divides. It handles signed zeros, infinities, negative inputs and NaN quieting, and refuses the modifiers it does not model. It also generalizes round16 into FpMode16, which now tracks the f64/f16 denorm bits (MODE 7:6) as well as the round mode. I read the mode tracking: s_denorm_mode's bits 3:2 and the s_setreg field arithmetic are right, and an unknown mode is refused rather than guessed. The accuracy target is AMD's public RDNA2 ISA bound, and the tests run on hardware and WARP. Note that it overlaps the FpMode16 refactor in #2743/#2791/#2799, so whichever lands second needs a rebase.

CI: passing.


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7180697753 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2811#issuecomment-7180697753). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T02:19:48Z. --- <!-- victory-triage v1 head=f6a7d2159dcf0427e97e12a5654499baa6aaeb78 verdict=merge-candidate at=2026-11-26T02:19:42Z --> Automated triage for the maintainers. This lowers `v_rsq_f64`, which translation refuses today, in 32-bit integer DXIL (`emit/lower/rsqrt.rs`): it normalizes the input, takes a 28-bit integer square root of the significand, then divides. It handles signed zeros, infinities, negative inputs and NaN quieting, and refuses the modifiers it does not model. It also generalizes `round16` into `FpMode16`, which now tracks the f64/f16 denorm bits (MODE 7:6) as well as the round mode. I read the mode tracking: `s_denorm_mode`'s bits 3:2 and the `s_setreg` field arithmetic are right, and an unknown mode is refused rather than guessed. The accuracy target is AMD's public RDNA2 ISA bound, and the tests run on hardware and WARP. Note that it overlaps the `FpMode16` refactor in #2743/#2791/#2799, so whichever lands second needs a rebase. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
fuzeofficlal commented 2026-11-27 02:16:34 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T02:16:34Z.

Review: APPROVED


Reviewed f6a7d2159dcf0427e97e12a5654499baa6aaeb78.

LGTM — Lower::rsqrt_f64 cleanly decomposes v_rsq_f64 into pure 32-bit integer arithmetic (rsqrt_denominator 28-bit sqrt + rsqrt_quotient 26-bit reciprocal, staying well inside RDNA2's 2^29 ULP bound across all normal and subnormal binary64 exponents without enabling INT64 or DOUBLE in SFI0), handles special values (±0 -> ±Inf, +Inf -> +0, negative inputs -> qNaN, NaN quieting, and FpMode16 input denormal flush vs preservation), and preserves existing narrow16/v_fma_f16 rounding behavior.

Verified locally on RTX 5090 + WARP:

  • cargo xtask policy (192 files ok), cargo clippy -p victory-shader --all-targets / --all-features (-D warnings), and rustfmt --edition 2024 --check on rsqrt.rs pass cleanly (zero new rustfmt diffs in lower.rs / valu_tests.rs; lower.rs is 521,361 bytes).
  • emit::valu_tests:: passed (37 passed, 0 failed), and both rsqrt_f64_tracks_binary64_range_and_rounding and rsqrt_f64_obeys_the_input_denormal_mode pass under both VICTORY_WARP=0 and VICTORY_WARP=1 with SFI0 & 0x8001 == 0.

Non-blocking note:

  • v_rsq_f64 only depends on MODE bit 6 (input denormal preservation) since its finite outputs are never subnormal; tracking bits 7:6 as a single denorm field means a write to bit 7 alone (or a join of denorm modes 1 and 3) sets denorm to None and rejects v_rsq_f64.
<!-- greyfinch-sync:Marvisak/VIctory:review:6082622355 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2811#pullrequestreview-6082622355). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T02:16:34Z. **Review:** APPROVED --- Reviewed `f6a7d2159dcf0427e97e12a5654499baa6aaeb78`. **LGTM** — `Lower::rsqrt_f64` cleanly decomposes `v_rsq_f64` into pure 32-bit integer arithmetic (`rsqrt_denominator` 28-bit sqrt + `rsqrt_quotient` 26-bit reciprocal, staying well inside RDNA2's `2^29` ULP bound across all normal and subnormal binary64 exponents without enabling `INT64` or `DOUBLE` in `SFI0`), handles special values (`±0 -> ±Inf`, `+Inf -> +0`, negative inputs -> `qNaN`, `NaN` quieting, and `FpMode16` input denormal flush vs preservation), and preserves existing `narrow16`/`v_fma_f16` rounding behavior. Verified locally on RTX 5090 + WARP: - `cargo xtask policy` (`192 files ok`), `cargo clippy -p victory-shader --all-targets` / `--all-features` (`-D warnings`), and `rustfmt --edition 2024 --check` on `rsqrt.rs` pass cleanly (zero new `rustfmt` diffs in `lower.rs` / `valu_tests.rs`; `lower.rs` is 521,361 bytes). - `emit::valu_tests::` passed (37 passed, 0 failed), and both `rsqrt_f64_tracks_binary64_range_and_rounding` and `rsqrt_f64_obeys_the_input_denormal_mode` pass under both `VICTORY_WARP=0` and `VICTORY_WARP=1` with `SFI0 & 0x8001 == 0`. Non-blocking note: - `v_rsq_f64` only depends on MODE bit 6 (input denormal preservation) since its finite outputs are never subnormal; tracking bits `7:6` as a single `denorm` field means a write to bit 7 alone (or a join of `denorm` modes `1` and `3`) sets `denorm` to `None` and rejects `v_rsq_f64`.
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/2811:upstream-pr/2811'
git switch 'upstream-pr/2811'

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