fix(shader): compute v_cvt_pknorm_*_f32, v_ldexp_f32 and v_pack_b32_f16 on the bits #596

Merged
adriadam10 merged 1 commit from upstream-pr/596 into main 2026-10-05 05:11:02 +00:00
adriadam10 commented 2026-10-05 01:29:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-05T01:29:23Z.


What

Found by running decoded-but-never-executed VOP3 ops against the hardware.

  • v_pack_b32_f16: neg/abs were applied to bit 31 of the source dword, not to the selected half. On the low half they were ignored (fails on RADV and lavapipe). The halves are now read with readF16Bits, as the other f16 ops do.
  • v_cvt_pknorm_i16_f32 / v_cvt_pknorm_u16_f32 used GLSL PackSnorm2x16/PackUnorm2x16. The spec leaves the rounding to the driver (round()), and NaN is undefined there. Lavapipe gives 0x8001 for NaN and rounds some ties the other way. Now done on the bits by normF32, the f32 version of normF16: exact mantissa * scale in 64 bits, round to nearest even, NaN to 0, saturate at |x| >= 1.
  • v_ldexp_f32 used GLSL Ldexp, whose NaN payload and denormal results are up to the driver. Lavapipe quiets sNaN and drops the sign of -|NaN|. Now the exponent field is rewritten on the bits.

FPLdexp, PackSnorm2x16 and PackUnorm2x16 had no other users, so this PR removes them from the IR and the SPIR-V backend.

Behaviour measured on HW

Measured on an RDNA2 GPU (gfx1035) with 4096 rows, in the mode the recompiler assumes (IEEE=0, f32 denormals flushed). The Python model matches all 4096 rows:

  • pknorm: NaN → 0. Denormal inputs → 0. |x| >= 1 saturates (snorm -1 → 0x8001). The rest is x * 32767 or x * 65535 rounded once to nearest even. The product is not rounded to f32 first: 376 of the rows differ if it is.
  • ldexp: NaN comes back unchanged (sNaN stays signalling). ±inf comes back unchanged. Denormal and zero inputs give a signed zero. A result exponent >= 255 gives signed inf, and <= 0 gives a signed zero. The exponent operand is the whole signed 32-bit value.
  • pack_b32_f16: neg/abs act on bit 15 of each selected half, and op_sel picks the half.

With IEEE=1 and denormals on, the HW quiets sNaN and returns denormal ldexp results. That mode isn't modelled, same as for the other f32 ops.

Tested

New agc_driver_vop3_pack_ldexp (tests/execution/Vop3PackLdexp.cpp). It runs 32 lanes with the expected values from the HW. The rows cover NaN/sNaN, ±0, denormals, inf, ±1 boundaries, exact rounding ties for both scales, and ldexp exponents reaching 255 and 0. It also runs v_lerp_u8 and v_mul_lo_i32, which no test executed. Both were already correct. gfx1035 has no v_mul_lo_i32, so its expected values are the low 32 bits of the product.

  • Linux, RADV + lavapipe (local) + CI's lavapipe (Mesa 25.2.8). With 1024 HW rows in a temporary version of the test, all values match on both local drivers.
  • On main the test fails: on RADV for v_pack_b32_f16 with neg/abs (lane 0 pack_b32_f169 is 0x0001d40f, expected 0x0001540f), and on lavapipe also for pknorm and ldexp NaN rows. Each piece (pack modifiers, norm sticky bit, tie rule, NaN, ldexp overflow, underflow, NaN, denormal input and exponent clamp) was mutated, and each mutation makes the test fail.
  • Full ctest in Release: all pass except agc_driver_global_load_addtid, agc_driver_pixel_helper_lanes and agc_driver_vop3_carry_out. Those fail on local RADV on main too (wave32 on RADV's 64-wide subgroup).

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:4739104926 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/596). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T01:29:23Z. --- ### What Found by running decoded-but-never-executed VOP3 ops against the hardware. - `v_pack_b32_f16`: neg/abs were applied to bit 31 of the source dword, not to the selected half. On the low half they were ignored (fails on RADV and lavapipe). The halves are now read with `readF16Bits`, as the other f16 ops do. - `v_cvt_pknorm_i16_f32` / `v_cvt_pknorm_u16_f32` used GLSL `PackSnorm2x16`/`PackUnorm2x16`. The spec leaves the rounding to the driver (`round()`), and NaN is undefined there. Lavapipe gives `0x8001` for NaN and rounds some ties the other way. Now done on the bits by `normF32`, the f32 version of `normF16`: exact `mantissa * scale` in 64 bits, round to nearest even, NaN to 0, saturate at |x| >= 1. - `v_ldexp_f32` used GLSL `Ldexp`, whose NaN payload and denormal results are up to the driver. Lavapipe quiets sNaN and drops the sign of `-|NaN|`. Now the exponent field is rewritten on the bits. `FPLdexp`, `PackSnorm2x16` and `PackUnorm2x16` had no other users, so this PR removes them from the IR and the SPIR-V backend. #### Behaviour measured on HW Measured on an RDNA2 GPU (gfx1035) with 4096 rows, in the mode the recompiler assumes (IEEE=0, f32 denormals flushed). The Python model matches all 4096 rows: - pknorm: NaN → 0. Denormal inputs → 0. |x| >= 1 saturates (snorm -1 → `0x8001`). The rest is `x * 32767` or `x * 65535` rounded once to nearest even. The product is not rounded to f32 first: 376 of the rows differ if it is. - ldexp: NaN comes back unchanged (sNaN stays signalling). ±inf comes back unchanged. Denormal and zero inputs give a signed zero. A result exponent >= 255 gives signed inf, and <= 0 gives a signed zero. The exponent operand is the whole signed 32-bit value. - pack_b32_f16: neg/abs act on bit 15 of each selected half, and op_sel picks the half. With IEEE=1 and denormals on, the HW quiets sNaN and returns denormal ldexp results. That mode isn't modelled, same as for the other f32 ops. ### Tested New `agc_driver_vop3_pack_ldexp` (`tests/execution/Vop3PackLdexp.cpp`). It runs 32 lanes with the expected values from the HW. The rows cover NaN/sNaN, ±0, denormals, inf, ±1 boundaries, exact rounding ties for both scales, and ldexp exponents reaching 255 and 0. It also runs `v_lerp_u8` and `v_mul_lo_i32`, which no test executed. Both were already correct. gfx1035 has no `v_mul_lo_i32`, so its expected values are the low 32 bits of the product. - Linux, RADV + lavapipe (local) + CI's lavapipe (Mesa 25.2.8). With 1024 HW rows in a temporary version of the test, all values match on both local drivers. - On `main` the test fails: on RADV for `v_pack_b32_f16` with neg/abs (`lane 0 pack_b32_f169 is 0x0001d40f, expected 0x0001540f`), and on lavapipe also for pknorm and ldexp NaN rows. Each piece (pack modifiers, norm sticky bit, tie rule, NaN, ldexp overflow, underflow, NaN, denormal input and exponent clamp) was mutated, and each mutation makes the test fail. - Full ctest in Release: all pass except `agc_driver_global_load_addtid`, `agc_driver_pixel_helper_lanes` and `agc_driver_vop3_carry_out`. Those fail on local RADV on `main` too (wave32 on RADV's 64-wide subgroup). ### 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
adriadam10 commented 2026-10-05 01:43:05 +00:00 (Migrated from github.com)

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


Rebased on current main (ebf29f8d): conflict was only the adjacent vFmaLegacyF32 declaration from #595 in TranslationContext.hpp, both kept. Commit content otherwise unchanged; agc_driver_vop3_pack_ldexp_tests and the full ctest pass again (RADV + lavapipe).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5986684696 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/596#issuecomment-5986684696). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-05T01:43:05Z. --- Rebased on current main (ebf29f8d): conflict was only the adjacent `vFmaLegacyF32` declaration from #595 in `TranslationContext.hpp`, both kept. Commit content otherwise unchanged; `agc_driver_vop3_pack_ldexp_tests` and the full ctest pass again (RADV + lavapipe).
oneandonlydean commented 2026-10-05 02:47:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T02:47:22Z.


Thanks, this checks out. I disassembled the test kernel with llvm-mc -mcpu=gfx1013 and every word is what the comments say: both pknorm forms (with -v4, |v5| and -|v4|, -v5), v_ldexp_f32 with -|v4|, v_pack_b32_f16 with neg/abs and both op_sel cases, v_lerp_u8 and v_mul_lo_i32.

normF32 keeps the whole mantissa * scale product in 64 bits with one guard bit and a sticky bit, so the rounding is a single round to nearest even, and NaN, saturation, the snorm 0x8001 minimum and the flushed denormals all come out as you measured. vLdexpF32 rewrites the exponent field with the offset clamped to 512 either way, which covers every exponent, keeps NaN and inf unchanged, and still goes through the F32 result path, so clamp and omod behave as before. readU32 still applies abs and neg on bit 31, so the pknorm and ldexp source modifiers keep working. The v_pack_b32_f16 fix is a real bug: main applied neg and abs to bit 31 of the dword, so on the low half they were lost. Nothing else on main or in the other open PRs uses FPLdexp, PackSnorm2x16 or PackUnorm2x16, so removing them is fine, and the branch merges cleanly with your other open shader PRs.

One note for the merge order rather than the code: PPSA21564 uses v_cvt_pknorm_i16_f32 and v_cvt_pknorm_u16_f32 a lot (891 of the 5,688 shaders I have dumped, all without modifiers). For ordinary inputs only exact ties and NaN change, but since it touches so many shaders I'll run it through my visual check of PPSA21564 before it goes in. v_ldexp_f32 and v_pack_b32_f16 don't appear there at all.

Ready to merge from my side once that run is done.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5987229819 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/596#issuecomment-5987229819). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T02:47:22Z. --- Thanks, this checks out. I disassembled the test kernel with `llvm-mc -mcpu=gfx1013` and every word is what the comments say: both pknorm forms (with `-v4, |v5|` and `-|v4|, -v5`), `v_ldexp_f32` with `-|v4|`, `v_pack_b32_f16` with neg/abs and both op_sel cases, `v_lerp_u8` and `v_mul_lo_i32`. `normF32` keeps the whole `mantissa * scale` product in 64 bits with one guard bit and a sticky bit, so the rounding is a single round to nearest even, and NaN, saturation, the snorm `0x8001` minimum and the flushed denormals all come out as you measured. `vLdexpF32` rewrites the exponent field with the offset clamped to 512 either way, which covers every exponent, keeps NaN and inf unchanged, and still goes through the F32 result path, so clamp and omod behave as before. `readU32` still applies abs and neg on bit 31, so the pknorm and ldexp source modifiers keep working. The `v_pack_b32_f16` fix is a real bug: main applied neg and abs to bit 31 of the dword, so on the low half they were lost. Nothing else on main or in the other open PRs uses `FPLdexp`, `PackSnorm2x16` or `PackUnorm2x16`, so removing them is fine, and the branch merges cleanly with your other open shader PRs. One note for the merge order rather than the code: PPSA21564 uses `v_cvt_pknorm_i16_f32` and `v_cvt_pknorm_u16_f32` a lot (891 of the 5,688 shaders I have dumped, all without modifiers). For ordinary inputs only exact ties and NaN change, but since it touches so many shaders I'll run it through my visual check of PPSA21564 before it goes in. `v_ldexp_f32` and `v_pack_b32_f16` don't appear there at all. Ready to merge from my side once that run is done.
oneandonlydean commented 2026-10-05 05:04:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-05T05:04:09Z.


Gated on PPSA21564: no visual or FPS change.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:5988443210 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/596#issuecomment-5988443210). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-05T05:04:09Z. --- Gated on PPSA21564: no visual or FPS change.
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!596
No description provided.