Skip to content

Shaders: lower v_trig_preop_f64 argument reduction - #2988

Open
andrewpozdnakov7-jpg wants to merge 2 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/trig-preop-f64
Open

andrewpozdnakov7-jpg wants to merge 2 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/trig-preop-f64

Conversation

@andrewpozdnakov7-jpg

@andrewpozdnakov7-jpg andrewpozdnakov7-jpg commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Lower v_trig_preop_f64 by reading the selected 53-bit window of 2/pi, applying the large-input scale and packing both binary64 words with truncation. The table uses the measured 1184-bit RDNA2 prefix. All operations use i32 DXIL under VIctory's current f64 denormal model.

Verification

  • The same six synthetic forms are rejected as unsupported on main. The patch matches both words for all 106 rows on Agility WARP with DXIL validation, including the measured rows with f64 denormals kept, exponent thresholds, VGPR and SGPR pairs, literals and modifiers.
  • Three model-derived tail cases distinguish the table widths. Temporarily restoring the remaining 17 bits makes the same fixture fail all three cases, with and without modifiers. The 1184-bit choice follows the Radeon 660M end-of-table sweep reported in AnyPS5 Shaders: a signed 64-bit operand sign-extends its 32-bit literal #447.
  • A separate WARP check compared two translated instructions per input against an independent arbitrary-precision calculation across all 65,536 exponent/selector pairs. All 131,072 results matched.
  • 35 VALU, 35 semantics (one ignored), and 33 lowerer tests passed. Strict Clippy in both feature sets and asset policy passed. The only pairwise overlap with Shaders: lower binary64 division fixup in DXIL #2953 or Shaders: lower v_div_scale_f64 and its lane mask #2975 is the adjacent module declaration.

Sources: AMD RDNA2 ISA guide, RDNA2 table measurements, and hardware result rows.

AI-assisted with Codex.

