Skip to content

Recompiler: round normalized f32 conversions once (#659) - #683

Draft
ilyaux wants to merge 1 commit into
xcl-dev:mainfrom
ilyaux:review/issue-659-exact-normalized-f32
Draft

ilyaux wants to merge 1 commit into
xcl-dev:mainfrom
ilyaux:review/issue-659-exact-normalized-f32

Conversation

@ilyaux

@ilyaux ilyaux commented Nov 24, 2026

Copy link
Copy Markdown

Fixes #659. Avoid rounding an FP32 product to a halfway value before converting it to UNORM/SNORM.

The runtime fix and first two regression tests are @acaracargo's existing proposal, carried from f4c03b5 and 0bca2d7. This PR adds independent packed-format coverage and native Windows validation. acaracargo is credited as a co-author in the commit.

What is in it

  • emit.rs: round the significand/scale product once with integer arithmetic, preserving clamps and NaN handling.
  • tests.rs: the original two regressions plus eight packed UNORM/SNORM formats, with 64 boundary/edge vectors and different values per channel.
  • README behavior note.

Tested
Head: 02fd1b03c357e5cb4457ca56fd343202de1d3736 (clean tree), based on main 91f9a5e9f03156ea82dc55a18f7704862da7a5db. Windows 11 Home 26300, Ryzen AI 7 350, Rust 1.98.1 MSVC, debug, DXC/DXIL 1.8.2502.11.

  • RTX 5070 Laptop, driver 32.0.16.2124, XCL_ADAPTER=nvidia:
    cargo build --workspace --locked;
    cargo test --workspace --no-fail-fast --locked -- --test-threads=4:
    695 passed, 0 failed, 41 ignored. Two SKIPPED: no wave64 tests are included in that passed count: gpu_geometry_wave32_prefix_count_over_a_mask and gpu_wave32_program_in_wave64_hardware. All three normalization regressions and the synthetic-title end-to-end test ran and passed.
  • Radeon 860M, driver 32.0.32029.2010, XCL_ADAPTER=radeon, XCL_REQUIRE_GPU=wave64:
    cargo test -p xcl --lib gpu_pknorm_f32_rounds_once -- --nocapture;
    cargo test -p xcl --lib norm_formats_round_once -- --nocapture;
    cargo test -p xcl --lib gpu_wave32_program_in_wave64_hardware -- --nocapture:
    3 normalization tests + 1 wave64 control passed, 0 failed/ignored/skipped.
  • Negative controls on the initial main dd2ae9a, keeping tests but removing the runtime fix: 41 pknorm and 54 typed-store mismatches. The added packed-format test also fails (11_11_10_UNORM, got 6291458, expected 6291457).
  • git diff --check passed.

Regression run
Pending: matched scripts/smoke.ps1 runs on main and this PR's clean head. No complete retail title was available locally, so no smoke blocks were generated. Keeping this PR draft until that validation is available.

Not tested
Retail gameplay/performance, real console rounding, Intel GPUs, and the full suite on AMD. XCL_SAMPLE_* was unset, so opt-in real-game-data paths were not exercised.

AI use

  • Original proposal: acaracargo disclosed Claude Code with Claude Opus 5.5 for the fix and its first two tests.
  • Codex, GPT-6: reviewed/carried that proposal onto current main, added the packed-format test and README note, ran the native Windows checks above, and drafted this description.

Carry acaracargo's normalization fix and its two regression tests from
f4c03b5 and
0bca2d7.

Round the significand/scale product once with integer arithmetic, rather
than rounding an intermediate f32 product to a halfway value. Add an
independent packed-color GPU test and document the behavior.

Fixes xcl-dev#659
Co-authored-by: acaracargo <58486686+acaracargo@users.noreply.github.com>
Assisted-by: Codex (GPT-6)
@acaracargo

Copy link
Copy Markdown

Thanks for extending my fix and for the attribution! I was actually preparing to open the original branch as a draft PR.

I've also completed local retail regression runs, and @requiemsnkrp offered independent smoke testing on #659.

Let's coordinate so we don't end up with duplicate PRs. I'm happy to help validate this version and get the fix merged.

@ItsMrDubey

Copy link
Copy Markdown

Test report for 02fd1b03c357e5cb4457ca56fd343202de1d3736 (this PR's head)

Tested

  • NVIDIA GeForce RTX 4070 Ti 12 GB (Ada), driver 617.14; Intel Core i9-10900K, 32 GB RAM; Windows 11 Pro build 26300
  • Rust 1.88.0 (stable-x86_64-pc-windows-msvc), debug; DXC 1.9.2602.17 from Windows SDK 10.0.26100
cargo build --workspace
cargo test --workspace --no-fail-fast -- --test-threads=4

16 test binaries: 694 passed, 1 failed, 41 ignored. Same machine, main 64b3f3559: 700 passed, 1 failed, 41 ignored.

This PR's tests:

  • gpu::recomp::tests::gpu_buffer_store_norm_formats_round_once: ok
  • gpu::recomp::tests::gpu_pknorm_f32_rounds_once: ok
  • gpu::recomp::tests::gpu_packed_norm_formats_round_once: ok

Failures:

16 test binaries instead of main's 17 because this branch predates tests/sparse_reinit.rs; nothing crashed.

SKIPPED (no wave64 on this GPU): gpu_geometry_wave32_prefix_count_over_a_mask, gpu_wave32_program_in_wave64_hardware.

AI use: Claude Code (Claude Opus 5.5) ran the builds and tests and wrote this report from their output.

@Anon64

Anon64 commented Nov 24, 2026

Copy link
Copy Markdown

Regression runs for this PR's head 02fd1b0 and for its base main 91f9a5e, 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: 91f9a5e9f03156ea82dc55a18f7704862da7a5db
gpu: NVIDIA GeForce RTX 3060
title: retail-title
seconds: 60
exit: 0x0
frames shown: 1648 (PresentX 1648)
presents per 2 s (last 3): 60 60 60
device removals: 0
crashes: 0
peak commit: 14684 MB

this PR:

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

Tested

  • NVIDIA GeForce RTX 3060 12 GB (driver 581.80, wave32), Intel Core i9-14900KF, Windows 11 25H2 26200.9974, Rust 1.90.0

Not tested

  • the unit tests on this machine
  • whether the title reaches the changed conversions; this is a regression check only

Report written with Claude Code (Claude Opus 5.5) from runs on this machine.

@KENAJBBY

Copy link
Copy Markdown

Native RDNA2 (gfx1036): every double-rounding input, and no MODE dependence

This extends @luckyteagirl's 13-input native check on #659 to the whole problem set. I ran v_cvt_pknorm_u16_f32 and v_cvt_pknorm_i16_f32 on raw f32 bits on an RDNA2 GPU. The inputs were every f32 in (0, 1) whose binary32 product with the scale is exactly k + 0.5: 65,536 inputs for 65535 and 32,768 for 32767, each also negated for SNORM16. On top of that came 20,000 random bit patterns and 26 special values. Both halves were checked, with src0 and src1 holding different inputs.

inputs differ from this PR's helpers differ from main's round(f * scale) (CPU model)
v_cvt_pknorm_u16_f32 85,562 0 (both halves) 32,769
v_cvt_pknorm_i16_f32 85,562 0 (both halves) 32,770

The result was 0 in each of 11 MODE values: f32 round RNE / +inf / −inf / RTZ, f32 and f16 denormals flushed or kept, DX10_CLAMP 0/1 and IEEE 0/1. Each mode was set with s_setreg_b32 and read back with s_getreg_b32. The round mode does not apply here: these conversions always round to nearest even.

The special inputs give the same result in all 1024 values of MODE[9:0], and every one matches this PR's helpers:

f32 input u16 i16
±0, denormals (0x00000001, 0x80000001, 0x007FFFFF), 0x00800000, 2^-17 0x0000 0x0000
1.0, 2.0, +inf, 0x3F7FFFFF 0xFFFF 0x7FFF
−1.0, −2.0, −inf, 0xBF7FFFFF 0x0000 0x8001
qNaN, −qNaN, sNaN 0x7F800011 0x0000 0x0000
0.5 / −0.5 0x8000 / 0x0000 0x4000 / 0xC000
0x37C000C0 (#659's example) 0x0001 0x0001
0x38400180 / 0xB8400180 0x0003 / 0x0000 0x0001 / 0xFFFF

@Kadthicu's negative SNORM16 case on #659, -0x38400180, gives 0xFFFF (−1) on the hardware, as they computed. So it is a valid vector for gpu_buffer_store_norm_formats_round_once, if the store path rounds like the ALU instruction (not measured, see below).

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. Reference: this PR's cvt_norm_u16_f32 / cvt_norm_i16_f32 transcribed to Python with exact rational arithmetic.

Not tested

  • Typed buffer stores to UNORM / SNORM formats (buffer_store_format_*) and the f16-source v_cvt_pknorm_*_f16 on the hardware.
  • This PR's HLSL on my GPUs (I compared against a transcription of it); console hardware.

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.

Recompiler: f32 to UNORM / SNORM conversions round twice instead of once

5 participants