Shaders: lower binary64 square root with 32-bit operations #2791

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

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 20:35 UTC · original
andrewpozdnakov7-jpg:codex2/sqrt-f64 → main · +403 −49 · 3 files · 2 commits · open

Main stops translation on V_SQRT_F64. The lowering normalizes the two-word significand, computes 24 root bits with integer arithmetic, and applies the known rounding mode. It handles subnormals according to the known input-denormal mode, preserves signed zeros and positive infinity, and quiets NaNs. The DXIL requires only 32-bit operations. AMD specifies an accuracy bound of 2^29 binary64 ULP for this instruction. RDNA 2 ISA reference

Both synthetic readbacks fail on 13f91ca with unsupported v_sqrt_f64. The same fixtures pass all 107 cases on app-local WARP SM 6.6 with the DXIL validator. They exercise VOP1 VGPR and VOP3 SGPR sources, all four rounding modes, a flush-input mode, special values, and the cross-dword normalization boundary. They also check the DXIL feature flags for optional doubles and int64. An independent integer model stayed within the specified bound in 262,016 comparisons against CPU binary64 square roots across all finite exponent fields.

The full serial shader suite moved from 268 passed, 5 failed, and 3 ignored to 270 passed, 3 failed, and 3 ignored with both new tests present. The same gather-at-level, lane-permute, and edge-flagged triangle tests failed on both revisions. All 33 VALU tests in the patched run passed, including the runtime identity check. Strict shader Clippy and staged asset policy passed.

This uses the same FP16/FP64 mode analysis as #2743. 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]>.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 20:35 UTC · [original](https://github.com/Marvisak/VIctory/pull/2791) `andrewpozdnakov7-jpg:codex2/sqrt-f64` → `main` · +403 −49 · 3 files · 2 commits · open Main stops translation on `V_SQRT_F64`. The lowering normalizes the two-word significand, computes 24 root bits with integer arithmetic, and applies the known rounding mode. It handles subnormals according to the known input-denormal mode, preserves signed zeros and positive infinity, and quiets NaNs. The DXIL requires only 32-bit operations. AMD specifies an accuracy bound of 2^29 binary64 ULP for this instruction. [RDNA 2 ISA reference](https://docs.amd.com/v/u/en-US/rdna2-shader-instruction-set-architecture) Both synthetic readbacks fail on `13f91ca` with unsupported `v_sqrt_f64`. The same fixtures pass all 107 cases on app-local WARP SM 6.6 with the DXIL validator. They exercise VOP1 VGPR and VOP3 SGPR sources, all four rounding modes, a flush-input mode, special values, and the cross-dword normalization boundary. They also check the DXIL feature flags for optional doubles and int64. An independent integer model stayed within the specified bound in 262,016 comparisons against CPU binary64 square roots across all finite exponent fields. The full serial shader suite moved from 268 passed, 5 failed, and 3 ignored to 270 passed, 3 failed, and 3 ignored with both new tests present. The same gather-at-level, lane-permute, and edge-flagged triangle tests failed on both revisions. All 33 VALU tests in the patched run passed, including the runtime identity check. Strict shader Clippy and staged asset policy passed. This uses the same FP16/FP64 mode analysis as #2743. [Coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633) 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]>`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:14:19 +00:00
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 20:39 UTC · original

Stacked on the same FpMode16 refactor as #2743 (same author, codex2/* series) - the base reads consistently, and the sqrt approach (normalize the two-word significand, 24 root bits via integer ops, apply tracked rounding mode, handle subnormals per denorm mode) is the right way to do binary64 in 32-bit DXIL. The 2^29 ULP bound quoted is AMD's own sqrt_f64 spec, so matching it is the correct bar. Same merge-order note as #2743: whichever of this/#2743/#2782 lands second touches mode16.

**@⁠kotekompotek** on GitHub · 2026-11-25 20:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/2791#issuecomment-7174004273) Stacked on the same FpMode16 refactor as #2743 (same author, codex2/* series) - the base reads consistently, and the sqrt approach (normalize the two-word significand, 24 root bits via integer ops, apply tracked rounding mode, handle subnormals per denorm mode) is the right way to do binary64 in 32-bit DXIL. The 2^29 ULP bound quoted is AMD's own sqrt_f64 spec, so matching it is the correct bar. Same merge-order note as #2743: whichever of this/#2743/#2782 lands second touches mode16.
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 20:58 UTC · original

WARP/CPU verification of head 8a53189143333bd54ecf2ae65885d581ccb36151

Environment: Windows 11 x64, rustc/cargo 1.98.1, WARP/CPU only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), no physical GPU, no game files. DXIL validation with Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11. PR head merged cleanly onto origin/main 13f91caee2.

