cpu: lower 32-bit and 64-bit BMI2 instructions #287

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

@⁠Vonsek on GitHub · 2026-11-21 20:53 UTC · original
Vonsek:ag/cpu-lower-bmi2 → main · +468 −0 · 6 files · 3 commits · merged 2026-11-22 06:39 UTC

Change

  • Lower all eight 32-bit and 64-bit BMI2 instructions (bzhi, mulx, pdep, pext, rorx, sarx, shlx, shrx) in tools/ffx_version_query.py, preserving flags on the flag-free forms, updating ZF/CF on bzhi, zero-extending 32-bit destinations, and resolving same-destination mulx to the high half.
  • Add a source-built MASM fixture (tests/cpu/bmi2.asm, tests/cpu/bmi2.def) and differential test (tests/cpu/bmi2.py) wired into CMakeLists.txt as cpu_aot_bmi2, verifying all 17 exports (values, bzhi ZF/CF, preserved ZF/CF on rorx/shlx, and same-destination mulx64_same) against both the native DLL and a reference model.
  • Classify bmi2 -> cpu_aot_bmi2 in tools/form_coverage.py (covering 8/16 of the BMI/ADX group 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_bmi2_lifted failed with:
    ValueError: unsupported query operation at 0x1007: bzhi eax,ecx,edx
    
  • After the change:
    • cmake --build --preset dev --target cpu_bmi2_lifted succeeded.
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_bmi2|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V: 4/4 passed (2249 BMI2 cases (32/64-bit values, BZHI flags, preserved flags, MULX same-dest) matched native and translated DLLs).
    • python -B tests/form_coverage_check.py: form coverage checks passed.

Evidence and follow-up

  • Covers all eight BMI2 mnemonics in docs/coverage/x64_zen2.tsv; complements #286 (BMI1, 6 mnemonics).
  • 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:53 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/287) `Vonsek:ag/cpu-lower-bmi2` → `main` · +468 −0 · 6 files · 3 commits · merged 2026-11-22 06:39 UTC ## Change - Lower all eight 32-bit and 64-bit BMI2 instructions (`bzhi`, `mulx`, `pdep`, `pext`, `rorx`, `sarx`, `shlx`, `shrx`) in `tools/ffx_version_query.py`, preserving flags on the flag-free forms, updating ZF/CF on `bzhi`, zero-extending 32-bit destinations, and resolving same-destination `mulx` to the high half. - Add a source-built MASM fixture (`tests/cpu/bmi2.asm`, `tests/cpu/bmi2.def`) and differential test (`tests/cpu/bmi2.py`) wired into `CMakeLists.txt` as `cpu_aot_bmi2`, verifying all 17 exports (values, `bzhi` ZF/CF, preserved ZF/CF on `rorx`/`shlx`, and same-destination `mulx64_same`) against both the native DLL and a reference model. - Classify `bmi2` -> `cpu_aot_bmi2` in `tools/form_coverage.py` (covering 8/16 of the BMI/ADX group 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_bmi2_lifted` failed with: ```text ValueError: unsupported query operation at 0x1007: bzhi eax,ecx,edx ``` - After the change: - `cmake --build --preset dev --target cpu_bmi2_lifted` succeeded. - `ctest --preset dev --no-tests=error -R "^(cpu_aot_bmi2|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V`: 4/4 passed (`2249 BMI2 cases (32/64-bit values, BZHI flags, preserved flags, MULX same-dest) matched native and translated DLLs`). - `python -B tests/form_coverage_check.py`: `form coverage checks passed`. ## Evidence and follow-up - Covers all eight BMI2 mnemonics in `docs/coverage/x64_zen2.tsv`; complements #286 (BMI1, 6 mnemonics). - 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 all eight BMI2 instructions (bzhi,
mulx, pdep, pext, rorx, sarx, shlx, shrx) in tools/ffx_version_query.py,
preserving flags on flag-free forms, updating ZF/CF on bzhi, and
resolving same-destination mulx to the high half.

Add a source-built MASM differential fixture (tests/cpu/bmi2.asm,
tests/cpu/bmi2.def, tests/cpu/bmi2.py) registered as cpu_aot_bmi2 in
CMakeLists.txt, classified in tools/form_coverage.py, and documented in
docs/experiments.md.
# Conflicts:
#	CMakeLists.txt
#	docs/experiments.md
#	tools/ffx_version_query.py
#	tools/form_coverage.py
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:40:13 +00:00
github manually merged commit 5bbaf8539e into gh-pr/287/base 2026-11-26 21:40:16 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 20:56 UTC · original · edited 2026-11-21 20:59 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-21T20:59:56.100817Z 9905644 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 20:56 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/287#issuecomment-7069112128) · edited 2026-11-21 20:59 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"990564458719910ff64c7ddad823999b363b4de1","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-21T20:59:56.100817Z">2026-11-21T20:59:56.100817Z</relative-time> | `9905644` | 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>
@ -0,0 +159,4 @@
want_v, want_out = fn(a, b, bits)
for lib, label in ((native, "native"), (lifted, "lifted")):
slot.value = 0xdeadbeef
got_v = getattr(lib, name)(a, b, ctypes.byref(slot))
Author
Owner

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

P2 Badge Check host BMI2 support before executing the native fixture

On an x64 Windows host without BMI2, such as an Ivy Bridge CPU or a VM that masks the feature, the first native bzhi32 invocation raises an illegal-instruction exception. Because CMakeLists.txt:606-607 registers this test unconditionally, the standard CPU/full suite now fails on those hosts, and the portable lifted/reference checks never run. Probe BMI2 before executing native exports and either omit the unavailable native comparison while retaining reference coverage or report a configured capability skip.

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

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 20:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/287#discussion_r4654982614) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Check host BMI2 support before executing the native fixture** On an x64 Windows host without BMI2, such as an Ivy Bridge CPU or a VM that masks the feature, the first native `bzhi32` invocation raises an illegal-instruction exception. Because `CMakeLists.txt:606-607` registers this test unconditionally, the standard CPU/full suite now fails on those hosts, and the portable lifted/reference checks never run. Probe BMI2 before executing native exports and either omit the unavailable native comparison while retaining reference coverage or report a configured capability skip. AGENTS.md reference: [AGENTS.md:L37-L39](https://github.com/R0Y00/Xbox2Windows/blob/990564458719910ff64c7ddad823999b363b4de1/AGENTS.md#L37-L39) Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Author
Owner

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

Added a host_has_bmi2() CPUID leaf 7 EBX bit 8 check in the portable lifted DLL before loading/invoking the native BMI2 fixture, exiting 77 when BMI2 is absent, and registered SKIP_RETURN_CODE 77 on cpu_aot_bmi2 in CMakeLists.txt.

**@⁠Vonsek** on GitHub · 2026-11-21 22:13 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/287#issuecomment-7070033386) Added a `host_has_bmi2()` CPUID leaf 7 EBX bit 8 check in the portable lifted DLL before loading/invoking the native BMI2 fixture, exiting 77 when BMI2 is absent, and registered `SKIP_RETURN_CODE 77` on `cpu_aot_bmi2` in `CMakeLists.txt`.
Author
Owner

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9905644587

ℹ️ 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 20:59 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/287#pullrequestreview-6008144625) · **reviewed** <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `9905644587` <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.