shader: v_mad_u32_u16 and v_mad_i32_i16 ignore op_sel on the addend and the result #10772

Closed
opened 2026-10-24 23:44:19 +00:00 by TechLevelZero · 0 comments
TechLevelZero commented 2026-10-24 23:44:19 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by TechLevelZero on 2026-10-24T23:44:19Z.


What happens
For v_mad_u32_u16 and v_mad_i32_i16, the recompiler honours op_sel bits 0 and 1 (which half of each 16-bit factor is used) and ignores bits 2 and 3. RDNA2 honours both:

  • Bit 2: the addend is the high 16 bits of src2, zero-extended. This is the same for v_mad_i32_i16, which does not sign-extend it.
  • Bit 3: the destination gets the low 16 bits of the (clamped) sum in its high half, and its low half is cleared.

For example, v_mad_u32_u16 v10, v4, v5, v6 op_sel:[0,0,1,0] with v4 = 0x00030002, v5 = 0x00050007 and v6 = 0x80000001 gives 0x0000800e (2 × 7 + 0x8000) on hardware and 0x8000000f (2 × 7 + 0x80000001) in AnyPS5.

The decoder drops both bits on purpose (applyNativeVop3I16TernarySelectors, RdnaVectorOpDecoder.cpp). e144f43f05 reasoned 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-oracle and through the recompiler on Vulkan, with the results compared word by word. A sweep of every non-packed VOP3 instruction that accepts op_sel, each bit on its own, found only these two instructions differing (145 cases, 138 match; the other 3 are refusals).

AnyPS5 commit
main c8d34aa8f4.

System

  • Hardware: the integrated Radeon Graphics of a Ryzen 7 9700X (gfx1036, RDNA2) through tools/hw-oracle, ROCm 7.1.1, wave32.
  • AnyPS5: an execution-test-style runner on an RX 9070 XT (RADV, Mesa 26.0.8), ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1.

The PS5's gfx1013 is not measured.

Steps to reproduce
body.s:

  v_mov_b32 v10, 0xaaaabbbb
  v_mad_u32_u16 v10, v4, v5, v6 op_sel:[0,0,1,0]
  v_mad_u32_u16 v11, v4, v5, v6 op_sel:[0,0,0,1]
echo "0x00030002 0x00050007 0x80000001 0" > rows.txt
python3 tools/hw-oracle/hw_oracle.py body.s rows.txt --ieee 0 --denorm32 0 --denorm16 3 --dx10-clamp 1 --round32 0 --round16 0 --fp16-overflow 0 --outs 2
0000800e 000f0000

The recompiler gives 8000000f 8000000f.

Log
Hardware results. Each operand is lo / hi 16 bits:

v4, v5, src2 op_sel v_mad_u32_u16 v_mad_i32_i16
0x00030002, 0x00050007, 0x80000001 none 8000000f 8000000f
same [0,0,1,0] 0000800e 0000800e
same [0,0,0,1] 000f0000 000f0000
same [1,1,1,1] 800f0000 (3 × 5 + 0x8000) 800f0000
0xffff0002, 0x0003fffe, 0x7fff8000 [0,0,1,0] 00027ffb 00007ffb (2 × -2 + 0x7fff)
0x00020002, 0x00020002, 0xfffe0005 [0,0,1,0] 00010002 00010002 (zero-extended 0xfffe)
0x00030002, 0x00050007, literal 0x12345678 [0,0,1,0] 00001242 00001242
0xffffffff, 0xffffffff, 0xffffffff [0,0,0,1] clamp ffff0000 (sum saturates to ffffffff first) 00000000
0x7fff7fff, 0x7fff7fff, 0x7fffffff [0,0,0,1] clamp 00000000 ffff0000 (sum saturates to 7fffffff first)

What was checked

  • Wave32, every lane the same. The results repeat across runs.
  • The other 30 non-packed VOP3 instructions that accept op_sel match the hardware for every bit.
  • Not checked: wave64, SGPR addends, the PS5's gfx1013, RDNA1.

