Skip to content

Recompiler: f16 results round to nearest even in the default mode (#111) - #842

Open
joeypercia wants to merge 1 commit into
xcl-dev:mainfrom
joeypercia:recompiler-f16-default-rne
Open

joeypercia wants to merge 1 commit into
xcl-dev:mainfrom
joeypercia:recompiler-f16-default-rne

Conversation

@joeypercia

@joeypercia joeypercia commented Nov 24, 2026 •

Copy link
Copy Markdown

In the default mode every f16 result narrowed from binary32 went through HLSL f32tof16, which rounds toward zero, so 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 raw-ISA measurements on #672: ldexp(1, 20) = inf, subnormal ties to even). to_f16_expr now narrows with f32_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_f16 needs one extra step: fract_f 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 (#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_expr narrows v_interp_p2_f16, v_fma_mixlo_f16 and v_fma_mixhi_f16, so in a program that does not change the rounding mode they round toward zero on main too, 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's shaders.rpf (xcl shaders --json, 26 503 shaders) there are 59 367 v_interp_p2_f16 (4 776 shaders), 33 900 v_fma_mixlo_f16 (6 155) and 2 135 v_fma_mixhi_f16 (842), against a few hundred v_add/sub/mul/fma_f16 and no v_ldexp_f16 or v_fract_f16. (v_cvt_f16_f32, 38 871, already rounds to nearest even on main.) gpu_s_round_mode_controls_f16_rounding_across_modes checks v_fma_mixlo_f16 only 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_f16 of x·1 + 0 with x = 1 + 2^-11 + 2^-20, no mode change in the program, give 0x3C00 (toward zero) on main 037aecd and 0x3C01 (nearest even) with this PR merged on it, on both GPUs below. v_interp_p2_f16 is from reading the emitter, not measured.

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: to_f16_expr rounds to nearest even in the default mode; v_fract_f16 keeps 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 ones
    • gpu_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^-14 and -2^-24 give 0x3BFF, NaN stays NaN

Tested

Regression run

From @Anon64 (RTX 3060, release builds, 60 s each), pasted unedited from their comment:

main:

xcl-smoke v1
commit: beaa245ca04e212f064efc7cafcdce104bc5c0a2
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1636 (PresentX 1637)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18113 MB

PR:

xcl-smoke v1
commit: feaeb7b0b437abfddcb6612f9d4bd1c853ebfb6a
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17753 MB

Not tested

AI use

  • Claude Code with Claude Opus 5.5: wrote the fix, the tests and this description, and ran the tests above on my machine.

…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)
@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

Regression runs for this PR's head feaeb7b and for its base main beaa245, on a retail title (same title, 60 s each), made with scripts/smoke.ps1 (release, clean tree at each commit) and pasted unedited, for Regression run.

main:

xcl-smoke v1
commit: beaa245ca04e212f064efc7cafcdce104bc5c0a2
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1636 (PresentX 1637)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 18113 MB

this PR:

xcl-smoke v1
commit: feaeb7b0b437abfddcb6612f9d4bd1c853ebfb6a
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1633 (PresentX 1633)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 17753 MB

Tested

  • NVIDIA GeForce RTX 3060 12 GB (Ampere, driver 581.80, wave32), Intel Core i9-14900KF, Windows 11 25H2 26200.9974, Rust 1.90.0
  • cargo test -p xcl --lib f16 (debug) on feaeb7b: 15 passed, 0 failed, including gpu_f16_results_round_to_nearest_even_by_default, gpu_f16_unary_results_round_to_nearest_even and gpu_fract_f16_stays_below_one_with_nearest_even

Not tested

  • the whole suite on this machine
  • whether the title reaches these f16 results; this is a regression check only

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

