Skip to content

x64_recomp: SSE/SSE2 scalar floating-point and XMM move/bitwise emitters (X64-RECOMP-SSE-SCALAR) - #1202

Open
IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:feat/x64-recomp-sse-scalar
Open

IlyassRS3 wants to merge 1 commit into
ProjectLeonida:mainfrom
IlyassRS3:feat/x64-recomp-sse-scalar

Conversation

@IlyassRS3

Copy link
Copy Markdown
Contributor

Claimed in the coordination issue as: X64-RECOMP-SSE-SCALAR (#14 (comment))

What and why

Advances Next Step #2 of docs/mobile.md by adding legacy 0F SSE/SSE2 scalar floating-point, conversion, comparison, and 128-bit XMM move/bitwise decoding and arm64 emission to tools/x64_recomp.py (independent of #1174 and conflict-free with all open x64_recomp PRs).

  • Prefix-aware 0F SSE/SSE2 decoder (_OF_SSE): decodes xmm0–xmm15 (("x", reg, size)), width specs x (16B) and y (4/8B via REX.W, ignoring 0x66), mandatory F2/F3 precedence over 66, and keeps unprefixed MMX encodings as unhandled "simd".
  • arm64 FP/SIMD assembler (Asm): adds fldr/fstr (S/D/Q), fadd/fsub/fmul/fdiv/fsqrt, fcmp/fcsel, fmov_r/fmov_to_fp/fmov_to_gpr, fcvt_s2d/fcvt_d2s, scvtf/fcvtzs/fcvtns, vins0, vand16b/vbic16b/vorr16b/veor16b, plus EL1 CPACR_EL1.FPEN enable in build_test_kernel for bare-metal QEMU runs.
  • Translator (XMAP: xmm0–xmm15 -> v0–v15, scratch v28–v30):
    • Scalar add/sub/mul/div/sqrt/min/max (ss/sd), cvt*, and register movss/movsd write lane 0 via ins so upper XMM bits are preserved; memory movss/movsd and movd/movq zero-extend to 128 bits.
    • minss/minsd/maxss/maxsd implement exact x86 NaN and ±0.0 second-operand rules via fcmp + fcsel (mi/gt) while preserving live NZCV.
    • ucomis*/comis* (FCMP_KIND) map all 16 x86 conditions across ordered and unordered (PF/ZF/CF, OF=SF=AF=0) results on both the arm64 NZCV fast path and materialized CTX_EFLAGS (including setcc, cmov, pushf, lahf, adc/sbb/cmc, and intervening inc/dec).
    • Float-to-int conversions (cvttss2si/cvttsd2si/cvtss2si/cvtsd2si, 32/64-bit) return x86 integer indefinite (INT32_MIN/INT64_MIN) for NaN and overflow (including 2147483647.5 for cvtsd2si vs cvttsd2si) while preserving live NZCV.
    • 128-bit moves (movaps/movups/movapd/movupd/movdqa/movdqu) and bitwise ops (xorps/xorpd/pxor, andps/andpd/pand, andnps/andnpd/pandn as (~dst) & src, orps/orpd/por).
  • Two pre-existing flag-path bugs surfaced by hardware execution and fixed:
    • _ctx_jcc_reg emitted tst_i(w, EF_CF | EF_ZF) (0x41 is not a valid arm64 bitmask immediate) on cross-block ja/jbe/seta/setbe; replaced with movz + tst_r.
    • _classify tagged lahf/sahf as kind="conv" instead of "alu", making their _emit_alu handler unreachable.
  • Tests (tests/test_x64_recomp_sse.py): 16 tests covering decoding, LLVM 21–verified arm64 FP/SIMD encodings, translation/liveness, and 8 end-to-end execution tests (SSEExecTests, which run automatically with the Mach-O runner once #1174 lands or under QEMU).

Tested

  • Standalone on main: python3 -I tests/test_x64_recomp_sse.py -v (8 cross-platform passed, 8 exec cleanly skipped without QEMU), tests/test_x64_recomp.py, tests/test_recomp_census.py, tests/test_ci_registration.py, tests/test_text_write_line_endings.py, tests/test_tool_startup.py, all 11 tests/test_docs_*.py, tests/test_i18n_index.py, compileall, make_game.py --help, check_no_game_data.py: all passed.
  • Native arm64 hardware execution on Apple M5 / macOS 27.2.1 (Python 3.14, cleanly merged atop #1174 in a scratch checkout): tests/test_x64_recomp_sse.py 16/16 passed (all 8 SSEExecTests executed natively) and tests/test_x64_recomp.py 46/46 passed.
  • Verified zero conflicts (git apply --check) against open x64_recomp PRs #847, #950, #1120, #1121, and #1174.

GPUs tested: n/a (CPU x86-64 -> arm64 recompiler; tested natively on Apple M5 arm64, macOS 27.2.1)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

Copilot AI balanced review requested due to automatic review settings November 24, 2026 20:07

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@kevincommits

Copy link
Copy Markdown

Independent run with QEMU. Apple M5 Pro, macOS 27.2.1 (26B131), Python 3.14.2, QEMU 11.1.3 from Homebrew. I merged main at 61732aca1 into the branch locally (980 commits behind, no conflicts). Head tested: e61deb5b1.

I found nothing wrong. 3,012 of 3,013 generated checks match Python's float arithmetic, and the one that does not is main's mov r64, -1, not this change.

  • python -I tests/test_x64_recomp_sse.py: 16/16 under QEMU. The file calls x.run_result and gates on HAS_QEMU, so its exec tests really run. tests/test_x64_recomp.py 33/33, tests/test_recomp_census.py 11/11. Full standalone suite: 514 run, 509 pass; the 5 that fail are the 5 that fail on main (the usual 4 plus test_image_load_typed, red since Improve post-load GPU memory and pipeline handling (RT-POST-LOAD-GPU-MEMORY) #2098).

  • Beyond the file, generated cases under QEMU, each compared against a Python model (operands loaded with movq xmm, r64, result read back with movq r64, xmm):

    • addss/subss/mulss/divss/minss/maxss over 14 single-precision values (±0, 1, 1.5, −2.25, near-max, the smallest denormal, ±inf, a quiet NaN, 2^24, 0.1, values around 2^31) against 8 partners each, and the sd forms over 15 doubles. Each check is the whole low qword, so for the ss forms the upper 32 bits of the destination must survive. Where the model says NaN, any NaN is accepted; min/max are checked bit-exactly, including "return the second operand" for NaN and for equal operands.
    • sqrtss, sqrtsd, cvtss2sd, cvtsd2ss over the same values.
    • cvtsi2ss, cvtsi2sd from ecx and rcx, including 16777217 and 2^53+1 (rounding) and both integer limits.
    • cvttss2si, cvtss2si, cvttsd2si, cvtsd2si into 32- and 64-bit registers: truncation against round-half-to-even on 0.5, 1.5, 2.5, 3.5, the 32-bit boundary cases 2147483647.4, 2147483647.5 and −2147483648.5, and the integer-indefinite result for NaN, ±inf and out-of-range values.
    • ucomiss, comiss, ucomisd, comisd over 7×7 operand pairs including NaN on either side, each followed by one of setb/setae/sete/setne/setbe/seta/setp/setnp.
  • The single mismatch: cvtsi2sd xmm0, rcx with rcx loaded by 48 b9 ff ff ff ff ff ff ff ff timed out. mov rcx, -1 in that imm64 form hangs on main too, with no SSE involved (Asm.li(-1), which x64_recomp: sign-extend ALU/CMP/TEST/MOV immediates, fix Asm.li(-1), and handle movnti (X64-RECOMP-SIGNEXT-IMM) #1339 and x64_recomp: bt/bts/btr/btc, bsf/bsr, tzcnt/lzcnt, and popcnt emitters (X64-RECOMP-BITOPS) #1205 both fix). With rcx set to −1 by xor ecx, ecx; dec rcx the same cvtsi2sd gives bff0000000000000, which is right.

Not tested: the packed and bitwise 128-bit forms, memory operands, non-default MXCSR rounding modes and the exception flags, the signalling difference between comiss and ucomiss, and which NaN payload comes back from arithmetic. No game files used. AI-assisted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs rebase Conflicts with the base branch: rebase it on main

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants