tombox-xshader: decode and translate the f32 IEEE division helpers #272

Closed
github-bot wants to merge 1 commit from gh-pr/272/head into gh-pr/272/base
Owner

@⁠azerioxfr on GitHub · 2026-11-23 10:28 UTC · original
azerioxfr:xshader-div-f32-helpers → main · +320 −2 · 4 files · 1 commits · closed without merging

Library / area: tombox-xshader, v_div_scale_f32 / v_div_fmas_f32 / v_div_fixup_f32 (hlsl.rs next to the f64 helpers)

Exports changed (exports.txt rows): none

Source of the behaviour: opcodes from tests/isa_table/gfx1013.txt (VOP3 0x15f, 0x16d, 0x16f). Semantics from the RDNA2 ISA pseudocode, structured exactly like the already-verified tb_div_scale64 / tb_div_fmas64 / tb_div_fixup64 with the f32 constants.

One topic, +320 / -2: 4 lines in rdna2/mnem.rs, 49 in hlsl.rs, the rest tests.

What was missing

Only the f64 division helpers were decoded. Compilers emit the f32 trio (scale → rcp/fma → fmas → fixup) for every correctly rounded f32 divide, so such a shader failed to translate.

The change

opcode lowering
v_div_scale_f32 (0x16d, VOP3B) exponent difference ≥ 96, ldexp by ±64; VCC set for the zero and the 1/S1 && S2/S1 denormal cases
v_div_fmas_f32 (0x16f) fma, scaled by 2^64 / 2^-64 when VCC is set (direction from exponent(S2) > 127), with the f64 helper's denormal-result correction. 2^64 is the f32 counterpart of the f64 helper's 2^128, matching div_scale's ldexp by 64.
v_div_fixup_f32 (0x15f) NaN / inf / zero and sign special cases; underflow at exponent difference < -150
shader-written single-precision MODE refused with an explicit error. The f64 forms have MODE-aware variants (tb_d_fmas) and the f32 helpers do not yet, so they are not silently run with default rounding.

Tests

