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

Open
opened 2026-11-26 20:20:32 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1202
Author: @IlyassRS3
Created: 11/24/2026
Status: 🔄 Open

Base: main ← Head: feat/x64-recomp-sse-scalar


📝 Commits (1)

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

📊 Changes

3 files changed (+1141 additions, -32 deletions)

View changed files

📝 docs/mobile.md (+9 -0)
➕ tests/test_x64_recomp_sse.py (+723 -0)
📝 tools/x64_recomp.py (+409 -32)

📄 Description

Claimed in the coordination issue as: X64-RECOMP-SSE-SCALAR (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143077050)

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.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/1202 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-sse-scalar` --- ### 📝 Commits (1) - [`e61deb5`](https://github.com/ubiquitarianism/leonida/commit/e61deb5b14ed47c1ae899e26915dab01f19bf453) x64_recomp: SSE/SSE2 scalar floating-point and XMM move/bitwise emitters (X64-RECOMP-SSE-SCALAR) ### 📊 Changes **3 files changed** (+1141 additions, -32 deletions) <details> <summary>View changed files</summary> 📝 `docs/mobile.md` (+9 -0) ➕ `tests/test_x64_recomp_sse.py` (+723 -0) 📝 `tools/x64_recomp.py` (+409 -32) </details> ### 📄 Description Claimed in the coordination issue as: X64-RECOMP-SSE-SCALAR (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143077050) ### 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 - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] 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. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
starred/leonida#1234
No description provided.