Recompiler: f16 results round to nearest even in the default mode (#111) #836
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/842"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from ivall/xcl. Originally posted by joeypercia on 2026-11-24T06:39:48Z.
In the default mode every f16 result narrowed from binary32 went through HLSL
f32tof16, which rounds toward zero, so every inexactv_add/sub/sqrt/rcp/rsq/exp/log/ldexp_f16result came out one binary16 step low and an overflow stopped at 65504 instead of inf. Native RDNA2 rounds to nearest even (@luckyteagirl's raw-ISA measurements on #672:ldexp(1, 20)= inf, subnormal ties to even).to_f16_exprnow narrows withf32_to_f16_rm(e, 0u)in the default mode, the helper the multiplies already use (#166); under a dynamic rounding mode nothing changes. Part of #111.Since #3243 every stage goes through the direct DXIL translator by default, and its
to_f16(dxil/prelude.rs) still narrowed withlegacyF32ToF16: onmainb10cc4c6this PR's own tests failed on the default path and passed only withXCL_DIRECT_DXIL=0. A second commit gives the direct translator the same change:to_f16rounds in the shader's mode, nearest even by default, andv_fract_f16keeps its result below 1.0.v_fract_f16needs one extra step:fract_fclamps its binary32 result below 1.0, and1 - 2^-24narrows to 1.0 by nearest even, so its binary16 result is kept at0x3BFF(#655 found this).This supersedes #696 (add / sub only), as its author suggested on #111; its cases are covered by the census test below.
In a retail title. The same
to_f16_exprnarrowsv_interp_p2_f16,v_fma_mixlo_f16andv_fma_mixhi_f16, so in a program that does not change the rounding mode they round toward zero onmaintoo, and this PR makes them round to nearest even. They are most of what it changes in a retail game: in the newer retail title'sshaders.rpf(xcl shaders --json, 26 503 shaders) there are 59 367v_interp_p2_f16(4 776 shaders), 33 900v_fma_mixlo_f16(6 155) and 2 135v_fma_mixhi_f16(842), against a few hundredv_add/sub/mul/fma_f16and nov_ldexp_f16orv_fract_f16. (v_cvt_f16_f32, 38 871, already rounds to nearest even onmain.)gpu_s_round_mode_controls_f16_rounding_across_modeschecksv_fma_mixlo_f16only in a program that changes the mode, where it already took the rounding path.Measured for the mix ops (a local test, not in this PR, so the regression runs below stay on its head):
v_fma_mixlo_f16/v_fma_mixhi_f16of x·1 + 0 with x = 1 + 2^-11 + 2^-20, no mode change in the program, give0x3C00(toward zero) onmain037aecdand0x3C01(nearest even) with this PR merged on it, on both GPUs below.v_interp_p2_f16is from reading the emitter, not measured.What is in it
crates/xcl/src/gpu/dxil/prelude.rs,crates/xcl/src/gpu/dxil/lower_wide.rs(second commit): the same two changes in the direct DXIL translatorcrates/xcl/src/gpu/recomp/emit.rs:to_f16_exprrounds to nearest even in the default mode;v_fract_f16keeps its result below 1.0 (only without OMOD)crates/xcl/src/gpu/recomp/tests.rs:gpu_f16_results_round_to_nearest_even_by_default: random inputs against an exact reference for add, sqrt, rcp, rsq, exp, log, ldexp and mul; fails on any toward-zero result of the exact ones (add, ldexp, mul) and prints the counts for the approximate onesgpu_f16_unary_results_round_to_nearest_even: fixed inputs whose exact result sits 0.69–0.88 of a step above the lower neighbour (so a 1-ulp binary32 approximation can't change it), plus @upipibi's two ldexp cases from #672 (overflow to inf, a subnormal tie to even)gpu_fract_f16_stays_below_one_with_nearest_even:-2^-14and-2^-24give0x3BFF, NaN stays NaNTested
86c32c31(this PR rebased ontomainb10cc4c6):cargo test -p xcl --lib f16: 24 passed, 0 failed on both GPUsmain's default path,gpu_f16_results_round_to_nearest_even_by_defaultandgpu_f16_unary_results_round_to_nearest_evenfail on both GPUs (sqrt(0x3420)=0x380F, want0x3810); withXCL_DIRECT_DXIL=0they passcargo test --workspace --no-fail-fast -- --test-threads=4, RTX 4070 Ti: 2607 passed, 4 failed, 49 ignored. The failures weree2e's two title tests andgpu_two_pass_cache_e2e, which needcargo build --workspacefirst; after it,cargo test -p xcl --test e2e --test gpu_two_pass_cache_e2e: 6 passed, 0 failed on both GPUs0x80004005) partway through the suite, single-threaded too and with the test running then skipped;mainb10cc4c6ends the same way on this GPU.gpu::dxil::fuzz_tests::fuzz_validate_and_gpu_1(seed 783) fails here and onmainalike. So the RDNA2 counts are not complete for this headSKIPPEDon the RTX 4070 Ti: 30, 21 of themno wave64; the restno d3d12SDKLayers.dll(2),no heap windows, and direct-DXIL f16 / f32 per-lane forms refused for #1426's semanticsRegression run
RTX 4070 Ti, release builds, 60 s each, with
XCL_PREPARE=0on both (main's launch preparation of this title takes over 15 minutes on a cold cache here):main:
PR:
Not tested
v_fma_f16/v_fmac_f16/v_pk_fma_f16: rounding the f32 result to nearest even is closer than toward zero, but still not always single rounding (#111's FMA point; #128 handles the exact sum for directed modes)v_fract_f16with OMOD: narrowed to nearest even without the below-1.0 step (a scaled fraction can legitimately reach 1.0 or more); not checked on hardwareXCL_PREPAREon)AI use
Imported from ivall/xcl. Originally posted by Anon64 on 2026-11-24T06:51:20Z.
Regression runs for this PR's head
feaeb7band for its basemainbeaa245, on a retail title (same title, 60 s each), made withscripts/smoke.ps1(release, clean tree at each commit) and pasted unedited, for Regression run.main:
this PR:
Tested
cargo test -p xcl --lib f16(debug) onfeaeb7b: 15 passed, 0 failed, includinggpu_f16_results_round_to_nearest_even_by_default,gpu_f16_unary_results_round_to_nearest_evenandgpu_fract_f16_stays_below_one_with_nearest_evenNot tested
Since this covers #696's add / sub, I'll close #696 once this one is merged.
Report written with Claude Code (Claude Opus 5.5) from runs on this machine.
Imported from ivall/xcl. Originally posted by futurepro62 on 2026-11-24T07:04:10Z.
Full-suite run to go with the smoke runs above, so the new GPU tests have a result on hardware. Tested exact head
feaeb7b(on its basebeaa245).Tested
cargo build --workspace, thenXCL_REQUIRE_GPU=mesh cargo test --workspace -- --test-threads=4: 716 passed, 0 failed, 41 ignored. SKIPPED (no wave64):gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware.gpu_f16_results_round_to_nearest_even_by_default,gpu_f16_unary_results_round_to_nearest_even,gpu_fract_f16_stays_below_one_with_nearest_even.Not tested
AI use
Imported from ivall/xcl. Originally posted by zhouzhidong3-hash on 2026-11-24T07:04:55Z.
Paired RTX 5070 Release check of the 11 raw binary16/exponent pairs from the native gfx1031 reference, using the same wave32 probe, driver 591.86 and DXC 1.8.2502.11 across runs.
6ec4036feaeb7bThe #842 head corrects all three rounding differences from the #672 baseline. Its checked-in gpu_f16_results_round_to_nearest_even_by_default test also passed. The #842 emitter still calls HLSL ldexp for the f16 instruction; the third row locally substituted #672's ldexp_f call, leaving #842's narrowing in place. This is a focused combination check, not evidence that #842 introduced the extreme-exponent behavior. Default-mode synthetic cases only; no full-branch merge, wave64 or game run tested here. OpenAI Codex ran the comparison and drafted the report.
Integration check: the complete two-file #672 patch applied cleanly to
feaeb7b. The Release f16 name-filtered suite on this local combination passed 16/16, with 681 filtered out and no runtime skip reported. The filter also matches UTF-16 tests. The relevant passes include the original ldexp, rounding census, unary rounding and fract fixtures from these two PRs. Full workspace and retail runs remain untested here.Imported from ivall/xcl. Originally posted by SlopTart on 2026-11-24T07:17:40Z.
Linux / AMD RDNA3 datapoint.
Merged state: head
feaeb7bonto mainbeaa245, clean merge.Full suite,
cargo test --workspace --release --no-fail-fast --target x86_64-pc-windows-gnu -- --test-threads=4: 706 passed, 10 failed, 41 ignored. Failures are the known Wine-lane set from my #42 rows plus #718 trio members; nothing here fails because of your change. Your three f16 round-to-nearest-even tests all pass on RDNA3 native wave64:gpu_f16_results_round_to_nearest_even_by_default,gpu_f16_unary_results_round_to_nearest_even,gpu_fract_f16_stays_below_one_with_nearest_evenok. Follow-on datapoint to my #696 row for the same f16 axis.Not tested: retail titles (none here), Windows, RDNA4.
Counts only, no game data.
Machine: RX 7900 XT (RADV NAVI31), Mesa 26.2.5, kernel 7.2 (Arch), Proton Experimental (wine-11.0), patched vkd3d-proton v3.1, Rust cross
x86_64-pc-windows-gnu,XCL_NETWORK=0, release profile,--test-threads=4. Linux rows: #42 / #718.AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting.
Imported from ivall/xcl. Originally posted by eizispark on 2026-11-24T08:08:01Z.
Hardware check of
feaeb7b0b437abfddcb6612f9d4bd1c853ebfb6a(verified clean merge withmain1c87b1d): pass.Tested
rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profilecargo test --workspace -- --test-threads=4:716 passed, 0 failed, 41 ignored(GPU tests skipped on this adapter:none)gpu_f16_results_round_to_nearest_even_by_default,gpu_f16_unary_results_round_to_nearest_even,gpu_fract_f16_stays_below_one_with_nearest_even,gpu_f16_defaults_to_round_to_nearest_even,gpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware): allok;--nocapturecensus on RX 9070 XT recorded[N, 0, 0]([nearest even, toward zero, neither]) across all 8 ops (add,sqrt,rcp,rsq,exp,log,ldexp,mul) in bothwave64andwave32Regression run
main(beaa245):PR (
feaeb7b):(Note: an initial cold-shader-cache smoke run exited early with
0xc0000409,presents per 2 s (last 3): 0 21 59,device removals: 0,crashes: 0,peak commit: 9490 MBduring first-run DXC compilation; the immediate rerun above ran the full 60 s cleanly.)Not tested
AI use
scripts/smoke.ps1harness on this machine and formatted this report.Imported from ivall/xcl. Originally posted by KENAJBBY on 2026-11-24T09:31:06Z.
Native RDNA2 (
gfx1036): f16 add / sub round once, in every rounding modeThis is a hardware reference for the add / sub part of this PR (which covers #696 / #685). I ran the instructions on raw bits on an RDNA2 GPU under an explicitly set MODE (
s_setreg_b32, read back withs_getreg_b32before the instruction). Every result was compared with an exact integer reference: an f16 sum is exact in units of 2^-24, rounded once in the mode under test.v_add_f16,v_sub_f16,v_subrev_f16andv_pk_add_f16(both halves) show 0 differences from a single correct rounding on 60,000 operand pairs, in each of 37 modes. The pairs lean towards ones that need rounding, plus ties, overflow, zero signs and subnormals. The modes are every f16 round mode × every f16 denorm mode × IEEE 0/1, plus f32 round, f32 denorm and DX10_CLAMP variations, which change nothing.0x1C0, give exactly #696's expected bits:0x3C00,0x12000x3C010x3BFE0xBBFE0x3C01/0x3C010x3C00,0x08000x3C000x3C000xBC000x3C00/0x3C000xBC00,0x92000xBC010xBBFE0x3BFE0xBC01/0xBC010x3C00,0x10000x3C000x3BFF0xBBFF0x3C00/0x3C000x7BFF,0x50000x7C000x7BFE0xFBFE0x7C00/0x7C00Outside this PR's scope, for #111 and #73:
s_round_mode: the hardware writes MODE[3:0] = the immediate.s_round_mode 0..15read back as MODE[3:0] = 0..15 with MODE[9:4] untouched, ands_denorm_mode nwrites MODE[7:4] = n. So0xCis f16/f64 round toward zero with f32 RNE, as #91 assumes; #73 listed this as unconfirmed. In0xCthe hardware rounds the f16 sum once toward zero. The dynamic-mode path (binary32 sum, thenf32_to_f16_rm(…, 3)), as I model it, differs on 601 of the 60,000 pairs forv_add_f16, always where the binary32 sum is inexact. #111's three cases give the values #111 calls correct:0x3C00 + 0x0001toward +inf =0x3C01,0x4000 + 0x8001RTZ =0x3FFF,v_fma_f16(0x4000, 0x3C00, 0x8001)RTZ =0x3FFF.s_denorm_mode.Tested
gfx1036, RDNA2), driver 32.0.21030.2001, AMD OpenCL3652.0 (PAL,LC), native wave64; Windows 11 Pro 26200.s_setreg_b32 hwreg(HW_REG_MODE, 0, 10), reads it back withs_getreg_b32(checked against the requested value in every run), runs the instruction and restores MODE. Inputs: 4 instructions × 37 modes × 60,000 pairs.Not tested
v_fma_f16, NaN / inf inputs; console hardware and its entry MODE.AI use
Imported from ivall/xcl. Originally posted by JJAutoCompare on 2026-11-25T06:57:31Z.
Tested
Clean detached PR head
feaeb7b0b437abfddcb6612f9d4bd1c853ebfb6aon STOR-WINLAB. Windows 11 Enterprise Evaluation 25H2 x64 build 26200.9974, Ryzen 7 3800X, Intel Arc A770 driver 32.0.101.9175, Rust/Cargo 1.100.0 MSVC, DXC/DXIL 1.9.2609.5.cargo test --locked -p xcl --lib --no-runbuilt outside the Arc mutex in 1m 13s, exit 0. WithXCL_ADAPTER=A770,XCL_DIRECT_DXIL=0and DXC/DXIL 1.9.2609.5 configured, the following exact filters were run underInvoke-ArcTest.ps1 -Test.3 passed, 0 failed, 0 ignored, 0 skipped, 695 filtered out per invocation. Native and wrapper exits were 0. On standalone wave32, 4,727 inexact results matched nearest-even across eight operations, with assertions enforcing 2,041 add/ldexp/multiply cases. The unary and fract fixtures passed 224 and 32 lane comparisons respectively, including overflow, a subnormal tie, clamping below one and NaN preservation.
Not tested
Split or native wave64, direct DXIL, other adapters, negative controls, full workspace suite, and retail smoke.
AI use
T3 Code / Codex (
gpt-6.2-sol) ran the verification and wrote this report.Imported from ivall/xcl. Originally posted by DavidM1911 on 2026-11-25T10:27:48Z.
Verification on RTX 3060 Ti (Windows 11), branch merged with current
main:cargo test -p xcl --lib: all passed, 0 failed, ~26 ignored. (Intermittent single-test flake / 0xc0000005 abort under parallel scheduling — tracked in #1978; clean on single-threaded rerun.)Thanks!
Imported from ivall/xcl. Originally posted by throny on 2026-11-28T02:13:54Z.
merge-check claim
head:
feaeb7b0b4Imported from ivall/xcl. Originally posted by throny on 2026-11-28T02:30:28Z.
merge-check
base:
753e8b1ahead:
feaeb7b0b4merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 1515 passed, 0 failed, 47 ignored (the suite runs the HLSL backend; see the review for the other one) (26 SKIPPED lines); base alone: 1512 passed, 0 failed, 47 ignored
review: one thing to fix: the direct-DXIL backend is not updated. gpu/dxil/prelude.rs Lw::to_f16, which is documented to compute the same bits as the HLSL helper, still converts with f32_to_f16 (round toward zero), and the fract change is not mirrored either; the PR does not touch that file. With XCL_DIRECT_DXIL=1 the two backends would then disagree on every default-mode f16 result, and the PR's three new tests would fail in that mode (by reading the code; not run here). The rounding helper itself is right: ties to even, the carry to infinity just below 65536, subnormal ties; double rounding through binary32 is harmless for add, mul and sqrt. Open from the thread, not caused by this PR: ldexp at extreme exponents.
performance: default path, and not measured beyond a 60 s run in the thread: every default-mode f16 result (the PR counts about 95,000 instances across the title's shaders, interpolation and fused multiply-add included) now goes through a software conversion of about 20 ALU operations with branches instead of the intrinsic, which can cost pixel-shader time. It changes a translator source: every tester re-translates all shaders once.
verdict: not yet: mirror the change in the direct-DXIL helper (and its fract path), and a longer frame-time comparison on a title. I will rerun on the new head and can add a world run on NVIDIA.
hardware / tool and model: RTX 4080 16 GB (621.40), i9-12900K (no SSE4a), Windows 11 26300, Rust 1.100.0, debug. Claude Code; merge, build and suite run by Claude Fable 5.2, diff review by Claude Opus 5.5 (every hunk read; nothing run on the GPU beyond the suite).
Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-28T10:19:29Z.
merge-check claim
head:
feaeb7b0b4Imported from ivall/xcl. Originally posted by KOLESNiii on 2026-11-28T11:37:49Z.
merge-check
base:
e429936c10head:
feaeb7b0b4merge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 2129 passed, 9 failed, 48 ignored, 25 SKIPPED: 21 no wave64, 3 per-lane f16/f32 forms refused (#1426 semantics), 1 no heap windows; failing: gpu::dxil::module::split::tests::dispatch_chain_oversized_loop_matches_whole, gpu::dxil::module::split::tests::dispatch_chain_wave32_live_loops, gpu::dxil::module::split::tests::dispatch_chain_wave64_live_loops_and_exchange, gpu::recomp::heap_window_tests::gpu_heap_flat_reads_match_guarded_and_root_output, gpu::recomp::tests::gpu_compute_mode_dispatch_abi, gpu::recomp::tests::gpu_float_cas_uses_ieee_equality, test_title_draws_with_a_root_signature_under_the_debug_layer, test_title_runs_and_game_folder_is_untouched, test_title_runs_under_the_debug_layer_with_dred: these 9 fail on the base too (the base e429936c's suite, run alone on this PC with the same command: 2126 passed, 9 failed, 48 ignored, 25 SKIPPED, these same 9 failing), and each also failed when rerun alone on this merge (the two debug-layer e2e tests stop on the D3D12 debug layer rejecting a pixel shader as corrupt or unsigned, and test_title_runs_and_game_folder_is_untouched on the test title's console command list memory-methods check, "post-Wait32/64 write failed"); not caused by this PR. The suite ran on this PC at the same time as 3 other PRs' suites, in one queued job.
review: none. Whole diff read (emit.rs: to_f16_expr always narrows through f32_to_f16_rm, nearest even in the default mode instead of legacy f32tof16's toward zero, the dynamic mode unchanged; v_fract_f16 without omod keeps a result that narrows to 1.0 at 0x3BFF; tests.rs: an exact binary16 rounding reference, a census of add / sqrt / rcp / rsq / exp / log / ldexp / mul that fails only on the exact ops, fixed unary cases and fract cases). Every f16 result that went through to_f16_expr (VOP1/2/3 f16 arithmetic, v_interp_p2_f16, fma_mix, the f16 add helper) now rounds like v_mul_f16 already did; v_cvt_pkrtz_f16_f32 keeps its own toward-zero form; f32_to_f16_rm is in the always-included PRELUDE. Only the HLSL path changes; the direct DXIL path is not in this diff.
performance: reasoned, not measured: f32_to_f16_rm costs a few more ALU ops per f16 result than f32tof16, in shaders using f16 ops; translator output changes, so such shaders are translated again once.
verdict: safe to merge
hardware / tool and model: Windows 11 build 26300.9974, NVIDIA GeForce RTX 2070 SUPER 8 GB (driver 621.40, wave32 only), AMD Ryzen 5 3600XT; rustc 1.100.1 (20ddd0171 2026-11-25) x86_64-pc-windows-msvc, debug test profile; trial merge 3465b5a847f189eaca26d05f5f4a0549f898a05c (local, not pushed). Claude Code: Claude Opus 5.5 (coordination, full-diff review, draft).
correction (2026-11-28): the base counts above were first given as 2128 passed: that base run reused this PC's merge test binary (one cargo target dir for both sides). The base was rerun with its own target dir; the same 9 tests fail on it, so the classification and the verdict stand.
Imported from ivall/xcl. Originally posted by Foniass on 2026-12-01T04:17:15Z.
Triage (#1607) – head
86c32c3Overlap: #1841 targets the same #111 default-mode truncation, but only for add/sub/fma/ldexp, and replaces
madwith anfma_rtoround-to-odd so fma is not double-rounded. This PR instead makesto_f16_expr(and Direct DXILto_f16) round to nearest even for every f16 result, plus thev_fract_f160x3BFF clamp. The earlier group members #515 and #696 (single ops only) are closed. Main still usesf32tof16in the non-dynamicto_f16_expr.Two points:
v_fma_f16/v_fmac_f16/v_pk_fma_f16still go throughmadin f32 and then RNE to f16, so a double rounding remains there (#1841 handles it); and every f16 result now takes the softwaref32_to_f16_rm(.., 0u)instead of the native conversion, a per-op ALU cost that is not measured in the description.Recommendation: keep open.
AI review: Claude Code (Claude Opus 5.5), diffs read only, not built or run; not checked by a human.
Imported from ivall/xcl. Originally posted by fromthemoonx on 2026-12-03T10:35:51Z.
merge-check
base:
16c2fd082ahead:
86c32c314cmerge: clean
build: cargo build --workspace -> ok
tests: cargo test --workspace --no-fail-fast -- --test-threads=4 -> 3015 passed, 0 failed, 56 ignored (base: 3012 passed, 0 failed, 56 ignored, counted on
395405e4; only docs and scripts changed since); SKIPPED: 25, the same as base (22 no wave64, 2 no heap windows, 1 no GPU_UPLOAD heaps); failing: nonereview: read the whole diff (4 files, +186 -14). No correctness finding; one open point, the cost (see performance).
cargo test -p xcl --lib f16: 27 passed, 0 failed withXCL_DIRECT_DXIL=1and with=0. The census test shows every inexact result of add, sqrt, rcp, rsq, exp, log, ldexp and mul at nearest even, none toward zero.Lw::to_f16and thev_fract_f16step now match the HLSL side.v_fma_f16and its relatives round twice (#1841 is about that).performance: it adds shader work by default. I timed the HLSL helper alone on this RTX 3060: one group of 1024 threads, 1 million conversions each, whole dispatch by wall clock, best of 12.
f32tof16(main)f32_to_f16_rm(f, 0)(this PR)This says what one conversion costs, not what a frame costs. The default direct translator emits its own select-only version, which I did not time; it also computes the normal and the subnormal case for every value. The smoke pair in the description is at the 60 fps cap in the menus, so it cannot show the cost. The longer frame-time comparison throny asked for is still missing.
If the cost matters in a title, this form gives the same bits as
f32_to_f16_rm(f, 0u)for all 4,294,967,296 binary32 inputs on this card (checked on the GPU;f32tof16as a control differs for 2,248,098,814):It is checked on NVIDIA only. It could also be a follow-up.
verdict: not yet: the rounding is correct, but the frame-time cost in a title's world is not measured (main against this head, below the 60 fps cap). With that comparison showing no real loss, I would call it safe to merge.
hardware / tool and model: RTX 3060 12 GB (610.88), Ryzen 7 5700X, Windows 11 Pro 26300 / Claude Code (Claude Opus 5.5)
@joeypercia, the missing piece is that frame-time comparison, from anyone with a title that reaches the world.
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.