Tests (cargo test --no-fail-fast -p victory-shader)

  • origin/main baseline: 267 passed, 3 failed, 3 ignored.
  • With this PR: 269 passed, 3 failed, 3 ignored (+2: sqrt_f64_tracks_binary64_range_and_rounding, sqrt_f64_preserves_signed_zero_when_mode_flushes_subnormals).
  • The 3 failures are the same on origin/main and unrelated to this PR: a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_read_the_lane_named_and_zero_from_lanes_left_out (they compare against hardware lane results and fail on WARP).

Negative control (each mutation applied alone, then reverted, new tests only)

  • v_sqrt_f64 dispatch removed: both new tests fail.
  • Rounding increment disabled (if false && matches!(mode, 0 | 1)): range/rounding test fails.
  • Signed-zero result replaced by +0: both new tests fail.

Lint: cargo clippy --all-targets -- -D warnings clean. cargo fmt --check: +3 hunks in crates/victory-shader/src/emit/lower.rs (28 on main, 31 with the PR), all in the new FpMode16 block (INITIAL, the s_round_mode/s_denorm_mode arms, and the final Self { round: update(..), .. }); sqrt.rs and valu_tests.rs add none. Note: cargo fmt --check is already red on origin/main here (891 hunks, none from these PRs), so I compared per-file hunk counts against that baseline.

Review: read the full diff. The MODE bits 3:2 / 7:6 tracking with per-field join is correct, and the sqrt lowering handles sign, abs/neg, non-finite, and subnormal operands (flush vs preserve) coherently. The tests check the 2^29-ULP bound and rounding direction, not bit-exactness against hardware, and I did not run any hardware. No game files, dumps, shader names, or retail hashes in the diff.

AI disclosure: this verification was done by Claude Code (Claude Sonnet 5.5).

**@⁠oni-swr** on GitHub · 2026-11-25 20:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/2791#issuecomment-7174445114) **WARP/CPU verification of head `8a53189143333bd54ecf2ae65885d581ccb36151`** **Environment:** Windows 11 x64, rustc/cargo 1.98.1, WARP/CPU only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), no physical GPU, no game files. DXIL validation with Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11. PR head merged cleanly onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca. **Tests (`cargo test --no-fail-fast -p victory-shader`)** - `origin/main` baseline: 267 passed, 3 failed, 3 ignored. - With this PR: 269 passed, 3 failed, 3 ignored (+2: `sqrt_f64_tracks_binary64_range_and_rounding`, `sqrt_f64_preserves_signed_zero_when_mode_flushes_subnormals`). - The 3 failures are the same on `origin/main` and unrelated to this PR: `a_mask_joined_with_data_at_a_uniform_loop_is_its_number`, `gathers_at_a_level_match_the_hardware`, `permutes_read_the_lane_named_and_zero_from_lanes_left_out` (they compare against hardware lane results and fail on WARP). **Negative control** (each mutation applied alone, then reverted, new tests only) - `v_sqrt_f64` dispatch removed: both new tests fail. - Rounding increment disabled (`if false && matches!(mode, 0 | 1)`): range/rounding test fails. - Signed-zero result replaced by +0: both new tests fail. **Lint:** `cargo clippy --all-targets -- -D warnings` clean. `cargo fmt --check`: **+3 hunks in `crates/victory-shader/src/emit/lower.rs`** (28 on main, 31 with the PR), all in the new `FpMode16` block (`INITIAL`, the `s_round_mode`/`s_denorm_mode` arms, and the final `Self { round: update(..), .. }`); `sqrt.rs` and `valu_tests.rs` add none. Note: `cargo fmt --check` is already red on `origin/main` here (891 hunks, none from these PRs), so I compared per-file hunk counts against that baseline. **Review:** read the full diff. The MODE bits 3:2 / 7:6 tracking with per-field join is correct, and the sqrt lowering handles sign, abs/neg, non-finite, and subnormal operands (flush vs preserve) coherently. The tests check the 2^29-ULP bound and rounding direction, not bit-exactness against hardware, and I did not run any hardware. No game files, dumps, shader names, or retail hashes in the diff. AI disclosure: this verification was done by Claude Code (Claude Sonnet 5.5).
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 21:05 UTC · original

Reviewed and verified 8a531891 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll) against main (13f91ca).

Code review

  • Pure 32-bit DXIL lowering (SFI0 & 0x8001 == 0, no double/int64 capability needed).
  • Special cases and MODE[7:6] denorm handling check out cleanly: ±0.0 preserved, negative finite / -Inf -> canonical qNaN 0xfff8_0000_0000_0000, sNaN quietened with payload intact (including low-word-only payloads), input subnormal flush (-denorm -> -0.0) vs. FirstbitHi cross-dword normalization, and 24-bit integer square-root iteration across all 4 rounding modes (RNE/RTP/RTN/RTZ) meeting RDNA2's 2^29 ULP bound.
  • Cross-PR merge notes:
    • Clean git merge-tree vs 13f91ca, #2713, #2728, and #2782; conflicts in lower.rs with #2743 (both introduce the identical FpMode16 / fp_modes16 refactor).
    • Semantic compile interaction with #2782: even though git merge-tree with #2782 is textually clean, changing Lower::mode16 from Option<u8> to FpMode16 { round, denorm } means #2782's self.mode16.ok_or(...) (lower.rs:3384) will need to become self.mode16.round.ok_or(...) when both land.

