Recompiler: f16 add / sub round toward zero in the default mode (f32tof16), not to nearest even #685

Open
opened 2026-11-26 04:29:19 +00:00 by github-bot · 0 comments
Owner

@⁠Anon64 on GitHub · 2026-11-24 03:29 UTC · original

In the default rounding mode (MODE[3:2] = 0, round to nearest even), the f16 add and sub results are narrowed with HLSL f32tof16, which rounds toward zero (to_f16_expr in crates/xcl/src/gpu/recomp/emit.rs). deccf2b moved v_cvt_f16_f32 and the f16 multiplies to the RNE helper f32_to_f16_rm(…, 0u); add and sub still truncate, so a result that is not a binary16 value comes out one ulp toward zero, and an overflow stops at ±65504 instead of ±inf.

Measured on main 91f9a5e with a throwaway GPU test (V_ADD_F16, VOP3, wave32), NVIDIA GeForce RTX 3060, driver 581.80:

a + b XCL RNE
0x3c00 + 0x1200 (1 + 0.75 ulp) 0x3c00 0x3c01
0xbc00 + 0x9200 0xbc00 0xbc01
0x3c00 + 0x1000 (tie) 0x3c00 0x3c00
0x7bff + 0x5000 (65504 + 32) 0x7bff 0x7c00

Plan (one small PR): send v_add_f16, v_sub_f16, v_subrev_f16 and v_pk_add_f16 through f32_to_f16_rm(…, 0u) in the default mode, as deccf2b did for the multiplies. The f32 sum of two binary16 values rounded once more to binary16 equals the correctly rounded binary16 sum, because 24 ≥ 2·11 + 2 (Figueroa, "When is double rounding innocuous?", 1995). FMA (the sum is not exact) and the directed modes (#111 / #128) stay out of it. A per-instruction GPU test with the cases above comes with it.

Not tested: AMD. Whether f32tof16 rounds the same way on AMD's driver is not checked here.

AI use: Claude Code (Claude Opus 5.5) found this while testing #672, ran the measurement and wrote this report.

**@⁠Anon64** on GitHub · 2026-11-24 03:29 UTC · [original](https://github.com/xcl-dev/xcl/issues/685) In the default rounding mode (MODE[3:2] = 0, round to nearest even), the f16 add and sub results are narrowed with HLSL `f32tof16`, which rounds toward zero (`to_f16_expr` in `crates/xcl/src/gpu/recomp/emit.rs`). deccf2b moved `v_cvt_f16_f32` and the f16 multiplies to the RNE helper `f32_to_f16_rm(…, 0u)`; add and sub still truncate, so a result that is not a binary16 value comes out one ulp toward zero, and an overflow stops at ±65504 instead of ±inf. Measured on `main` `91f9a5e` with a throwaway GPU test (`V_ADD_F16`, VOP3, wave32), NVIDIA GeForce RTX 3060, driver 581.80: | a + b | XCL | RNE | |---|---|---| | `0x3c00 + 0x1200` (1 + 0.75 ulp) | `0x3c00` | `0x3c01` | | `0xbc00 + 0x9200` | `0xbc00` | `0xbc01` | | `0x3c00 + 0x1000` (tie) | `0x3c00` | `0x3c00` | | `0x7bff + 0x5000` (65504 + 32) | `0x7bff` | `0x7c00` | **Plan** (one small PR): send `v_add_f16`, `v_sub_f16`, `v_subrev_f16` and `v_pk_add_f16` through `f32_to_f16_rm(…, 0u)` in the default mode, as deccf2b did for the multiplies. The f32 sum of two binary16 values rounded once more to binary16 equals the correctly rounded binary16 sum, because 24 ≥ 2·11 + 2 (Figueroa, "When is double rounding innocuous?", 1995). FMA (the sum is not exact) and the directed modes (#111 / #128) stay out of it. A per-instruction GPU test with the cases above comes with it. **Not tested:** AMD. Whether `f32tof16` rounds the same way on AMD's driver is not checked here. AI use: Claude Code (Claude Opus 5.5) found this while testing #672, ran the measurement and wrote this report.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:29:19 +00:00
This discussion has been locked. Commenting is limited to contributors.
No description provided.