@kotekompotek kotekompotek left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Verified: correct Payne-Hanek window semantics — segment&31 *53 + max(0, exp-1077) indexes the 2/pi bitstream, the 1968 large-exp boost shifts by +128 (table clamp to the zero tail handles overflow), and the pack path normalizes the 53-bit window via leading-zero count with a truncate-toward-zero subnormal path. Exponent-only input handling is sound (inline consts are all below the shift threshold; abs/neg don't touch the exponent). The test rows pin the exact 1077 and 1968 boundaries plus sel=0x20 wrap and inf/NaN inputs.

@oni-swr

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

Independent test report: Windows, software WARP only, synthetic inputs only. Not a code review.

Head 9b0f44f0c3d62470cb25a23b1d2a6c052ccc9c84; exact parent 63bcca43c6a00a758510398fa43096ce7e4f7df0.

Setup: VICTORY_WARP=1, VICTORY_D3D12_WARP=1, VICTORY_DXIL set to the Windows SDK 10.0.26100.0 x64 dxil.dll (1.8.2502.11), and a dedicated CARGO_TARGET_DIR. Before any dispatch, cargo test -j 4 --locked -p victory-gpu --features diagnostics --lib -- --exact host::tests::warp_adapter_accepts_only_software --nocapture reported EnumWarpAdapter: vendor=0x1414, flags=0x2 (software). With VICTORY_WARP=1, Host::new takes the WARP device for both of check()'s hosts, so the "hardware" label in the output is also WARP. No physical GPU was used.

Results (cargo test -j 4 --locked -p victory-shader --lib -- <test> --exact --nocapture --test-threads=1):

Run Result
Head: binary64_trig_preop_returns_the_argument_reduction_segment pass, 2/2 runs
Head: emit::semantics_tests:: (whole module) 35 passed, 1 ignored, 0 failed
Head: focused sweep (local test, not pushed), 16,704 rows 0 bitwise mismatches, 2/2 runs
Parent + this PR's test hunk only fails on all six probe forms with not lowered: GFX10 code at 0x44/0x48/0x58: v_trig_preop_f64 is not supported, 2/2 runs

Independent reference: I wrote a Python model from the RDNA2 ISA pseudocode. Its 2/π bits come from mpmath at 1600 bits of precision, not from the PR's table. The model's rules:

  • shift = (S1 & 31) * 53 + max(0, exp - 1077)
  • add +128 to the scale when exp ≥ 1968
  • truncate subnormal results toward zero

It matches all 48 rows of the PR's test exactly.

Sweep: 16,704 rows, with the expected values taken from that model:

  • exponents 0–2, 1022–1024, 1068–1087, 1960–1975, 2040–2047, plus 8 random exponents
  • for each exponent: mantissas 0, 1, all ones and one random value, both signs
  • selectors 0–31, 0x20, 0x21, 0x3f and 0xffffffff

Limitation: table length. If the model uses the full 1201-bit table that the ISA text gives ((2/PI)[1200:0]), 144 of the 16,704 rows differ from this head's WARP output. Those are exactly the rows whose 53-bit window reaches past fractional bit 1184. Two examples: exp 1968–1975 with selector 5, and exp 2040–2047 with selector 4. In each case the head returns zeros for the low bits. Truncating the model's table to 1184 bits removes every mismatch, which matches the PR's documented 1184-bit measured table. I can't confirm on RDNA2 silicon which length is right. The PR's own rows don't distinguish the two lengths: all 48 match both. A measured hardware row whose window reaches past bit 1184 (for example exp 1968, selector 5) would pin it.

Disclosure: tested with Claude Code (Claude Opus 5.5).

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code review on 9b0f44f0 (static source & bit-exact integer model verification; emitted DXIL was not executed on hardware):

  • crates/victory-shader/src/emit/lower.rs:6319 wires v_trig_preop_f64 into crates/victory-shader/src/emit/lower/trig_preop.rs:49-101, checking the destination register pair, masking the selector, bounding table loads/shifts (:158-230), and writing both output words under the active execution guard.
  • Verified the lowered word arithmetic and table indexing against an independent integer model across all 65,536 exponent × masked-selector combinations, matching every case including all 48 fixture rows in semantics_tests.rs:3022.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor Author

Thanks for checking both tables. I also compared the 96 published trig results in AnyPS5 #447. Both models match those cells. The width comes from its separately reported Radeon 660M end-of-table sweep.

313acc3 adds three model-derived cases that distinguish the widths. I ran the same fixture on Agility WARP with the production table and with a temporary 1201-bit table.

Biased exponent Selector 1184 bits 1201 bits
1968 5 0x0000180cc1180000 0x0000180cc11bf1ec
2000 4 0x00f5d08560330460 0x00f5d0856033046f
2047 4 0x0000000000080000 0x00000000000bf1ec

The production table passed all 106 cases. The extended table failed these three cases in each modifier form. The table here contains model and WARP results. Direct RDNA2 observations for these three input pairs would provide a separate check of the reported tail width.

Original hardware sweep

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Verdict: merge candidate. This lowers v_trig_preop_f64, which main refuses, in a new emit/lower/trig_preop.rs. It's a new capability, and the evidence is stated: the RDNA2 ISA pseudocode plus published RDNA2 hardware rows.

The lowering reads a 53-bit window of 2/pi starting at (sel & 31) * 53 + max(0, exp - 1077), adds the +128 scale when exp >= 1968, and packs the binary64 result, truncating subnormals. All of it is i32 DXIL, with no f64 ops needed. The table is the 1184-bit prefix the hardware sweep measured, not the ISA text's 1201 bits, and the code comment says so. The three tail cases added in 313acc3 pin that choice. An independent tester above matched 16,704 rows against an mpmath model. operand64_dwords is split out of the existing 64-bit operand read without a behaviour change, apart from a checked reg + 1.

One point to check before merging: the source Const(128..=208) arm uses 0 as the high word. For a 64-bit operand I'd expect the negative inline integers (193-208) to be sign-extended, giving a high word of 0xffffffff (exponent 0x7ff). If so, those few forms should be refused or given that high word. No compiler is likely to emit them for this opcode.

CI: passing (check). Recommending a look, not approving.


Generated by Claude Code

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

  • Diff review: Lowering v_trig_preop_f64 in its own emit/lower/trig_preop.rs submodule keeps lower.rs within the 512 KiB file-size policy limit and verifies argument reduction end-to-end on GPU.
  • cargo test -p victory-shader trig_preop -- --test-threads=1 (RTX 5090 & WARP): all tests passed on both.
  • 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).

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…ment reduction

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…ment reduction

# Conflicts:
#	crates/victory-shader/src/emit/lower.rs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants