fix: HLSL f32 operations folded at compile time (x + 0, 0 - x, x - x, 0 × x) #220

Manually merged
github merged 2 commits from gh-pr/220/head into gh-pr/220/base 2026-11-26 05:38:13 +00:00
Owner

@⁠IxMxAMAR on GitHub · 2026-11-25 16:22 UTC · original
IxMxAMAR:fix/hlsl-f32-zero → main · +50 −5 · 3 files · 2 commits · merged 2026-11-25 18:28 UTC

Change

Replaces #169, which was closed during the 25 November triage. It is rebased on main (ab55a2a), and it fixes the cases of #166 (closed in the same triage; they still fail on main, see below).

The f32 templates v_add, v_sub, v_subrev and v_mul_f32 go through three precise functions, F32Add, F32Sub and F32Mul, now in emu/rdna2_hlsl_helpers.py where main moved the HLSL helpers. dxc no longer removes these operations, and the DXIL marks them !dx.precise.

What changed in the rebase: the FMA family (v_fma, v_fmac, v_fmamk, v_fmaak, v_mac and v_mad_f32) keeps main's templates. main's FmaF32 works on the bits with no float arithmetic, so the sign of zero is right by construction. This PR's former F32Fma is dropped, and the test's fma(x, 1, 0) and v_fmac cases pass with FmaF32.

Drivers still fold some operations despite precise (measured: AMD fadd x, 0.0 and fsub x, x; Intel fsub 0.0, x; RDNA 2 also 0 - x and inf - inf). Two cases are therefore computed in integer:

  • the sign of an exactly zero sum or difference comes from the operands (-0 only for -0 + -0);
  • x - x with identical operands gives +0, the default NaN 0xFFC00000 for ±infinity (as the interpreter does), or the NaN itself, made quiet.

Cost: a few integer operations per addition and subtraction.

Out of scope: the sign of a subtracted NaN (0 - NaN) differs between the interpreter (x86 rules) and Intel and AMD hardware, even without folding. As in the test of #138, the new test avoids NaNs whose pattern depends on the GPU.

Linked issue: Refs #166

Verification

python -m pytest → 906 passed, 287 skipped
python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed!
$env:ANYXBOXX_GPU="Radeon"; python -m pytest tests/test_rdna2_exec_specials.py → 21 passed (AMD Radeon RX 9070 XT, on the previous base `f5cdd8e`)
$env:ANYXBOXX_GPU="Intel";  python -m pytest tests/test_rdna2_exec_specials.py → 21 passed (Intel(R) Graphics)
$env:ANYXBOXX_GPU="warp";   python -m pytest tests/test_rdna2_exec_specials.py → 21 passed (Microsoft Basic Render Driver)
  • Without this change (main's rdna2_hlsl_ops.py and rdna2_hlsl_helpers.py, this PR's test), test_constant_zero_folds_match_on_gpu fails on all three: byte 0x20003 is 0x80 instead of 0x00, the sign of zero.
  • main's new rounding test (test_rounding_and_fract_match_on_gpu) passes alongside it.
  • The skips are the tests that need the C++ runtime, the Rust crate or the TomBox DLLs, none of which were built in this checkout.
  • The GPU runner came from an older build of runtime/ (ANYXBOXX_RUNTIME).
  • The Radeon run and the check without the change were on the previous base f5cdd8e; the f32 templates and helpers have not changed since. The Intel and WARP runs are on ab55a2a.
  • 1,193 tests, so the README stays at 1100+.

Earlier revisions were also checked by others:

  • akimfomin, RTX 4070 Ti SUPER: 12 passed, and the new test failed on the base with the test file alone (same byte).
  • Cai-piroska, on RX 9070 XT and WARP.
  • minecraftafk072, on an RDNA 2 iGPU, an RTX 4070 Ti SUPER and WARP.

Oracle and test data

test_constant_zero_folds_match_on_gpu (tests/test_rdna2_exec_specials.py) compares the whole memory, byte for byte, with the reference interpreter.

  • It covers 0 + x and x + 0, 0 - x (v_sub and v_subrev), x - x, 0 × x, fma(x, 1, 0) and v_fmac.
  • The values are ±0, ±1, ±3 and ±infinity (no infinity for 0 × x).

Checklist

  • The PR covers a single reviewable behaviour (HLSL f32 operations folded at compile time).
  • A test fails without this change and passes with it.
  • The commands and results of each check are given above.
  • An unimplemented function returns an error, never a fake success.
  • README: no change needed (test_readme_sync.py checks it).
  • No game, console or SDK file, no key, no dump, no download link.
  • Findings drawn from a title: none.
  • Code taken from another project: none.

Made with AI assistance.

