feat(shader): f16 ldexp/frexp/norm/class, v_sat_pk_u8_i16, v_mul/mac_legacy_f32 #318

Merged
adriadam10 merged 4 commits from upstream-pr/318 into main 2026-10-03 07:31:55 +00:00
adriadam10 commented 2026-10-02 04:46:30 +00:00 (Migrated from github.com)

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


What

These threw in the decoder:

  • VOP2 v_ldexp_f16, v_mul_legacy_f32, v_mac_legacy_f32
  • VOP1 v_frexp_mant_f16, v_frexp_exp_i16_f16, v_cvt_norm_i16_f16, v_cvt_norm_u16_f16, v_sat_pk_u8_i16
  • VOP3 v_cvt_pknorm_i16_f16, v_cvt_pknorm_u16_f16
  • VOPC v_cmp_class_f16, v_cmpx_class_f16

The reference emulators don't cover these, so I measured them on an RDNA2 GPU (Radeon 660M, gfx1035; all exist on gfx1013 too) with raw kernels through the ROCm HSA runtime. A Python model of each matched 2048/2048 hardware results before I wrote the C++:

  • v_ldexp_f16: the exponent is src1 as an i16 (low 16 bits, sign-extended). The translation clamps it to ±64 and multiplies in f32, which is exact, then converts once.
  • v_frexp_mant_f16 / v_frexp_exp_i16_f16: zero and ±inf pass through with exponent 0; NaN is quieted.
  • v_cvt_norm_*_f16 and v_cvt_pknorm_*_f16: clamp, ×32767 or ×65535, round to nearest even; NaN gives 0. Done in integer arithmetic, because x * 32767 needs up to 26 significant bits and f32 could move a value onto a tie.
  • v_cmp_class_f16: the 10 class bits of v_cmp_class_f32, read from the f16 bits.
  • v_sat_pk_u8_i16: both i16 halves saturated to [0, 255] and packed into the low 16 bits; the upper half is zeroed.
  • v_mul_legacy_f32: if either factor is ±0 the result is +0, even when the other is inf or NaN. v_mac_legacy_f32 adds that rounded product to D, like v_mac_f32. That one can't be checked on gfx10.3 hardware and is checked against the model.
  • f16 results of VOP1/VOP2 keep the upper 16 bits of the destination, which the existing write16Bits already does.

v_mad_legacy_f32 is left out because #308 implements it (I left a note there about the legacy zero rule).

Test agc_driver_float16_misc: 256 input vectors with the expected results of all of the above, captured on the RDNA2 GPU, plus v_mac_legacy_f32 from the model. They include f16 NaN/inf/zero/subnormal edges, exponent edges for ldexp, random class masks, and 0×inf/NaN for the legacy multiply. f16 NaNs are compared as NaN. The f32 legacy rows with denormals are skipped, since those follow the driver's float controls.

Tested

