shader: v_mad_u32_u16 and v_mad_i32_i16 ignore op_sel on the addend and the result #10772
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by TechLevelZero on 2026-10-24T23:44:19Z.
What happens
For
v_mad_u32_u16andv_mad_i32_i16, the recompiler honoursop_selbits 0 and 1 (which half of each 16-bit factor is used) and ignores bits 2 and 3. RDNA2 honours both:v_mad_i32_i16, which does not sign-extend it.For example,
v_mad_u32_u16 v10, v4, v5, v6 op_sel:[0,0,1,0]withv4 = 0x00030002,v5 = 0x00050007andv6 = 0x80000001gives0x0000800e(2 × 7 + 0x8000) on hardware and0x8000000f(2 × 7 + 0x80000001) in AnyPS5.The decoder drops both bits on purpose (
applyNativeVop3I16TernarySelectors,RdnaVectorOpDecoder.cpp).e144f43f05reasoned that "the u32_u16/i32_i16 forms take a 32-bit addend and write all 32 bits, so they do not get the 16-bit destination select". It cites no measurement.Title, test or build step
Shader recompiler. Found by differential testing: random RDNA programs run on RDNA2 hardware through
tools/hw-oracleand through the recompiler on Vulkan, with the results compared word by word. A sweep of every non-packed VOP3 instruction that acceptsop_sel, each bit on its own, found only these two instructions differing (145 cases, 138 match; the other 3 are refusals).AnyPS5 commit
mainc8d34aa8f4.System
tools/hw-oracle, ROCm 7.1.1, wave32.ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.The PS5's gfx1013 is not measured.
Steps to reproduce
body.s:The recompiler gives
8000000f 8000000f.Log
Hardware results. Each operand is
lo/hi16 bits:v_mad_u32_u16v_mad_i32_i160x00030002, 0x00050007, 0x800000018000000f8000000f[0,0,1,0]0000800e0000800e[0,0,0,1]000f0000000f0000[1,1,1,1]800f0000(3 × 5 + 0x8000)800f00000xffff0002, 0x0003fffe, 0x7fff8000[0,0,1,0]00027ffb00007ffb(2 × -2 + 0x7fff)0x00020002, 0x00020002, 0xfffe0005[0,0,1,0]0001000200010002(zero-extended 0xfffe)0x00030002, 0x00050007, literal0x12345678[0,0,1,0]00001242000012420xffffffff, 0xffffffff, 0xffffffff[0,0,0,1] clampffff0000(sum saturates tofffffffffirst)000000000x7fff7fff, 0x7fff7fff, 0x7fffffff[0,0,0,1] clamp00000000ffff0000(sum saturates to7ffffffffirst)What was checked
op_selmatch the hardware for every bit.A fix with an execution test built from these measurements is on
TechLevelZero/AnyPS5branchfix/vmad-u32-u16-op-sel. I'll open it as a pull request once my first one is merged (open-PR limit).AI-assisted: yes (Claude Code)