The f32 fma reference is computed exactly in f64. There is no f32::mul_add, which the GNU toolchain rounds wrongly (#126).

run (WARP, --release, DXC 1.9.2609) result
div_scale_and_fixup_f32 2 mnemonics verified, 0 failing, 45 cases (+ runtime inputs)
div_fmas_f32_direct 1 verified, 0 failing, 68 cases (+ runtime inputs)
div_f32_sequence (full scale → rcp/fma → fmas → fixup, against correctly rounded a/b) 1 verified, 0 failing, 32 cases (+ runtime inputs)
f32_division_helpers_refuse_a_shader_written_mode + translation rows (--test opcodes) 28 passed
mutations: fixup QNaN / scale QNaN / fmas 2^64 → 2^63 / MODE guard disabled 2 / 4 / 21 failing / refusal test fails
f64 regressions: div_scale_and_fixup_f64, div_f64 pass. div_fmas_f64_direct fails here as on main: the GNU fmaf reference issue (#126)

Cases are normal, denormal, near-overflow, zero, inf and NaN per op. Cases whose reference could not be made exact were dropped, not loosened. Only WARP was run, no hardware. A silicon cross-check of the f32 scale constant would be welcome if someone has the probe set up.

  • tests pass (x86_64-pc-windows-gnu)
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠azerioxfr** on GitHub · 2026-11-23 10:28 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/272) `azerioxfr:xshader-div-f32-helpers` → `main` · +320 −2 · 4 files · 1 commits · closed without merging **Library / area:** tombox-xshader, `v_div_scale_f32` / `v_div_fmas_f32` / `v_div_fixup_f32` (`hlsl.rs` next to the f64 helpers) **Exports changed (exports.txt rows):** none **Source of the behaviour:** opcodes from `tests/isa_table/gfx1013.txt` (VOP3 0x15f, 0x16d, 0x16f). Semantics from the RDNA2 ISA pseudocode, structured exactly like the already-verified `tb_div_scale64` / `tb_div_fmas64` / `tb_div_fixup64` with the f32 constants. One topic, +320 / -2: 4 lines in `rdna2/mnem.rs`, 49 in `hlsl.rs`, the rest tests. ### What was missing Only the f64 division helpers were decoded. Compilers emit the f32 trio (scale → rcp/fma → fmas → fixup) for every correctly rounded f32 divide, so such a shader failed to translate. ### The change | opcode | lowering | |---|---| | `v_div_scale_f32` (0x16d, VOP3B) | exponent difference ≥ 96, `ldexp` by ±64; VCC set for the zero and the 1/S1 && S2/S1 denormal cases | | `v_div_fmas_f32` (0x16f) | fma, scaled by 2^64 / 2^-64 when VCC is set (direction from exponent(S2) > 127), with the f64 helper's denormal-result correction. 2^64 is the f32 counterpart of the f64 helper's 2^128, matching div_scale's `ldexp` by 64. | | `v_div_fixup_f32` (0x15f) | NaN / inf / zero and sign special cases; underflow at exponent difference < -150 | | shader-written single-precision MODE | **refused** with an explicit error. The f64 forms have MODE-aware variants (`tb_d_fmas`) and the f32 helpers do not yet, so they are not silently run with default rounding. | ### Tests The f32 fma reference is computed **exactly in f64**. There is no `f32::mul_add`, which the GNU toolchain rounds wrongly (#126). | run (WARP, `--release`, DXC 1.9.2609) | result | |---|---| | `div_scale_and_fixup_f32` | 2 mnemonics verified, 0 failing, 45 cases (+ runtime inputs) | | `div_fmas_f32_direct` | 1 verified, 0 failing, 68 cases (+ runtime inputs) | | `div_f32_sequence` (full scale → rcp/fma → fmas → fixup, against correctly rounded a/b) | 1 verified, 0 failing, 32 cases (+ runtime inputs) | | `f32_division_helpers_refuse_a_shader_written_mode` + translation rows (`--test opcodes`) | 28 passed | | mutations: fixup QNaN / scale QNaN / fmas 2^64 → 2^63 / MODE guard disabled | 2 / 4 / 21 failing / refusal test fails | | f64 regressions: `div_scale_and_fixup_f64`, `div_f64` | pass. `div_fmas_f64_direct` fails here as on main: the GNU `fmaf` reference issue (#126) | Cases are normal, denormal, near-overflow, zero, inf and NaN per op. Cases whose reference could not be made exact were dropped, not loosened. Only WARP was run, no hardware. A silicon cross-check of the f32 scale constant would be welcome if someone has the probe set up. - [x] tests pass (`x86_64-pc-windows-gnu`) - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
The three f32 division-helper opcodes were unnamed in the decoder
(vop3<0x15f>, vop3<0x16d>, vop3<0x16f>); only the f64 forms were
decoded and lowered, so any shader with a correctly rounded f32 divide
failed to translate. They now decode and lower to HLSL helpers that
mirror the verified f64 ones, with the f32 constants of the RDNA2 ISA:

- v_div_scale_f32: exponent difference >= 96, ldexp by +/-64, VCC set
  for the zero and the 1/S1 && S2/S1 denormal cases.
- v_div_fmas_f32: fma, then scaled by 2^64 / 2^-64 when VCC is set,
  direction from exponent(S2) > 127 (the f32 counterpart of the f64
  helper's 2^128, matching div_scale's ldexp by 64), with the same
  denormal-result correction as the f64 helper.
- v_div_fixup_f32: NaN / inf / zero and sign special cases, underflow
  at exponent difference < -150.

The f64 forms have MODE-aware variants (tb_d_fmas) for shaders that
write the MODE register; the f32 helpers do not yet, so all three f32
ops are refused with an explicit error when the shader writes the
single-precision round or denorm bits, instead of silently using the
default rounding.

Tests: exec_vop gains div_scale_and_fixup_f32, div_fmas_f32_direct and
div_f32_sequence (45 / 68 / 32 cases, constant and runtime inputs). The
f32 fma reference is computed exactly in f64 (no f32::mul_add, which the
GNU toolchain rounds wrong), with normal / denormal / near-overflow /
zero / inf / NaN cases per op, plus the full scale -> rcp/fma -> fmas ->
fixup division sequence against correctly rounded a/b. opcodes.rs gains
translation rows and f32_division_helpers_refuse_a_shader_written_mode.
Mutations: fixup QNaN changed -> 2 failing, scale QNaN changed -> 4
failing, fmas scale 2^64 -> 2^63 -> 21 failing, MODE guard disabled ->
the refusal test fails.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:16:47 +00:00
github-bot closed this pull request 2026-11-26 06:16:47 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 14:08 UTC · original

Applied locally on main with your authorship kept (cherry-picked, ebc3148); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 14:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/272#issuecomment-7103531649) Applied locally on main with your authorship kept (cherry-picked, ebc3148); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

@⁠QuentinDanblon on GitHub · 2026-11-23 18:37 UTC · original

Ran this PR's exec tests on an NVIDIA RTX 5090 (driver 617.42, TOMBOX_EXEC_HW=1; the adapter is checked, not WARP). div_scale_and_fixup_f32 and div_f32_sequence pass; div_fmas_f32_direct fails 3 runs out of 3 on one case, while WARP passes:

  • case 5: v_div_fmas_f32 with a = 0, b = +inf, c = 1, vcc = 1. Expected 0x7fc00000, the 5090 returns 0x7fffffff.

The cause is in the translation, not a driver defect: tb_fma returns the canonical NaN, then the vcc = 1 path multiplies it by 2^-64. NVIDIA returns 0x7fffffff for any NaN produced or propagated by float math (a HLSL probe on the 5090: mad(0, inf, 1), qnan * 2^-64, qnan + 1, 0 * inf all give 0x7fffffff; WARP keeps the payload). This is the same host behaviour as the f16 entry "RDNA2 f16 FMA NaN payloads on non-AMD hosts" in BLOCKERS.

Fix tested locally: return r as soon as it is a NaN, before any scaling. On RDNA2 a NaN times 2^±64 is the same NaN, so the result does not change there, and the bits stop depending on the host. With it, the four div_* tests (f32 and f64) pass on the 5090 and on WARP.

-float tb_div_fmas32(float a, float b, float c, uint vcc) { precise float r = tb_fma(a, b, c); if ((vcc & 1u) == 0u) return r; ...
+float tb_div_fmas32(float a, float b, float c, uint vcc) { precise float r = tb_fma(a, b, c); if ((asuint(r) & 0x7fffffffu) > 0x7f800000u || (vcc & 1u) == 0u) return r; ...

The other division helpers pass their tests on the 5090, but I did not check that their NaN cases are covered, so the same pattern (scaling or adding to a NaN result) may exist elsewhere. Other PRs replayed on the 5090 with no failure: #247, #249, #251, #258, #263, #264, #305.

**@⁠QuentinDanblon** on GitHub · 2026-11-23 18:37 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/272#issuecomment-7110414914) Ran this PR's exec tests on an NVIDIA RTX 5090 (driver 617.42, `TOMBOX_EXEC_HW=1`; the adapter is checked, not WARP). `div_scale_and_fixup_f32` and `div_f32_sequence` pass; `div_fmas_f32_direct` fails 3 runs out of 3 on one case, while WARP passes: - case 5: `v_div_fmas_f32` with a = 0, b = +inf, c = 1, vcc = 1. Expected `0x7fc00000`, the 5090 returns `0x7fffffff`. The cause is in the translation, not a driver defect: `tb_fma` returns the canonical NaN, then the vcc = 1 path multiplies it by 2^-64. NVIDIA returns `0x7fffffff` for any NaN produced or propagated by float math (a HLSL probe on the 5090: `mad(0, inf, 1)`, `qnan * 2^-64`, `qnan + 1`, `0 * inf` all give `0x7fffffff`; WARP keeps the payload). This is the same host behaviour as the f16 entry "RDNA2 f16 FMA NaN payloads on non-AMD hosts" in BLOCKERS. Fix tested locally: return `r` as soon as it is a NaN, before any scaling. On RDNA2 a NaN times 2^±64 is the same NaN, so the result does not change there, and the bits stop depending on the host. With it, the four `div_*` tests (f32 and f64) pass on the 5090 and on WARP. ```diff -float tb_div_fmas32(float a, float b, float c, uint vcc) { precise float r = tb_fma(a, b, c); if ((vcc & 1u) == 0u) return r; ... +float tb_div_fmas32(float a, float b, float c, uint vcc) { precise float r = tb_fma(a, b, c); if ((asuint(r) & 0x7fffffffu) > 0x7f800000u || (vcc & 1u) == 0u) return r; ... ``` The other division helpers pass their tests on the 5090, but I did not check that their NaN cases are covered, so the same pattern (scaling or adding to a NaN result) may exist elsewhere. Other PRs replayed on the 5090 with no failure: #247, #249, #251, #258, #263, #264, #305.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-24 12:49 UTC · original

I recorded the three ops on RDNA2 silicon: the gfx1036 iGPU of a Ryzen 5 9600X, ROCm/HIP 5.7.1, raw wave32 kernels. That is RDNA2, not the console's gfx1013.

Setup:

  • MODE matches the translator's assumed one: IEEE 0, DX10_CLAMP 1, f32 denormals kept.
  • 1,048,576 inputs per op: the special values in every combination, plus random inputs aimed at each branch.
  • I also recorded the full division sequence from div_f32_sequence, with the same instruction order.
  • Each set was run twice, and both runs were identical.

I compared the recordings against my own C++ port of this PR's HLSL helpers. I did not run the PR's code here.

Summary (counts are cases where the result bits differ):

op differs from silicon what differs
v_div_fixup_f32 0 non-NaN NaN payloads only
full div_f32 sequence 0 non-NaN NaN payloads only. Silicon gives the correctly rounded quotient on all cases
v_div_fmas_f32 7,834 results whose unscaled a·b+c overflows or underflows f32; NaN payloads
v_div_scale_f32 156,986 (148,004 of them VCC) the "quotient near the denormal range" and "tiny numerator" tests

v_div_scale_f32. The hardware decides with exponent tests only and never forms the quotient. This rule reproduces all 1,048,576 recorded results and VCC values bit-exactly:

  • ed and en are the biased exponent fields of d (S1) and n (S2): 0 for zero or denormal, 255 for inf or NaN. df = en − ed.
  • VCC = (df ≥ 96 || df ≤ −96), in every case, including the zero and NaN ones.
  • D, first match wins:
    1. d or n is ±0: 0xffc00000.
    2. df ≥ 96: x == d ? x·2^64 : x.
    3. d is denormal: x·2^64.
    4. ed ≥ 253: (df ≤ −96 && x != d) ? x : x·2^−64.
    5. df ≤ −96: x == d ? x : x·2^64.
    6. en ≤ 24: x·2^64.
    7. Otherwise: x.

Here x·2^k is rounded to f32 with denormal results kept, and inf or NaN x is returned unchanged.

Where the PR differs:

  • It uses exact 1/d and n/d tests where the hardware uses ed ≥ 253 and df ≤ −96. For example, x = n = 0x9bcdf504, d = 0x5a7f8c2f gives 0xbbcdf504 with VCC 1 on silicon; the PR gives x unchanged with VCC 0.
  • It uses en ≤ 23 where the hardware uses en ≤ 24. x = d = 0x8cdfb772, n = 0x8c5d532b gives 0xacdfb772 on silicon.
  • For zero inputs it gives 0x7fc00000 with VCC 1. Silicon gives 0xffc00000 with VCC from the exponent rule, so 0/0 gives VCC 0 and 1/0 gives VCC 1.

v_div_fmas_f32. Silicon matches a single rounding of the exact a·b + c × 2^(±64), with no f32 range limit on the intermediate. The scale is +64 when VCC is set and exp(c) > 127, and −64 when VCC is set otherwise. That model matches every non-NaN case. The PR's model rounds to f32 first, so it differs when the unscaled result is out of range. Examples, all with VCC 1:

  • a = 3, b = −FLT_MAX, c = 1: silicon 0xe03fffff, PR 0xff800000.
  • a = 0x6a533e4f, b = 0x5f0439ce, c = 0x0bfdc135: silicon 0x69da37a7, PR 0x7f800000.

NaN results, all three ops. These are relevant to the RTX 5090 report above:

  • A NaN operand comes back unchanged, not quieted. 0x7f800001 stays 0x7f800001.
  • The priority order:
    • fmas: a, then b, then an invalid product (0·inf, or inf + −inf), then c;
    • fixup: n, then d, then 0/0 or inf/inf.
  • A NaN the hardware generates is 0xffc00000, not 0x7fc00000. Case 5 of div_fmas_f32_direct (a = 0, b = +inf, c = 1, VCC = 1) gives 0xffc00000 on gfx1036.
  • Scaling a NaN by 2^±64 leaves it unchanged, as the proposed early return assumes. With that change the case would give 0x7fc00000, so its expected value needs to be 0xffc00000 too.
  • These rules matched all 49,370 NaN results of fmas and 147,054 of fixup.

So on RDNA2 neither 0x7fc00000 nor the 5090's 0x7fffffff is the hardware value. If NaN payloads stay host-dependent, as for the f16 FMA ops, comparing those cases as a NaN class on hardware runs would match what #197 did.

Not covered:

  • With f32 denormals flushed (MODE f32 denorm 0), fmas keeps a denormal c in some cases (1 + 2^−149 from 1·2^−126 + 2^−149 gives 0x00800001). I didn't model it, since the PR refuses shader-written MODE.
  • The f64 forms already on main are not recorded here. I can record them the same way if that's useful.

I can turn the recordings into a WARP replay test (exec_vop, like rdna2_silicon) once this lands, if you want it.

(Measured and written with an AI assistant, Claude.)

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 12:49 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/272#issuecomment-7131969508) I recorded the three ops on RDNA2 silicon: the gfx1036 iGPU of a Ryzen 5 9600X, ROCm/HIP 5.7.1, raw wave32 kernels. That is RDNA2, not the console's gfx1013. Setup: - MODE matches the translator's assumed one: IEEE 0, DX10_CLAMP 1, f32 denormals kept. - 1,048,576 inputs per op: the special values in every combination, plus random inputs aimed at each branch. - I also recorded the full division sequence from `div_f32_sequence`, with the same instruction order. - Each set was run twice, and both runs were identical. I compared the recordings against my own C++ port of this PR's HLSL helpers. I did not run the PR's code here. **Summary** (counts are cases where the result bits differ): | op | differs from silicon | what differs | |---|---|---| | `v_div_fixup_f32` | 0 non-NaN | NaN payloads only | | full `div_f32` sequence | 0 non-NaN | NaN payloads only. Silicon gives the correctly rounded quotient on all cases | | `v_div_fmas_f32` | 7,834 | results whose unscaled a·b+c overflows or underflows f32; NaN payloads | | `v_div_scale_f32` | 156,986 (148,004 of them VCC) | the "quotient near the denormal range" and "tiny numerator" tests | **v_div_scale_f32.** The hardware decides with exponent tests only and never forms the quotient. This rule reproduces all 1,048,576 recorded results and VCC values bit-exactly: - ed and en are the biased exponent fields of d (S1) and n (S2): 0 for zero or denormal, 255 for inf or NaN. df = en − ed. - VCC = (df ≥ 96 || df ≤ −96), in every case, including the zero and NaN ones. - D, first match wins: 1. d or n is ±0: `0xffc00000`. 2. df ≥ 96: x == d ? x·2^64 : x. 3. d is denormal: x·2^64. 4. ed ≥ 253: (df ≤ −96 && x != d) ? x : x·2^−64. 5. df ≤ −96: x == d ? x : x·2^64. 6. en ≤ 24: x·2^64. 7. Otherwise: x. Here x·2^k is rounded to f32 with denormal results kept, and inf or NaN x is returned unchanged. Where the PR differs: - It uses exact `1/d` and `n/d` tests where the hardware uses `ed ≥ 253` and `df ≤ −96`. For example, x = n = `0x9bcdf504`, d = `0x5a7f8c2f` gives `0xbbcdf504` with VCC 1 on silicon; the PR gives x unchanged with VCC 0. - It uses en ≤ 23 where the hardware uses en ≤ 24. x = d = `0x8cdfb772`, n = `0x8c5d532b` gives `0xacdfb772` on silicon. - For zero inputs it gives `0x7fc00000` with VCC 1. Silicon gives `0xffc00000` with VCC from the exponent rule, so 0/0 gives VCC 0 and 1/0 gives VCC 1. **v_div_fmas_f32.** Silicon matches a single rounding of the exact a·b + c × 2^(±64), with no f32 range limit on the intermediate. The scale is +64 when VCC is set and exp(c) > 127, and −64 when VCC is set otherwise. That model matches every non-NaN case. The PR's model rounds to f32 first, so it differs when the unscaled result is out of range. Examples, all with VCC 1: - a = 3, b = −FLT_MAX, c = 1: silicon `0xe03fffff`, PR `0xff800000`. - a = `0x6a533e4f`, b = `0x5f0439ce`, c = `0x0bfdc135`: silicon `0x69da37a7`, PR `0x7f800000`. **NaN results, all three ops.** These are relevant to the RTX 5090 report above: - A NaN operand comes back unchanged, not quieted. `0x7f800001` stays `0x7f800001`. - The priority order: - fmas: a, then b, then an invalid product (0·inf, or inf + −inf), then c; - fixup: n, then d, then 0/0 or inf/inf. - A NaN the hardware generates is `0xffc00000`, not `0x7fc00000`. Case 5 of `div_fmas_f32_direct` (a = 0, b = +inf, c = 1, VCC = 1) gives `0xffc00000` on gfx1036. - Scaling a NaN by 2^±64 leaves it unchanged, as the proposed early return assumes. With that change the case would give `0x7fc00000`, so its expected value needs to be `0xffc00000` too. - These rules matched all 49,370 NaN results of fmas and 147,054 of fixup. So on RDNA2 neither `0x7fc00000` nor the 5090's `0x7fffffff` is the hardware value. If NaN payloads stay host-dependent, as for the f16 FMA ops, comparing those cases as a NaN class on hardware runs would match what #197 did. **Not covered:** - With f32 denormals flushed (MODE f32 denorm 0), fmas keeps a denormal c in some cases (1 + 2^−149 from 1·2^−126 + 2^−149 gives `0x00800001`). I didn't model it, since the PR refuses shader-written MODE. - The f64 forms already on main are not recorded here. I can record them the same way if that's useful. I can turn the recordings into a WARP replay test (`exec_vop`, like `rdna2_silicon`) once this lands, if you want it. (Measured and written with an AI assistant, Claude.)
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:08 UTC · original

Applied on main in ebc3148 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:08 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/272#issuecomment-7163337195) Applied on main in ebc3148 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.