Verification (Windows 11, RTX 5090 + WARP + dxil.dll)

  • cargo test --locked -p victory-shader --features host --lib -- valu_tests::: 32 passed, 0 failed (both new sqrt_f64_* tests pass on RTX 5090 and WARP with dxil.dll).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 185 files ok.
**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2791#issuecomment-7174594717) Reviewed and verified `8a531891` on Windows 11 (RTX 5090 + WARP + Windows SDK `10.0.26100.0` `dxil.dll`) against `main` (`13f91ca`). ### Code review - Pure 32-bit DXIL lowering (`SFI0 & 0x8001 == 0`, no double/int64 capability needed). - Special cases and `MODE[7:6]` denorm handling check out cleanly: `±0.0` preserved, negative finite / `-Inf` -> canonical qNaN `0xfff8_0000_0000_0000`, `sNaN` quietened with payload intact (including low-word-only payloads), input subnormal flush (`-denorm -> -0.0`) vs. `FirstbitHi` cross-dword normalization, and 24-bit integer square-root iteration across all 4 rounding modes (`RNE`/`RTP`/`RTN`/`RTZ`) meeting RDNA2's `2^29` ULP bound. - **Cross-PR merge notes**: - Clean `git merge-tree` vs `13f91ca`, `#2713`, `#2728`, and `#2782`; conflicts in `lower.rs` with `#2743` (both introduce the identical `FpMode16` / `fp_modes16` refactor). - **Semantic compile interaction with `#2782`**: even though `git merge-tree` with `#2782` is textually clean, changing `Lower::mode16` from `Option<u8>` to `FpMode16 { round, denorm }` means `#2782`'s `self.mode16.ok_or(...)` (`lower.rs:3384`) will need to become `self.mode16.round.ok_or(...)` when both land. ### Verification (Windows 11, RTX 5090 + WARP + `dxil.dll`) - `cargo test --locked -p victory-shader --features host --lib -- valu_tests::`: **32 passed, 0 failed** (both new `sqrt_f64_*` tests pass on RTX 5090 and WARP with `dxil.dll`). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default & `--features host`): **0 warnings**; `cargo xtask policy`: **185 files ok**.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:21 UTC · original

Automated triage for the maintainers.

This PR replaces the round-only round16 analysis in crates/victory-shader/src/emit/lower.rs with FpMode16, which tracks MODE bits 3:2 and 7:6 with a per-field join. It also lowers v_sqrt_f64 (lower/sqrt.rs, sqrt_f64) with 32-bit integer operations to the 2^29 ULP bound the public RDNA2 ISA reference gives for V_SQRT_F64. I checked the s_round_mode/s_denorm_mode/s_setreg bit extraction, the join, and the special-value ordering (zero overrides the negative-NaN path, so sqrt(-0) = -0). Input-denormal flushing uses bit 6, as the ISA defines. CI passes.

Notes for the maintainer:

  • FpMode16::INITIAL assumes subnormals are preserved at shader entry. That is an assumption, like the existing nearest-even round.
  • The FpMode16 refactor is the same as in #2743 (same author), so whichever lands second needs a rebase. #2782's self.mode16.ok_or(...) would have to become self.mode16.round.ok_or(...).
  • A few new lines in FpMode16::after are longer than rustfmt would leave them.

This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:21 UTC · [original](https://github.com/Marvisak/VIctory/pull/2791#issuecomment-7177456200) <!-- victory-triage v1 head=f30cf4eb62534d7f1d59903f3a44e6879fd873ea verdict=merge-candidate at=2026-11-25T23:21:38Z --> Automated triage for the maintainers. This PR replaces the round-only `round16` analysis in `crates/victory-shader/src/emit/lower.rs` with `FpMode16`, which tracks MODE bits 3:2 and 7:6 with a per-field join. It also lowers `v_sqrt_f64` (`lower/sqrt.rs`, `sqrt_f64`) with 32-bit integer operations to the 2^29 ULP bound the public RDNA2 ISA reference gives for V_SQRT_F64. I checked the `s_round_mode`/`s_denorm_mode`/`s_setreg` bit extraction, the join, and the special-value ordering (zero overrides the negative-NaN path, so sqrt(-0) = -0). Input-denormal flushing uses bit 6, as the ISA defines. CI passes. Notes for the maintainer: - `FpMode16::INITIAL` assumes subnormals are preserved at shader entry. That is an assumption, like the existing nearest-even round. - The `FpMode16` refactor is the same as in #2743 (same author), so whichever lands second needs a rebase. #2782's `self.mode16.ok_or(...)` would have to become `self.mode16.round.ok_or(...)`. - A few new lines in `FpMode16::after` are longer than rustfmt would leave them. This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

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

Merge

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