Shaders: lower v_trig_preop_f64 argument reduction #2988

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

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 02:25 UTC · original
andrewpozdnakov7-jpg:codex2/trig-preop-f64 → main · +344 −1 · 3 files · 2 commits · open

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 #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 #2953 or #2975 is the adjacent module declaration.

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

AI-assisted with Codex.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 02:25 UTC · [original](https://github.com/Marvisak/VIctory/pull/2988) `andrewpozdnakov7-jpg:codex2/trig-preop-f64` → `main` · +344 −1 · 3 files · 2 commits · open ## 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 #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 #2953 or #2975 is the adjacent module declaration. Sources: [AMD RDNA2 ISA guide](https://docs.amd.com/api/khub/documents/Et~wpu9g~Ffl7d9q0QZ~Og/content), [RDNA2 table measurements](https://github.com/boykopovar/AnyPS5/pull/447), and [hardware result rows](https://github.com/boykopovar/AnyPS5/pull/22688). AI-assisted with Codex.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:19:27 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-26 02:48 UTC · original

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).

**@⁠oni-swr** on GitHub · 2026-11-26 02:48 UTC · [original](https://github.com/Marvisak/VIctory/pull/2988#issuecomment-7181138073) 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).
Author
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-26 03:05 UTC · original

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

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-26 03:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/2988#issuecomment-7181382950) 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](https://github.com/Marvisak/VIctory/commit/313acc318caabb79730df05b73f4ef2befbd779a) 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](https://github.com/boykopovar/AnyPS5/pull/447)
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 05:44 UTC · original

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

**@⁠Marvisak** on GitHub · 2026-11-26 05:44 UTC · [original](https://github.com/Marvisak/VIctory/pull/2988#issuecomment-7183762913) <!-- victory-triage v1 head=313acc318caabb79730df05b73f4ef2befbd779a verdict=merge-candidate at=2026-11-26T05:44:18Z --> 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](https://claude.ai/code)_
This pull request can be merged automatically.
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/2988/head:github-bot/gh-2988'
git switch 'github-bot/gh-2988'

Merge

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