Skip to content

gpu/recomp: exact RNE rounding and NaN handling for V_CVT_(PK)NORM_*_F16 - #80

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
certified-retart:fix/recomp-cvt-norm-f16
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
certified-retart:fix/recomp-cvt-norm-f16

Conversation

@certified-retart

Copy link
Copy Markdown
Contributor

Summary

Fixes two accuracy bugs in V_CVT_NORM_U16_F16, V_CVT_NORM_I16_F16, V_CVT_PKNORM_U16_F16, and V_CVT_PKNORM_I16_F16 in the RDNA2 → HLSL shader recompiler:

  1. Binary32 double-rounding: Previously, all four instructions promoted f16 via f16tof32(x), multiplied by 65535.0f / 32767.0f in fp32, and called round(...). Near $2^{14}$–$2^{15}$, fp32 ULP exceeds $2^{-10}$, causing exact products just below .5 to round up to .5 and then tie-to-even to the wrong integer. For example, 0x3801 ($\frac{1025}{2048}$):
    • $\frac{1025}{2048} \times 65535 = 32799.49951171875 \rightarrow 32799$ (0x801F), previously 32800 (0x8020)
    • $\frac{1025}{2048} \times 32767 = 16399.49951171875 \rightarrow 16399$ (0x400F), previously 16400 (0x4010)
  2. NaN clamping: HLSL clamp(x, -1.0f, 1.0f) lowers to min/max in DXIL, suppressing NaN to ±1.0f (±32767) instead of returning 0.

Changes

  • crates/xcl/src/gpu/recomp/emit.rs:
    • Added pure 32-bit uint HLSL helpers cvt_norm_f16(uint h, uint scale), cvt_norm_u16_f16(uint h), and cvt_norm_i16_f16(uint h) operating directly on raw IEEE-754 binary16 bit patterns (no fp64 dependency; exact round-to-nearest-even across all 65,536 binary16 inputs, NaN → 0, ±Inf / |x| ≥ 1 → ±scale, exact subnormal handling).
    • Switched v_cvt_norm_u16_f16, v_cvt_norm_i16_f16, v_cvt_pknorm_u16_f16, and v_cvt_pknorm_i16_f16 to apply VOP3 abs/neg modifiers directly on bit 15 before calling the helpers.
  • crates/xcl/src/gpu/recomp/tests.rs:
    • Updated cvt_norm_f16_instructions for the new helper emission.
    • Added gpu_cvt_norm_f16_exact_rounding_and_edge_cases testing all four instructions across 0x3801/0xB801, ±0.5, +qNaN/-sNaN, ±Inf, ±0.0, subnormals (0x0080, 0x0081, 0x0101), 0x3BFF, ±1.0, and VOP3 ABS, NEG, and combined ABS+NEG modifier order.

Verification

  • Exhaustive reference check across all 65,536 binary16 bit patterns: 0 mismatches for both unsigned and signed wrappers.
  • Live wave64 GPU verification on Windows 10 Pro + AMD Radeon(TM) Graphics (Ryzen 7 9800X3D RDNA2 iGPU, XCL_ADAPTER=1, XCL_REQUIRE_GPU=wave64, D3D12 Agility SDK 1.620.2):
    • cargo test -p xcl --lib cvt_norm_f16 -- --test-threads=1 — passed (2 passed; 0 failed).
    • cargo test -p xcl --lib gpu_more_atomics_sub_dword_scratch_valu -- --test-threads=1 — passed (1 passed; 0 failed).

@Lum7nt

Lum7nt commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Tested locally on Windows — both cvt_norm_f16_instructions and gpu_cvt_norm_f16_exact_rounding_and_edge_cases pass cleanly.

Key improvements verified:

  1. Exact integer arithmetic: Operating directly on IEEE-754 binary16 bits via 32-bit integer arithmetic avoids fp32 double-rounding near $2^{14}$–$2^{15}$, guaranteeing exact round-to-nearest-even conversions across all edge cases (such as 0x3801 rounding to 0x801F/0x400F instead of 0x8020/0x4010).
  2. IEEE-754 compliance: Proper handling of subnormals, infinities, and exact NaN -> 0 mapping without relying on clamp/min/max suppression.
  3. Bit-level modifier handling: Applying VOP3 abs/neg directly to bit 15 before conversion is clean and preserves sign semantics.

Great precision fix for shader math.

@ivall
ivall merged commit 92456af into xcl-dev:main Nov 23, 2026
ivall added a commit that referenced this pull request Nov 23, 2026
ivall added a commit that referenced this pull request Nov 23, 2026
The previous commit (9284dcd, titled like this one by mistake) is the follow-up to #86:
placed textures that get memory of their own retry with COMMON when the PC refuses the
title's state, as the past-the-end fallback does, instead of falling back to the overlapping
placement.
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged. Tested on Windows 11 with an RX 9070 (RDNA4): cargo test --workspace green with the retail samples (shader pack, console DLLs, texture archive), lib tests also with XCL_SPLIT_WAVE64=1; Unicorn Overlord, Minecraft and Fortnite run with this round merged. Thanks!

rootw0rm pushed a commit to rootw0rm/xcl that referenced this pull request Nov 27, 2026
…nd backend

1d split 1 of 2 (honest split: route halves stay under the line budget).
Device frame-clock slots route on Vulkan objects only: xcl-dev#78
SetFrameIntervalX, xcl-dev#79 ScheduleFrameEventX (with ancillary handle routing),
xcl-dev#80 WaitFrameEventX, xcl-dev#81 GetFrameStatisticsX — thin adapters over the Linux
twin (d3d12x_frame_linux). register_ancillary remembers the schedule block's
handles per type (E_INVALIDARG past the 63-handle cap, like Windows; Linux
trusts the block — readable() is windows-only); the clock thread signals
each with kernelx SetEvent at fire. Unscheduling clears. Null device keeps
logging stubs: no behavior change while XCL_VULKAN_BACKEND is unset. Queue
xcl-dev#30 PresentX + the real present follow in 1d(ii).

Failing-first: the routing test failed pre-fix (E0599 no VulkanQueue;
then E_FAIL at xcl-dev#81 with PresentX still stubbed); passes now (slots +
ancillary-signal asserts, serialised on CLOCK_SERIAL with the twin's tests).
Gates: check -p xcl --tests EXIT 0 (40 pre-existing warnings, none new);
check --workspace EXIT 0; lib 794 passed/0 failed/1 ignored (+1 new).
Title e2e + retail controls run at 1d(ii).
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.

3 participants