Repository navigation
Recompiler: f16 results round to nearest even in the default mode (#111) - #842
joeypercia wants to merge 1 commit into
Conversation
…l-dev#111) In the default mode every f16 result that is narrowed from binary32 went through HLSL f32tof16, which rounds toward zero: on an RTX 4070 Ti and an RDNA2 iGPU every inexact v_add/sub/sqrt/rcp/rsq/exp/log/ldexp_f16 result came out one binary16 step low, and an overflow stopped at 65504 instead of inf. Native RDNA2 rounds to nearest even (luckyteagirl's measurements on xcl-dev#672). to_f16_expr now narrows with f32_to_f16_rm(e, 0u) in the default mode, the helper the multiplies already use (xcl-dev#166); under a dynamic mode nothing changes. v_fract_f16 clamps its binary32 result below 1.0, and 1 - 2^-24 narrows to 1.0 by nearest even, so its binary16 result is kept at 0x3BFF (xcl-dev#655). Supersedes xcl-dev#696, whose add / sub cases this covers. Assisted-by: Claude Code (Claude Opus 5.5)
|
Regression runs for this PR's head main: this PR: Tested
Not 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. |
|
Full-suite run to go with the smoke runs above, so the new GPU tests have a result on hardware. Tested exact head Tested
Not tested
AI use
|
|
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.
The #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. |
|
Linux / AMD RDNA3 datapoint. Merged state: head Full suite, Not tested: retail titles (none here), Windows, RDNA4. 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 AI use: Hermes agent (local Qwen-class) ran the suite and drafted this; I checked it before posting. |
|
Hardware check of Tested
Regression run PR ( (Note: an initial cold-shader-cache smoke run exited early with Not tested
AI use
|
|
Native RDNA2 ( This 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 (
Outside this PR's scope, for #111 and #73:
Tested
Not tested
AI use
|
|
Tested Clean detached PR head
$exe = 'L:/XclTooling/pr1801-e00963f-t3-3c561aa2/target/debug/build/xcl/77aca8ccb20e299f/out/xcl-77aca8ccb20e299f.exe'
& $exe gpu::recomp::tests::gpu_f16_results_round_to_nearest_even_by_default --exact --nocapture --test-threads=1
& $exe gpu::recomp::tests::gpu_f16_unary_results_round_to_nearest_even --exact --nocapture --test-threads=1
& $exe gpu::recomp::tests::gpu_fract_f16_stays_below_one_with_nearest_even --exact --nocapture --test-threads=13 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 ( |
|
Verification on RTX 3060 Ti (Windows 11), branch merged with current
Thanks! |
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.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 Recompiler: v_ldexp_f16 keeps ±0 and ±inf for any exponent (#658) #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-faston feaeb7b: 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 (Tests: placed_title_textures_share_vram_and_preserve_pixels fails now and then at its cubemap reuse check (main 9ce2070) #718 / D3D12.X: back kept-in-step title textures by shared VRAM pages (#248) #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 Tests: float rounding ops and fract's clamp below 1.0 on the GPU #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 (f16 add / FMA under a directed rounding mode are rounded twice (through f32) #111's FMA point; Recompiler: f16 add / sub / fma rounded once under a directed mode (#… #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