gpu/recomp: exact RNE rounding and NaN handling for V_CVT_(PK)NORM_*_F16 #80
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
goal:world-load
good first issue
help wanted
invalid
keep-open
off-goal
question
stale
test-only
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/xcl-dev--xcl!80
Loading…
Reference in a new issue
No description provided.
Delete branch "gh-pr/80/head"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@certified-retart on GitHub · 2026-11-23 01:45 UTC · original
certified-retart:fix/recomp-cvt-norm-f16→main· +126 −19 · 2 files · 1 commits · merged 2026-11-23 10:36 UTCSummary
Fixes two accuracy bugs in
V_CVT_NORM_U16_F16,V_CVT_NORM_I16_F16,V_CVT_PKNORM_U16_F16, andV_CVT_PKNORM_I16_F16in the RDNA2 → HLSL shader recompiler:f16viaf16tof32(x), multiplied by65535.0f/32767.0finfp32, and calledround(...). Near $2^{14}$–2^{15},fp32ULP exceeds2^{-10}, causing exact products just below.5to round up to.5and then tie-to-even to the wrong integer. For example,0x3801(\frac{1025}{2048}):\frac{1025}{2048} \times 65535 = 32799.49951171875 \rightarrow 32799(0x801F), previously32800(0x8020)\frac{1025}{2048} \times 32767 = 16399.49951171875 \rightarrow 16399(0x400F), previously16400(0x4010)clamp(x, -1.0f, 1.0f)lowers tomin/maxin DXIL, suppressing NaN to±1.0f(±32767) instead of returning0.Changes
crates/xcl/src/gpu/recomp/emit.rs:uintHLSL helperscvt_norm_f16(uint h, uint scale),cvt_norm_u16_f16(uint h), andcvt_norm_i16_f16(uint h)operating directly on raw IEEE-754 binary16 bit patterns (nofp64dependency; exact round-to-nearest-even across all 65,536 binary16 inputs,NaN → 0,±Inf / |x| ≥ 1 → ±scale, exact subnormal handling).v_cvt_norm_u16_f16,v_cvt_norm_i16_f16,v_cvt_pknorm_u16_f16, andv_cvt_pknorm_i16_f16to apply VOP3abs/negmodifiers directly on bit 15 before calling the helpers.crates/xcl/src/gpu/recomp/tests.rs:cvt_norm_f16_instructionsfor the new helper emission.gpu_cvt_norm_f16_exact_rounding_and_edge_casestesting all four instructions across0x3801/0xB801,±0.5,+qNaN/-sNaN,±Inf,±0.0, subnormals (0x0080,0x0081,0x0101),0x3BFF,±1.0, and VOP3ABS,NEG, and combinedABS+NEGmodifier order.Verification
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 on GitHub · 2026-11-23 05:57 UTC · original
Tested locally on Windows — both
cvt_norm_f16_instructionsandgpu_cvt_norm_f16_exact_rounding_and_edge_casespass cleanly.Key improvements verified:
2^{15}, guaranteeing exact round-to-nearest-even conversions across all edge cases (such as0x3801rounding to0x801F/0x400Finstead of0x8020/0x4010).NaN -> 0mapping without relying on clamp/min/max suppression.abs/negdirectly to bit 15 before conversion is clean and preserves sign semantics.Great precision fix for shader math.
@ivall on GitHub · 2026-11-23 10:36 UTC · original
Merged. Tested on Windows 11 with an RX 9070 (RDNA4):
cargo test --workspacegreen with the retail samples (shader pack, console DLLs, texture archive), lib tests also withXCL_SPLIT_WAVE64=1; Unicorn Overlord, Minecraft and Fortnite run with this round merged. Thanks!