Copy link
Copy Markdown

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 base beaa245).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 616.64, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.100.0 MSVC, debug, D3D12 debug layer installed.
  • cargo build --workspace, then XCL_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.
  • The new tests ran and passed: 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

  • AMD, a release build, a game (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.

@zhouzhidong3-hash

zhouzhidong3-hash commented Nov 24, 2026 •

Copy link
Copy Markdown

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.

Emitter Native matches Differences
#672 at 6ec4036 8/11 Both finite overflows and the 0x0003 / 2 halfway case
#842 at feaeb7b 8/11 ldexp(+0,32767), ldexp(+inf,-32768), ldexp(-inf,-32768) gave 0x7fff
#842 plus only the v_ldexp_f16 emitter arm from #672 11/11 None in this fixture

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.

@SlopTart

Copy link
Copy Markdown

Linux / AMD RDNA3 datapoint.

Merged state: head feaeb7b onto main beaa245, 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_even ok. 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

Copy link
Copy Markdown

Hardware check of feaeb7b0b437abfddcb6612f9d4bd1c853ebfb6a (verified clean merge with main 1c87b1d): pass.

Tested

  • Hardware: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32015.16001), AMD Ryzen 7 9800X3D, 32 GB RAM, Windows 11, rustc 1.100.0 (0ef1bc386 2026-11-09), release + test profile
  • cargo test --workspace -- --test-threads=4: 716 passed, 0 failed, 41 ignored (GPU tests skipped on this adapter: none)
  • Verified f16 nearest-even rounding & wave64 GPU tests (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): all ok; --nocapture census 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 both wave64 and wave32

Regression run
main (beaa245):

xcl-smoke v1
commit: beaa245ca04e212f064efc7cafcdce104bc5c0a2
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1660 (PresentX 1660)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14862 MB

PR (feaeb7b):

xcl-smoke v1
commit: feaeb7b0b437abfddcb6612f9d4bd1c853ebfb6a
gpu: AMD Radeon RX 9070 XT
title: GTA6.Xbox.v1.0.2.1
seconds: 60
exit: 0x0
frames shown: 1665 (PresentX 1665)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14858 MB

(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 MB during first-run DXC compilation; the immediate rerun above ran the full 60 s cleanly.)

Not tested

  • wave32-only GPUs (NVIDIA / Intel); gameplay past the menus

AI use

  • Antigravity (Gemini 4 Carbon) ran the build, test suite, and scripts/smoke.ps1 harness on this machine and formatted this report.

@KENAJBBY

Copy link
Copy Markdown

Native RDNA2 (gfx1036): f16 add / sub round once, in every rounding mode

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 (s_setreg_b32, read back with s_getreg_b32 before 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_f16 and v_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.
  • In the default mode (f16 RNE, f16 denormals on), this PR's binary32-sum-then-nearest-even model differs from the hardware on 0 of the 60,000 pairs, for all four instructions.
  • Recompiler: round f16 add / sub to nearest-even by default (#685) #696's five vectors, raw on the hardware at MODE 0x1C0, give exactly Recompiler: round f16 add / sub to nearest-even by default (#685) #696's expected bits:
a, b add sub subrev pk_add lo / hi
0x3C00, 0x1200 0x3C01 0x3BFE 0xBBFE 0x3C01 / 0x3C01
0x3C00, 0x0800 0x3C00 0x3C00 0xBC00 0x3C00 / 0x3C00
0xBC00, 0x9200 0xBC01 0xBBFE 0x3BFE 0xBC01 / 0xBC01
0x3C00, 0x1000 0x3C00 0x3BFF 0xBBFF 0x3C00 / 0x3C00
0x7BFF, 0x5000 0x7C00 0x7BFE 0xFBFE 0x7C00 / 0x7C00

Outside this PR's scope, for #111 and #73:

Tested

  • AMD Radeon iGPU of a Ryzen 7 9800X3D (gfx1036, RDNA2), driver 32.0.21030.2001, AMD OpenCL 3652.0 (PAL,LC), native wave64; Windows 11 Pro 26200.
  • Inline asm in OpenCL C, one block per lane. It saves MODE, sets it with s_setreg_b32 hwreg(HW_REG_MODE, 0, 10), reads it back with s_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

  • This PR's HLSL on my GPUs: I compared against a model of it (binary32 sum, then nearest even).
  • The PR's unary f16 results (sqrt, rcp, rsq, exp, log, ldexp), v_fma_f16, NaN / inf inputs; console hardware and its entry MODE.

AI use

  • Claude Code (Claude Opus 5.5) wrote the probe and the reference, ran them on this machine and drafted this comment.

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.

7 participants