Skip to content

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

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

ic3bear-ai wants to merge 1 commit into
ivall: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
ivall#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.

@nullmarsh

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.

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.

4 participants