Recompiler: f16 add / sub round toward zero in the default mode (f32tof16), not to nearest even #685
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#685
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
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?
@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_exprincrates/xcl/src/gpu/recomp/emit.rs).deccf2bmovedv_cvt_f16_f32and the f16 multiplies to the RNE helperf32_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
main91f9a5ewith a throwaway GPU test (V_ADD_F16, VOP3, wave32), NVIDIA GeForce RTX 3060, driver 581.80:0x3c00 + 0x1200(1 + 0.75 ulp)0x3c000x3c010xbc00 + 0x92000xbc000xbc010x3c00 + 0x1000(tie)0x3c000x3c000x7bff + 0x5000(65504 + 32)0x7bff0x7c00Plan (one small PR): send
v_add_f16,v_sub_f16,v_subrev_f16andv_pk_add_f16throughf32_to_f16_rm(…, 0u)in the default mode, asdeccf2bdid 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
f32tof16rounds 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.