Shaders: lower binary64 reciprocal with 32-bit operations #2797

Open
andrewpozdnakov7-jpg wants to merge 2 commits from upstream-pr/2799 into main
andrewpozdnakov7-jpg commented 2026-11-25 20:49:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T20:49:24Z.


Main stops translation on V_RCP_F64. This lowering normalizes the binary64 input and computes a 26-bit quotient with 32-bit operations. It applies the known rounding direction and input/output denormal modes. It also handles signed zeros and infinities, quiets NaNs, and uses the exact exponent to classify overflow. AMD specifies an accuracy bound of 2^29 binary64 ULP for this instruction. RDNA 2 ISA reference

Both synthetic shaders stop on unsupported v_rcp_f64 at 13f91ca. The same fixtures pass all 160 cases on app-local WARP SM 6.6 with the DXIL validator. The cases cover VOP1 VGPR and VOP3 SGPR sources, both signs, the four rounding and denormal modes, and the finite boundary next to overflow. The fixtures verify that the emitted DXIL requires neither doubles nor int64. An independent integer model passed 1,572,096 comparisons to CPU binary64, with the largest measured finite discrepancy below 197 million ULP (the limit is 536,870,912).

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

This uses the same FP16/FP64 mode analysis as #2743 and #2791. 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:5498943191 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2799). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T20:49:24Z. --- Main stops translation on `V_RCP_F64`. This lowering normalizes the binary64 input and computes a 26-bit quotient with 32-bit operations. It applies the known rounding direction and input/output denormal modes. It also handles signed zeros and infinities, quiets NaNs, and uses the exact exponent to classify overflow. 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 shaders stop on unsupported `v_rcp_f64` at `13f91ca`. The same fixtures pass all 160 cases on app-local WARP SM 6.6 with the DXIL validator. The cases cover VOP1 VGPR and VOP3 SGPR sources, both signs, the four rounding and denormal modes, and the finite boundary next to overflow. The fixtures verify that the emitted DXIL requires neither doubles nor int64. An independent integer model passed 1,572,096 comparisons to CPU binary64, with the largest measured finite discrepancy below 197 million ULP (the limit is 536,870,912). The full serial shader suite changed from 268 passed, 5 failed, and 3 ignored to 270 passed, 3 failed, and 3 ignored with both new tests present. The other three failures were the same gather-at-level, lane-permute, and edge-flagged-triangle tests on both revisions. The patched VALU run passed all 33 tests including runtime identity. Strict shader Clippy and staged asset policy passed. This uses the same FP16/FP64 mode analysis as #2743 and #2791. [Coordination claim](https://github.com/Marvisak/VIctory/issues/1633#issuecomment-7174008731) 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:18:23 +00:00 (Migrated from github.com)

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

Review: COMMENTED


