tombox-xshader: decode and translate v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8 and v_mqsad_u32_u8 #247

Closed
github-bot wants to merge 1 commit from gh-pr/247/head into gh-pr/247/base
Owner

@⁠azerioxfr on GitHub · 2026-11-23 07:21 UTC · original
azerioxfr:xshader-vop3-qsad-mqsad → main · +159 −1 · 4 files · 1 commits · closed without merging

Library / area: tombox-xshader, VOP3 multi-SAD ops (hlsl.rs next to v_sad_* / v_msad_u8)

Exports changed (exports.txt rows): none

Source of the behaviour: opcodes from tests/isa_table/gfx1013.txt (VOP3 0x172, 0x173, 0x175). Semantics from the RDNA2 ISA definitions. The masked form skips zero bytes of S1, as the existing v_msad_u8 lowering does.

One topic, +159 / -1: 2 lines in rdna2/mnem.rs, 83 in hlsl.rs, the rest tests.

What was missing

tests/isa_table.rs lists these three under "vop3: gfx1013 opcodes not named by the decoder".

The change

opcode lowering
v_qsad_pk_u16_u8 (0x172) four SAD4 of the byte slices S0[31:0], S0[39:8], S0[47:16], S0[55:24] (64-bit S0) against S1, each plus one 16-bit field of the 64-bit S2, truncated to 16 bits and packed into the 64-bit D
v_mqsad_pk_u16_u8 (0x173) the same with the masked SAD4 (a zero byte of S1 drops that byte)
v_mqsad_u32_u8 (0x175) the four masked sums, each plus one dword of the 128-bit S2, into the 128-bit D. CLAMP saturates each 32-bit sum, as v_sad_u32 / v_msad_u8 do here.

CLAMP on the two packed 16-bit forms is refused with an explicit error, because the hardware saturation width for the 16-bit fields is not established. Sources are copied to temporaries first, so a destination that overlaps a source reads the issue-time values.

Tests

run (WARP, --release, DXC 1.9.2609) result
sad_quad_packed (Rust reference model: cross-dword slices, zero reference bytes, 16-bit wrap, 32-bit wrap with CLAMP on v_mqsad_u32_u8) 3 mnemonics verified, 0 failing, 20 cases
mutation: S0 byte selection reads the high dword one byte early fails
--test opcodes (translation rows + new packed_multi_sad_refuses_clamp) 27 passed
--test isa_table 3 passed

Run on x86_64-pc-windows-gnu (no MSVC linker here). Integer only.

  • cargo test -p tombox-xshader --test exec -- --exact sad_quad_packed passes
  • No game or console files and no binaries included
  • No fake success; anything not possible is left missing with a note
**@⁠azerioxfr** on GitHub · 2026-11-23 07:21 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/247) `azerioxfr:xshader-vop3-qsad-mqsad` → `main` · +159 −1 · 4 files · 1 commits · closed without merging **Library / area:** tombox-xshader, VOP3 multi-SAD ops (`hlsl.rs` next to `v_sad_*` / `v_msad_u8`) **Exports changed (exports.txt rows):** none **Source of the behaviour:** opcodes from `tests/isa_table/gfx1013.txt` (VOP3 0x172, 0x173, 0x175). Semantics from the RDNA2 ISA definitions. The masked form skips zero bytes of S1, as the existing `v_msad_u8` lowering does. One topic, +159 / -1: 2 lines in `rdna2/mnem.rs`, 83 in `hlsl.rs`, the rest tests. ### What was missing `tests/isa_table.rs` lists these three under "vop3: gfx1013 opcodes not named by the decoder". ### The change | opcode | lowering | |---|---| | `v_qsad_pk_u16_u8` (0x172) | four SAD4 of the byte slices S0[31:0], S0[39:8], S0[47:16], S0[55:24] (64-bit S0) against S1, each plus one 16-bit field of the 64-bit S2, truncated to 16 bits and packed into the 64-bit D | | `v_mqsad_pk_u16_u8` (0x173) | the same with the masked SAD4 (a zero byte of S1 drops that byte) | | `v_mqsad_u32_u8` (0x175) | the four masked sums, each plus one dword of the 128-bit S2, into the 128-bit D. CLAMP saturates each 32-bit sum, as `v_sad_u32` / `v_msad_u8` do here. | CLAMP on the two packed 16-bit forms is refused with an explicit error, because the hardware saturation width for the 16-bit fields is not established. Sources are copied to temporaries first, so a destination that overlaps a source reads the issue-time values. ### Tests | run (WARP, `--release`, DXC 1.9.2609) | result | |---|---| | `sad_quad_packed` (Rust reference model: cross-dword slices, zero reference bytes, 16-bit wrap, 32-bit wrap with CLAMP on `v_mqsad_u32_u8`) | **3 mnemonics verified, 0 failing, 20 cases** | | mutation: S0 byte selection reads the high dword one byte early | fails | | `--test opcodes` (translation rows + new `packed_multi_sad_refuses_clamp`) | 27 passed | | `--test isa_table` | 3 passed | Run on `x86_64-pc-windows-gnu` (no MSVC linker here). Integer only. - [x] `cargo test -p tombox-xshader --test exec -- --exact sad_quad_packed` passes - [x] No game or console files and no binaries included - [x] No fake success; anything not possible is left `missing` with a note
VOP3 opcodes 0x172, 0x173 and 0x175 were not named (the isa_table test
lists them under "vop3: gfx1013 opcodes not named by the decoder").
They are the quad and quad-masked byte SAD forms:

- v_qsad_pk_u16_u8 (0x172): S0 is a 64-bit source read as eight bytes;
  four SAD4 operations use the byte slices S0[31:0], S0[39:8],
  S0[47:16] and S0[55:24] against the whole 32-bit S1, each added to one
  of the four 16-bit fields of the 64-bit S2; each sum is truncated to
  16 bits and the four are packed into the 64-bit D.
- v_mqsad_pk_u16_u8 (0x173): the same packing with the masked SAD4: a
  reference byte of S1 equal to zero drops that byte from the sum (as
  the existing v_msad_u8 lowering does).
- v_mqsad_u32_u8 (0x175): the same four masked SAD4 sums, each added to
  one dword of the 128-bit S2 and kept as a full dword of the 128-bit D.
  CLAMP saturates each 32-bit sum, as v_sad_u32 / v_msad_u8 do here.

CLAMP on the two packed 16-bit forms is refused with an explicit error:
a 32-bit saturation never triggers there and the hardware saturation
width for the 16-bit fields is not established, so it is not guessed.
All sources are copied to temporaries first so a destination that
overlaps a source reads the issue-time values.

Tests: sad_quad_packed, a bit-exact exec test with a Rust reference
model over 20 cases (cross-dword byte slices, zero reference bytes,
16-bit wraparound, 32-bit wraparound with CLAMP on v_mqsad_u32_u8): 3
mnemonics verified, 0 failing on WARP. With the S0 byte selection
mutated to read the high dword one byte early the test fails.
opcodes.rs: translation rows plus packed_multi_sad_refuses_clamp.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 06:15:43 +00:00
github-bot closed this pull request 2026-11-26 06:15:43 +00:00
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-23 14:07 UTC · original

Applied locally on main with your authorship kept (cherry-picked, 74ea579); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.

**@⁠omonkeyz** on GitHub · 2026-11-23 14:07 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/247#issuecomment-7103528640) Applied locally on main with your authorship kept (cherry-picked, 74ea579); the tombox-xshader suite passes in a sandbox (exec 155/155 on WARP). It ships in the next release; this PR will be closed when that is pushed. Thanks.
Author
Owner

@⁠SchmashinPrunes on GitHub · 2026-11-24 13:20 UTC · original

I recorded these three ops on RDNA2 silicon: a gfx1036 iGPU (Ryzen 5 9600X), ROCm/HIP 5.7.1, raw wave32 kernels. It's RDNA2, not the console's gfx1013.

  • Each form, with and without CLAMP, got 65,536 inputs: random values plus byte, 16-bit and 32-bit edge values.
  • Each run was done twice, and both runs were identical.
  • I compared against my own port of this PR's model (sad4, with the zero-byte mask for the mqsad forms). I did not run the PR's code.

Results:

  • v_qsad_pk_u16_u8, v_mqsad_pk_u16_u8 and v_mqsad_u32_u8 without CLAMP match the model on all cases.
  • v_mqsad_u32_u8 with CLAMP matches t < c ? 0xffffffff : t on all cases, including 5,096 where the clamp changes the result.
  • CLAMP on the packed forms, which this PR refuses, saturates each 16-bit field to 0xffff: field = min(SAD + acc16, 0xffff). That matched all 65,536 cases of each form, including 14,610 (qsad) and 13,895 (mqsad_pk) where the clamp changes the result. So the CLAMP path could be lowered with min(..., 0xffffu) per field instead of refused, if you want it.

(Measured with an AI assistant, Claude.)

**@⁠SchmashinPrunes** on GitHub · 2026-11-24 13:20 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/247#issuecomment-7132682115) I recorded these three ops on RDNA2 silicon: a gfx1036 iGPU (Ryzen 5 9600X), ROCm/HIP 5.7.1, raw wave32 kernels. It's RDNA2, not the console's gfx1013. - Each form, with and without CLAMP, got 65,536 inputs: random values plus byte, 16-bit and 32-bit edge values. - Each run was done twice, and both runs were identical. - I compared against my own port of this PR's model (`sad4`, with the zero-byte mask for the `mqsad` forms). I did not run the PR's code. Results: - `v_qsad_pk_u16_u8`, `v_mqsad_pk_u16_u8` and `v_mqsad_u32_u8` without CLAMP match the model on all cases. - `v_mqsad_u32_u8` with CLAMP matches `t < c ? 0xffffffff : t` on all cases, including 5,096 where the clamp changes the result. - **CLAMP on the packed forms**, which this PR refuses, saturates each 16-bit field to `0xffff`: field = min(SAD + acc16, 0xffff). That matched all 65,536 cases of each form, including 14,610 (`qsad`) and 13,895 (`mqsad_pk`) where the clamp changes the result. So the CLAMP path could be lowered with `min(..., 0xffffu)` per field instead of refused, if you want it. (Measured with an AI assistant, Claude.)
Author
Owner

@⁠omonkeyz on GitHub · 2026-11-25 13:07 UTC · original

Applied on main in 74ea579 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

**@⁠omonkeyz** on GitHub · 2026-11-25 13:07 UTC · [original](https://github.com/omonkeyz/xbox-emulator/pull/247#issuecomment-7163330825) Applied on main in 74ea579 (cherry-picked onto the current history with your authorship kept; comment lines stripped per CONTRIBUTING). Thank you.

Pull request closed

This discussion has been locked. Commenting is limited to contributors.
No description provided.