[PR #1205] x64_recomp: bt/bts/btr/btc, bsf/bsr, tzcnt/lzcnt, and popcnt emitters (X64-RECOMP-BITOPS) #1236

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

📋 Pull Request Information

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

Base: main ← Head: feat/x64-recomp-bitops


📝 Commits (2)

  • fad93a5 x64_recomp: bt/bts/btr/btc, bsf/bsr, tzcnt/lzcnt, and popcnt emitters (X64-RECOMP-BITOPS)
  • a491079 tests: encode xor eax, 0x80 with imm32 (35 80 00 00 00) instead of sign-extended imm8

📊 Changes

2 files changed (+481 additions, -3 deletions)

View changed files

➕ tests/test_x64_recomp_bitops.py (+311 -0)
📝 tools/x64_recomp.py (+170 -3)

📄 Description

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

What and why

Implements the remaining decoded-but-unemitted bit-manipulation instructions in tools/x64_recomp.py (independent of #1174 and #1202, and conflict-free with all open x64_recomp PRs):

  • Asm.rbit / Asm.clz: adds 32-bit and 64-bit bit-reverse and count-leading-zeros encodings (verified against Apple Clang / LLVM 21).
  • bt / bts / btr / btc (_emit_bt):
    • Register forms wrap the bit index modulo operand width (16/32/64), update CF via _set_cf without clobbering N/Z/V, and preserve upper bits on 16-bit writes.
    • Memory immediate forms (0F BA /4../7) mask the immediate modulo operand width; memory register forms (0F A3/AB/B3/BB) sign-extend the bit index from operand width (sxth/sxtw/64-bit) to index bytes before or after the base address (base + (idx >>s 3), bit idx & 7).
    • Drops the spurious flags_r = "C" dependency on bts/btr/btc in _classify.
  • bsf / bsr: sets ZF = (src == 0) and leaves dst unmodified when src == 0 (matching x86-64 hardware); computes LSB/MSB index via rbit/clz for 16/32/64-bit operands.
  • tzcnt (F3 0F BC) / lzcnt (F3 0F BD): decodes F3-prefixed 0F BC/0F BD separately from bsf/bsr, returns operand width (16/32/64) when src == 0, and sets CF = (src == 0) and ZF = (result == 0).
  • popcnt (F3 0F B8): maps F3-prefixed popcnt_jmpe to popcnt (leaving unprefixed 0F B8 unhandled as sys), computes 16/32/64-bit Hamming weight with pure-GPR SWAR (no FP/SIMD dependency), and sets ZF = (src == 0) (N=C=V=0).
  • Pre-existing bug fix in Asm.li(rd, -1): when all four 16-bit chunks are 0xFFFF, nzc was empty and chunks[nzc[0]] raised IndexError; now emits movn rd, #0.
  • Tests (tests/test_x64_recomp_bitops.py): 9 tests covering decoding, LLVM 21–verified encodings, translation, and 5 end-to-end execution tests (BitopsExecTests).

Tested

  • Standalone on main: tests/test_x64_recomp_bitops.py (4 cross-platform passed, 5 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, 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 triple-merged with #1174 and #1202 in a scratch checkout): tests/test_x64_recomp_bitops.py 9/9 passed, tests/test_x64_recomp_sse.py 16/16 passed, tests/test_x64_recomp.py 46/46 passed (71/71 total).
  • Verified zero conflicts (git apply --check) against open x64_recomp PRs #847, #950, #1120, #1121, #1174, and #1202.

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/1205 **Author:** [@IlyassRS3](https://github.com/IlyassRS3) **Created:** 11/24/2026 **Status:** 🔄 Open **Base:** `main` ← **Head:** `feat/x64-recomp-bitops` --- ### 📝 Commits (2) - [`fad93a5`](https://github.com/ubiquitarianism/leonida/commit/fad93a58e6cef734d9c8a4c2e5fbfb7685593c7a) x64_recomp: bt/bts/btr/btc, bsf/bsr, tzcnt/lzcnt, and popcnt emitters (X64-RECOMP-BITOPS) - [`a491079`](https://github.com/ubiquitarianism/leonida/commit/a491079296737146a83cb3594bce1b2a2c59e28e) tests: encode xor eax, 0x80 with imm32 (35 80 00 00 00) instead of sign-extended imm8 ### 📊 Changes **2 files changed** (+481 additions, -3 deletions) <details> <summary>View changed files</summary> ➕ `tests/test_x64_recomp_bitops.py` (+311 -0) 📝 `tools/x64_recomp.py` (+170 -3) </details> ### 📄 Description Claimed in the coordination issue as: X64-RECOMP-BITOPS (https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7143260452) ### What and why Implements the remaining decoded-but-unemitted bit-manipulation instructions in `tools/x64_recomp.py` (independent of `#1174` and `#1202`, and conflict-free with all open `x64_recomp` PRs): - **`Asm.rbit` / `Asm.clz`**: adds 32-bit and 64-bit bit-reverse and count-leading-zeros encodings (verified against Apple Clang / LLVM 21). - **`bt` / `bts` / `btr` / `btc`** (`_emit_bt`): - Register forms wrap the bit index modulo operand width (`16`/`32`/`64`), update `CF` via `_set_cf` without clobbering `N`/`Z`/`V`, and preserve upper bits on 16-bit writes. - Memory immediate forms (`0F BA /4../7`) mask the immediate modulo operand width; memory register forms (`0F A3`/`AB`/`B3`/`BB`) sign-extend the bit index from operand width (`sxth`/`sxtw`/64-bit) to index bytes before or after the base address (`base + (idx >>s 3)`, bit `idx & 7`). - Drops the spurious `flags_r = "C"` dependency on `bts`/`btr`/`btc` in `_classify`. - **`bsf` / `bsr`**: sets `ZF = (src == 0)` and leaves `dst` unmodified when `src == 0` (matching x86-64 hardware); computes LSB/MSB index via `rbit`/`clz` for 16/32/64-bit operands. - **`tzcnt` (`F3 0F BC`) / `lzcnt` (`F3 0F BD`)**: decodes `F3`-prefixed `0F BC`/`0F BD` separately from `bsf`/`bsr`, returns operand width (`16`/`32`/`64`) when `src == 0`, and sets `CF = (src == 0)` and `ZF = (result == 0)`. - **`popcnt` (`F3 0F B8`)**: maps `F3`-prefixed `popcnt_jmpe` to `popcnt` (leaving unprefixed `0F B8` unhandled as `sys`), computes 16/32/64-bit Hamming weight with pure-GPR SWAR (no FP/SIMD dependency), and sets `ZF = (src == 0)` (`N=C=V=0`). - **Pre-existing bug fix in `Asm.li(rd, -1)`**: when all four 16-bit chunks are `0xFFFF`, `nzc` was empty and `chunks[nzc[0]]` raised `IndexError`; now emits `movn rd, #0`. - **Tests (`tests/test_x64_recomp_bitops.py`)**: 9 tests covering decoding, LLVM 21–verified encodings, translation, and 5 end-to-end execution tests (`BitopsExecTests`). ### Tested - Standalone on `main`: `tests/test_x64_recomp_bitops.py` (4 cross-platform passed, 5 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`, `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 triple-merged with `#1174` and `#1202` in a scratch checkout): `tests/test_x64_recomp_bitops.py` **9/9 passed**, `tests/test_x64_recomp_sse.py` **16/16 passed**, `tests/test_x64_recomp.py` **46/46 passed** (71/71 total). - Verified zero conflicts (`git apply --check`) against open `x64_recomp` PRs `#847`, `#950`, `#1120`, `#1121`, `#1174`, and `#1202`. 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#1236
No description provided.