fix(shader): choose packed f16 NaN results on the bits, as RDNA does #533

Merged
adriadam10 merged 1 commit from upstream-pr/533 into main 2026-10-08 21:41:34 +00:00
adriadam10 commented 2026-10-04 15:46:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T15:46:30Z.


What

The packed f16 ops (v_pk_add_f16, v_pk_mul_f16, v_pk_fma_f16, v_pk_fmac_f16, v_pk_min_f16, v_pk_max_f16) left NaN results to the driver, or got them wrong:

  • min/max treated every NaN with a nonzero low payload as signaling, so min(0xffff, 0) returned the NaN instead of 0.
  • The NaN override read the sources without neg_lo/neg_hi, so a negated NaN kept its old sign.
  • add/mul/fma(c) returned whatever NaN the driver produced (payload and sign unspecified in Vulkan, canonical on NVIDIA).

NaN results are now chosen on the bits of each half, after op_sel/op_sel_hi/neg_lo/neg_hi. Numeric results are unchanged. With clamp the result keeps the current path: on hardware it is +0 for any NaN, which belongs to the f16 clamp work (#323, #500).

Behaviour measured on HW (RDNA2, gfx1035)

  • add/mul/fma/fmac: the first NaN source in operand order (src0, src1, src2/accumulator), with the quiet bit set. If the product is invalid (inf·0) the result is 0xfe00, even when src2 is a NaN; if no source is a NaN and the result is invalid (inf−inf), it is 0xfe00 too.
  • min/max (IEEE mode): a signaling NaN source wins, quieted (src0 before src1). Otherwise, if one source is a NaN, the result is the other source. If both are quiet NaNs, the result is src0.
  • v_pk_fmac_f16 gives the same result as v_pk_fma_f16 with src2 = the destination.
  • The rules above are at IEEE=1. At IEEE=0 (the mode titles use, e.g. every Astro's Playroom shader) NaN sources pass through without the quiet bit, and min/max return the other source for any NaN, signaling or not. The shader's mode comes from GuestContext::floatMode (#1816); a shader with no known mode is translated as IEEE=0. A mode that flushes f16 denormals or rounds f16 other than to nearest even throws (FLOAT_MODE step of #981).

Also added to TechnicalDebt (not changed here): v_fma_f16, v_pk_fma_f16 and v_pk_fmac_f16 are an f32 FMA rounded to f16, so the result can be rounded twice (0x62e9 * 0xd159 + 0x23ec gives 0xf89e; the hardware gives 0xf89f).

Tested

New execution test agc_driver_packed_alu_tests runs 44 instructions covering all 19 VOP3P packed ops plus v_pk_fmac_f16: each plain op, op_sel/op_sel_hi variants, and neg_lo/neg_hi variants on the f16 ops. Rows were picked by class (integer overflow, sign and shift counts; f16 inf/zero/NaN, quiet and signaling NaN per source), with fixed rows for inf·0, inf−inf and inf·0 with a NaN src2. The expected values are what the hardware returns at IEEE=1 (Expected) and IEEE=0 (ExpectedIeeeOff), and the Python model matches them in every cell. The test runs with no mode (checked against IEEE=0), with IEEE=1, and with a flush-f16 mode that must throw. Denormal inputs and results are excluded.
It passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The integer ops already passed on main. The test fails with each of these changes: no NaN override (= main), quiet NaNs treated as signaling, neg ignored on NaN sources, a positive invalid NaN, the last NaN source picked first, NaN not quieted, src1 picked when both min/max sources are NaN, the wrong signaling-NaN order, and the fmac accumulator ignored.
The full Release ctest passes (489/489).

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: #1816, #1826 (both merged)
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4735591500 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T15:46:30Z. --- ### What The packed f16 ops (`v_pk_add_f16`, `v_pk_mul_f16`, `v_pk_fma_f16`, `v_pk_fmac_f16`, `v_pk_min_f16`, `v_pk_max_f16`) left NaN results to the driver, or got them wrong: - min/max treated every NaN with a nonzero low payload as signaling, so `min(0xffff, 0)` returned the NaN instead of `0`. - The NaN override read the sources without `neg_lo`/`neg_hi`, so a negated NaN kept its old sign. - add/mul/fma(c) returned whatever NaN the driver produced (payload and sign unspecified in Vulkan, canonical on NVIDIA). NaN results are now chosen on the bits of each half, after `op_sel`/`op_sel_hi`/`neg_lo`/`neg_hi`. Numeric results are unchanged. With `clamp` the result keeps the current path: on hardware it is `+0` for any NaN, which belongs to the f16 clamp work (#323, #500). #### Behaviour measured on HW (RDNA2, gfx1035) - add/mul/fma/fmac: the first NaN source in operand order (src0, src1, src2/accumulator), with the quiet bit set. If the product is invalid (inf·0) the result is `0xfe00`, even when src2 is a NaN; if no source is a NaN and the result is invalid (inf−inf), it is `0xfe00` too. - min/max (IEEE mode): a signaling NaN source wins, quieted (src0 before src1). Otherwise, if one source is a NaN, the result is the other source. If both are quiet NaNs, the result is src0. - `v_pk_fmac_f16` gives the same result as `v_pk_fma_f16` with src2 = the destination. - The rules above are at IEEE=1. At IEEE=0 (the mode titles use, e.g. every Astro's Playroom shader) NaN sources pass through without the quiet bit, and min/max return the other source for any NaN, signaling or not. The shader's mode comes from `GuestContext::floatMode` (#1816); a shader with no known mode is translated as IEEE=0. A mode that flushes f16 denormals or rounds f16 other than to nearest even throws (FLOAT_MODE step of #981). Also added to TechnicalDebt (not changed here): `v_fma_f16`, `v_pk_fma_f16` and `v_pk_fmac_f16` are an f32 FMA rounded to f16, so the result can be rounded twice (`0x62e9 * 0xd159 + 0x23ec` gives `0xf89e`; the hardware gives `0xf89f`). ### Tested New execution test `agc_driver_packed_alu_tests` runs 44 instructions covering all 19 VOP3P packed ops plus `v_pk_fmac_f16`: each plain op, `op_sel`/`op_sel_hi` variants, and `neg_lo`/`neg_hi` variants on the f16 ops. Rows were picked by class (integer overflow, sign and shift counts; f16 inf/zero/NaN, quiet and signaling NaN per source), with fixed rows for inf·0, inf−inf and inf·0 with a NaN src2. The expected values are what the hardware returns at IEEE=1 (`Expected`) and IEEE=0 (`ExpectedIeeeOff`), and the Python model matches them in every cell. The test runs with no mode (checked against IEEE=0), with IEEE=1, and with a flush-f16 mode that must throw. Denormal inputs and results are excluded. It passes on RADV, local lavapipe and CI's lavapipe (Mesa 25.2.8). The integer ops already passed on `main`. The test fails with each of these changes: no NaN override (= `main`), quiet NaNs treated as signaling, `neg` ignored on NaN sources, a positive invalid NaN, the last NaN source picked first, NaN not quieted, src1 picked when both min/max sources are NaN, the wrong signaling-NaN order, and the fmac accumulator ignored. The full Release ctest passes (489/489). ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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: #1816, #1826 (both merged) - [x] AI-assisted: yes
adriadam10 commented 2026-10-04 20:36:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T20:36:22Z.


Rebased on current main: the only Windows failure was guest_audio_in_no_device, fixed by #532. No other changes.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984141484 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533#issuecomment-5984141484). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T20:36:22Z. --- Rebased on current main: the only Windows failure was `guest_audio_in_no_device`, fixed by #532. No other changes.
oneandonlydean commented 2026-10-04 21:19:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-04T21:19:11Z.


Thanks. I checked the min/max part against the RDNA2 ISA pseudocode for V_MIN_F16/V_MAX_F16: a signaling NaN wins and is quieted (src0 before src1), and a single NaN returns the other source, which is what the selects do. For two quiet NaNs the pseudocode would give src1, while your capture and the code give src0; I'm fine going with the hardware there, and the mutation row covers it. The old sNaN test (payload & 0x1ff) was indeed wrong for 0xffff, and testing the quiet bit fixes it. The accumulator is read through accumulatorOperand, so this composes with #514's change to the numeric path, and the neg_lo/neg_hi and op_sel/op_sel_hi handling on the bits matches what readF16LaneAsF32 does for the value.

One note, not blocking: quieting a signaling NaN on add/mul/fma is the IEEE=1 result, and the recompiler doesn't read the mode from PGM_RSRC1. Main already made the same assumption for v_pk_min/max, so this keeps it consistent.

agc_driver_packed_alu fails on main and passes with this branch on my NVIDIA machine. Since packed f16 ops are on PPSA21564's path, I'll run it through my PPSA21564 check before merging. Otherwise it's ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984493626 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533#issuecomment-5984493626). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:19:11Z. --- Thanks. I checked the min/max part against the RDNA2 ISA pseudocode for V_MIN_F16/V_MAX_F16: a signaling NaN wins and is quieted (src0 before src1), and a single NaN returns the other source, which is what the selects do. For two quiet NaNs the pseudocode would give src1, while your capture and the code give src0; I'm fine going with the hardware there, and the mutation row covers it. The old sNaN test (`payload & 0x1ff`) was indeed wrong for 0xffff, and testing the quiet bit fixes it. The accumulator is read through `accumulatorOperand`, so this composes with #514's change to the numeric path, and the neg_lo/neg_hi and op_sel/op_sel_hi handling on the bits matches what `readF16LaneAsF32` does for the value. One note, not blocking: quieting a signaling NaN on add/mul/fma is the IEEE=1 result, and the recompiler doesn't read the mode from PGM_RSRC1. Main already made the same assumption for v_pk_min/max, so this keeps it consistent. `agc_driver_packed_alu` fails on main and passes with this branch on my NVIDIA machine. Since packed f16 ops are on PPSA21564's path, I'll run it through my PPSA21564 check before merging. Otherwise it's ready to merge from my side.
adriadam10 commented 2026-10-05 17:05:21 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T17:05:21Z.


Rebased on main (TechnicalDebt conflict with the v_mullit_f32 line: both kept). PackedAlu passes on RADV and lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5999248676 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533#issuecomment-5999248676). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T17:05:21Z. --- Rebased on main (TechnicalDebt conflict with the v_mullit_f32 line: both kept). PackedAlu passes on RADV and lavapipe.
adriadam10 commented 2026-10-05 19:01:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T19:01:15Z.


Rebased on main (TechnicalDebt conflict with the v_fma_legacy_f32 line, both kept). packed_alu passes on RADV and on CI's lavapipe.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6001124380 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533#issuecomment-6001124380). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T19:01:15Z. --- Rebased on main (TechnicalDebt conflict with the v_fma_legacy_f32 line, both kept). packed_alu passes on RADV and on CI's lavapipe.
adriadam10 commented 2026-10-08 21:35:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-08T21:35:49Z.


Taken off hold for #981. The ops now follow the shader's float mode, which #1816 carries into the request. Rebased on main after #1826.

  • With IEEE=1, behaviour is unchanged: signaling NaN inputs are quieted and min/max follow the IEEE rules.
  • With IEEE=0 (the mode titles use, e.g. every Astro's Playroom shader) NaN inputs pass through unquieted and min/max don't treat signaling NaNs specially. A shader with no known float mode is translated this way.
  • New fma rule, measured: an invalid product (inf × 0) gives 0xfe00 even when src2 is a NaN. Rows 30 and 31 now cover this.
  • A mode that flushes f16 denormals or rounds f16 other than to nearest even throws. Those modes are the FLOAT_MODE step of #981.
  • Expected was recaptured on gfx1035 at IEEE=1. A second table, ExpectedIeeeOff, was captured at IEEE=0. The test runs both, plus the throwing mode.
  • Buffer descriptor uses raw bounds (0x31016fac), as #639 requires. TechnicalDebt has a line for the measured rules.

Tested: packed_alu on RADV and CI's lavapipe, full ctest 489/489. Negative checks each fail: quieting at IEEE=0, src2 NaN beating an invalid product, sNaN min/max rule applied at IEEE=0, the flush mode not refused.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069509491 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533#issuecomment-6069509491). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-08T21:35:49Z. --- Taken off hold for #981. The ops now follow the shader's float mode, which #1816 carries into the request. Rebased on `main` after #1826. - With IEEE=1, behaviour is unchanged: signaling NaN inputs are quieted and min/max follow the IEEE rules. - With IEEE=0 (the mode titles use, e.g. every Astro's Playroom shader) NaN inputs pass through unquieted and min/max don't treat signaling NaNs specially. A shader with no known float mode is translated this way. - New fma rule, measured: an invalid product (inf × 0) gives `0xfe00` even when src2 is a NaN. Rows 30 and 31 now cover this. - A mode that flushes f16 denormals or rounds f16 other than to nearest even throws. Those modes are the FLOAT_MODE step of #981. - `Expected` was recaptured on gfx1035 at IEEE=1. A second table, `ExpectedIeeeOff`, was captured at IEEE=0. The test runs both, plus the throwing mode. - Buffer descriptor uses raw bounds (`0x31016fac`), as #639 requires. TechnicalDebt has a line for the measured rules. Tested: packed_alu on RADV and CI's lavapipe, full ctest 489/489. Negative checks each fail: quieting at IEEE=0, src2 NaN beating an invalid product, sNaN min/max rule applied at IEEE=0, the flush mode not refused.
SleglJan commented 2026-10-08 21:40:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SleglJan on 2026-10-08T21:40:52Z.


Scalar cross-check of the NaN rules, measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with the hardware oracle: v_min_f64 and v_max_f64 over every pair of 14 operands (±0, ±1, ±inf, ±denormal, three quiet and three signalling NaNs; 196 rows), and v_min_f16, v_max_f16, v_min3_f16, v_max3_f16, v_med3_f16 over every triple (2,744 rows), each in IEEE_MODE 0 and 1 with f16/f64 denormals kept and flushed. 0 mismatches against this PR's rules: with IEEE_MODE=1 the first signalling NaN source (src0 before src1) is returned quieted; with IEEE_MODE=0 a NaN source loses to a non-NaN one; and in both modes two quiet NaN sources give src0, not src1 as the ISA pseudocode has it (with IEEE_MODE=0, any two NaN sources give src0). Rows, results and the fit script: https://gist.github.com/SleglJan/30456e5687240f0b639a78e5477f2f91 (minmax-matrix.json, minmax_fit.py).

AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6069588527 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/533#issuecomment-6069588527). Originally posted by [SleglJan](https://github.com/SleglJan) on 2026-10-08T21:40:52Z. --- Scalar cross-check of the NaN rules, measured on RDNA2 (gfx1036, Ryzen 7950X3D iGPU) with the hardware oracle: `v_min_f64` and `v_max_f64` over every pair of 14 operands (±0, ±1, ±inf, ±denormal, three quiet and three signalling NaNs; 196 rows), and `v_min_f16`, `v_max_f16`, `v_min3_f16`, `v_max3_f16`, `v_med3_f16` over every triple (2,744 rows), each in `IEEE_MODE` 0 and 1 with f16/f64 denormals kept and flushed. 0 mismatches against this PR's rules: with `IEEE_MODE=1` the first signalling NaN source (src0 before src1) is returned quieted; with `IEEE_MODE=0` a NaN source loses to a non-NaN one; and in both modes two quiet NaN sources give src0, not src1 as the ISA pseudocode has it (with `IEEE_MODE=0`, any two NaN sources give src0). Rows, results and the fit script: https://gist.github.com/SleglJan/30456e5687240f0b639a78e5477f2f91 (`minmax-matrix.json`, `minmax_fit.py`). AI-assisted: yes (Claude Code).
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!533
No description provided.