Skip to content

Recompiler: round f16 conversions and products to nearest-even by default - #166

Merged
ivall merged 1 commit into
xcl-dev:mainfrom
ebarr24:fix/recomp-default-f16-rounding
Nov 23, 2026
Merged

ivall merged 1 commit into
xcl-dev:mainfrom
ebarr24:fix/recomp-default-f16-rounding

Conversation

@ebarr24

@ebarr24 ebarr24 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

The default RDNA binary16 rounding mode is round-to-nearest-even. XCL has used legacy HLSL f32tof16 for that path, which narrows toward zero. On the RTX 3080, converting 3075.0 gives 0x6A01 instead of 0x6A02, and converting 65520.0 gives 0x7BFF instead of infinity. Direct3D conversion rules, sections 3.2.2 and 22.13.2

Use the existing f32_to_f16_rm helper for v_cvt_f16_f32, v_mul_f16, and v_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.

AI-assisted patch preparation and verification.

…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.
@WarmMilkCodes

Copy link
Copy Markdown
Contributor

Tested #166 deccf2b merged onto main 956a476 on AMD Radeon RX 6600 XT (RDNA2), driver 32.0.21045.29003, Windows 11 23H2 (22631.6199): pass

cargo test --workspace --no-fail-fast -- --test-threads=4, no XCL_SAMPLE_* data:

main this
passed 568 569
failed 1 1
GPU tests skipped 0 0

No new failures vs main. The PR's new test passes on RDNA2. The one failure on both, gpu_wave64_ps_whole_wave_reductions, is the 32-lane PS case on this card (#155).

@TheHoracio

Copy link
Copy Markdown
Contributor

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 x86_64-pc-windows-msvc, DXC 1.9.2602.17. The PR head deccf2b was tested as is, with no other PRs applied.

cargo test --workspace --no-fail-fast: 568 passed, 1 failed, 39 ignored, 0 SKIPPED.

  • gpu_f16_defaults_to_round_to_nearest_even passes on AMD as well. The existing f16 rounding tests also pass: gpu_s_round_mode_controls_f16_rounding_across_modes, gpu_mad_nf_f16_rounds_intermediate_in_binary16, gpu_cvt_norm_f16_exact_rounding_and_edge_cases, gpu_cmp_class_f16_preserves_snan.
  • The one failure is gpu_wave64_ps_whole_wave_reductions. It fails the same way on main 956a476 on this GPU and is fixed by VS / PS: the 32-lane form on every GPU that can run 32-lane waves (RD… #155 (see my comment there). It is unrelated to this PR.

@Kalmarv

Kalmarv commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Additional default-mode check on the RTX 5070 Ti (driver 610.74, Windows x64 MSVC, Rust 1.99.0): I tested PR head deccf2ba3c0f09023bc9ccf2f49cb5af8aae798f directly, and carried its test plus a 14-lane synthetic conversion probe to main 956a476bf738a098f39df4f4c8acef682e43030f.

Binary32 input to V_CVT_F16_F32 main, binary16 bits PR head
387fe000 (midpoint between the largest subnormal and smallest normal) 03ff 0400
3f801000, 3f803000 (successive midpoints) 3c00, 3c01 3c00, 3c02
33000000, 33000001, b3000001 0000, 0000, 8000 0000, 0001, 8001
Both signs of zero and infinity Signs preserved Signs preserved
Quiet and signaling NaN inputs Both quiet NaNs Both quiet NaNs

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 f16, five passed on main and all seven passed on the head. The failures were the verbatim PR regression (including scalar and packed products) and the added conversion probe. Each dispatched synthetic shaders. No retail shaders, merge build, or explicit denorm-mode sweep. AI-assisted review.

@ivall
ivall merged commit ad71cde into xcl-dev:main Nov 23, 2026
@ivall

ivall commented Nov 23, 2026

Copy link
Copy Markdown
Collaborator

Merged (main 2abc562). Checked on an RX 9070 (RDNA4) with the 11 PRs merged together on main 38b05b7: cargo build --workspace && cargo test --workspace -- --test-threads=4 - 547 unit tests passed, 0 failed (19 ignored); the integration and e2e tests passed too.

CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
…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)
oni-swr added a commit to oni-swr/xcl that referenced this pull request Nov 24, 2026
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>
CalvyPZ pushed a commit to CalvyPZ/xcl that referenced this pull request Nov 24, 2026
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>
oni-swr added a commit to oni-swr/xcl that referenced this pull request Nov 25, 2026
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>
oni-swr added a commit to oni-swr/xcl that referenced this pull request Nov 25, 2026
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>
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.

5 participants