Recompiler: f16 results round to nearest even in the default mode (#111) #842
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!842
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-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?
@joeypercia on GitHub · 2026-11-24 06:39 UTC · original
joeypercia:recompiler-f16-default-rne→main· +175 −6 · 2 files · 1 commits · openIn 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.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/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
cargo test --workspace --no-fail-fastonfeaeb7b: RTX 4070 Ti 716 passed, 0 failed, 41 ignored; RDNA2 713 passed, 3 failed, 41 ignored; the 3 areplaced_title_textures_share_vram_and_preserve_pixelsand its two lock-poison followers (#718 / #288), under load, and pass alone on this head (2 runs of 2)cargo test -p xcl --lib -- f16 fract: 16 passed on both, the existings_round_modeand f16 rounding tests includedmain'semit.rs, the census and unary tests fail on both GPUs (the unary test stops at its first case,sqrt(0x3420)); with nearest even but without the fract clamp, the fract test fails on both (fract(0x8400)=0x3C00, want0x3BFF), which is #655's regressionSKIPPED: RTX 4070 Tigpu_geometry_wave32_prefix_count_over_a_mask,gpu_wave32_program_in_wave64_hardware(no wave64); RDNA2gpu_geometry_mode_es_gs_strip,gpu_mesh_mode_two_passes(no mesh shaders)Regression run
From @Anon64 (RTX 3060, release builds, 60 s each), pasted unedited from their comment:
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 hardwareAI use
@Anon64 on GitHub · 2026-11-24 06:51 UTC · original
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.
@futurepro62 on GitHub · 2026-11-24 07:04 UTC · original
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
@zhouzhidong3-hash on GitHub · 2026-11-24 07:04 UTC · original · edited 2026-11-24 07:08 UTC
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.
feaeb7bThe #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.@SlopTart on GitHub · 2026-11-24 07:17 UTC · original
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.
@eizispark on GitHub · 2026-11-24 08:08 UTC · original
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.@KENAJBBY on GitHub · 2026-11-24 09:31 UTC · original
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
@JJAutoCompare on GitHub · 2026-11-25 06:57 UTC · original
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.@DavidM1911 on GitHub · 2026-11-25 10:27 UTC · original
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!
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.