Repository navigation
Recompiler: round f16 conversions and products to nearest-even by default - #166
Conversation
…ault Use the existing rounding helper for v_cvt_f16_f32 and scalar and packed half multiplication. A finite product of two half operands fits in binary32, so the final binary16 conversion rounds the exact product.
|
Tested #166
No new failures vs main. The PR's new test passes on RDNA2. The one failure on both, |
|
Tested on AMD RDNA3: Radeon RX 7800 XT (16 GB, wave 32–64), driver 32.0.32015.7029, Windows 11 Pro 10.0.26300, Rust 1.100.0
|
|
Additional default-mode check on the RTX 5070 Ti (driver 610.74, Windows x64 MSVC, Rust 1.99.0): I tested PR head
The tie results agree with half-precision round mode 0. The NaN assertion checked the quiet-NaN class only. AMD RDNA 2 ISA, §6.4 and V_CVT_F16_F32 Of seven recompiler tests matching |
…l-dev#111) In the default mode every f16 result that is narrowed from binary32 went through HLSL f32tof16, which rounds toward zero: on an RTX 4070 Ti and an RDNA2 iGPU every inexact v_add/sub/sqrt/rcp/rsq/exp/log/ldexp_f16 result came out one binary16 step low, and an overflow stopped at 65504 instead of inf. Native RDNA2 rounds to nearest even (luckyteagirl's measurements on xcl-dev#672). to_f16_expr now narrows with f32_to_f16_rm(e, 0u) in the default mode, the helper the multiplies already use (xcl-dev#166); under a dynamic mode nothing changes. v_fract_f16 clamps its binary32 result below 1.0, and 1 - 2^-24 narrows to 1.0 by nearest even, so its binary16 result is kept at 0x3BFF (xcl-dev#655). Supersedes xcl-dev#696, whose add / sub cases this covers. Assisted-by: Claude Code (Claude Opus 5.5)
The HLSL backend rounds v_cvt_f16_f32, v_mul_f16 and v_pk_mul_f16 with to_f16_rounded_expr: round-to-nearest-even in the default mode, the program's MODE[3:2] when it sets one (xcl-dev#166). The direct DXIL backend used to_f16 for them, which without a dynamic mode is dx.op.legacyF32ToF16, rounding toward zero: 3075.0 became 0x6A01 instead of 0x6A02, 65520.0 stayed finite and products just above half the smallest denormal flushed to 0. Add Lw::to_f16_rounded (the counterpart of to_f16_rounded_expr) and use it for the three instructions; v_mad_f16's product, which already rounded this way inline, calls it too. Bump TRANSLATOR_VERSION so cached containers are rebuilt. The GPU test runs the three instructions in wave32, split wave64 and native wave64. Based on itsSio's released patch (7452171, on xcl-dev#1269). Co-authored-by: itsSio <31954644+itsSio@users.noreply.github.com> Assisted-by: T3 Code through Antigravity (Gemini 4 Neon) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The HLSL backend rounds v_cvt_f16_f32, v_mul_f16 and v_pk_mul_f16 with to_f16_rounded_expr: round-to-nearest-even in the default mode, the program's MODE[3:2] when it sets one (xcl-dev#166). The direct DXIL backend used to_f16 for them, which without a dynamic mode is dx.op.legacyF32ToF16, rounding toward zero: 3075.0 became 0x6A01 instead of 0x6A02, 65520.0 stayed finite and products just above half the smallest denormal flushed to 0. Add Lw::to_f16_rounded (the counterpart of to_f16_rounded_expr) and use it for the three instructions; v_mad_f16's product, which already rounded this way inline, calls it too. Bump TRANSLATOR_VERSION so cached containers are rebuilt. The GPU test runs the three instructions in wave32, split wave64 and native wave64. Based on itsSio's released patch (7452171, on xcl-dev#1269). Co-authored-by: itsSio <31954644+itsSio@users.noreply.github.com> Assisted-by: T3 Code through Antigravity (Gemini 4 Neon) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The HLSL backend rounds v_cvt_f16_f32, v_mul_f16 and v_pk_mul_f16 with to_f16_rounded_expr: round-to-nearest-even in the default mode, the program's MODE[3:2] when it sets one (xcl-dev#166). The direct DXIL backend used to_f16 for them, which without a dynamic mode is dx.op.legacyF32ToF16, rounding toward zero: 3075.0 became 0x6A01 instead of 0x6A02, 65520.0 stayed finite and products just above half the smallest denormal flushed to 0. Add Lw::to_f16_rounded (the counterpart of to_f16_rounded_expr) and use it for the three instructions; v_mad_f16's product, which already rounded this way inline, calls it too. The GPU test runs the three instructions in wave32, split wave64 and native wave64. Based on itsSio's released patch (7452171, on xcl-dev#1269). Co-authored-by: itsSio <31954644+itsSio@users.noreply.github.com> Assisted-by: T3 Code through Antigravity (Gemini 4 Neon) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The HLSL backend rounds v_cvt_f16_f32, v_mul_f16 and v_pk_mul_f16 with to_f16_rounded_expr: round-to-nearest-even in the default mode, the program's MODE[3:2] when it sets one (xcl-dev#166). The direct DXIL backend used to_f16 for them, which without a dynamic mode is dx.op.legacyF32ToF16, rounding toward zero: 3075.0 became 0x6A01 instead of 0x6A02, 65520.0 stayed finite and products just above half the smallest denormal flushed to 0. Add Lw::to_f16_rounded (the counterpart of to_f16_rounded_expr) and use it for the three instructions; v_mad_f16's product, which already rounded this way inline, calls it too. The GPU test runs the three instructions in wave32, split wave64 and native wave64. Based on itsSio's released patch (7452171, on xcl-dev#1269). Co-authored-by: itsSio <31954644+itsSio@users.noreply.github.com> Assisted-by: T3 Code through Antigravity (Gemini 4 Neon) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The default RDNA binary16 rounding mode is round-to-nearest-even. XCL has used legacy HLSL
f32tof16for that path, which narrows toward zero. On the RTX 3080, converting3075.0gives0x6A01instead of0x6A02, and converting65520.0gives0x7BFFinstead of infinity. Direct3D conversion rules, sections 3.2.2 and 22.13.2Use the existing
f32_to_f16_rmhelper forv_cvt_f16_f32,v_mul_f16, andv_pk_mul_f16, with a constant nearest-even mode when the shader leaves the default in place. The finite product of two binary16 inputs fits exactly in binary32, so one narrowing is sufficient. The regression covers midpoint and adjacent values, negative input, overflow, a subnormal and scalar and packed multiplication with midpoint products.Issue #111 tracks the exact product and sum needed for a single-rounding FMA. Rounding issue #111 · CPU reproduction
Verification
Windows 11 25H2 (26200.9711), Rust 1.100.0
x86_64-pc-windows-gnu, RTX 3080 (driver 616.92), DXC 1.9.2609.5.db5dc26.deccf2bthe expanded conversion, scalar product and packed product regression passes. The full workspace run on10f25f4, with Exercise the synthetic Xbox fixtures through XCL #157, VFS: report existing game files on CREATE_ALWAYS #158 and VFS: preserve the destination when an overlay rename fails #160 applied alongside this patch, reported 571 passes, zero failures and 39 ignored. Two native-wave64 tests and the unconfigured retail-sample tests returned early.mainat956a476.AI-assisted patch preparation and verification.