Skip to content

Shaders: lower v_div_fixup_f16 - #379

Open
LouisRiverstone wants to merge 3 commits into
Marvisak:mainfrom
LouisRiverstone:shader-div-fixup-f16
Open

LouisRiverstone wants to merge 3 commits into
Marvisak:mainfrom
LouisRiverstone:shader-div-fixup-f16

Conversation

@LouisRiverstone

Copy link
Copy Markdown
Contributor

v_div_fixup_f16 (VOP3 0x35f) was not lowered. Compilers emit it after every f16 division, so those programs failed to translate.

It follows the ISA's checks, in the ISA's order. S0 is the quotient, S1 the denominator, S2 the numerator:

  • a NaN numerator, then a NaN denominator, comes back quieted;
  • 0/0 and inf/inf give NaN;
  • x/0 and inf/x give an infinity with the quotient's sign (sign of S1 xor sign of S2);
  • 0/x and x/inf give a signed zero;
  • anything else is |S0| with that sign.

The operands are worked on as their f32 values, which are exact, and the result is narrowed like the other f16 three-operand ops: clamp applies, an output modifier is refused. #200 adds v_div_fixup_f32 in another part of lower.rs. This PR doesn't touch it, and the two don't conflict.

Test: v_div_fixup_f16_follows_the_isa_special_cases in valu_tests.rs. It runs 300 cases on the host GPU and WARP and checks them against a reference written from the ISA on the f16 bits: three quotients (one of them NaN) × ten denominators × ten numerators, covering ±0, ±1.5, a subnormal, max finite, ±inf and ±NaN.

  • Windows 11, RTX 4080 and WARP: passes, and DXIL validation with dxil.dll 10.0.26100 passes.
  • Without the lower.rs change it fails with Unsupported { what: "v_div_fixup_f16" }.
  • Clippy and cargo xtask policy are clean.

No game files.

AI-assisted (Claude Code): I reviewed the change and ran the tests above.

muhammed-aksoy pushed a commit to muhammed-aksoy/VIctory that referenced this pull request Nov 24, 2026
@jeremygold02

Copy link
Copy Markdown
Contributor

Thanks @LouisRiverstone for adding this missing f16 fixup lowering. I tested head dfaa5aaaf9398fcadfaa2af014802e2800da0398 with:

cargo test --locked -p victory-shader --lib v_div_fixup_f16_follows_the_isa_special_cases -- --test-threads=1

Result: 1 passed, 0 failed (69 filtered out). The synthetic quotient/denominator/numerator cases cover the ISA special-value rules for signed zeros, subnormal and finite values, infinities, and NaNs against the bit-based reference. This does not measure behavior on console hardware.

@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

RDNA2 hardware measurement against the test's reference: it matches everywhere except one case: a NaN quotient with a finite, non-zero numerator and denominator gives ±infinity on the hardware, not NaN.

Setup. AMD Radeon iGPU of a Ryzen 7 9800X3D, gfx1036 (RDNA2), AMD driver 32.0.21042.62, Windows 11 Pro 26200. A HIP kernel (assembled by the AMD driver's amd_comgr) runs v_div_fixup_f16 v10, v4, v5, v6 (quotient, denominator, numerator) on 1084 triples. 484 come from your value set plus 0x7d00 (a signalling NaN), with quotients 0x4248, 0xc248, 0x7e00, 0x7c01; the other 600 are random 16-bit triples. The destination was preset to 0xdead0000. RDNA2 desktop silicon, not the console's. No game data.

Fit: 1034/1084 match div_fixup_f16_reference. All 50 that don't are the same case:

quotient denominator numerator hardware reference
0x7e00 0x3c00 0x3c00 0x7c00 NaN
0x7e00 0x3c00 0xbe00 0xfc00 NaN
0x7e00 0x0001 0x7bff 0x7c00 NaN
0x7c01 0xbe00 0xbe00 0x7c00 NaN

The result is sign | 0x7c00, with the usual sign (denominator xor numerator). That fits the ISA's later "exponent of S0 all ones → overflow" step coming before "else |S0|". The zero, infinity and 0/0 cases come first, as in your reference: a NaN quotient with a zero numerator still gives a signed zero (matched).

Other values seen: 0/0 and inf/inf give 0xfe00, and a NaN numerator or denominator comes back quieted with its payload (0x7d00 → 0x7f00, 0xfe01 → 0xfe01), as your reference allows. The high half is kept: 0xdead on all 1084, which your test cannot see because it starts from 0.

The same holds for v_div_fixup_f32: a NaN quotient with 1.0 / 1.0 gives 0x7f800000.

AI use: Claude Code (Claude Opus 5.5) wrote the probes, ran them on the GPU above and drafted this comment; the numbers come from the hardware run.

@slint6614-dev

Copy link
Copy Markdown

AMD hardware check, Windows 11, Adrenalin driver: RX 9070 (RDNA4) and Ryzen 9600X iGPU (RDNA2, gfx1036), each picked as the hardware device of run_cases (local adapter-selection patch, not part of this PR); cargo test -p victory-shader --release valu_tests at the PR head. Negative control: the PR's non-test files reverted to its merge base, its new tests kept.

  • With this PR, v_div_fixup_f16_follows_the_isa_special_cases pass on both AMD GPUs.
  • Negative control: it fail on both GPUs without the fix, so the tests do catch the change on AMD hardware.

The ISA's special cases in its order: a NaN numerator or denominator comes
back quieted, 0/0 and inf/inf give NaN, x/0 and inf/x a signed infinity,
0/x and x/inf a signed zero, otherwise |S0| with the quotient's sign.
Worked on the f32 values of the f16 operands, which are exact.
@LouisRiverstone

LouisRiverstone commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor Author

Thanks @Lyrnc for the hardware run. The NaN-quotient case is fixed in 5ecb6ab.

  • Lowering: a quotient with an all-ones exponent (NaN included) now gives sign | 0x7c00 (sign | inf in f32), applied after the zero, infinity and invalid checks. A NaN quotient with a zero numerator still gives a signed zero, as you measured.
  • Test: div_fixup_f16_reference follows the hardware for this case, and the inputs now include quotient 0x7c01 (sNaN).
  • Rebased onto main to resolve the conflict.

cargo test -p victory-shader --release on Windows: 121 passed, 0 failed. Negative control: with only the lowering change reverted, the test fails with v_div_fixup_f16(0x7e00, 0x3c00, 0x3c00) = 0x7fff, want 0x7c00.

v_div_fixup_f32 isn't lowered on main yet, so it has nothing to change for now.

AI use: Claude Code (Claude Opus 5.5) wrote the fix and ran the tests.

The ISA's "exponent of S0 all ones -> overflow" check runs before the
final |S0| case, so a NaN quotient with a finite, non-zero numerator and
denominator gives sign | infinity, as measured on gfx1036 hardware. The
zero, infinity and invalid checks still come first.
@Halfect

Halfect commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent check of the corrected head 5ecb6ab on Windows 11:

  • cargo test --locked -p victory-shader --lib -- --test-threads=1: 121 passed, 0 failed, 1 ignored.
  • v_div_fixup_f16_follows_the_isa_special_cases: passed on the default D3D12 hardware adapter, covering all 400 quotient/denominator/numerator combinations, including the corrected NaN/sNaN-quotient overflow cases. DXIL validation passed as part of the test. WARP was unavailable, so no WARP result is claimed here.
  • cargo clippy --locked -p victory-shader --lib --tests -- -D warnings: clean.
  • cargo xtask policy: 154 files ok.

No game data or title execution used.

@catminou4 catminou4 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.

v_div_fixup_f16 is the fiddly one — NaN propagation (numerator first), 0/0 and inf/inf → NaN, x/0 and inf/x → signed infinity, 0/x and x/inf → signed zero, all-ones-exponent quotient → signed infinity (gfx1036 behavior, noted), else |q| with quotient sign. The select chain is ordered "last check to first" so the first matching rule wins, which is the correct way to encode the ISA's priority list, and the comment documents each rule.

Test quality is excellent: div_fixup_f16_reference is an independent Rust implementation of the ISA table, and the execution test sweeps 4 quotients × 10 den × 10 num = 400 cases including NaNs, asserting the high half stays untouched.

Verified locally: test-merge with current main (ffe310e) clean; cargo test -p victory-shader --locked (macOS arm64, rustc 1.98.1) green — the new valu_tests execution cases are #[cfg(windows)]-gated like the rest of that suite, so they will run on the Windows CI lane, not here.: 96 passed / 0 failed / 1 ignored (execution test Windows-gated).

nazmi pushed a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Applied as the net diff of upstream PR Marvisak#379 (head 5ecb6ab), 2 commits: 5ecb6ab,85b04965.
@Lyrnc

Lyrnc commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Re-checked 5ecb6ab against the hardware, using the same encodings on both sides:

  • Hardware: Radeon iGPU (Ryzen 7 9800X3D), gfx1036 / RDNA2, driver 32.0.21042.62. I ran v_div_fixup_f16 on 1,414 (q, den, num) triples. That's 6 quotients (0x4248, 0xc248, 0x7e00, 0x7c01, 0xfc00, 0x7bff) against 13 edge values for den and num (zeros, ±inf, NaNs, denormals, max), plus 400 random triples.
  • Translator: the same VOP3 word through build_valu_cs, on the RTX 5080 (610.88) and on the same Radeon iGPU (GPU: prefer high-performance D3D12 adapter and support VICTORY_ADAPTER #94's VICTORY_ADAPTER).
Host matches the hardware bit for bit NaN with a different payload other differences
Radeon gfx1036 1,366 / 1,414 48 0
RTX 5080 926 / 1,414 488 0

So the NaN-quotient case now gives sign | 0x7c00, as the hardware does. Every non-NaN result matches, including the signed zero for a NaN quotient with a zero numerator. The only differences are which NaN comes back (low 16 bits compared). No game data.

AI use: Claude Code (Claude Opus 5.5) wrote the probe and the comparison and ran them; the numbers are from those runs.

@Yaekai

Yaekai commented Nov 24, 2026

Copy link
Copy Markdown
Contributor

Independent verification on Windows 11 + NVIDIA RTX 3070 (Rust 1.98.1) at head 5ecb6ab:

  • cargo test -p victory-shader --lib -- --skip valu_tests --skip waterfall_tests: 102 passed, 0 failed.
  • cargo clippy -p victory-shader -- -D warnings: 0 warnings.
  • Live GTA VI (1.0.2.1, --console series-s, 19-min world run): no regressions. Ready to merge!

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026
KainanP pushed a commit to KainanP/VIctory that referenced this pull request Nov 24, 2026
…fied together

Merges Marvisak#156 Marvisak#162 Marvisak#168 Marvisak#169 Marvisak#368 Marvisak#379 Marvisak#456 Marvisak#587 Marvisak#990 Marvisak#1021 Marvisak#1048 Marvisak#1052 Marvisak#1053
Marvisak#1095 Marvisak#1142 Marvisak#1190 Marvisak#1211 Marvisak#1223 Marvisak#1259 Marvisak#1293 on top of the bundle of Marvisak#1402
(which is on Marvisak#1319, on main ffe310e), each head unchanged. Hunks that
conflicted in emit/lower.rs, emit/valu_tests.rs and emit/diff_tests.rs were
merged by keeping both sides (additions next to each other); a PR was kept
only if cargo clippy -p victory-shader --all-targets -- -D warnings still
passed with it. No instruction name is handled in two places.

Marvisak#98 is not in this version: an offline measurement on a retail dump showed
it loses 31 programs that validate on Marvisak#1402.

cargo clippy --workspace --all-targets --locked -- -D warnings, with and
without --all-features, is clean and cargo test --workspace --locked passes
(381 passed, 6 ignored; main has 342 passed, 6 ignored).

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 24, 2026

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Rebased to 55019c8, which merges cleanly into main. The change is the same one reviewed before. v_div_fixup_f16 joins the f16 three-operand list in crates/victory-shader/src/emit/lower.rs, and the new div_fixup does the ISA's checks in the ISA's order on the exact f32 values: a NaN numerator, then a NaN denominator, comes back quieted; 0/0 and inf/inf give NaN; x/0 and inf/x give a signed infinity; 0/x and x/inf give a signed zero; an all-ones quotient exponent gives a signed infinity; anything else is |q| with the sign. main has no other lowering of this opcode. The NaN-quotient case is backed by Lyrnc's bit-for-bit comparison against gfx1036 hardware (only NaN payloads differ). v_div_fixup_f16_follows_the_isa_special_cases checks 400 cases against a reference written from the ISA.

CI: passing.


Generated by Claude Code

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.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.

8 participants