cpu: lower 32-bit and 64-bit integer bit-scan, bit-count, byte-swap, and bit-test instructions #289

Manually merged
github merged 4 commits from gh-pr/289/head into gh-pr/289/base 2026-11-26 21:40:26 +00:00
Owner

@⁠Vonsek on GitHub · 2026-11-21 20:55 UTC · original
Vonsek:ag/cpu-lower-bit-ops → main · +511 −0 · 6 files · 4 commits · merged 2026-11-22 07:18 UTC

Change

  • Lower 32-bit and 64-bit integer bit-scan, bit-count, byte-swap, and register bit-test instructions (bsf, bsr, bswap, popcnt, lzcnt, bt, bts, btr, btc) in tools/ffx_version_query.py, preserving zero-extending 32-bit writes, exact ZF/CF flag updates, and rejecting memory-operand bt/bts/btr/btc.
  • Add a source-built MASM fixture (tests/cpu/bit_ops.asm, tests/cpu/bit_ops.def) and differential test (tests/cpu/bit_ops.py) wired into CMakeLists.txt as cpu_aot_bit_ops, verifying all 19 exports (32-bit and 64-bit, including btc64_imm and a memory-operand bt rejection control) against both the native DLL and a reference model.
  • Classify bit_ops -> cpu_aot_bit_ops in tools/form_coverage.py (+9 Integer mnemonics in docs/coverage/x64_zen2.tsv) and document the test in docs/experiments.md.

Verification

Tested on Windows 11 (VS 2022 Build Tools 17.14, MSVC 19.44, ML64 14.44, Windows SDK 10.0.26100, Python 3.12):

  • Before the change, cmake --build --preset dev --target cpu_bit_ops_lifted failed with:
    ValueError: unsupported query operation at 0x100a: bsf eax,ecx
    
  • After the change:
    • cmake --build --preset dev --target cpu_bit_ops_lifted succeeded.
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_bit_ops|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V: 4/4 passed (1605 integer bit-op cases (BSF, BSR, BSWAP, POPCNT, LZCNT, BT/BTS/BTR/BTC) matched native and translated DLLs).
    • python -B tests/form_coverage_check.py: form coverage checks passed.

Evidence and follow-up

  • Adds nine uncovered x64 Integer mnemonics (bsf, bsr, bswap, bt, btc, btr, bts, lzcnt, popcnt) to docs/coverage/x64_zen2.tsv coverage.
  • All binaries are source-built via MASM and CMake; no game files or external dumps are included.

Checklist

  • Focused, single-purpose change
  • Source-built test that fails without the change and passes with it
  • Verified locally on Windows
**@⁠Vonsek** on GitHub · 2026-11-21 20:55 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/289) `Vonsek:ag/cpu-lower-bit-ops` → `main` · +511 −0 · 6 files · 4 commits · merged 2026-11-22 07:18 UTC ## Change - Lower 32-bit and 64-bit integer bit-scan, bit-count, byte-swap, and register bit-test instructions (`bsf`, `bsr`, `bswap`, `popcnt`, `lzcnt`, `bt`, `bts`, `btr`, `btc`) in `tools/ffx_version_query.py`, preserving zero-extending 32-bit writes, exact ZF/CF flag updates, and rejecting memory-operand `bt`/`bts`/`btr`/`btc`. - Add a source-built MASM fixture (`tests/cpu/bit_ops.asm`, `tests/cpu/bit_ops.def`) and differential test (`tests/cpu/bit_ops.py`) wired into `CMakeLists.txt` as `cpu_aot_bit_ops`, verifying all 19 exports (32-bit and 64-bit, including `btc64_imm` and a memory-operand `bt` rejection control) against both the native DLL and a reference model. - Classify `bit_ops` -> `cpu_aot_bit_ops` in `tools/form_coverage.py` (+9 Integer mnemonics in `docs/coverage/x64_zen2.tsv`) and document the test in `docs/experiments.md`. ## Verification Tested on Windows 11 (VS 2022 Build Tools 17.14, MSVC 19.44, ML64 14.44, Windows SDK 10.0.26100, Python 3.12): - Before the change, `cmake --build --preset dev --target cpu_bit_ops_lifted` failed with: ```text ValueError: unsupported query operation at 0x100a: bsf eax,ecx ``` - After the change: - `cmake --build --preset dev --target cpu_bit_ops_lifted` succeeded. - `ctest --preset dev --no-tests=error -R "^(cpu_aot_bit_ops|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V`: 4/4 passed (`1605 integer bit-op cases (BSF, BSR, BSWAP, POPCNT, LZCNT, BT/BTS/BTR/BTC) matched native and translated DLLs`). - `python -B tests/form_coverage_check.py`: `form coverage checks passed`. ## Evidence and follow-up - Adds nine uncovered x64 Integer mnemonics (`bsf`, `bsr`, `bswap`, `bt`, `btc`, `btr`, `bts`, `lzcnt`, `popcnt`) to `docs/coverage/x64_zen2.tsv` coverage. - All binaries are source-built via MASM and CMake; no game files or external dumps are included. ## Checklist - [x] Focused, single-purpose change - [x] Source-built test that fails without the change and passes with it - [x] Verified locally on Windows
Add 32-bit and 64-bit lowering for bsf, bsr, bswap, popcnt, lzcnt, and
register-operand bt/bts/btr/btc in tools/ffx_version_query.py, including
zero-extending 32-bit writes and ZF/CF flag updates.

