Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:fix/vop3-pack-ldexp-bits
Oct 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
adriadam10:fix/vop3-pack-ldexp-bits

Conversation

@adriadam10

Copy link
Copy Markdown
Collaborator

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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
…16 on the bits

v_pack_b32_f16 applied neg/abs to bit 31 of the source dword instead of the
selected half. v_cvt_pknorm_{i16,u16}_f32 and v_ldexp_f32 used GLSL
PackSnorm2x16/PackUnorm2x16/Ldexp, which leave NaN, rounding ties and
denormals to the driver. Compute them on the bits as measured on RDNA2.

Co-Authored-By: Claude <noreply@anthropic.com>
@adriadam10

Copy link
Copy Markdown
Collaborator Author

Rebased on current main (ebf29f8): 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).

@adriadam10
adriadam10 force-pushed the fix/vop3-pack-ldexp-bits branch from fd487f4 to a4482e0 Compare October 5, 2026 01:43
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 5, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

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

Copy link
Copy Markdown
Collaborator

Gated on PPSA21564: no visual or FPS change.

@oneandonlydean
oneandonlydean merged commit e90d2ed into boykopovar:main Oct 5, 2026
3 checks passed
@adriadam10
adriadam10 deleted the fix/vop3-pack-ldexp-bits branch October 5, 2026 06:47
oneandonlydean added a commit to oneandonlydean/AnyPS5 that referenced this pull request Oct 5, 2026
Conflicts resolved:
- libSceAudioPropagation/Export.cpp: upstream's boykopovar#482 + boykopovar#489 version taken; it is
  the same no-propagation model as astrobot's stand-in, with SourceRender and the
  handle-only UnregisterMaterial already in it.
- State.cpp (boykopovar#453): astrobot's resident depth model kept; it already skips the
  tests of an absent plane (boykopovar#453's test is on astrobot since e087a4f).
- Graphics.cpp, State.hpp, ShaderResources.cpp: astrobot's DepthTarget model kept
  (exportIndex, no DepthSurface.hpp), upstream's ThreadOwned include added.
- BdaResources.cpp: astrobot's form with boykopovar#390's message.
- VulkanDevice.cpp, Context.hpp, PacketExecution.cpp: both sides kept
  (calibrated timestamps, host query reset, GDS, memory budget; pipeline stats).
- Spirv: astrobot's split-wave rules first, then boykopovar#608's wave-mask ballot and
  boykopovar#587's host subgroup lane; astrobot's function LDS sizing and boykopovar#573's LDS lock
  both kept; conditionalDepth kept in the atomic update loops; upstream's
  buffer atomics (boykopovar#321 boykopovar#332 boykopovar#594) and 64-bit LDS atomics taken.
- TechnicalDebt.md: both sides, duplicate lines dropped.


Build fixes folded in: the upstream boykopovar#453 test block duplicated next to
astrobot's adapted copy removed; a lost closing brace after the host
query reset block restored; lookupLog() is a function since boykopovar#590; the
duplicate MUTEX_TYPE_ADAPTIVE constant (astrobot's 93b2d0e equals boykopovar#156)
removed; the ops boykopovar#596 drops removed from the masked-select list and the
BDA test message updated (the gate-batch-2 integ commits 559e4fab6 and
2ec968fb5).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants