fix(shader): VOP3P integer clamp on v_pk_mad/add/sub_{i16,u16} #502

Merged
adriadam10 merged 1 commit from upstream-pr/502 into main 2026-10-04 21:10:55 +00:00
adriadam10 commented 2026-10-04 10:34:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-04T10:34:09Z.


What

The VOP3P clamp bit threw VOP3P integer clamp is not implemented on every packed integer op. It now saturates each 16-bit half on v_pk_mad_i16, v_pk_mad_u16, v_pk_add_i16, v_pk_add_u16, v_pk_sub_i16 and v_pk_sub_u16: the result is computed at full width (signed sources sign-extended, unsigned zero-extended) and limited to [-32768, 32767] or [0, 65535]. On v_pk_mul_lo_u16 the bit is accepted and has no effect, as on the hardware. Clamp on the other packed integer ops (min/max/shifts) and on the integer dot ops still throws.

Behaviour measured on HW (RDNA2, gfx1035)

  • mad/add/sub i16/u16 with clamp: per-half saturation of the exact result (0xffff * 0xffff + c → 0xffff, 0 - 1 → 0 on u16, -32768 * -1 + 0 → 0x7fff on i16); op_sel/op_sel_hi pick the halves as without clamp.
  • v_pk_mul_lo_u16 with clamp: low 16 bits of the product, same as without it.

Tested

New execution test agc_driver_packed_integer_clamp_tests: the six ops with clamp, v_pk_mul_lo_u16 with clamp, mad without clamp, and op_sel/op_sel_hi variants (16 instructions, 32 rows picked so each half reaches above-range, in-range and below-range results). Expected values come from the hardware, and the Python model matches them in all 512 cells.
Passes on RADV and lavapipe. The test fails without the fix: dropping the clamp, zero-extending the signed sources, not bounding the u16 product before the add (the 32-bit sum wraps), wrong unsigned bounds, and throwing on v_pk_mul_lo_u16 all fail. Full Release ctest is green except agc_driver_pixel_helper_lanes, which also fails on main on local RADV.

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: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4733677018 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/502). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-04T10:34:09Z. --- ### What The VOP3P `clamp` bit threw `VOP3P integer clamp is not implemented` on every packed integer op. It now saturates each 16-bit half on `v_pk_mad_i16`, `v_pk_mad_u16`, `v_pk_add_i16`, `v_pk_add_u16`, `v_pk_sub_i16` and `v_pk_sub_u16`: the result is computed at full width (signed sources sign-extended, unsigned zero-extended) and limited to `[-32768, 32767]` or `[0, 65535]`. On `v_pk_mul_lo_u16` the bit is accepted and has no effect, as on the hardware. Clamp on the other packed integer ops (min/max/shifts) and on the integer dot ops still throws. #### Behaviour measured on HW (RDNA2, gfx1035) - mad/add/sub i16/u16 with `clamp`: per-half saturation of the exact result (`0xffff * 0xffff + c` → `0xffff`, `0 - 1` → `0` on u16, `-32768 * -1 + 0` → `0x7fff` on i16); `op_sel`/`op_sel_hi` pick the halves as without clamp. - `v_pk_mul_lo_u16` with `clamp`: low 16 bits of the product, same as without it. ### Tested New execution test `agc_driver_packed_integer_clamp_tests`: the six ops with `clamp`, `v_pk_mul_lo_u16` with `clamp`, mad without `clamp`, and `op_sel`/`op_sel_hi` variants (16 instructions, 32 rows picked so each half reaches above-range, in-range and below-range results). Expected values come from the hardware, and the Python model matches them in all 512 cells. Passes on RADV and lavapipe. The test fails without the fix: dropping the clamp, zero-extending the signed sources, not bounding the u16 product before the add (the 32-bit sum wraps), wrong unsigned bounds, and throwing on `v_pk_mul_lo_u16` all fail. Full Release ctest is green except `agc_driver_pixel_helper_lanes`, which also fails on `main` on local RADV. ### 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: none - [x] AI-assisted: yes
oneandonlydean commented 2026-10-04 21:07:57 +00:00 (Migrated from github.com)

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


Thanks, checked the bounds: add and sub fit in 32 bits for both signednesses, the u16 mad bounds the product to 0xffff first (needed, since u16 times u16 can pass 2^31), and the i16 mad stays below 2^31. LLVM selects v_pk_add/sub_{u,i}16 with clamp for uaddsat/saddsat/usubsat/ssubsat (VOP3PSatPat in VOP3PInstructions.td), which matches the per-half saturation. Without clamp the sign-extended sources give the same low 16 bits and packU16Lanes masks them, so existing results don't change. Ready to merge from my side.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5984395754 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/502#issuecomment-5984395754). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-04T21:07:57Z. --- Thanks, checked the bounds: add and sub fit in 32 bits for both signednesses, the u16 mad bounds the product to 0xffff first (needed, since u16 times u16 can pass 2^31), and the i16 mad stays below 2^31. LLVM selects `v_pk_add/sub_{u,i}16` with clamp for `uaddsat`/`saddsat`/`usubsat`/`ssubsat` (`VOP3PSatPat` in VOP3PInstructions.td), which matches the per-half saturation. Without clamp the sign-extended sources give the same low 16 bits and `packU16Lanes` masks them, so existing results don't change. Ready to merge from my side.
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!502
No description provided.