Linux, full ctest (101) passes.

  • RADV (Radeon 660M) and lavapipe: the test passes.
  • Without the change it fails with "VOP2 opcode is not implemented".

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), #317 (on RADV, f16 overflow must round to infinity), #263 (the mac add must not fuse); review only the last commit
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4712787378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/318). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-02T04:46:30Z. --- ### What These threw in the decoder: - VOP2 `v_ldexp_f16`, `v_mul_legacy_f32`, `v_mac_legacy_f32` - VOP1 `v_frexp_mant_f16`, `v_frexp_exp_i16_f16`, `v_cvt_norm_i16_f16`, `v_cvt_norm_u16_f16`, `v_sat_pk_u8_i16` - VOP3 `v_cvt_pknorm_i16_f16`, `v_cvt_pknorm_u16_f16` - VOPC `v_cmp_class_f16`, `v_cmpx_class_f16` The reference emulators don't cover these, so I measured them on an RDNA2 GPU (Radeon 660M, gfx1035; all exist on gfx1013 too) with raw kernels through the ROCm HSA runtime. A Python model of each matched **2048/2048** hardware results before I wrote the C++: - `v_ldexp_f16`: the exponent is src1 as an **i16** (low 16 bits, sign-extended). The translation clamps it to ±64 and multiplies in f32, which is exact, then converts once. - `v_frexp_mant_f16` / `v_frexp_exp_i16_f16`: zero and ±inf pass through with exponent 0; NaN is quieted. - `v_cvt_norm_*_f16` and `v_cvt_pknorm_*_f16`: clamp, ×32767 or ×65535, round to nearest even; NaN gives 0. Done in **integer** arithmetic, because `x * 32767` needs up to 26 significant bits and f32 could move a value onto a tie. - `v_cmp_class_f16`: the 10 class bits of `v_cmp_class_f32`, read from the f16 bits. - `v_sat_pk_u8_i16`: both i16 halves saturated to `[0, 255]` and packed into the low 16 bits; the upper half is zeroed. - `v_mul_legacy_f32`: if either factor is ±0 the result is **+0**, even when the other is inf or NaN. `v_mac_legacy_f32` adds that rounded product to D, like `v_mac_f32`. That one can't be checked on gfx10.3 hardware and is checked against the model. - f16 results of VOP1/VOP2 keep the upper 16 bits of the destination, which the existing `write16Bits` already does. `v_mad_legacy_f32` is left out because #308 implements it (I left a note there about the legacy zero rule). Test `agc_driver_float16_misc`: 256 input vectors with the expected results of all of the above, **captured on the RDNA2 GPU**, plus `v_mac_legacy_f32` from the model. They include f16 NaN/inf/zero/subnormal edges, exponent edges for ldexp, random class masks, and 0×inf/NaN for the legacy multiply. f16 NaNs are compared as NaN. The f32 legacy rows with denormals are skipped, since those follow the driver's float controls. ### Tested Linux, full ctest (101) passes. - RADV (Radeon 660M) and lavapipe: the test passes. - Without the change it fails with "VOP2 opcode is not implemented". ### 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), #317 (on RADV, f16 overflow must round to infinity), #263 (the mac add must not fuse); review only the last commit - [x] AI-assisted: yes
github-actions[bot] commented 2026-10-02 04:46:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T04:46:51Z.


Progress report (709d7fe → 8fd19cd)

📈 GPU shader instructions: 53.69% (+1.03%, +12 implemented instructions)

✅ 12 implemented
Encoding Name
VOP1 V_CVT_NORM_I16_F16
VOP1 V_CVT_NORM_U16_F16
VOP1 V_FREXP_EXP_I16_F16
VOP1 V_FREXP_MANT_F16
VOP1 V_SAT_PK_U8_I16
VOP2 V_LDEXP_F16
VOP2 V_MAC_LEGACY_F32
VOP2 V_MUL_LEGACY_F32
VOP3 V_CVT_PKNORM_I16_F16
VOP3 V_CVT_PKNORM_U16_F16
VOPC V_CMPX_CLASS_F16
VOPC V_CMP_CLASS_F16
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5945771838 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/318#issuecomment-5945771838). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T04:46:51Z. --- <!-- progress-report --> ### Progress report (709d7fe → 8fd19cd) 📈 **GPU shader instructions**: 53.69% (+1.03%, +12 implemented instructions) <details> <summary>✅ 12 implemented</summary> | Encoding | Name | | - | - | | VOP1 | `V_CVT_NORM_I16_F16` | | VOP1 | `V_CVT_NORM_U16_F16` | | VOP1 | `V_FREXP_EXP_I16_F16` | | VOP1 | `V_FREXP_MANT_F16` | | VOP1 | `V_SAT_PK_U8_I16` | | VOP2 | `V_LDEXP_F16` | | VOP2 | `V_MAC_LEGACY_F32` | | VOP2 | `V_MUL_LEGACY_F32` | | VOP3 | `V_CVT_PKNORM_I16_F16` | | VOP3 | `V_CVT_PKNORM_U16_F16` | | VOPC | `V_CMPX_CLASS_F16` | | VOPC | `V_CMP_CLASS_F16` | </details>
github-actions[bot] commented 2026-10-02 06:46:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-02T06:46:20Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#264 core/libs/prx/libSceAgcDriver/tests/execution/FmaRounding.cpp
#323 core/libs/prx/libSceAgcDriver/tests/execution/Float16Rounding.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5946911736 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/318#issuecomment-5946911736). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-02T06:46:20Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #264 | | | `core/libs/prx/libSceAgcDriver/tests/execution/FmaRounding.cpp` | | #323 | | | `core/libs/prx/libSceAgcDriver/tests/execution/Float16Rounding.cpp` |
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!318
No description provided.