Skip to content

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

Merged
ivall merged 2 commits into
xcl-dev:mainfrom
grooving6:fix/f64-math-nan-compares
Nov 23, 2026
Merged

ivall merged 2 commits into
xcl-dev:mainfrom
grooving6:fix/f64-math-nan-compares

Conversation

@grooving6

Copy link
Copy Markdown
Contributor

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.

@ivall

ivall commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

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.

@grooving6
grooving6 force-pushed the fix/f64-math-nan-compares branch from 0f48804 to 5b65d40 Compare November 22, 2026 21:43
@Foniass

Foniass commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

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.

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.
@grooving6
grooving6 force-pushed the fix/f64-math-nan-compares branch from 5b65d40 to b8fca79 Compare November 22, 2026 23:30
@ivall
ivall merged commit be1994b into xcl-dev:main Nov 23, 2026
ivall added a commit that referenced this pull request Nov 23, 2026
#67 removed gpu_ready_wave32; gpu_ready (a GPU with 32- or 64-lane waves, DXC) is what #46's
gpu_waves needs.
ivall added a commit that referenced this pull request Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

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.

@grooving6
grooving6 deleted the fix/f64-math-nan-compares branch November 25, 2026 23:08
oni-swr added a commit to oni-swr/xcl that referenced this pull request Nov 26, 2026
A shared stand-in (`standin::shell`) is a Rust object, not a D3D12Core
pipeline. Device xcl-dev#34 MakeResident and xcl-dev#35 Evict keep the PC layout for
console code and had no override, so a stand-in pipeline in the title's
object array reached the driver as `*mut Shell`.

Both are overridden now. Microsoft's MakeResident / Evict support
residency changes on descriptor heaps, heaps, committed resources and
query heaps only; a pipeline state is ID3D12Pageable but not among them,
so the pipeline behind a stand-in is no valid entry either. Each shell
is left out of the array: the driver gets the other objects in order, in
a copy (the title's array is left as it is), and its HRESULT is returned
as it is. A call with stand-ins only returns S_OK without a driver call.
Arrays without a stand-in (empty, null, supported objects) go as they are.
Nothing builds or waits for a lazy pipeline.

The other entry points named in xcl-dev#2455 are not reachable by title code on
main: device xcl-dev#46 SetResidencyPriority and xcl-dev#51 EnqueueMakeResident are
console extensions there (a logged S_OK stub), xcl-dev#44 is the console's
CreatePlacedResourceX (no native pipeline library for StorePipeline),
and xcl-dev#12 CreateCommandList goes to `create_command_list_pc`, which
resolves the initial pipeline.

Fixes xcl-dev#2455

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants