fix(shader): round f32 to f16 to nearest even on every driver #317

Merged
adriadam10 merged 2 commits from upstream-pr/317 into main 2026-10-03 07:31:57 +00:00
adriadam10 commented 2026-10-02 04:40:14 +00:00 (Migrated from github.com)

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 backs writeF16 (all f16 VALU results: v_add/mul/fma_f16, v_cvt_f16_f32, f16 transcendental, …), the PackHalf2x16 IR op, and f16 image stores. Its rounding is implementation-defined. RADV truncates (v_cvt_pkrtz), so on AMD:

  • f16 results are one ulp low whenever they round up: v_add_f16 of 0x21da and 0xea00 gives 0xe9ff instead of 0xea00.
  • Overflow gives 65504 (0x7bff) instead of infinity.

RDNA rounds to nearest even, as measured on an RDNA2 GPU: 4096/4096 v_cvt_f16_f32 results equal IEEE RTE. lavapipe already rounds to nearest, which is why CI never saw it.

EmitF32ToF16BitsRte does 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. EmitPackHalf2x16 and the image store path use it. v_cvt_pkrtz_f16_f32 is unchanged, since it has its own truncating PackFloat2x16Rtz.

Test agc_driver_float16_rounding: 256 vectors of v_cvt_f16_f32, v_add_f16 and v_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.

  • RADV (Radeon 660M) and lavapipe: the test passes.
  • Without the fix, RADV: v_add_f16 is 0xabcde9ff, expected 0xabcdea00. lavapipe passes either way.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #251 (test harness); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4712750767 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/317). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T04:40:14Z. --- ### What Every f32→f16 conversion the recompiler emits went through GLSL `packHalf2x16`: `ConvertF16F32`, which backs `writeF16` (all f16 VALU results: `v_add/mul/fma_f16`, `v_cvt_f16_f32`, f16 transcendental, …), the `PackHalf2x16` IR op, and f16 image stores. Its rounding is implementation-defined. **RADV truncates** (`v_cvt_pkrtz`), so on AMD: - f16 results are one ulp low whenever they round up: `v_add_f16` of `0x21da` and `0xea00` gives `0xe9ff` instead of `0xea00`. - Overflow gives 65504 (`0x7bff`) instead of infinity. RDNA rounds to nearest even, as measured on an RDNA2 GPU: 4096/4096 `v_cvt_f16_f32` results equal IEEE RTE. lavapipe already rounds to nearest, which is why CI never saw it. `EmitF32ToF16BitsRte` does 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. `EmitPackHalf2x16` and the image store path use it. `v_cvt_pkrtz_f16_f32` is unchanged, since it has its own truncating `PackFloat2x16Rtz`. Test `agc_driver_float16_rounding`: 256 vectors of `v_cvt_f16_f32`, `v_add_f16` and `v_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. - RADV (Radeon 660M) and lavapipe: the test passes. - Without the fix, RADV: `v_add_f16 is 0xabcde9ff, expected 0xabcdea00`. lavapipe passes either way. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #251 (test harness); review only the last commit - [x] AI-assisted: yes
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!317
No description provided.