Shaders: lower binary64 square root with 32-bit operations #2789
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2791"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T20:35:59Z.
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 referenceBoth synthetic readbacks fail on
13f91cawith unsupportedv_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]>.Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T20:39:51Z.
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.
Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T20:58:43Z.
WARP/CPU verification of head
8a53189143333bd54ecf2ae65885d581ccb36151Environment: 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/mainbaseline: 267 passed, 3 failed, 3 ignored.sqrt_f64_tracks_binary64_range_and_rounding,sqrt_f64_preserves_signed_zero_when_mode_flushes_subnormals).origin/mainand 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_f64dispatch removed: both new tests fail.if false && matches!(mode, 0 | 1)): range/rounding test fails.Lint:
cargo clippy --all-targets -- -D warningsclean.cargo fmt --check: +3 hunks incrates/victory-shader/src/emit/lower.rs(28 on main, 31 with the PR), all in the newFpMode16block (INITIAL, thes_round_mode/s_denorm_modearms, and the finalSelf { round: update(..), .. });sqrt.rsandvalu_tests.rsadd none. Note:cargo fmt --checkis already red onorigin/mainhere (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).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T21:05:17Z.
Reviewed and verified
8a531891on Windows 11 (RTX 5090 + WARP + Windows SDK10.0.26100.0dxil.dll) againstmain(13f91ca).Code review
SFI0 & 0x8001 == 0, no double/int64 capability needed).MODE[7:6]denorm handling check out cleanly:±0.0preserved, negative finite /-Inf-> canonical qNaN0xfff8_0000_0000_0000,sNaNquietened with payload intact (including low-word-only payloads), input subnormal flush (-denorm -> -0.0) vs.FirstbitHicross-dword normalization, and 24-bit integer square-root iteration across all 4 rounding modes (RNE/RTP/RTN/RTZ) meeting RDNA2's2^29ULP bound.git merge-treevs13f91ca,#2713,#2728, and#2782; conflicts inlower.rswith#2743(both introduce the identicalFpMode16/fp_modes16refactor).#2782: even thoughgit merge-treewith#2782is textually clean, changingLower::mode16fromOption<u8>toFpMode16 { round, denorm }means#2782'sself.mode16.ok_or(...)(lower.rs:3384) will need to becomeself.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 newsqrt_f64_*tests pass on RTX 5090 and WARP withdxil.dll).cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default &--features host): 0 warnings;cargo xtask policy: 185 files ok.Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:21:45Z.
Automated triage for the maintainers.
This PR replaces the round-only
round16analysis incrates/victory-shader/src/emit/lower.rswithFpMode16, which tracks MODE bits 3:2 and 7:6 with a per-field join. It also lowersv_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 thes_round_mode/s_denorm_mode/s_setregbit 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::INITIALassumes subnormals are preserved at shader entry. That is an assumption, like the existing nearest-even round.FpMode16refactor is the same as in #2743 (same author), so whichever lands second needs a rebase. #2782'sself.mode16.ok_or(...)would have to becomeself.mode16.round.ok_or(...).FpMode16::afterare longer than rustfmt would leave them.This is a recommendation for a maintainer to look, not an approval.
Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T03:07:38Z.
Review: APPROVED
Tested merged onto
origin/main(63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP,dxil.dllvalidator active):cargo test -p victory-shader --lib emit::valu_tests::sqrt: 2 passed, 0 failed (sqrt_f64_preserves_signed_zero_when_mode_flushes_subnormalsandsqrt_f64_tracks_binary64_range_and_roundingpass 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).
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T00:34:51Z.
Review: CHANGES_REQUESTED
Nice 32-bit fixed-point
v_sqrt_f64implementation — the 24-bit digit-recurrence math, rounding (within2^28ULP for RTE and2^29ULP for directed modes), special cases (including-0.0, NaN quietening/payloads,abs/neg, and all source kinds), andSFI0(0x8001 == 0) check out cleanly.emit::valu_tests::sqrt(2/2 on RTX 5090 and WARP),emit::lower::tests(33/33),clippy, andxtask policyall pass, andsqrt.rs/ the newvalu_tests.rslines passrustfmt.One live-reproduced silent miscompilation in
FpMode16tracking needs fixing before merge (plus two smaller items):MODE writes inside whole-wave sections are skipped during lowering (
crates/victory-shader/src/emit/lower.rs:1383-1389)self.sectionsrunssection_instforj in i + 1..endand jumpsi = end,self.mode16is not updated acrossi + 1..end(even thoughfp_modes16does walk every instruction in block order).s_denorm_mode 8(flush FP16/FP64 inputs) betweens_or_saveexec_b32and theexecrestore left a subsequentv_sqrt_f64in the same block using stale preserve mode, returning0x1e60_0000_0000_0000and0xfff8_0000_0000_0000for1andSIGN | 1instead of+0.0and-0.0. Updatingself.mode16across the skipped section instructions fixes this.Partial-field MODE writes and mode
1/3joins drop known input-denorm state (lower.rs:311-333)s_setreg_imm32_b32 hwreg(MODE, 7, 1), 0writes only bit 7 (output denorm flush), leaving bit 6 (input denorm preserve) unchanged, yetFpMode16::afterrequiresoffset <= start && end >= start + 2and otherwise resets the entiredenormfield toNone. Similarly, joining paths withdenorm = 1anddenorm = 3loses the shared input-denorm bit.s_setreg_imm32_b32 hwreg(MODE, 7, 1), 0beforev_sqrt_f64fails to lower withbinary64 square root with unknown floating-point modes, even thoughsqrt_f64only depends on the input-denorm bit (denorm & 1). Tracking the input-denorm bit independently (or merging partial literal bitfield updates) avoids rejecting valid shaders.Minor formatting (
lower.rs:296,:303,:319)rustfmt --edition 2024 --checkflags line wrapping on the addedFpMode16methods atcrates/victory-shader/src/emit/lower.rs:296,:303, and:319.Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T06:40:10Z.
The follow-up ce63f02 carries MODE updates across whole-wave sections and tracks the input-denormal bit independently. A write to the output-denormal bit and a join of input-preserving modes now retain the known input mode.
On the previous head, the synthetic whole-wave case returned nonzero/NaN for signed subnormal inputs that should flush to signed zero. The partial output-only write was rejected as an unknown mode. On the updated head, both regressions and the two existing binary64 square-root tests passed on app-local WARP with DXIL validation. Strict shader Clippy passed in the default and all-features configurations.
Ready for another review. If reused, please retain Git authorship when cherry-picking or rebasing. If folded into another commit, please add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.AI-assisted with Codex. Synthetic shaders only.
I also resolved the merge overlap with #2782 in b0dab3c on current
main63bcca4. The scalar MODE read and sqrt use a shared mode state in that tree. All 42 VALU tests passed on app-local WARP.Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-27T06:45:04Z.
Thanks @fuzeofficlal. The three cases from your review reproduced on
f30cf4e: flushing inside a whole-wave section left the two signed subnormal results wrong, and the output-only write and the 1/3 join both made the input mode unknown. The current ce63f02 addresses these cases.I also checked the interaction you called out with #2782. The combined branch carries both published heads on main
63bcca4, preserving their commits. Its integration resolves the whole-wave update once throughFpMode16::afterand readsself.mode16.roundfors_getreg_b32. I ran both sides of the 1/3 join with distinct subnormal inputs.WARP passed all 42 VALU tests, including the sqrt and MODE-read cases. Strict all-features shader Clippy and asset policy passed on the combined tree. The branch is available for the second PR's rebase when the first lands.
AI-assisted with Codex. Synthetic shaders only.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.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.