Skip to content

R5900: MOVZ/MOVN move the low 64 bits and keep rd's upper half - #445

Open
Fructose0517 wants to merge 1 commit into
ran-j:mainfrom
Fructose0517:fix-movz-movn-64bit
Open

Fructose0517 wants to merge 1 commit into
ran-j:mainfrom
Fructose0517:fix-movz-movn-64bit

Conversation

@Fructose0517

@Fructose0517 Fructose0517 commented Dec 2, 2026 •

Copy link
Copy Markdown

What

On the R5900, MOVZ and MOVN are 64-bit conditional moves: when the condition holds they copy the low doubleword of rs into rd and leave the upper 64 bits of rd unchanged (PCSX2: cpuRegs.GPR.r[_Rd_].UD[0] = cpuRegs.GPR.r[_Rs_].UD[0]).

The translator emitted a full 128-bit copy:

if (GPR_U64(ctx, rt) == 0) SET_GPR_VEC(ctx, rd, GPR_VEC(ctx, rs));

so the upper half of rd was replaced by the upper half of rs. Code that keeps 128-bit data in a register and later uses its upper half (MMI, sq) then reads the wrong value.

This change emits SET_GPR_U64, which writes only the low 64 bits:

if (GPR_U64(ctx, rt) == 0) SET_GPR_U64(ctx, rd, GPR_U64(ctx, rs));

Tests

  • code_generator_tests.cpp: the generated text for MOVZ and MOVN uses SET_GPR_U64 and no SET_GPR_VEC.
  • ps2_runtime_expansion_tests.cpp: the emitted statements, run on an R5900Context, move rs's low 64 bits and keep rd's upper 64 bits for both instructions.

Built and run on Windows with MSVC 19.51 the way the CI job builds (cmake -B build, cmake --build build --config Release), with #446 on top because main does not compile with MSVC as is: ps2x_tests passes 525 of 525, the two new tests included. (ps2xStudio fails to build for an unrelated reason, fixed by #450.)

Edit: an earlier version of this description said 6 tests failed on unchanged main. That came from my local build, not from upstream: my flags replaced CMake's default MSVC flags (the VU0 macro-mapping test then fails), and a Ninja Release build adds /fp:fast /arch:AVX2 /GL through EnableFastReleaseMode (four GS parallel-drawing tests then fail). Built the CI way, everything passes.

Checked on a game

Found while porting a game (about 1,020 MOVZ/MOVN sites). Its port has carried an equivalent fix as a post-translation rewrite of the generated code; with this change the translator's output, after that port's other rewrites, is identical file for file to what it ships today, and the rewrite finds nothing left to fix.

Related

#135 includes the same fix together with a larger set of changes; this PR is only the MOVZ/MOVN part, so it can be reviewed on its own.

🤖 Generated with Claude Code

MOVZ and MOVN are 64-bit conditional moves on the R5900: they copy the
low doubleword of rs into rd and leave rd's upper 64 bits unchanged
(PCSX2: GPR.r[rd].UD[0] = GPR.r[rs].UD[0]). The translator emitted a
full 128-bit copy (SET_GPR_VEC), which also replaced the upper half of
rd with the upper half of rs.

Emit SET_GPR_U64 instead, and test both the generated text and that
the emitted statements keep the destination's upper 64 bits.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Fructose0517 added a commit to Fructose0517/falena-reborn that referenced this pull request Dec 2, 2026
First of the fixes offered upstream: the translator emits a 64-bit
move instead of a 128-bit copy. Upstream's tests pass (the 6 failures
are the same on unchanged main), and with the patched translator our
generated code is identical byte for byte, with no MOVZ/MOVN site left
for our rewrite.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant