tombox-xshader: v_div_scale_f64 and the f64 div ops under MODE as RDNA2 does #391

Open
SchmashinPrunes wants to merge 1 commit from upstream-pr/391 into main
SchmashinPrunes commented 2026-11-25 20:56:16 +00:00 (Migrated from github.com)

Imported from omonkeyz/xbox-emulator. Originally posted by SchmashinPrunes on 2026-11-25T20:56:16Z.


@omonkeyz, this fixes v_div_scale_f64 at every MODE, and v_div_fixup_f64 / v_div_fmas_f64 under f64 denormal flush. It came out of the translator-vs-gfx1036 comparison from #387, plus four small targeted runs on the same iGPU.

1. v_div_scale_f64 doesn't follow the documented pseudo-code

The translator (and the exec reference model) implemented AMD's documented rule. gfx1036 disagreed on 210 of the 1,024 recorded lanes at the default MODE, mostly on VCC. VCC is what v_div_fmas_f64 uses to undo the scaling, so a full f64 division can come out off by 2^±64 when it's wrong. With d/n exponent fields ed/en, silicon does this:

condition result VCC
d or n zero 0xfff8000000000000 (−qNaN, not +qNaN) abs(en − ed) ≥ 768
en − ed ≥ 768 x == d ? x·2¹²⁸ : x 1
d denormal x·2¹²⁸ 0
ed ≥ 2045 and en − ed ≤ −768 x == d ? x·2⁻¹²⁸ : x 1
ed ≥ 2045 x·2⁻¹²⁸ 0
en − ed ≤ −768 x == d ? x : x·2¹²⁸ 1
en ≤ 53 x·2¹²⁸ 0
otherwise x 0

Where this departs from the documented rule:

  • Both "tiny" tests use exponent fields, not exact values.

    • "1/d is denormal" is ed ≥ 2045: d = 2¹⁰²² counts, though 1/d is exactly normal.
    • "n/d is denormal" is en − ed ≤ −768. That also sets VCC for quotients like 2⁻⁷⁹⁰, which are far from denormal.
  • VCC for a zero operand depends on the same exponent difference, in either direction.

  • The tiny-quotient case scales any x other than d. The documented rule scales only x == n.

  • On the 2⁻¹²⁸ paths, an x with exponent field ≤ 116 (denormals included) gives ±inf instead of underflowing. Fields 117 and up give the correctly rounded, possibly denormal, result. Zero, inf and NaN pass through unchanged.

    This can't happen inside a real division sequence, where x is d or n. I measured it so the model is exact.

The rule matches all 1,024 recorded lanes in each of the three MODEs where f64 denormals are enabled (0xf0, 0xc0, and 0xf0 with IEEE). Under f64 flush, the MODE path's existing operand and result flush covers the rest.

2. v_div_fixup_f64 / v_div_fmas_f64 under f64 denormal flush

The MODE path flushed every f64 operand and result of both ops. gfx1036 flushes:

  • v_div_fixup_f64: only the d and n operands. The quotient operand and the result keep denormals; fixup(2⁻¹⁰⁷⁴, −1, 1) gives 0x8000000000000001.
  • v_div_fmas_f64: only the result. Its inputs aren't flushed. fmas(2⁻¹⁰⁷⁴, −1, 2⁻¹⁰²²) gives 0, the flushed 0x000fffffffffffff. The translator flushed the input 2⁻¹⁰⁷⁴ first and returned 2⁻¹⁰²².

Tests

  • New f64_div_scale_edge_cases_as_rdna2: 6 gfx1036 vectors, including the inf and denormal results on the 2⁻¹²⁸ path.
  • New f64_div_fixup_and_fmas_under_f64_flush_as_rdna2: 4 gfx1036 vectors under f64 flush set by s_setreg.
  • The div_scale reference model in exec_vop/mod.rs follows the table above, and its operand table gains 6 rows that exercise the changed branches.
run result
WARP, without the src/ change the new tests fail (3 + 3 cases), plus 19 of the div_scale_and_fixup_f64 rows
WARP, tests/exec.rs 191/191
Radeon RX 9060 XT, tests/exec.rs 191/191
full f64 division sequences (div_f64, 118 cases) unchanged, passing on both hosts
isa_sweep target, WARP ok (existing WARP_KNOWN deviations only)
translator vs gfx1036, initial MODE 0xf0 / 0x30 / 0x00, RX 9060 XT no v_div_* mnemonic differs any more (16 / 44 / 45 differ in total, down from 17 / 45 / 46)
same, WARP only v_div_fmas_f64, which is WARP's existing f64 fma deviation (WARP_KNOWN)

The PR gate is clean. No game or console files are involved.

🤖 Generated with Claude Code

<!-- greyfinch-sync:omonkeyz/xbox-emulator:issue:5499038394 --> Imported from [omonkeyz/xbox-emulator](https://github.com/omonkeyz/xbox-emulator/pull/391). Originally posted by [SchmashinPrunes](https://github.com/SchmashinPrunes) on 2026-11-25T20:56:16Z. --- @omonkeyz, this fixes `v_div_scale_f64` at every MODE, and `v_div_fixup_f64` / `v_div_fmas_f64` under f64 denormal flush. It came out of the translator-vs-gfx1036 comparison from #387, plus four small targeted runs on the same iGPU. ### 1. `v_div_scale_f64` doesn't follow the documented pseudo-code The translator (and the exec reference model) implemented AMD's documented rule. gfx1036 disagreed on 210 of the 1,024 recorded lanes at the default MODE, mostly on VCC. VCC is what `v_div_fmas_f64` uses to undo the scaling, so a full f64 division can come out off by 2^±64 when it's wrong. With `d`/`n` exponent fields `ed`/`en`, silicon does this: | condition | result | VCC | |---|---|---| | `d` or `n` zero | `0xfff8000000000000` (−qNaN, not +qNaN) | `abs(en − ed) ≥ 768` | | `en − ed ≥ 768` | `x == d` ? `x·2¹²⁸` : `x` | 1 | | `d` denormal | `x·2¹²⁸` | 0 | | `ed ≥ 2045` and `en − ed ≤ −768` | `x == d` ? `x·2⁻¹²⁸` : `x` | 1 | | `ed ≥ 2045` | `x·2⁻¹²⁸` | 0 | | `en − ed ≤ −768` | `x == d` ? `x` : `x·2¹²⁸` | 1 | | `en ≤ 53` | `x·2¹²⁸` | 0 | | otherwise | `x` | 0 | Where this departs from the documented rule: - **Both "tiny" tests use exponent fields, not exact values.** - "1/d is denormal" is `ed ≥ 2045`: `d = 2¹⁰²²` counts, though `1/d` is exactly normal. - "n/d is denormal" is `en − ed ≤ −768`. That also sets VCC for quotients like `2⁻⁷⁹⁰`, which are far from denormal. - **VCC for a zero operand** depends on the same exponent difference, in either direction. - **The tiny-quotient case** scales any `x` other than `d`. The documented rule scales only `x == n`. - **On the 2⁻¹²⁸ paths,** an `x` with exponent field ≤ 116 (denormals included) gives **±inf** instead of underflowing. Fields 117 and up give the correctly rounded, possibly denormal, result. Zero, inf and NaN pass through unchanged. This can't happen inside a real division sequence, where `x` is `d` or `n`. I measured it so the model is exact. The rule matches all 1,024 recorded lanes in each of the three MODEs where f64 denormals are enabled (0xf0, 0xc0, and 0xf0 with IEEE). Under f64 flush, the MODE path's existing operand and result flush covers the rest. ### 2. `v_div_fixup_f64` / `v_div_fmas_f64` under f64 denormal flush The MODE path flushed every f64 operand and result of both ops. gfx1036 flushes: - `v_div_fixup_f64`: only the `d` and `n` operands. The quotient operand and the result keep denormals; `fixup(2⁻¹⁰⁷⁴, −1, 1)` gives `0x8000000000000001`. - `v_div_fmas_f64`: only the result. Its inputs aren't flushed. `fmas(2⁻¹⁰⁷⁴, −1, 2⁻¹⁰²²)` gives `0`, the flushed `0x000fffffffffffff`. The translator flushed the input `2⁻¹⁰⁷⁴` first and returned `2⁻¹⁰²²`. ### Tests - New `f64_div_scale_edge_cases_as_rdna2`: 6 gfx1036 vectors, including the inf and denormal results on the 2⁻¹²⁸ path. - New `f64_div_fixup_and_fmas_under_f64_flush_as_rdna2`: 4 gfx1036 vectors under f64 flush set by `s_setreg`. - The `div_scale` reference model in `exec_vop/mod.rs` follows the table above, and its operand table gains 6 rows that exercise the changed branches. | run | result | |---|---| | WARP, without the `src/` change | the new tests fail (3 + 3 cases), plus 19 of the `div_scale_and_fixup_f64` rows | | WARP, `tests/exec.rs` | 191/191 | | Radeon RX 9060 XT, `tests/exec.rs` | 191/191 | | full f64 division sequences (`div_f64`, 118 cases) | unchanged, passing on both hosts | | `isa_sweep` target, WARP | ok (existing `WARP_KNOWN` deviations only) | | translator vs gfx1036, initial MODE 0xf0 / 0x30 / 0x00, RX 9060 XT | no `v_div_*` mnemonic differs any more (16 / 44 / 45 differ in total, down from 17 / 45 / 46) | | same, WARP | only `v_div_fmas_f64`, which is WARP's existing f64 fma deviation (`WARP_KNOWN`) | The PR gate is clean. No game or console files are involved. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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/391:upstream-pr/391'
git switch 'upstream-pr/391'

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