Skip to content

MMI: use sa[3:0] for PSLLH/PSRLH/PSRAH shift amounts - #443

Open
dean-k-pearce wants to merge 1 commit into
ran-j:mainfrom
dean-k-pearce:fix-434-halfword-shift-sa
Open

dean-k-pearce wants to merge 1 commit into
ran-j:mainfrom
dean-k-pearce:fix-434-halfword-shift-sa

Conversation

@dean-k-pearce

@dean-k-pearce dean-k-pearce commented Nov 18, 2026 •

Copy link
Copy Markdown

Fixes #434.

The halfword shift-immediate ops passed the full 5-bit sa to _mm_slli_epi16 / _mm_srli_epi16 / _mm_srai_epi16. The EE only uses the low 4 bits for halfwords. SSE zeroes the lanes (or sign fills for srai) for any count above 15, so sa 16-31 gave wrong results.

Change (mmi_translator.cpp): the three halfword cases now emit sa & 0xF. PSLLW/PSRLW/PSRAW are unchanged because all 5 bits count there.

Tests (code_generator_tests.cpp, two new cases):

  • MMI halfword shift immediates use sa[3:0]: translates PSLLH/PSRLH/PSRAH at sa 0, 1, 15, 16, 17 and 31 (plus 3 and 30 where the reference has them), runs the emitted SSE shift on the host and compares every word with the real PS2 result. 17 rows are taken as-is from ps2autotests tests/cpu/ee_simd/logic.expected, for example psllh -1, 16 unchanged and psllh -1, 31 giving 8000 per halfword. The 7 rows marked derived aren't in that file and follow the same sa[3:0] rule (PCSX2 _PSLLH uses _Sa_ & 0xf, DobieStation uses (instruction >> 6) & 0xF).
  • MMI word shift immediates keep all five sa bits: the word forms still emit sa 31 as is.

One honest gap: none of the psrah vectors in logic.expected can tell the two rules apart (-1 and C_ONE give the same result masked or not), so the PSRAH rows that fail without the fix (kS16Min at sa 16 and 17) are derived from the PCSX2/DobieStation rule rather than taken from real-PS2 output. The PSRAH -1 rows at sa 16 and 31 are kept as controls.

Without the fix the new halfword test fails at PSLLH sa 16/17/30/31, PSRLH sa 16/17/31 and PSRAH sa 16/17. PSRAH sa 31 passes either way, because a sign fill happens to equal a shift by 15.

Checked: Linux, GCC, Release. The full build passes, and ps2x_tests passes all 448 (446 before, plus the 2 new). The gs_cache ctest suite passes all 45. I haven't built with Clang, MSVC or on ARM. The test picks its intrinsics header the same way ps2_runtime_macros.h does (intrin.h / sse2neon.h / immintrin.h).

AI tools helped write this patch and its tests. I've reviewed the change and the test values myself.

The halfword shift-immediate ops passed the full 5-bit sa to
_mm_slli/srli/srai_epi16. The EE only uses the low 4 bits for halfwords,
while SSE zeroes the lanes (or sign fills for srai) for any count above
15, so sa 16-31 gave wrong results. Mask the count with 0xF for the three
halfword forms; the word forms keep all five bits.

Tests: run the emitted shift on the host for sa 0, 1, 15, 16, 17 and 31
on all three ops and compare with real PS2 results from ps2autotests
tests/cpu/ee_simd/logic.expected (rows not in that file follow the same
sa[3:0] rule, as in PCSX2 and DobieStation), and check that the word
forms still emit sa 31 unchanged.
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.

PSLLH/PSRLH/PSRAH: shift amounts 16-31 should use sa[3:0] (SSE gives 0 / sign fill)

1 participant