Add a source-built MASM differential fixture (tests/cpu/bit_ops.asm,
tests/cpu/bit_ops.def, tests/cpu/bit_ops.py) registered as
cpu_aot_bit_ops in CMakeLists.txt, classified in tools/form_coverage.py,
and documented in docs/experiments.md.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:40:24 +00:00
github manually merged commit 0fe3f8a673 into gh-pr/289/base 2026-11-26 21:40:26 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 22:11 UTC · original · edited 2026-11-21 22:16 UTC

Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-21T22:16:43.130139Z 93468fa Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review" or "@⁠codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 22:11 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/289#issuecomment-7070009157) · edited 2026-11-21 22:16 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"93468fa209b441d24d6bf3b9d481a3d29d7e4f6b","has_any_findings":true,"has_unresolved_findings":true,"status":"completed"},"security_review":{"commit_sha":null,"has_any_findings":null,"has_unresolved_findings":null,"status":null}} --> <details> <summary><strong>Codex Review Summary</strong>: ✅ Completed</summary> This comment shows the latest Codex review activity on this pull request. | Review | Status | Commit | Review trigger | | --- | --- | --- | --- | | 📝 **Code Review** | ✅ **Completed** <relative-time datetime="2026-11-21T22:16:43.130139Z">2026-11-21T22:16:43.130139Z</relative-time> | `93468fa` | Draft marked ready | <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review" or "@⁠codex security review". Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. </details> </details>
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 22:16 UTC · original

P2 Badge Feature-gate the native POPCNT and LZCNT checks

On an otherwise supported x64 Windows host without LZCNT, such as Intel Sandy Bridge or Ivy Bridge, this loop unconditionally invokes the native LZCNT fixtures. Those processors execute the encoding as BSR, so an input of 1 returns 0 instead of 31 or 63, causing cpu_aot_bit_ops to fail despite a correct translation. Hosts without POPCNT instead encounter an illegal-instruction error. Check the respective host CPUID features before invoking these native cases, while retaining the lifted/reference checks, following the existing AVX guard in tests/cpu/heap_layout.py.

AGENTS.md reference: AGENTS.md:L37-L39

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 22:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/289#discussion_r4655275348) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Feature-gate the native POPCNT and LZCNT checks** On an otherwise supported x64 Windows host without LZCNT, such as Intel Sandy Bridge or Ivy Bridge, this loop unconditionally invokes the native LZCNT fixtures. Those processors execute the encoding as BSR, so an input of 1 returns 0 instead of 31 or 63, causing `cpu_aot_bit_ops` to fail despite a correct translation. Hosts without POPCNT instead encounter an illegal-instruction error. Check the respective host CPUID features before invoking these native cases, while retaining the lifted/reference checks, following the existing AVX guard in `tests/cpu/heap_layout.py`. AGENTS.md reference: [AGENTS.md:L37-L39](https://github.com/R0Y00/Xbox2Windows/blob/93468fa209b441d24d6bf3b9d481a3d29d7e4f6b/AGENTS.md#L37-L39) Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Author
Owner

@⁠Vonsek on GitHub · 2026-11-21 22:21 UTC · original

Addressed in 236630c: added a host_has_popcnt_lzcnt() CPUID check (leaf 1 ECX bit 23 for POPCNT and leaf 0x80000001 ECX bit 5 for LZCNT) in the portable lifted DLL before loading/invoking the native fixture, exiting 77 when absent, and registered SKIP_RETURN_CODE 77 on cpu_aot_bit_ops in CMakeLists.txt.

**@⁠Vonsek** on GitHub · 2026-11-21 22:21 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/289#issuecomment-7070127797) Addressed in `236630c`: added a `host_has_popcnt_lzcnt()` CPUID check (leaf 1 ECX bit 23 for POPCNT and leaf `0x80000001` ECX bit 5 for LZCNT) in the portable lifted DLL before loading/invoking the native fixture, exiting 77 when absent, and registered `SKIP_RETURN_CODE 77` on `cpu_aot_bit_ops` in `CMakeLists.txt`.
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 22:16 UTC · original · reviewed

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 93468fa209

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@⁠codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback".

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 22:16 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/289#pullrequestreview-6008560499) · **reviewed** <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `93468fa209` <details> <summary>ℹ️ About Codex in GitHub</summary> <br/> [Your team has set up Codex to review pull requests in this repo](https://chatgpt.com/codex/cloud/settings/general). Reviews are triggered when you - Open a pull request for review - Mark a draft as ready - Comment "@⁠codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@⁠codex address that feedback". </details>
This discussion has been locked. Commenting is limited to contributors.
No description provided.