Skip to content

Recompiler: v_cvt_f16_u16 / v_cvt_f16_i16 round to nearest even - #515

Closed
ic3bear-ai wants to merge 1 commit into
xcl-dev:mainfrom
ic3bear-ai:recomp/cvt-f16-from-16-bit-rne
Closed

ic3bear-ai wants to merge 1 commit into
xcl-dev:mainfrom
ic3bear-ai:recomp/cvt-f16-from-16-bit-rne

Conversation

@ic3bear-ai

Copy link
Copy Markdown

v_cvt_f16_u16 and v_cvt_f16_i16 rounded toward zero in the default mode, where RDNA rounds to nearest even. The integer is exact in binary32, so this rounds it once with f32_to_f16_rm, as #166 did for v_mul_f16. Found while measuring #111 (that issue is about add / sub / fma; these two conversions are not in its list, nor in #128).

What is in it

  • crates/xcl/src/gpu/recomp/emit.rs: the two conversions use to_f16_rounded_expr, like v_mul_f16.
  • crates/xcl/src/gpu/recomp/tests.rs:
    • f16_exact / f16_round_exact: an exact reference, every f16 as an integer in units of 2^-24, rounded to nearest even in integer arithmetic;
    • f16_exact_reference_round_trips: checks the reference against all 65 536 f16 values, plus tie, overflow and subnormal cases;
    • gpu_cvt_f16_from_16_bit_integers_exhaustive: both instructions on every 16-bit input, against the reference.

Tested

  • NVIDIA GeForce RTX 3070 8 GB (driver 616.92), AMD Ryzen 5 5600X, Windows 11 build 26200, Rust 1.95.0, debug build, DXC 1.8.2502.11.
  • gpu_cvt_f16_from_16_bit_integers_exhaustive before the fix:
    • v_cvt_f16_u16: 29 184 of 65 536 wrong;
    • v_cvt_f16_i16: 26 624 of 65 536 wrong;
    • every one of them a step toward zero (e.g. 2051 → 2050, want 2052). A separate count of the inputs where truncation and rounding to nearest even differ gives the same numbers.
  • After the fix: 0 wrong.
  • cargo test --workspace --no-fail-fast -- --test-threads=4: 678 passed, 0 failed, 41 ignored on this branch (based on main 2997c9a). main at a6f5f7a gave 676 passed; the only commit between the two changes a link in the README, and the 2 new tests are the difference.
  • SKIPPED: gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware (no wave64).

Regression run

I don't own a console title, so these two runs are on the repo's own test title (tests/testtitle, the folder the e2e tests use), built from main. Each run used its own tree's scripts/smoke.ps1 and release build, and a fresh XCL_DATA_ROOT.

main:

xcl-smoke v1
commit: 2997c9aca6ebf4577fed34ebdb3cb0a77dd9ec21
gpu: NVIDIA GeForce RTX 3070
title: xcl-testtitle
seconds: 60
exit: 0x1
frames shown: none logged (the run did not reach its end)
presents per 2 s (last 3): 1
device removals: 0
crashes: 1
peak commit: 2208 MB

This PR's head:

xcl-smoke v1
commit: 021f71bcafdea44ed1cad2c08ac285cba29b42b9
gpu: NVIDIA GeForce RTX 3070
title: xcl-testtitle
seconds: 60
exit: 0x1
frames shown: none logged (the run did not reach its end)
presents per 2 s (last 3): 1
device removals: 0
crashes: 1
peak commit: 2137 MB