A fix with an execution test built from these measurements is on TechLevelZero/AnyPS5 branch fix/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)

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6018888097 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/10772). Originally posted by [TechLevelZero](https://github.com/TechLevelZero) on 2026-10-24T23:44:19Z. --- **What happens** For `v_mad_u32_u16` and `v_mad_i32_i16`, the recompiler honours `op_sel` bits 0 and 1 (which half of each 16-bit factor is used) and ignores bits 2 and 3. RDNA2 honours both: - Bit 2: the addend is the high 16 bits of src2, zero-extended. This is the same for `v_mad_i32_i16`, which does not sign-extend it. - Bit 3: the destination gets the low 16 bits of the (clamped) sum in its high half, and its low half is cleared. For example, `v_mad_u32_u16 v10, v4, v5, v6 op_sel:[0,0,1,0]` with `v4 = 0x00030002`, `v5 = 0x00050007` and `v6 = 0x80000001` gives `0x0000800e` (2 × 7 + 0x8000) on hardware and `0x8000000f` (2 × 7 + 0x80000001) in AnyPS5. The decoder drops both bits on purpose (`applyNativeVop3I16TernarySelectors`, `RdnaVectorOpDecoder.cpp`). e144f43f05 reasoned 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-oracle` and through the recompiler on Vulkan, with the results compared word by word. A sweep of every non-packed VOP3 instruction that accepts `op_sel`, each bit on its own, found only these two instructions differing (145 cases, 138 match; the other 3 are refusals). **AnyPS5 commit** `main` c8d34aa8f4. **System** - Hardware: the integrated Radeon Graphics of a Ryzen 7 9700X (gfx1036, RDNA2) through `tools/hw-oracle`, ROCm 7.1.1, wave32. - AnyPS5: an execution-test-style runner on an RX 9070 XT (RADV, Mesa 26.0.8), `ANYPS5_REQUIRE_VULKAN=1 ANYPS5_NO_SHADER_CACHE=1`. The PS5's gfx1013 is not measured. **Steps to reproduce** `body.s`: ``` v_mov_b32 v10, 0xaaaabbbb v_mad_u32_u16 v10, v4, v5, v6 op_sel:[0,0,1,0] v_mad_u32_u16 v11, v4, v5, v6 op_sel:[0,0,0,1] ``` ``` echo "0x00030002 0x00050007 0x80000001 0" > rows.txt python3 tools/hw-oracle/hw_oracle.py body.s rows.txt --ieee 0 --denorm32 0 --denorm16 3 --dx10-clamp 1 --round32 0 --round16 0 --fp16-overflow 0 --outs 2 0000800e 000f0000 ``` The recompiler gives `8000000f 8000000f`. **Log** Hardware results. Each operand is `lo` / `hi` 16 bits: | v4, v5, src2 | op_sel | `v_mad_u32_u16` | `v_mad_i32_i16` | | --- | --- | --- | --- | | `0x00030002, 0x00050007, 0x80000001` | none | `8000000f` | `8000000f` | | same | `[0,0,1,0]` | `0000800e` | `0000800e` | | same | `[0,0,0,1]` | `000f0000` | `000f0000` | | same | `[1,1,1,1]` | `800f0000` (3 × 5 + 0x8000) | `800f0000` | | `0xffff0002, 0x0003fffe, 0x7fff8000` | `[0,0,1,0]` | `00027ffb` | `00007ffb` (2 × -2 + 0x7fff) | | `0x00020002, 0x00020002, 0xfffe0005` | `[0,0,1,0]` | `00010002` | `00010002` (zero-extended 0xfffe) | | `0x00030002, 0x00050007`, literal `0x12345678` | `[0,0,1,0]` | `00001242` | `00001242` | | `0xffffffff, 0xffffffff, 0xffffffff` | `[0,0,0,1] clamp` | `ffff0000` (sum saturates to `ffffffff` first) | `00000000` | | `0x7fff7fff, 0x7fff7fff, 0x7fffffff` | `[0,0,0,1] clamp` | `00000000` | `ffff0000` (sum saturates to `7fffffff` first) | **What was checked** - Wave32, every lane the same. The results repeat across runs. - The other 30 non-packed VOP3 instructions that accept `op_sel` match the hardware for every bit. - Not checked: wave64, SGPR addends, the PS5's gfx1013, RDNA1. A fix with an execution test built from these measurements is on `TechLevelZero/AnyPS5` branch [`fix/vmad-u32-u16-op-sel`](https://github.com/TechLevelZero/AnyPS5/tree/fix/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)
Sign in to join this conversation.
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#10772
No description provided.