Skip to content

Recompiler: v_rcp_f64 and v_rsq_f64 keep NaN, infinities and denormals - #530

Merged
raylitebull merged 1 commit into
xcl-dev:mainfrom
pablogonz12:recomp-f64-rcp-rsq
Nov 25, 2026
Merged

raylitebull merged 1 commit into
xcl-dev:mainfrom
pablogonz12:recomp-f64-rcp-rsq

Conversation

@pablogonz12

Copy link
Copy Markdown
Contributor

v_rcp_f64 was translated as (1.0 / x) and v_rsq_f64 as (1 / sqrt_d(x)). DXC marks a plain divide fast (no NaN, no infinities), and on RADV (through vkd3d-proton) the divide then gives 0 for 1/NaN: v_rsq_f64 of every negative number and of NaN was 0 instead of NaN, which is what gpu_f64_roots and gpu_f64_math_random report on main on this GPU. The same divide gave +0 for 1/-inf and flushed 1/DBL_MAX to 0 instead of a denormal.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: an rcp_d helper next to sqrt_d. It returns NaN, ±inf for ±0 and ±0 for ±inf itself (on the bits, with isnan_d / isinf_d), and for the rest divides the mantissa (x scaled into [0.5, 1) with frexp_exp_d / ldexp_d) as a precise divide and scales back with ldexp_d, so a result below 2^-1022 is a denormal. v_rcp_f64 is rcp_d(x), v_rsq_f64 is rcp_d(sqrt_d(x)).
  • crates/xcl/src/gpu/recomp/tests.rs: gpu_f64_roots also checks v_rcp_f64 over F64_EDGES (1 ulp); the translation test counts rcp_d(sqrt_d( instead of (double)1 / sqrt_d(.

Tested

Regression run

Made with scripts/linux/smoke.sh from #257 under Wine (not smoke.ps1), MSVC builds (cargo xwin) of both commits, with a local copy of the script in which two lines are changed so that a run which does not reach its end still prints its block (set -eu instead of set -euo pipefail; the data: fallback as an if).

xcl-smoke v1
commit: 508a0c74d7c0625d6d2de8d24494e69dc2e1926f
gpu: AMD Radeon RX 9070 XT (RADV GFX1201)
title: Build
seconds: 60
exit: 0x0
frames shown: 11 (PresentX 17)
presents per 2 s (last 3): 0 1 1
device removals: 0
crashes: 0
peak commit: 0 MB
xcl-smoke v1
commit: e6d5d332b9787c59951194ee1d3066cb081a9e89
gpu: AMD Radeon RX 9070 XT (RADV GFX1201)
title: Build
seconds: 60
exit: 0x0
frames shown: 18 (PresentX 27)
presents per 2 s (last 3): 2 0 1
device removals: 0
crashes: 0
peak commit: 0 MB

Not tested

  • Windows, NVIDIA and Intel GPUs, and the native Windows AMD driver: only RADV through vkd3d-proton.
  • Whether any retail shader uses v_rcp_f64 / v_rsq_f64 on NaN or infinities; the frame counts above are not evidence either way.

AI use

  • Claude Code (Claude Opus 5.5): found the cause, wrote the change, the test changes and this description, and ran the commands above on the machine described.

v_rcp_f64 was (1.0 / x) and v_rsq_f64 (1 / sqrt_d(x)). DXC marks a plain divide fast (no NaN, no infinities), and RADV (through vkd3d-proton) then gave 0 for 1/NaN: v_rsq_f64 of every negative number and of NaN was 0, not NaN (gpu_f64_roots, gpu_f64_math_random). 1/-inf was +0, and 1/DBL_MAX was flushed to 0 instead of a denormal. rcp_d takes NaN, ±0 and ±inf itself and divides the mantissa, precise, scaling back with ldexp_d; v_rsq_f64 is rcp_d(sqrt_d(x)). gpu_f64_roots checks v_rcp_f64 too.
@thehollowedone

Copy link
Copy Markdown

Tested

Native Windows D3D12 on RTX 3080 Ti (621.35), Ryzen 7 9800X3D, Windows 11 build 26300, Rust 1.99.1 MSVC, DXC 1.8.2502.11, debug. On both exact parent 508a0c7 and PR head e6d5d33, I ran cargo test --offline --locked -p xcl --lib f64 -- --test-threads=1 --nocapture with XCL_REQUIRE_GPU=1 and separate build directories.

Both revisions reported 13 passed, 0 failed, 0 ignored and no reported skips. The PR head passed the expanded root test with reciprocal edge cases. The randomized wave32 test covered 1,600 lanes on each revision. Reciprocal square root was exact on 1,584 lanes and within one ULP on the remaining 16 on both revisions.

Not tested

Full workspace, retail titles, native wave64, and identical reciprocal edge-case inputs on the old lowering. The parent ran its own unmodified tests.

AI use

Codex with GPT-6.2 Sol ran the tests and drafted this report.

@arcwiser

Copy link
Copy Markdown

Tested exact head e6d5d332b9787c59951194ee1d3066cb081a9e89 on native Windows / NVIDIA.

  • With XCL_REQUIRE_GPU=1, cargo test -p xcl --lib f64 -- --test-threads=4 --nocapture: 13 passed, 0 failed, 0 ignored. This includes gpu_f64_roots and gpu_f64_math_random; the randomized test reported 0 distinct failures (rsq ulps 0/1/2/>2: [1584, 16, 0, 0]).
  • .\scripts\test.ps1: workspace build passed; 680 passed, 0 failed, 41 ignored.
  • GPU tests reported 2 skips (both no wave64): gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware. These skips are not counted as coverage.
  • XCL_SAMPLE_* variables were unset, so optional game-data tests did not run. I did not run a retail-title smoke comparison.

Machine: Windows 11 Home build 26200; AMD Ryzen 5 7500F; NVIDIA GeForce RTX 4070 SUPER 12 GB, driver 610.88; Rust 1.100.0 MSVC; DXC 1.9.2602.17 from Windows SDK 10.0.28000.0; debug profile.

AI use: OpenAI Codex with GPT-5 selected the verification scope, ran the commands, and drafted this report; I am posting only the measured results above.

@towiice towiice left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested PR head e6d5d33 on NVIDIA GeForce GTX 1650 SUPER (Turing / TU116, wave32, driver 610.88), Windows 11 build 10.0.26300 (26H2), Rust 1.100.0 (stable-x86_64-pc-windows-gnullvm), DXC v1.9.2609:

  • cargo test -p xcl --lib f64 -- --test-threads=4: 13 passed, 0 failed (1.25 s), including gpu_f64_roots, gpu_f64_math_random, gpu_f64_compares_with_nan, gpu_f64_class_compare, and gpu_f64_rounding_and_conversions.

All f64 GPU and unit tests pass on Turing.

@CryingLaughing

Copy link
Copy Markdown

The exact head e6d5d332b9787c59951194ee1d3066cb081a9e89 passed the Windows workspace suite on an RTX 4080. I also ran the expanded reciprocal edge-case fixture with the parent emitter restored. It passed on both, so I did not reproduce the RADV edge-case failures on this Windows driver.

Tested

  • RTX 4080 (Ada), NVIDIA driver 621.01, Core i5-13600K, Windows 11 25H2 build 26200.9974, Rust 1.100.0 MSVC x64, DXC 1.8.2502.11, debug. XCL_ADAPTER=4080, XCL_REQUIRE_GPU=1, synthetic fixtures, all XCL_SAMPLE_* variables unset. Separate Cargo target directories.
  • cargo test -p xcl --lib --locked -j 4 gpu_f64_roots -- --show-output passed 1/1 at the exact head and 1/1 with only crates/xcl/src/gpu/recomp/emit.rs restored from parent 508a0c74d7c0625d6d2de8d24494e69dc2e1926f. The PR fixture and reciprocal inputs were identical in both runs. Neither run skipped.
  • At the exact head, cargo build --workspace --locked -j 4 passed. cargo test --workspace --locked -j 4 -- --test-threads=4 --show-output reported 680 passed, 0 failed, 41 ignored, including all 13 tests with f64 in the name. The pass count includes opt-in game-data tests that returned early and four explicit skips.
  • SKIPPED for native wave64: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware. SKIPPED for D3D12 debug-layer availability: test_title_draws_with_a_root_signature_under_the_debug_layer and uploads_start_from_each_subresources_state.

Not tested

  • Release builds, retail smoke, native wave64, or other adapters on this machine.

AI use

  • OpenAI Codex (GPT-6 family, exact variant unavailable) ran the tests and drafted this report.

@4MDomo

4MDomo commented Nov 24, 2026

Copy link
Copy Markdown

NVIDIA test report for e6d5d33 (this PR's head), native Windows D3D12.

Tested

  • NVIDIA GeForce RTX 3070, driver 32.0.16.1742; Intel i9-9900K, 32 GB; Windows 11 Home 10.0.26300 (native D3D12, no Wine); rustc 1.98.1 stable-x86_64-pc-windows-msvc; DXC 1.8.2502.11 (Windows SDK 10.0.26100.0); debug.
  • cargo test --workspace -- --test-threads=4 at e6d5d33: 680 passed, 0 failed, 41 ignored. gpu_f64_roots (with the new v_rcp_f64 checks over F64_EDGES) and gpu_f64_math_random pass.
  • On main 508a0c7 the same machine also passes gpu_f64_roots and gpu_f64_math_random (680 passed, 0 failed, 41 ignored), so the 1/NaN = 0 result does not show on NVIDIA's native driver. The PR keeps it passing there.
  • SKIPPED (no wave64): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware, as on main.

Not tested

  • A retail title; DXC 1.9 (this run used 1.8.2502).

AI use

  • Claude Code (Claude Opus 5.5) ran the suite and wrote this report; checked by me.

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

Native Windows AMD run, since Not tested lists the native AMD driver: e6d5d33 merged onto main 9ce2070 (clean merge): pass. The negative control may matter for how this is tested, though.

Tested

  • AMD Radeon RX 9060 XT (RDNA4, native 64-lane waves), driver 32.0.32029.2010; Ryzen 7 5800X3D; Windows 11 Pro 26200; Rust 1.100.0 MSVC; DXC 1.9.2602.17; debug.
  • .\scripts\test.ps1: 684 passed, 0 failed, 41 ignored; no SKIPPED GPU tests.
  • XCL_REQUIRE_GPU=wave64,mesh cargo test -p xcl --lib f64: 13 passed, including gpu_f64_roots (this PR's rcp / rsq checks) and gpu_f64_math_random.
  • Negative control, merged tree with only emit.rs back on main: only unary_f64_math_vop1_and_vop3 (the HLSL text check, rcp_d(sqrt_d( count 0 vs 2) fails. All 9 gpu_f64_* tests still pass, including gpu_f64_roots with this PR's checks. So on the native AMD driver the old emission already gives the expected values for these inputs. The GPU test doesn't separate old from new here, and the difference you saw may be specific to RADV through vkd3d-proton. If the RADV failure has an input that isn't in gpu_f64_roots yet, adding it would tell us whether native AMD is affected.

AI use

  • Claude Code (Claude Opus 5.5) ran the commands and wrote this report from the logs; I checked it before posting.

T-Crypt added a commit to T-Crypt/bathyal that referenced this pull request Nov 24, 2026
@tugasky

tugasky commented Nov 24, 2026

Copy link
Copy Markdown

Your "Not tested" list names the native Windows driver, and that is what this machine is:
the same GPU as yours (RX 9070 XT, GFX1201) but Windows 11 and AMD's own driver through
Vulkan, not RADV/vkd3d-proton.

Machine: Windows 11 Pro 26300, Ryzen 7 9700X (8C/16T), Rust 1.98.0
x86_64-pc-windows-msvc, DXC 1.9.2602.17, Radeon RX 9070 XT (RDNA4, 16 GB), driver
32.0.32029.2010. Release build. Head e6d5d33 already has main (7ebf1b8) as an
ancestor, so nothing had to be merged.

The f64 GPU tests pass here. XCL_REQUIRE_GPU=1 cargo test -p xcl --lib --release -- f64 -- --test-threads=4 → 13 passed, 0 failed, including the extended gpu_f64_roots
(rcp over F64_EDGES at 1 ulp) and gpu_f64_math_random.

But the fix changes nothing on this driver, and that is the interesting part. main's
f64 tests pass here too, which by itself means little (they do not check v_rcp_f64), so
I took only your tests.rs onto main's emit.rs – a worktree at 7ebf1b8 with
git checkout e6d5d33 -- crates/xcl/src/gpu/recomp/tests.rs, nothing else, rebuilt – and
ran the same two tests against the old (1.0 / x): both pass. On the native AMD Windows
driver that divide already gives NaN for NaN, −0 for ±inf and a denormal for 1/DBL_MAX, so
the flush you measured on RADV does not exist here. That supports your diagnosis rather
than contradicting it: this is a portability fix, and my machine is the case where the old
translation happened to be right. A 1-ulp tolerance cannot be hiding your failure, which
was 0 instead of NaN, so the new test would have caught it here if it were reachable.

Does this title use the instruction at all? No. A throwaway scanner (decode the way the
recompiler does, count by opcode name) over the whole carved pack, all stages:

opcode programs uses
v_rcp_f64 0 0
v_rsq_f64 0 0
v_sqrt_f64 0 0
v_rcp_f32 (control) 17 393 424 863
v_rsq_f32 (control) 17 221 269 310

of 26 698 programs (26 503 containers, 0 undecoded dwords; the 13 210 wave64 VS/PS from
the #517 census are inside). f64 code does exist in this pack, but only in 5 compute
programs, and only as v_add_f64 (216 uses). So v_rcp_f64 and v_rsq_f64 never run for
this title on any driver – nor does v_sqrt_f64, which main already translates with
sqrt_d. The PR is right for the recompiler and for the test suite, but it cannot move a
frame of this title, and neither can the RADV bug it fixes.

Suite: cargo build --workspace --release, then
cargo test --workspace --release -- --test-threads=4 → 680 passed, 0 failed, 41
ignored
, no test skipped (701 on the #341/#517/#528 stack, which main does not have
yet). Without the workspace build first, the two e2e tests fail on the missing
xcl_testtitle.exe – their own guard, not your change.

Not tested: the Windows smoke run (needs the retail title on this machine), NVIDIA and
Intel, and anything on RADV.

No game data in this comment – only counts printed by the tool.

AI use: written with Cline; the runs are from my machine.

@Tjabooo

Tjabooo commented Nov 24, 2026

Copy link
Copy Markdown

The setup this PR targets, RADV through vkd3d-proton, hasn't been tested yet. Here it is: e6d5d33 merged onto main dd2ae9a (clean merge), pass: it fixes both f64 failures there.

Tested

  • AMD Radeon RX 9070 XT (RDNA4, GFX1201), RADV / Mesa 26.2.6; Arch Linux; Wine 11.21 (Arch package, has kernel write watches); vkd3d-proton v3.1 with Linux: docs/linux.md, scripts/linux and Wine start-up checks for vkd3d-proton and DXVK (#154, #241) #257's vkd3d-proton-xcl.patch and -Db_ndebug=true, Wine's own dxgi; DXC 2026_09_29 (XCL_DXC_DIR); VKD3D_CONFIG=ignore_shared_fence.
  • Rust 1.100.0, cross-built for x86_64-pc-windows-gnu (mingw-w64 gcc 16.2), debug; tests run under Wine via CARGO_TARGET_X86_64_PC_WINDOWS_GNU_RUNNER=wine.
  • cargo test -p xcl --lib --locked --target x86_64-pc-windows-gnu -- --test-threads=4, 0 GPU tests SKIPPED:
gpu_f64_roots gpu_f64_math_random lib suite
main dd2ae9a FAILED: v_rsq_f64 of every negative input, -inf and NaN gives 0e0, want NaN FAILED: 1,252 distinct failures, all rsq(<negative>) = 0e0, want NaN (at bdab194: 644 passed, 3 failed)
this PR on dd2ae9a ok ok 649 passed, 1 failed, 21 ignored

The one failure left with this PR, gpu_nonfused_mad_and_mac_prevent_fma_contraction, is unrelated and fails on main too: v_fma_f32 overflow control should stay fused, left: 2139095040 (+inf) / right: 1946157055. RADV through dxil-spirv computes that fma unfused, so it overflows.

Not tested: native Windows (others covered it above), NVIDIA under Wine, a retail title. No game files used.

AI-assisted (Claude); every number above is from a run on this machine.

@Dolaned

Dolaned commented Nov 24, 2026

Copy link
Copy Markdown

Tested on RDNA2 as well: same fix, same result.

  • Steam Deck LCD (Van Gogh, RDNA2), SteamOS 3.9.5, Mesa 26.2.0-devel (RADV), stock Proton Experimental 11.0-20261112 (unpatched vkd3d-proton), DXC 1.9.2609
  • cross-built x86_64-pc-windows-msvc debug (Rust 1.100, clang-cl/lld-link, xwin SDK), each lib test run on its own under Proton
test 508a0c7 e6d5d33 (this PR)
gpu_f64_roots fail (v_rsq_f64(-1.5e0) = 0e0, want NaN, ...) pass, including the new v_rcp_f64 checks
gpu_f64_math_random fail (1252 distinct rsq failures) pass
unary_f64_math_vop1_and_vop3 pass pass

Whole lib suite on this PR: 633 passed, 2 failed, 21 ignored, 5 aborted. The failures and aborts are the ones main already has on this setup and aren't touched by this change: gpu_format_stores_atomics_global_lds (wave64 ds_bpermute, passes with XCL_SPLIT_WAVE64=1), xma2_source_voice_decodes_and_manages_lifetime, and five vkd3d-proton vtable asserts (heap, root signature, resource, fence x2). gpu_nonfused_mad_and_mac_prevent_fma_contraction and pe::tests::parses_own_test_binary_tls pass here.

Not tested: Windows, NVIDIA, a release build, any retail title.

AI use: Claude (Claude Code) built and ran the tests on the Deck and drafted this; I checked it.

@Dolaned

Dolaned commented Nov 24, 2026

Copy link
Copy Markdown

Also have AI pro 9700s I can test on

@crastinator crastinator left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested on RDNA2 under Wine (RADV), the GPU this PR wasn't run on.

Tested (head e6d5d33, and merged onto main 1ec4c98: no conflicts)

  • AMD Radeon RX 6700 XT (RDNA2, gfx1031, RADV NAVI22, Mesa 26.3.1), Ryzen 7 5700X, Linux 7.3.5 (CachyOS)
  • Wine: Proton Experimental 11.0-20261112 (its files/bin/wine, kernel write watches), with its own vkd3d-proton (stock, not the #257 patch) and DXVK v3.1.1-209 dxgi; VKD3D_CONFIG=ignore_shared_fence; DXC 1.9.2609
  • Cross-built on Linux for x86_64-pc-windows-msvc (clang-cl / lld-link 23.1.5, xwin SDK 10.0.26100), Rust 1.99.1, debug; the lib test binary run under Wine
  • On main 1ec4c98, gpu_f64_roots and gpu_f64_math_random fail here the same way as in the PR description: v_rsq_f64 of every negative number, -inf and NaN gives 0 (1252 distinct failures in the random test).
  • At e6d5d33, both pass, and they also pass with the PR merged onto 1ec4c98.
  • Whole lib suite, one test per process (see note): main 664 ok / 4 failed / 21 ignored / 5 aborted at the #241 assertion / 1 exits with code 5 (mid_list_write64_wait64_split_avoids_cycle_and_reuses_spares, right after its mid-list split; newer than this PR and probably the same stock-vkd3d-proton limit); PR head 633 ok / 2 failed / 21 ignored / 5 aborted. The only status changes in tests present on both sides are the two above, FAILED -> ok. The tests missing on the PR side were added to main after its base 508a0c7. The 2 that still fail are gpu_nonfused_mad_and_mac_prevent_fma_contraction (#676) and xma2_source_voice_decodes_and_manages_lifetime (#890), and both fail on main too.

Under stock vkd3d-proton, D3D12.X tests take down the test process (iface->lpVtbl == &d3d12_heap_vtbl, #241), so the suite was run one test per process: every test in the binary, each with --exact <name> --test-threads=1, compared against main the same way.

Not tested

  • Windows, NVIDIA, a retail title. The full suite with a patched vkd3d-proton (the 5 aborting tests).

AI use

  • Claude Code (Claude Opus 5.5) built the branches, ran the tests and wrote this report; I checked the results.

@raylitebull
raylitebull merged commit 7b9eabb into xcl-dev:main Nov 25, 2026
2 checks passed
raylitebull pushed a commit that referenced this pull request Nov 25, 2026
Direct DXIL lowering of v_rcp_f64 and v_rsq_f64 emitted raw unguarded double division (1.0 / a and 1.0 / sqrt_d(a)), unlike the HLSL path's rcp_d helper (#530). Drivers may return 0 for 1/NaN (e.g. v_rsq_f64 of negative numbers) or flush subnormal reciprocals such as 1/DBL_MAX to 0.

Add Lw::rcp_d in crates/xcl/src/gpu/dxil/lower_wide.rs handling NaN (preserving payload), ±0 -> ±inf, and ±inf -> ±0 explicitly, substituting 1.0 into the eager divide path for special inputs, and scaling finite operands into [0.5, 1) via frexp_exp_d and ldexp_d around ddiv(1.0, scaled) before rescaling back with ldexp_d. Wire both v_rcp_f64 and v_rsq_f64 through Lw::rcp_d and add DXIL validation/disassembly and wave32 GPU edge regression tests.

Part of #1269

Assisted-by: Google Antigravity (Gemini 4 Neon)
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.