crashes: 1 and exit: 0x1 are not crashes. The title runs its checks, fails only the one that expects a --hello argument (the e2e tests pass it; smoke.ps1 can't), prints done, failures: 1 and exits with status 1 after about 3 seconds. The script counts that exit line as a crash. The same happens on main, so PR checks will refuse this block until there's a run on a real title.

If you have a title: could you run scripts/smoke.ps1 -Game <folder> on main and on 021f71b and post the two blocks here? I'll keep this a draft until then.

Not tested

  • A real title (see Regression run), and the retail pack: I don't know how many shaders use these two instructions.
  • GPUs with 64-lane waves (AMD), and whether AMD's f32tof16 also truncates.
  • The directed round modes (the conversions use f32_to_f16_rm with xcl_fp16_rm there, as before).
  • The console itself: the reference is IEEE round to nearest even as documented for RDNA2.

AI use

  • Claude Code, Claude Opus 5.5: found the bug, wrote the fix, the tests and this description. I checked the results and the diff.

🤖 Generated with Claude Code

In the default mode (no dynamic f16 round mode) the 16-bit integer to
f16 conversions ended in the legacy f32tof16, which rounds toward zero:
2051 became 2050 instead of 2052. The integer is exact in binary32, so
rounding it once with f32_to_f16_rm gives the exactly rounded f16, as
xcl-dev#166 did for v_mul_f16.

Measured on an RTX 3070 (driver 616.92) over all 65 536 inputs of each
instruction: 29 184 (u16) and 26 624 (i16) results were one step toward
zero, exactly the inputs where truncation and rounding to nearest even
differ (counted separately); 0 after.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thehollowedone

thehollowedone commented Nov 24, 2026 •

Copy link
Copy Markdown

Tested

I checked the three directed rounding modes listed under “Not tested” using a local synthetic wave32 kernel at 021f71b. It set the two FP16 rounding bits with S_SETREG_IMM32_B32, executed both conversions, and compared the GPU readback with fixed binary16 bit patterns. All 24 comparisons matched (four input bit patterns × two instructions × three modes).

For example, signed −2051 rounded to −2050 toward positive infinity, −2052 toward negative infinity, and −2050 toward zero. Unsigned 65535 rounded to positive infinity, 65504, and 65504 respectively. The other inputs were 2051 and 32767.

RTX 3080 Ti (621.35), Ryzen 7 9800X3D, Windows 11 build 26300, Rust 1.99.1 MSVC, DXC 1.8.2502.11, debug. With the scratch test added, cargo test --offline --locked -p xcl --lib gpu_check_directed_integer_half_rounding -- --nocapture reported 1 passed, 0 failed, 0 ignored.

Regression run

One 60-second retail run per clean release build, same machine and title, with fresh XCL data and DXIL caches for each. The title label is anonymized. The baseline was main when the comparison started. It includes #486, which is absent from this PR head. The PR showed 463/597 frames (77.6% of baseline), below the 80% threshold. This pair does not isolate the rounding change.

Baseline 7ebf1b8

xcl-smoke v1
commit: 7ebf1b8044bef8f0ef296eb36e85010cceb4633a
gpu: NVIDIA GeForce RTX 3080 Ti
title: retail-title
seconds: 60
exit: 0x0
frames shown: 597 (PresentX 597)
presents per 2 s (last 3): 15 14 17
device removals: 0
crashes: 0
peak commit: 15817 MB

PR head 021f71b

xcl-smoke v1
commit: 021f71bcafdea44ed1cad2c08ac285cba29b42b9
gpu: NVIDIA GeForce RTX 3080 Ti
title: retail-title
seconds: 60
exit: 0x0
frames shown: 463 (PresentX 464)
presents per 2 s (last 3): 15 12 13
device removals: 0
crashes: 0
peak commit: 15783 MB

Not tested

Exhaustive directed-mode inputs, native wave64, and the full workspace suite. Whether this retail run exercised either conversion is unknown.

AI use

Codex with GPT-6.2 Astra wrote and ran the scratch test and retail smoke comparisons and drafted this report.

@Alpha325

Copy link
Copy Markdown

Independent AMD verification for 021f71bcafdea44ed1cad2c08ac285cba29b42b9.

Tested on Radeon RX 9060 XT (driver 32.0.32029.2010), Windows 11 build 26200.9974, Rust 1.100.0 MSVC x64, DXC 1.9.2602.17, release. Synthetic inputs only.

cargo test --locked --release -p xcl --lib gpu_cvt_f16_from_16_bit_integers_exhaustive -- --nocapture --test-threads=1

  • Base 7ebf1b8044bef8f0ef296eb36e85010cceb4633a, with only the PR's test fixture applied: wave64 checked 65,536 inputs per instruction. Unsigned had 29,184 mismatches (2051 → 2050, expected 2052). Signed had zero. The wave64 assertion stopped the test before wave32.
  • Clean PR head: zero mismatches for both instructions in both wave modes (262,144 comparisons). The GPU test passed with no skips. f16_exact_reference_round_trips also passed.

The unsigned issue reproduces on this AMD setup. The signed mismatch reported above for NVIDIA did not reproduce in the AMD baseline's wave64 run.

AI use: OpenAI Codex (GPT-6.2 Sol) ran the tests and drafted this report.

@ghost

ghost commented Nov 24, 2026

Copy link
Copy Markdown

A negative control for the RDNA4 run above, on the same card model: 021f71b merged onto main 508a0c7. gpu_cvt_f16_from_16_bit_integers_exhaustive passes (XCL_REQUIRE_GPU=wave64,mesh, full suite 682 / 0 / 41). With only emit.rs put back to main it fails with 29184 results differ from the exact reference, so the test catches the truncating conversion on AMD.

AI use

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

@tugasky

tugasky commented Nov 24, 2026

Copy link
Copy Markdown

AMD RDNA4 (RX 9070 XT) — the two "Not tested" bullets, measured here

Machine: AMD Radeon RX 9070 XT (RDNA4, driver 32.0.32029.2010, 2026-11-18), Ryzen 7 9700X, 31.1 GB, Windows 11 Pro 26300.9550, rustc 1.98.0. I merged main (7ebf1b8) into the PR head 021f71b, no conflicts.

1. gpu_cvt_f16_from_16_bit_integers_exhaustive on AMD, after the fix

wave64: 65536 inputs each; wrong: v_cvt_f16_u16 0, v_cvt_f16_i16 0
wave32: 65536 inputs each; wrong: v_cvt_f16_u16 0, v_cvt_f16_i16 0

This card reports both wave sizes, so the 64-lane bullet is covered: RDNA4 runs the test in wave64 as well and it passes.

2. Before the fix, on this driver. The PR's tests.rs alone on main's emit.rs (detached worktree at 7ebf1b8; the only edit was to sum both wave sizes instead of panicking on the first, so both lines are from one run):

wave v_cvt_f16_u16 wrong v_cvt_f16_i16 wrong
64 29 184 / 65 536 0
32 29 184 / 65 536 0

Same shape as your run, e.g. v_cvt_f16_u16(2051) = 0x6801 (2050), want 0x6802 (2052), every failure a step toward zero. So AMD's HLSL compiler truncates the unsigned conversion too, with the same count as your 3070 (29 184), while the signed one is already correct here (hypothesis: it lowers to the hardware instruction, which rounds). The u16 half of the fix is a real fix on AMD; the i16 half is a no-op on this driver, so here it can only be checked as "changes nothing", which it does.

3. The retail pack contains neither instruction. Counted over the 26 503 disassemblies of the carved corpus (programs / uses):

opcode programs uses
v_cvt_f16_u16 0 0
v_cvt_f16_i16 0 0
v_cvt_u16_f16 0 0
v_cvt_i16_f16 0 0
v_cvt_f32_f16 9 347 95 215
v_cvt_f16_f32 6 306 44 496
v_mul_f16 124 669
v_add_f16 41 847
v_sub_f16 28 160
v_rcp_f32 (control) 17 393 424 863

The whole integer-to-f16 group is absent, while f32-to-f16 conversions are common and f16 arithmetic is rare. The v_rcp_f32 row is a control: an independent harness I ran for another PR reported the same 17 393 programs and 424 863 uses, so the two counters agree. Conclusion: no retail shader is affected either way, so the change is correctness-only and cannot regress the pack — and the pack cannot confirm it either.

4. Suite on the merged tree: 679 passed, 0 failed, 41 ignored across 16 binaries; main at 7ebf1b8 gives 677 / 0 / 41. The +2 is exactly your two tests. The e2e binaries ran on this machine too (e2e.rs 3, gpu_compute_e2e 2, gpu_pso_e2e 7, console_dlls_e2e 13, predication_e2e 1, rpf_e2e 2, disasm_e2e 1, all passing).

5. Still open here

  • A real title: there is no retail install on this machine at the moment, so scripts/smoke.ps1 has nothing to run. Tell me when you want it and I will do the main/021f71b pair if the title is back.
  • Directed round modes: code-level only. Both names now go through to_f16_rounded_expr, which is f32_to_f16_rm(e, xcl_fp16_rm) under a non-default mode — the same helper v_mul_f16 uses (Recompiler: round f16 conversions and products to nearest-even by default #166). I did not run a hardware case with a non-default s_round_mode.

AI use: written with Cline; every run above is from my machine, and the counts are tool output.

@KENAJBBY

Copy link
Copy Markdown

Native RDNA2 (gfx1036): both instructions round per MODE[3:2], on all 65,536 inputs

This answers "the console itself: the reference is IEEE round to nearest even as documented" from Not tested. I ran v_cvt_f16_u16 and v_cvt_f16_i16 directly on an RDNA2 GPU, on every 16-bit input, under an explicitly set MODE (s_setreg_b32, read back with s_getreg_b32 before the instruction). Results were compared with an exact integer reference rounded once in that mode:

MODE[3:2] (f16 round) modes run v_cvt_f16_u16 wrong v_cvt_f16_i16 wrong
0, nearest even 0x1C0, 0x3C0, 0x100, 0x0F3 0 / 65,536 0 / 65,536
1, toward +inf 0x1C4, 0x3C4 0 0
2, toward −inf 0x1C8, 0x3C8 0 0
3, toward zero 0x1CC, 0x3CC, 0x10C, 0x3FF 0 0

So the hardware rounds to nearest even by default and never truncates there. It follows s_round_mode in the directed modes, which is what this PR's f32_to_f16_rm(…, xcl_fp16_rm) does. IEEE, DX10_CLAMP, the f32 round mode and the denorm modes change nothing. Samples: 2051 → 0x6802 at nearest even and 0x6801 toward zero; 65520 → 0x7C00 at nearest even and 0x7BFF toward zero; -2051 → 0xE801 toward +inf and 0xE802 toward −inf, the same values as @thehollowedone's run.

On the same RDNA2 iGPU through D3D12, the PR's gpu_cvt_f16_from_16_bit_integers_exhaustive at head 021f71b gives 0 / 0 wrong in wave64 and wave32. With only emit.rs from the base 2997c9a it gives 29,184 / 0, the same split as the RDNA4 runs above.

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.
  • Native: 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.
  • XCL: cargo test -p xcl --lib gpu_cvt_f16_from_16_bit_integers_exhaustive -- --nocapture --test-threads=1, XCL_ADAPTER=radeon, debug, Rust 1.100.0 MSVC, DXC 1.9.2602.17.

Not tested

  • SDWA / op_sel forms on the hardware; the whole suite; a retail title (the pack has neither instruction, per @tugasky).

AI use

  • Claude Code (Claude Opus 5.5) wrote the probe, ran it and the PR's test on this machine and drafted this comment.

@ivall

ivall commented Nov 24, 2026

Copy link
Copy Markdown
Collaborator

Closing this as covered by #842 (group D24 in #1607): it makes to_f16_expr itself round to nearest even, and v_cvt_f16_u16 / v_cvt_f16_i16 narrow through it. The exhaustive conversion test here is not in #842 and is welcome as a test-only PR once #842 is merged.

This says nothing about the quality of the code here. If this PR covers a case the other one does not, or #842 is closed without being merged, say so and I'll reopen it.

Thank you for the work.

AI use: Claude Code (Claude Opus 5.5) read the diffs and the checks in #1607 and wrote this comment, on the maintainer's behalf.

@ivall ivall closed this Nov 24, 2026
CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
The HLSL path converted the f32 result of v_add / sub / subrev / fma /
fmac / fmamk / fmaak / ldexp_f16 and v_pk_add / pk_fma / pk_fmac_f16 with
f32tof16, which rounds toward zero on NVIDIA (RDNA2's default is nearest
even). Only v_mul_f16 / v_pk_mul_f16 used f32_to_f16_rm. Measured on an
RTX 3080 Ti: v_add_f16(0x3C01, 2^-11) gave 0x3C01 (want 0x3C02),
v_pk_add_f16 hi lane 0xbfff (want 0xc000), v_ldexp_f16 into the
denormals 0x207 (want 0x208).

add / sub / ldexp are exact enough in binary32 (2p+2 bits), so they go
through the same rounded conversion as v_mul_f16. fma needs one rounding:
fma_rto computes the exact product, the TwoSum error of the sum, and
rounds the f32 result to odd, so the later binary16 rounding sees the
exact value's side of every tie (fma(0x3C01, 0x0FFE, 0x3C01) is 0x3C01;
an RNE f32 sum gives 0x3C02).

v_cvt_f16_u16 / i16 (xcl-dev#515), the directed modes (xcl-dev#128) and
v_fma_mix*_f16 are not touched.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Hri58aiQKmMwqS6K8CHq2s
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.

6 participants