Shaders: lower v_div_fixup_f16 #379

Open
github-bot wants to merge 3 commits from github-bot/gh-379 into main AGit
Owner

@⁠LouisRiverstone on GitHub · 2026-11-24 02:46 UTC · original
LouisRiverstone:shader-div-fixup-f16 → main · +116 −0 · 2 files · 3 commits · open

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.

**@⁠LouisRiverstone** on GitHub · 2026-11-24 02:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/379) `LouisRiverstone:shader-div-fixup-f16` → `main` · +116 −0 · 2 files · 3 commits · open `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.
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.
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.
# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:01:45 +00:00
Author
Owner

@⁠jeremygold02 on GitHub · 2026-11-24 04:15 UTC · original

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.

**@⁠jeremygold02** on GitHub · 2026-11-24 04:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7121851890) 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.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 05:58 UTC · original

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.

**@⁠Lyrnc** on GitHub · 2026-11-24 05:58 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7123454964) 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.
Author
Owner

@⁠slint6614-dev on GitHub · 2026-11-24 10:16 UTC · original

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.
**@⁠slint6614-dev** on GitHub · 2026-11-24 10:16 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7128560538) 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.
Author
Owner

@⁠LouisRiverstone on GitHub · 2026-11-24 10:40 UTC · original · edited 2026-11-24 10:52 UTC

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.

**@⁠LouisRiverstone** on GitHub · 2026-11-24 10:40 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7129087289) · edited 2026-11-24 10:52 UTC 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.
Author
Owner

@⁠Halfect on GitHub · 2026-11-24 11:28 UTC · original

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.

**@⁠Halfect** on GitHub · 2026-11-24 11:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7130147369) 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.
Author
Owner

@⁠Lyrnc on GitHub · 2026-11-24 15:23 UTC · original

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 (#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.

**@⁠Lyrnc** on GitHub · 2026-11-24 15:23 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7135941318) 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 (#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.
Author
Owner

@⁠Yaekai on GitHub · 2026-11-24 16:39 UTC · original

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!
**@⁠Yaekai** on GitHub · 2026-11-24 16:39 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7137999725) 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!
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:28 UTC · original · edited 2026-11-26 02:17 UTC

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

**@⁠Marvisak** on GitHub · 2026-11-25 00:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/379#issuecomment-7148548122) · edited 2026-11-26 02:17 UTC <!-- victory-triage v1 head=55019c8e245ce429a354e348bb7081ffc684b610 verdict=merge-candidate at=2026-11-26T02:18:30Z --> 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](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/379/head:github-bot/gh-379'
git switch 'github-bot/gh-379'

Merge

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