cpu: add BMI2 bit-manipulation lowering #487

Closed
agilesh08 wants to merge 1 commit from upstream-pr/493 into main
agilesh08 commented 2026-11-22 07:39:15 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by agilesh08 on 2026-11-22T07:39:15Z.


## Change

Lowers 8 uncovered x64 BMI2 bit-manipulation instructions in tools/ffx_version_query.py (BZHI, MULX, PDEP, PEXT, RORX, SARX, SHLX, SHRX) and wires cpu_aot_bmi2 into CMakeLists.txt and tools/form_coverage.py.

Key architectural lowerings implemented:

  • BZHI: masks low bits using (1 << n) - 1 (full operand if n >= width), setting ZF based on result and CF if index > width - 1.
  • MULX: computes unsigned double-width product src1 * RDX writing low and high dword/qword destinations without affecting architectural arithmetic flags; writes low destination first so high destination wins if destinations overlap.
  • PDEP / PEXT: parallel bit deposit and extract using bit-by-bit portable C++ loops with 32-bit zero extension.
  • RORX: rotates operand right by immediate without touching EFLAGS.
  • SARX / SHLX / SHRX: variable arithmetic and logical shifts by register without affecting flags, masking shift count modulo 32/64.
  • Negative controls: rejects invalid 16-bit operands across all 8 instructions.

Verification

  • python -B -c "import tools.ffx_version_query": passed locally on Windows.
  • python -B tests/form_coverage_check.py: passed locally on Windows (all 8 forms mapped into docs/coverage/x64_zen2.tsv).
  • python -B tests/docs_links_check.py and tests/bidi_controls_check.py: 0 errors.
  • git -c core.whitespace=cr-at-eol diff --check: 0 errors.
  • Note: Local host lacks ml64.exe; native MASM fixture compilation (tests/cpu/bmi2.asm) and end-to-end differential execution run in GitHub Actions CI (windows.yml).

Evidence and follow-up

Adds pure in-memory source-built MASM fixture tests/cpu/bmi2.asm, export definition tests/cpu/bmi2.def, and differential harness tests/cpu/bmi2.py checking CPUID leaf 7 EBX bit 8. No commercial binaries or copyrighted dumps are included.

Assisted by AI.

Checklist

  • This pull request covers one reviewable behavior.
  • I included the exact command and result for every check I ran.
  • I considered a negative control or failure case for a new fixture.
  • I updated README/docs when a capability or build requirement changed.
  • I did not include game files, dumps, distribution links or secrets.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6539890888 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/493). Originally posted by [agilesh08](https://github.com/agilesh08) on 2026-11-22T07:39:15Z. --- ## Change Lowers 8 uncovered x64 BMI2 bit-manipulation instructions in `tools/ffx_version_query.py` (`BZHI`, `MULX`, `PDEP`, `PEXT`, `RORX`, `SARX`, `SHLX`, `SHRX`) and wires `cpu_aot_bmi2` into `CMakeLists.txt` and `tools/form_coverage.py`. Key architectural lowerings implemented: - `BZHI`: masks low bits using `(1 << n) - 1` (full operand if `n >= width`), setting ZF based on result and CF if `index > width - 1`. - `MULX`: computes unsigned double-width product `src1 * RDX` writing low and high dword/qword destinations without affecting architectural arithmetic flags; writes low destination first so high destination wins if destinations overlap. - `PDEP` / `PEXT`: parallel bit deposit and extract using bit-by-bit portable C++ loops with 32-bit zero extension. - `RORX`: rotates operand right by immediate without touching EFLAGS. - `SARX` / `SHLX` / `SHRX`: variable arithmetic and logical shifts by register without affecting flags, masking shift count modulo 32/64. - Negative controls: rejects invalid 16-bit operands across all 8 instructions. ## Verification - `python -B -c "import tools.ffx_version_query"`: passed locally on Windows. - `python -B tests/form_coverage_check.py`: passed locally on Windows (all 8 forms mapped into `docs/coverage/x64_zen2.tsv`). - `python -B tests/docs_links_check.py` and `tests/bidi_controls_check.py`: 0 errors. - `git -c core.whitespace=cr-at-eol diff --check`: 0 errors. - *Note:* Local host lacks `ml64.exe`; native MASM fixture compilation (`tests/cpu/bmi2.asm`) and end-to-end differential execution run in GitHub Actions CI (`windows.yml`). ## Evidence and follow-up Adds pure in-memory source-built MASM fixture `tests/cpu/bmi2.asm`, export definition `tests/cpu/bmi2.def`, and differential harness `tests/cpu/bmi2.py` checking CPUID leaf 7 EBX bit 8. No commercial binaries or copyrighted dumps are included. Assisted by AI. ## Checklist - [x] This pull request covers one reviewable behavior. - [x] I included the exact command and result for every check I ran. - [x] I considered a negative control or failure case for a new fixture. - [x] I updated README/docs when a capability or build requirement changed. - [x] I did not include game files, dumps, distribution links or secrets.
upstream-sync closed this pull request 2026-11-24 06:43:20 +00:00
agilesh08 commented 2026-11-24 06:48:28 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by agilesh08 on 2026-11-22T07:43:53Z.


Closing: upstream/main already landed BMI2 lowering and cpu_aot_bmi2 coverage.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7076087570 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/493#issuecomment-7076087570). Originally posted by [agilesh08](https://github.com/agilesh08) on 2026-11-22T07:43:53Z. --- Closing: upstream/main already landed BMI2 lowering and cpu_aot_bmi2 coverage.

Pull request closed

Sign in to join this conversation.
No reviewers
No labels
No milestone
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.

Reference
R0Y00/Xbox2Windows!487
No description provided.