Float compares with NaN operands, f64 math past the f32 and int64 ranges #46

Manually merged
github merged 2 commits from gh-pr/46/head into gh-pr/46/base 2026-11-26 04:06:10 +00:00
Owner

@⁠grooving6 on GitHub · 2026-11-22 21:05 UTC · original
grooving6:fix/f64-math-nan-compares → main · +672 −51 · 2 files · 2 commits · merged 2026-11-23 10:13 UTC

Fixes float compares with NaN operands and several f64 operations in the shader translator.

  • NaN compares. On AMD, a shader with !(a >= b) followed by a < b on the same operands returned true for a < b when an operand was NaN. The DXIL is correct, so the driver causes it. Every f32 and f64 compare now tests for NaN explicitly.
  • f64 math. floor, ceil, trunc and fract went through int64_t, so large values, inf, NaN, signed zeros and abs and neg modifiers came out wrong. ldexp built 2^n as an f32, so ldexp(1, 1023) was inf. sqrt and rsq started from an f32 root, so sqrt(1e300) was inf. cvt_i32_f64(NaN) gave -2^31 instead of 0.

Testing

  • New GPU edge-case tests fail before the fix and pass after.
  • Two randomized GPU tests check the f64 math and all f32, f64 and f16 compares against the CPU, with 0 failures over 64 000 inputs on an RX 7900 XTX and an RDNA2 iGPU.
  • All 336 lib tests pass.
**@⁠grooving6** on GitHub · 2026-11-22 21:05 UTC · [original](https://github.com/xcl-dev/xcl/pull/46) `grooving6:fix/f64-math-nan-compares` → `main` · +672 −51 · 2 files · 2 commits · merged 2026-11-23 10:13 UTC Fixes float compares with NaN operands and several f64 operations in the shader translator. - **NaN compares.** On AMD, a shader with `!(a >= b)` followed by `a < b` on the same operands returned true for `a < b` when an operand was NaN. The DXIL is correct, so the driver causes it. Every f32 and f64 compare now tests for NaN explicitly. - **f64 math.** `floor`, `ceil`, `trunc` and `fract` went through `int64_t`, so large values, inf, NaN, signed zeros and `abs` and `neg` modifiers came out wrong. `ldexp` built 2^n as an f32, so `ldexp(1, 1023)` was inf. `sqrt` and `rsq` started from an f32 root, so `sqrt(1e300)` was inf. `cvt_i32_f64(NaN)` gave -2^31 instead of 0. ## Testing - New GPU edge-case tests fail before the fix and pass after. - Two randomized GPU tests check the f64 math and all f32, f64 and f16 compares against the CPU, with 0 failures over 64 000 inputs on an RX 7900 XTX and an RDNA2 iGPU. - All 336 lib tests pass.
On AMD, a shader with !(a >= b) followed by a < b on the same operands
returned true for a < b when an operand was NaN, although the DXIL is
correct. Every f32 and f64 compare now tests for NaN explicitly.

floor, ceil, trunc and fract went through int64_t and broke on large
values, inf, NaN, signed zeros and abs and neg modifiers. ldexp built 2^n
as an f32, sqrt and rsq started from an f32 root, and cvt_i32_f64(NaN)
gave -2^31. The f64 helpers now work on the bits.

New GPU tests cover the edge cases and compare random inputs against the
CPU.
The seven new GPU tests only ran on GPUs with wave64, so on NVIDIA they all
returned early. They check each lane on its own, so they now run once as a
wave64 program where the GPU has wave64 and once as a wave32 program on any
GPU, and check the 32 lanes that ran.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:06:08 +00:00
github manually merged commit b8fca793dc into gh-pr/46/base 2026-11-26 04:06:10 +00:00
Author
Owner

@⁠ivall on GitHub · 2026-11-22 21:27 UTC · original

Thanks! Merged onto current main it builds, and all tests pass (336 lib tests, your GPU tests included, on an RX 9070, plus the test title). But it breaks a retail title, so I'm holding it for now.

Unicorn Overlord: the title screen stays black. The SEGA logo and the opening text render. After them the picture stays black while the music plays. main shows the title screen. With XCL_FRAME_DUMP=1, non-black samples of 518 400 (3840×2160, every 4th pixel):

main:  13.6 s:15984  15.6 s:0  17.6 s:4617  19.6 s:17714  21.6 s:154062  23.6 s:518220  25.6 s:502664 …
#46:   13.6 s:15984  15.6 s:0  17.6 s:4617  19.6 s:17720  21.6 s:17723   23.6 s:13328   25.6 s:13390  27.6 s:24793  29.6 s:24797

The black frame at 15.6 s is a normal gap between logos; it is there on main too. On #46 the title screen never comes. It's the same with a warm DXIL cache: the run that was checked on screen compiled 1 new DXIL.

It's the f32 compare change. Unicorn's shaders have no f64 instructions at all:

instruction count
v_mul_legacy_f32 2503
v_rcp_f32 62
v_cmp_gt_f32 46
v_cmp_lt_f32 34
v_log_f32 10
v_cmp_le_f32 / v_cmp_ge_f32 4 each
v_sqrt_f32 1

Their HLSL bodies never call the new f64 helpers either. For non-NaN operands the new compares give what the old ones did, so a NaN must reach one of these v_cmp_*_f32. The old code's result (the driver's unordered one, as you describe) gave the console's picture.

