Skip to content

Fix the lane order of eight MMI permutation instructions - #291

Open
VariableDeclared wants to merge 1 commit into
ran-j:mainfrom
VariableDeclared:fix/mmi-permutation-lanes
Open

VariableDeclared wants to merge 1 commit into
ran-j:mainfrom
VariableDeclared:fix/mmi-permutation-lanes

Conversation

@VariableDeclared

Copy link
Copy Markdown

Problem

Several MMI permutation instructions moved the wrong lanes. Checked against the EE Core Instruction Set Manual:

Op Correct Was
PEXEW swap w0 and w2 swapped w0/w1 and w2/w3
PEXCW swap w1 and w2 swapped the 64-bit halves
PEXEH swap h0 and h2 in each doubleword swapped adjacent pairs
PEXCH swap h1 and h2 in each doubleword swapped adjacent pairs
PROT3W w0,w1,w2 ← w1,w2,w0; w3 kept rotated all four words
PREVH reverse the halfwords of each doubleword reversed all eight
PINTH rt's low halfwords with rs's high halfwords used rs's low halfwords
PINTEH even halfwords of rt and rs used unpackhi

Symptom

PEXEW is a common way for compiled code to move the z lane of a vector into the low word. With the wrong shuffle it got y instead; in one game an angle-normalising while (a < -k) a += 2k loop then never terminated.

Fix

  • Correct PS2_PEXEW, PS2_PEXEH, PS2_PROT3W, PS2_PINTH, PS2_PINTEH, and add PS2_PEXCW, PS2_PEXCH, PS2_PREVH in ps2_runtime_macros.h.
  • The translator emits those macros instead of its own inline shuffles for PEXEH, PEXCH, PREVH, PEXCW and PROT3W.

Generated code has to be regenerated to pick this up.

Verification

  • Configured and built all targets (ps2xRecomp, ps2xRuntime, ps2xIOP with PS2X_IOP_BUILD_TESTS=ON, ps2xTest) with CMake/Ninja, Release, macOS arm64.
  • ps2x_tests: 448/448 pass. ps2xIOP ctest: 4/4 pass.
  • New ps2_mmi_permutation_tests.cpp checks every output lane of each of the eight instructions.

🤖 Generated with Claude Code

Checked against the EE Core Instruction Set Manual:

  Op      Correct                                   Was
  PEXEW   swap w0 and w2                            swapped w0/w1 and w2/w3
  PEXCW   swap w1 and w2                            swapped the 64-bit halves
  PEXEH   swap h0 and h2 in each doubleword         swapped adjacent pairs
  PEXCH   swap h1 and h2 in each doubleword         swapped adjacent pairs
  PROT3W  w0,w1,w2 <- w1,w2,w0, w3 kept             rotated all four words
  PREVH   reverse the halfwords of each doubleword  reversed all eight
  PINTH   rt low halfwords with rs HIGH halfwords   used rs low halfwords
  PINTEH  even halfwords of rt and rs               used unpackhi

PEXEW in particular is the common way to pull the z lane of a vector
into the low word, so code that does it got the y lane instead (in one
game this turned an angle-wrap loop into an infinite loop).

The runtime macros are fixed, and the translator now emits the
PS2_PEXEH/PEXCH/PREVH/PEXCW/PROT3W macros instead of its own inline
shuffles. New unit tests check every lane of each instruction.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@GabrielNThayil

Copy link
Copy Markdown

I had checked the eight permutations independently against the EE Core Instruction Set Manual, a scalar,
lane by lane reference for each instruction, comparing with the new macros on 100k random inputs per
instruction and all eight matched. As a control, the same harness flags main's current PEXEW, PROT3W and
PEXEH on every input, so it catches the old behaviour. The translator's (rs, rt) argument order
for PINTH/PINTEH also matches the macro definitions.

This also fixes #435 (PROT3W rotating all four words).

Since #359 was merged this conflicts with main, but only in ps2xTest/CMakeLists.txt and
ps2xTest/src/main.cpp (both PRs register a new test file); the instruction changes merge cleanly.
@VariableDeclared, would you mind rebasing?

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