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

Merged
grooving6 merged 2 commits from upstream-pr/46 into main 2026-11-23 10:13:42 +00:00
grooving6 commented 2026-11-22 21:05:27 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by grooving6 on 2026-11-22T21:05:27Z.


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.
<!-- greyfinch-sync:ivall/xcl:issue:6547776672 --> Imported from [ivall/xcl](https://github.com/xcl-dev/xcl/pull/46). Originally posted by [grooving6](https://github.com/grooving6) on 2026-11-22T21:05:27Z. --- 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.
upstream-sync closed this pull request 2026-11-24 06:56:54 +00:00
ivall commented 2026-11-24 07:00:20 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-22T21:27:50Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7086068958 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/46#issuecomment-7086068958). Originally posted by [ivall](https://github.com/ivall) on 2026-11-22T21:27:50Z. --- 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.
Foniass commented 2026-11-24 07:00:21 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by Foniass on 2026-11-22T22:47:11Z.


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.
<!-- greyfinch-sync:ivall/xcl:comment:7087077996 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/46#issuecomment-7087077996). Originally posted by [Foniass](https://github.com/Foniass) on 2026-11-22T22:47:11Z. --- 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.
ivall commented 2026-11-24 07:00:25 +00:00 (Migrated from github.com)

Imported from ivall/xcl. Originally posted by ivall on 2026-11-23T10:14:17Z.


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.

<!-- greyfinch-sync:ivall/xcl:comment:7097669912 --> Imported from [ivall/xcl](https://github.com/ivall/xcl/pull/46#issuecomment-7097669912). Originally posted by [ivall](https://github.com/ivall) on 2026-11-23T10:14:17Z. --- 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.
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
xcl-dev/xcl!45
No description provided.