Since the console's compares are ordered too, I suspect the NaN itself is the real bug: something XCL computes differently from the console, so that the console never sees a NaN there. mul_legacy already gives 0 for 0 × ∞, so it isn't that one. rcp / log of 0 → ±∞ followed by a plain multiply or subtract would be my first guess.

Options:

  • find where the NaN comes from (the title-screen pixel shaders, <data>\shaders\*_ps.*, the ones with v_rcp_f32 / v_log_f32 and the compares);
  • or keep the plain f32 compares until then and land the f64 part on its own.

Note that the f64 helpers sit in the shared prelude, so any change there recompiles every shader of every title after a merge. XDefiant then stays black for minutes on its first run.

Not covered yet with #46: Fortnite and XDefiant. Minecraft looked fine.

**@⁠ivall** on GitHub · 2026-11-22 21:27 UTC · [original](https://github.com/xcl-dev/xcl/pull/46#issuecomment-7086068958) Thanks! Merged onto current `main` it builds, and all tests pass (336 lib tests, your GPU tests included, on an RX 9070, plus the test title). But it breaks a retail title, so I'm holding it for now. **Unicorn Overlord: the title screen stays black.** The SEGA logo and the opening text render. After them the picture stays black while the music plays. `main` shows the title screen. With `XCL_FRAME_DUMP=1`, non-black samples of 518 400 (3840×2160, every 4th pixel): ``` main: 13.6 s:15984 15.6 s:0 17.6 s:4617 19.6 s:17714 21.6 s:154062 23.6 s:518220 25.6 s:502664 … #46: 13.6 s:15984 15.6 s:0 17.6 s:4617 19.6 s:17720 21.6 s:17723 23.6 s:13328 25.6 s:13390 27.6 s:24793 29.6 s:24797 ``` The black frame at 15.6 s is a normal gap between logos; it is there on `main` too. On #46 the title screen never comes. It's the same with a warm DXIL cache: the run that was checked on screen compiled 1 new DXIL. **It's the f32 compare change.** Unicorn's shaders have no f64 instructions at all: | instruction | count | |---|---| | `v_mul_legacy_f32` | 2503 | | `v_rcp_f32` | 62 | | `v_cmp_gt_f32` | 46 | | `v_cmp_lt_f32` | 34 | | `v_log_f32` | 10 | | `v_cmp_le_f32` / `v_cmp_ge_f32` | 4 each | | `v_sqrt_f32` | 1 | Their HLSL bodies never call the new f64 helpers either. For non-NaN operands the new compares give what the old ones did, so a NaN must reach one of these `v_cmp_*_f32`. The old code's result (the driver's unordered one, as you describe) gave the console's picture. Since the console's compares are ordered too, I suspect the NaN itself is the real bug: something XCL computes differently from the console, so that the console never sees a NaN there. `mul_legacy` already gives 0 for 0 × ∞, so it isn't that one. `rcp` / `log` of 0 → ±∞ followed by a plain multiply or subtract would be my first guess. Options: - find where the NaN comes from (the title-screen pixel shaders, `<data>\shaders\*_ps.*`, the ones with `v_rcp_f32` / `v_log_f32` and the compares); - or keep the plain f32 compares until then and land the f64 part on its own. Note that the f64 helpers sit in the shared prelude, so any change there recompiles every shader of every title after a merge. XDefiant then stays black for minutes on its first run. Not covered yet with #46: Fortnite and XDefiant. Minecraft looked fine.
Author
Owner

@⁠Foniass on GitHub · 2026-11-22 22:47 UTC · original

Tested on RTX 5070 Ti only (Blackwell, driver 32.0.16.2114, D3D12 wave range 32..32). The RDNA2 iGPU on this machine is disabled, so it wasn't tested. The branch was merged onto main 521ec08. The only conflict was in recomp/tests.rs, where both sides just add tests, and I kept both. Run with cargo test --workspace --release --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_* set. Hardware details are in #42.

  • It builds. Result: 374 passed, 0 failed, 34 ignored (main: 367 / 0 / 34), with no regressions.
  • None of the 7 new GPU tests run on NVIDIA. gpu_f32_compares_with_nan, gpu_f64_compares_with_nan, gpu_f64_math_random, gpu_f64_roots, gpu_f64_rounding_and_conversions, gpu_float_compares_random and gpu_ldexp_f64 all return through gpu_ready() (no wave64). So on this card the new compares and f64 helpers are covered only by the translation tests, not by execution. If these kernels don't depend on wave width, a wave32 variant would let them run on NVIDIA too.
**@⁠Foniass** on GitHub · 2026-11-22 22:47 UTC · [original](https://github.com/xcl-dev/xcl/pull/46#issuecomment-7087077996) Tested on RTX 5070 Ti only (Blackwell, driver 32.0.16.2114, D3D12 wave range 32..32). The RDNA2 iGPU on this machine is disabled, so it wasn't tested. The branch was merged onto `main` 521ec08. The only conflict was in `recomp/tests.rs`, where both sides just add tests, and I kept both. Run with `cargo test --workspace --release --no-fail-fast -- --test-threads=4`, no `XCL_SAMPLE_*` set. Hardware details are in #42. - It builds. Result: 374 passed, 0 failed, 34 ignored (`main`: 367 / 0 / 34), with no regressions. - None of the 7 new GPU tests run on NVIDIA. `gpu_f32_compares_with_nan`, `gpu_f64_compares_with_nan`, `gpu_f64_math_random`, `gpu_f64_roots`, `gpu_f64_rounding_and_conversions`, `gpu_float_compares_random` and `gpu_ldexp_f64` all return through `gpu_ready()` (no wave64). So on this card the new compares and f64 helpers are covered only by the translation tests, not by execution. If these kernels don't depend on wave width, a wave32 variant would let them run on NVIDIA too.
Author
Owner

@⁠ivall on GitHub · 2026-11-23 10:14 UTC · original

Merged (be1994b), thanks – and sorry for the wait.

Re-tested on current main (with #67 / #84 / #68 / #69 in) on an RX 9070: Unicorn Overlord now renders its intro and scenes with this PR (frame dumps through the whole run, warm and cold DXIL cache), so whatever produced the NaN that turned the title screen black on the 2026-11-22 main is no longer there. Workspace tests green with the retail samples (shader pack, console DLLs, texture archive), and the lib tests also with XCL_SPLIT_WAVE64=1 (the 32-lane split path on RDNA).

One fix on top (6a41c55): #67 removed gpu_ready_wave32, so gpu_waves uses gpu_ready now (a GPU with 32- or 64-lane waves plus DXC) – your tests still run as wave32 programs on NVIDIA.

**@⁠ivall** on GitHub · 2026-11-23 10:14 UTC · [original](https://github.com/xcl-dev/xcl/pull/46#issuecomment-7097669912) Merged (be1994b), thanks – and sorry for the wait. Re-tested on current `main` (with #67 / #84 / #68 / #69 in) on an RX 9070: **Unicorn Overlord now renders its intro and scenes with this PR** (frame dumps through the whole run, warm and cold DXIL cache), so whatever produced the NaN that turned the title screen black on the 2026-11-22 `main` is no longer there. Workspace tests green with the retail samples (shader pack, console DLLs, texture archive), and the lib tests also with `XCL_SPLIT_WAVE64=1` (the 32-lane split path on RDNA). One fix on top (6a41c55): #67 removed `gpu_ready_wave32`, so `gpu_waves` uses `gpu_ready` now (a GPU with 32- or 64-lane waves plus DXC) – your tests still run as wave32 programs on NVIDIA.
This discussion has been locked. Commenting is limited to contributors.
No description provided.