The FpMode16 refactor is the same direction as the codex2 stack (#2743/#2791) — tracking MODE's f64 round (bits 3:2) and denorm (bits 7:6) independently is needed here because rcp64 must refuse when either is unknown or denorm-flushing. join is conservative in the right direction (mismatched or unknown on any incoming path -> unknown), and unreached blocks default to all-unknown, so nothing lowers a mode-dependent op on a guess. The significant-value work itself (28-bit divisor + 26-bit quotient + Newton + correct subnormal normalization and special-case handling) reads carefully; the test sweeps range/rounding/specials on both the emit and run sides. LGTM — worth noting this shares the mode16->FpMode16 base with the codex2 PRs, so whichever lands first wins the refactor.

<!-- greyfinch-sync:Marvisak/VIctory:review:6065119677 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2799#pullrequestreview-6065119677). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T21:18:23Z. **Review:** COMMENTED --- The FpMode16 refactor is the same direction as the codex2 stack (#2743/#2791) — tracking MODE's f64 round (bits 3:2) and denorm (bits 7:6) independently is needed here because rcp64 must refuse when either is unknown or denorm-flushing. `join` is conservative in the right direction (mismatched or unknown on any incoming path -> unknown), and unreached blocks default to all-unknown, so nothing lowers a mode-dependent op on a guess. The significant-value work itself (28-bit divisor + 26-bit quotient + Newton + correct subnormal normalization and special-case handling) reads carefully; the test sweeps range/rounding/specials on both the emit and run sides. LGTM — worth noting this shares the mode16->FpMode16 base with the codex2 PRs, so whichever lands first wins the refactor.
fuzeofficlal commented 2026-11-25 22:04:26 +00:00 (Migrated from github.com)

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


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

Code review

  • Pure 32-bit DXIL (SFI0 & 0x8001 == 0, no double/int64 capability required).
  • ±0.0 -> ±Inf, ±Inf -> ±0.0, sNaN -> qNaN with sign/payload preserved (including low-word-only payloads), MODE[7:6] input/output denormal flush vs. FirstbitHi normalization (2^1022 -> MIN_NORMAL not flushed), exact-exponent overflow & coarse_overflow clamp to ±f64::MAX, and 26-bit long division across all 4 rounding modes (within RDNA2's <= 2^29 binary64 ULP bound) all check out cleanly.
  • Cross-PR notes: clean git merge-tree vs 13f91ca; conflicts in lower.rs with #2743 and #2791 (shared FpMode16 refactor); textually clean vs #2782, but changing Lower::mode16 to FpMode16 requires updating #2782's self.mode16.ok_or(...) (lower.rs:3384) to 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 (including both new reciprocal_f64_* tests on RTX 5090 and WARP).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 185 files ok.
<!-- greyfinch-sync:Marvisak/VIctory:comment:7174945982 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2799#issuecomment-7174945982). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T21:20:31Z. --- Reviewed and verified `f37bb61a` on Windows 11 (RTX 5090 + WARP + Windows SDK `10.0.26100.0` `dxil.dll`) against `main` (`13f91ca`). ### Code review - Pure 32-bit DXIL (`SFI0 & 0x8001 == 0`, no double/int64 capability required). - `±0.0 -> ±Inf`, `±Inf -> ±0.0`, `sNaN -> qNaN` with sign/payload preserved (including low-word-only payloads), `MODE[7:6]` input/output denormal flush vs. `FirstbitHi` normalization (`2^1022 -> MIN_NORMAL` not flushed), exact-exponent overflow & `coarse_overflow` clamp to `±f64::MAX`, and 26-bit long division across all 4 rounding modes (within RDNA2's `<= 2^29` binary64 ULP bound) all check out cleanly. - **Cross-PR notes**: clean `git merge-tree` vs `13f91ca`; conflicts in `lower.rs` with `#2743` and `#2791` (shared `FpMode16` refactor); textually clean vs `#2782`, but changing `Lower::mode16` to `FpMode16` requires updating `#2782`'s `self.mode16.ok_or(...)` (`lower.rs:3384`) to `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** (including both new `reciprocal_f64_*` tests on RTX 5090 and WARP). - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default & `--features host`): **0 warnings**; `cargo xtask policy`: **185 files ok**.
Marvisak commented 2026-11-26 00:03:28 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T23:23:37Z.


Automated triage for the maintainers.

This adds a lowering for v_rcp_f64 (Lower::reciprocal_f64 in the new crates/victory-shader/src/emit/lower/reciprocal.rs) using only 32-bit operations, with a 26-bit quotient held to the 2^29 binary64 ULP bound the public RDNA2 ISA reference gives for V_RCP_F64, plus zero/infinity/NaN handling and the 16/64-bit round and denormal modes. To get the denormal mode it replaces round16/round16_after in lower.rs with an FpMode16 analysis that also tracks MODE bits 7:6 (s_denorm_mode, s_setreg); I checked that the round-mode part keeps main's behaviour for narrow16 and v_fma_f16, and the lowering refuses to translate when either mode is unknown. Two things for the maintainer to weigh: the entry value FpMode16::INITIAL assumes denormals preserved for 16/64-bit with no cited source, and the same FpMode16 refactor is in #2743 and #2791, so whichever lands second will need a rebase. CI (check) passes.

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


Generated by Claude Code

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177498244 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2799#issuecomment-7177498244). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-25T23:23:37Z. --- <!-- victory-triage v1 head=ace1ab9d53d35e558ef2ccd90429e455d09973c4 verdict=merge-candidate at=2026-11-25T23:23:30Z --> Automated triage for the maintainers. This adds a lowering for `v_rcp_f64` (`Lower::reciprocal_f64` in the new `crates/victory-shader/src/emit/lower/reciprocal.rs`) using only 32-bit operations, with a 26-bit quotient held to the 2^29 binary64 ULP bound the public RDNA2 ISA reference gives for V_RCP_F64, plus zero/infinity/NaN handling and the 16/64-bit round and denormal modes. To get the denormal mode it replaces `round16`/`round16_after` in `lower.rs` with an `FpMode16` analysis that also tracks MODE bits 7:6 (`s_denorm_mode`, `s_setreg`); I checked that the round-mode part keeps `main`'s behaviour for `narrow16` and `v_fma_f16`, and the lowering refuses to translate when either mode is unknown. Two things for the maintainer to weigh: the entry value `FpMode16::INITIAL` assumes denormals preserved for 16/64-bit with no cited source, and the same `FpMode16` refactor is in #2743 and #2791, so whichever lands second will need a rebase. CI (`check`) passes. This is a recommendation for a maintainer to look, not an approval. --- _Generated by [Claude Code](https://claude.ai/code)_
Dukestar69 commented 2026-11-26 02:51:55 +00:00 (Migrated from github.com)

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

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::reciprocal: 2 passed, 0 failed (reciprocal_f64_obeys_input_and_output_denormal_modes and reciprocal_f64_tracks_range_rounding_and_special_values 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:6067910499 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2799#pullrequestreview-6067910499). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T02:51:55Z. **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::reciprocal`: **2 passed, 0 failed** (`reciprocal_f64_obeys_input_and_output_denormal_modes` and `reciprocal_f64_tracks_range_rounding_and_special_values` 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).
fuzeofficlal commented 2026-11-27 00:18:44 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-27T00:18:44Z.

Review: APPROVED


LGTM — very clean 32-bit integer reciprocal lowering in lower/reciprocal.rs, keeping SFI0 free of INT64/FLOAT64 flags (0x8001) and lower.rs well under 512 KiB (521,169 B).

Verified locally with cargo xtask policy, cargo clippy (--all-targets and --all-features), emit::valu_tests:: on RTX 5090 (37/37 passed), and both reciprocal_f64_* tests on WARP (2/2 passed), covering all four rounding and denormal modes, overflow/underflow, and special values well within RDNA2's 2^29 ULP bound (checked across 5.7M model samples: max <= 3 * 2^25 ULP for RNE and <= 3 * 2^26 ULP for directed modes). Returning Err("binary64 reciprocal modifiers") for omod/clamp is cleanly fail-closed.

Minor non-blocking polish notes if you revisit:

  • rustfmt --edition 2024 --check flags line wrapping on the new FpMode16 lines at crates/victory-shader/src/emit/lower.rs:296, 303, 319 (reciprocal.rs and valu_tests.rs are clean).
  • Future follow-up ideas: supporting omod/clamp on v_rcp_f64_e64 and adding test cases for abs/neg, explicit qNaN input, and single-bit MODE[6] writes.
<!-- greyfinch-sync:Marvisak/VIctory:review:6081912805 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2799#pullrequestreview-6081912805). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-27T00:18:44Z. **Review:** APPROVED --- LGTM — very clean 32-bit integer reciprocal lowering in `lower/reciprocal.rs`, keeping `SFI0` free of `INT64`/`FLOAT64` flags (`0x8001`) and `lower.rs` well under 512 KiB (`521,169 B`). Verified locally with `cargo xtask policy`, `cargo clippy` (`--all-targets` and `--all-features`), `emit::valu_tests::` on RTX 5090 (37/37 passed), and both `reciprocal_f64_*` tests on WARP (2/2 passed), covering all four rounding and denormal modes, overflow/underflow, and special values well within RDNA2's `2^29` ULP bound (checked across 5.7M model samples: max <= `3 * 2^25` ULP for RNE and `<= 3 * 2^26` ULP for directed modes). Returning `Err("binary64 reciprocal modifiers")` for `omod`/`clamp` is cleanly fail-closed. Minor non-blocking polish notes if you revisit: - `rustfmt --edition 2024 --check` flags line wrapping on the new `FpMode16` lines at `crates/victory-shader/src/emit/lower.rs:296, 303, 319` (`reciprocal.rs` and `valu_tests.rs` are clean). - Future follow-up ideas: supporting `omod`/`clamp` on `v_rcp_f64_e64` and adding test cases for `abs`/`neg`, explicit qNaN input, and single-bit `MODE[6]` writes.
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/2799:upstream-pr/2799'
git switch 'upstream-pr/2799'

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