**@⁠IxMxAMAR** on GitHub · 2026-11-25 16:22 UTC · [original](https://github.com/LooperSalty/AnyXboxX/pull/220) `IxMxAMAR:fix/hlsl-f32-zero` → `main` · +50 −5 · 3 files · 2 commits · merged 2026-11-25 18:28 UTC ## Change Replaces #169, which was closed during the 25 November triage. It is rebased on `main` (`ab55a2a`), and it fixes the cases of #166 (closed in the same triage; they still fail on `main`, see below). The f32 templates `v_add`, `v_sub`, `v_subrev` and `v_mul_f32` go through three `precise` functions, `F32Add`, `F32Sub` and `F32Mul`, now in `emu/rdna2_hlsl_helpers.py` where `main` moved the HLSL helpers. dxc no longer removes these operations, and the DXIL marks them `!dx.precise`. What changed in the rebase: the FMA family (`v_fma`, `v_fmac`, `v_fmamk`, `v_fmaak`, `v_mac` and `v_mad_f32`) keeps `main`'s templates. `main`'s `FmaF32` works on the bits with no float arithmetic, so the sign of zero is right by construction. This PR's former `F32Fma` is dropped, and the test's `fma(x, 1, 0)` and `v_fmac` cases pass with `FmaF32`. Drivers still fold some operations despite `precise` (measured: AMD `fadd x, 0.0` and `fsub x, x`; Intel `fsub 0.0, x`; RDNA 2 also `0 - x` and `inf - inf`). Two cases are therefore computed in integer: - the sign of an exactly zero sum or difference comes from the operands (-0 only for -0 + -0); - `x - x` with identical operands gives +0, the default NaN 0xFFC00000 for ±infinity (as the interpreter does), or the NaN itself, made quiet. Cost: a few integer operations per addition and subtraction. Out of scope: the sign of a subtracted NaN (`0 - NaN`) differs between the interpreter (x86 rules) and Intel and AMD hardware, even without folding. As in the test of #138, the new test avoids NaNs whose pattern depends on the GPU. Linked issue: Refs #166 ## Verification ``` python -m pytest → 906 passed, 287 skipped python -m ruff check --select W605,F,E9 PrototypeScarlett tests tools → All checks passed! $env:ANYXBOXX_GPU="Radeon"; python -m pytest tests/test_rdna2_exec_specials.py → 21 passed (AMD Radeon RX 9070 XT, on the previous base `f5cdd8e`) $env:ANYXBOXX_GPU="Intel"; python -m pytest tests/test_rdna2_exec_specials.py → 21 passed (Intel(R) Graphics) $env:ANYXBOXX_GPU="warp"; python -m pytest tests/test_rdna2_exec_specials.py → 21 passed (Microsoft Basic Render Driver) ``` - Without this change (`main`'s `rdna2_hlsl_ops.py` and `rdna2_hlsl_helpers.py`, this PR's test), `test_constant_zero_folds_match_on_gpu` fails on all three: byte 0x20003 is 0x80 instead of 0x00, the sign of zero. - `main`'s new rounding test (`test_rounding_and_fract_match_on_gpu`) passes alongside it. - The skips are the tests that need the C++ runtime, the Rust crate or the TomBox DLLs, none of which were built in this checkout. - The GPU runner came from an older build of `runtime/` (`ANYXBOXX_RUNTIME`). - The Radeon run and the check without the change were on the previous base `f5cdd8e`; the f32 templates and helpers have not changed since. The Intel and WARP runs are on `ab55a2a`. - 1,193 tests, so the README stays at `1100+`. Earlier revisions were also checked by others: - akimfomin, RTX 4070 Ti SUPER: 12 passed, and the new test failed on the base with the test file alone (same byte). - Cai-piroska, on RX 9070 XT and WARP. - minecraftafk072, on an RDNA 2 iGPU, an RTX 4070 Ti SUPER and WARP. ## Oracle and test data `test_constant_zero_folds_match_on_gpu` (`tests/test_rdna2_exec_specials.py`) compares the whole memory, byte for byte, with the reference interpreter. - It covers `0 + x` and `x + 0`, `0 - x` (`v_sub` and `v_subrev`), `x - x`, `0 × x`, `fma(x, 1, 0)` and `v_fmac`. - The values are ±0, ±1, ±3 and ±infinity (no infinity for `0 × x`). ## Checklist - [x] The PR covers a single reviewable behaviour (HLSL f32 operations folded at compile time). - [x] A test fails without this change and passes with it. - [x] The commands and results of each check are given above. - [x] An unimplemented function returns an error, never a fake success. - [x] README: no change needed (`test_readme_sync.py` checks it). - [x] No game, console or SDK file, no key, no dump, no download link. - [x] Findings drawn from a title: none. - [x] Code taken from another project: none. Made with AI assistance.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:38:11 +00:00
github manually merged commit 9d1e2b8179 into gh-pr/220/base 2026-11-26 05:38:13 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.