fix(shader): round f32 to f16 to nearest even on every driver #317
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/317"
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?
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-02T04:40:14Z.
What
Every f32→f16 conversion the recompiler emits went through GLSL
packHalf2x16:ConvertF16F32, which backswriteF16(all f16 VALU results:v_add/mul/fma_f16,v_cvt_f16_f32, f16 transcendental, …), thePackHalf2x16IR op, and f16 image stores. Its rounding is implementation-defined. RADV truncates (v_cvt_pkrtz), so on AMD:v_add_f16of0x21daand0xea00gives0xe9ffinstead of0xea00.0x7bff) instead of infinity.RDNA rounds to nearest even, as measured on an RDNA2 GPU: 4096/4096
v_cvt_f16_f32results equal IEEE RTE. lavapipe already rounds to nearest, which is why CI never saw it.EmitF32ToF16BitsRtedoes the conversion with integer SPIR-V ops: round to nearest even with carry into the exponent, subnormals (down to 2^-25 ties-to-even), overflow from 65520 to infinity, and quiet NaNs keeping the top payload bits.EmitPackHalf2x16and the image store path use it.v_cvt_pkrtz_f16_f32is unchanged, since it has its own truncatingPackFloat2x16Rtz.Test
agc_driver_float16_rounding: 256 vectors ofv_cvt_f16_f32,v_add_f16andv_mul_f16, captured on the RDNA2 GPU (ROCm HSA, raw kernels): ties and near-ties, the overflow edge 65520, subnormal edges, NaN, ±inf, and random values. It also checks that the upper half is kept, which the hardware does. f32 denormal inputs are left out because they depend on the driver's float controls.Tested
Linux, full ctest (99) passes.
v_add_f16 is 0xabcde9ff, expected 0xabcdea00. lavapipe passes either way.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt.mdfiles or images added to the repository (attach them to this PR)