cpu: lift BMI1 ANDN, BEXTR, and TZCNT leaf functions to C++ #2660

Merged
Ammmae0x merged 3 commits from upstream-pr/2668 into main 2026-11-26 03:03:11 +00:00
Ammmae0x commented 2026-11-26 01:36:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Ammmae0x on 2026-11-26T01:36:10Z.


Change

Add a bounded x64-to-C++ leaf lifter (tools/cpu_bmi1_logic_aot.py) and source-built MASM/CTest differential suite (tests/cpu/bmi1_logic_aot.{asm,def,_check.py}, CTest cpu_aot_bmi1_logic) covering 32-bit and 64-bit register and flat [rcx|rdx|r8] memory-source forms of BMI1 ANDN, BEXTR, and TZCNT.

  • Models exact Intel SDM semantics with portable C++ helpers: 32-bit destination zero-extension; ANDN ((~src1) & src2, updating SF/ZF, clearing CF/OF, refusing PF/AF reads); BEXTR (start = op2[7:0], len = op2[15:8], len == 0 or start >= width yielding 0 with ZF=1, CF=OF=0, refusing SF/PF/AF reads); TZCNT (src == 0 yielding width with CF=1/ZF=0 else trailing-zero count with CF=0/ZF=(dst==0), refusing SF/OF/PF/AF reads).
  • Uses load_always() / store_always() with atomic_signal_fence and volatile observation so /O2 preserves faulting memory reads (verified by op_bmi1_logic_discarded_read).
  • Rejects BLSI/BLSMSK/BLSR, BMI2, EVEX/XOP, 16-bit TZCNT, BSF/LZCNT, nonvolatile GPRs, writes to argument GPRs, high-byte GPRs, undefined GPR/flag reads, displaced/indexed/RIP-relative memory operands, non-[r8] stores, prefixes, branches, and missing/trailing instructions after ret.
  • Registers cpu_aot_bmi1_logic with SKIP_RETURN_CODE 77 (CPUID.7.0:EBX[3] BMI1 gate) and FORM_COVERAGE_RECORD (; coverage: record cpu_aot_bmi1_logic).

Verification

  • Commit: 77b79c7f (based on upstream/main 31bcd9ad)
  • cmake --preset dev + cmake --build --preset dev --target cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted -> 0 errors, 0 warnings
  • ctest --preset dev -N -> Total Tests: 2165 (cpu_aot_bmi1_logic registered)
  • ctest --preset dev --output-on-failure -R "^cpu_aot_bmi1_logic$" -E "cpu_native_archive_" -> 1/1 Passed (1.32s); out\build\dev\form_coverage\cpu_aot_bmi1_logic.txt recorded 13 forms (including andn, bextr, tzcnt)
  • cmake --preset release + cmake --build --preset release --target cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted -> 0 errors, 0 warnings
  • ctest --preset release --output-on-failure -R "^cpu_aot_bmi1_logic$" -E "cpu_native_archive_" -> 1/1 Passed (1.32s); out\build\release\form_coverage\cpu_aot_bmi1_logic.txt recorded 13 forms
  • out\pe-env\Scripts\python.exe -B tests\form_coverage_check.py -> form coverage checks passed (exit 0)
  • out\pe-env\Scripts\python.exe -B tests\docs_links_check.py -> Self-test passed. Checked 19 Markdown files and 510 links. (exit 0)
  • out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py -> Self-test passed. Checked 1893 tracked source, script and build/CI files: no bidirectional controls. (exit 0)
  • Spot-mutation gate: 3/3 mutations killed (op_andn32 operand swap, op_tzcnt64 CF inversion, undefined-flag read guard bypass).
  • Negative controls: 12 ALTER_BMI1_LOGIC diverging exports, 17 SDM edge-case mutant models, 45 IR rejections + 5 raw-byte rejections + 12 PE-patched rejections + CLI error-exit/zero-temp checks, and discarded NULL-read fault preservation under /O2.

Evidence and follow-up

All assembly fixtures in tests/cpu/bmi1_logic_aot.asm are source-built MASM x64 routines authored under the repository MIT license. Portable lifted, mutant, rejection, and /O2 /W4 /WX compilation checks run unconditionally before the CPUID.07H.00H:EBX[3] native differential gate (exit 77 on hosts without BMI1).

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.

AI-assisted.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6617512304 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2668). Originally posted by [Ammmae0x](https://github.com/Ammmae0x) on 2026-11-26T01:36:10Z. --- ## Change Add a bounded x64-to-C++ leaf lifter (`tools/cpu_bmi1_logic_aot.py`) and source-built MASM/CTest differential suite (`tests/cpu/bmi1_logic_aot.{asm,def,_check.py}`, CTest `cpu_aot_bmi1_logic`) covering 32-bit and 64-bit register and flat `[rcx|rdx|r8]` memory-source forms of BMI1 `ANDN`, `BEXTR`, and `TZCNT`. - Models exact Intel SDM semantics with portable C++ helpers: 32-bit destination zero-extension; `ANDN` (`(~src1) & src2`, updating SF/ZF, clearing CF/OF, refusing PF/AF reads); `BEXTR` (`start = op2[7:0]`, `len = op2[15:8]`, `len == 0` or `start >= width` yielding 0 with ZF=1, CF=OF=0, refusing SF/PF/AF reads); `TZCNT` (`src == 0` yielding width with CF=1/ZF=0 else trailing-zero count with CF=0/ZF=(dst==0), refusing SF/OF/PF/AF reads). - Uses `load_always()` / `store_always()` with `atomic_signal_fence` and volatile observation so `/O2` preserves faulting memory reads (verified by `op_bmi1_logic_discarded_read`). - Rejects `BLSI`/`BLSMSK`/`BLSR`, BMI2, EVEX/XOP, 16-bit `TZCNT`, `BSF`/`LZCNT`, nonvolatile GPRs, writes to argument GPRs, high-byte GPRs, undefined GPR/flag reads, displaced/indexed/RIP-relative memory operands, non-`[r8]` stores, prefixes, branches, and missing/trailing instructions after `ret`. - Registers `cpu_aot_bmi1_logic` with `SKIP_RETURN_CODE 77` (CPUID.7.0:EBX[3] BMI1 gate) and `FORM_COVERAGE_RECORD` (`; coverage: record cpu_aot_bmi1_logic`). ## Verification - Commit: `77b79c7f` (based on `upstream/main` `31bcd9ad`) - `cmake --preset dev` + `cmake --build --preset dev --target cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted` -> 0 errors, 0 warnings - `ctest --preset dev -N` -> `Total Tests: 2165` (`cpu_aot_bmi1_logic` registered) - `ctest --preset dev --output-on-failure -R "^cpu_aot_bmi1_logic$" -E "cpu_native_archive_"` -> `1/1 Passed` (1.32s); `out\build\dev\form_coverage\cpu_aot_bmi1_logic.txt` recorded 13 forms (including `andn`, `bextr`, `tzcnt`) - `cmake --preset release` + `cmake --build --preset release --target cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted` -> 0 errors, 0 warnings - `ctest --preset release --output-on-failure -R "^cpu_aot_bmi1_logic$" -E "cpu_native_archive_"` -> `1/1 Passed` (1.32s); `out\build\release\form_coverage\cpu_aot_bmi1_logic.txt` recorded 13 forms - `out\pe-env\Scripts\python.exe -B tests\form_coverage_check.py` -> `form coverage checks passed` (exit 0) - `out\pe-env\Scripts\python.exe -B tests\docs_links_check.py` -> `Self-test passed. Checked 19 Markdown files and 510 links.` (exit 0) - `out\pe-env\Scripts\python.exe -B tests\bidi_controls_check.py` -> `Self-test passed. Checked 1893 tracked source, script and build/CI files: no bidirectional controls.` (exit 0) - Spot-mutation gate: 3/3 mutations killed (`op_andn32` operand swap, `op_tzcnt64` CF inversion, undefined-flag read guard bypass). - Negative controls: 12 `ALTER_BMI1_LOGIC` diverging exports, 17 SDM edge-case mutant models, 45 IR rejections + 5 raw-byte rejections + 12 PE-patched rejections + CLI error-exit/zero-temp checks, and discarded NULL-read fault preservation under `/O2`. ## Evidence and follow-up All assembly fixtures in `tests/cpu/bmi1_logic_aot.asm` are source-built MASM x64 routines authored under the repository MIT license. Portable lifted, mutant, rejection, and `/O2 /W4 /WX` compilation checks run unconditionally before the CPUID.07H.00H:EBX[3] native differential gate (exit 77 on hosts without BMI1). ## 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. AI-assisted.
tedin7 commented 2026-11-26 03:03:06 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:00:52Z.

Review: COMMENTED


Review of head e41d90f5 (main merged in), comment only.

Checked:

  • Lifter helpers in tools/cpu_bmi1_logic_aot.py against the SDM: ANDN (SF/ZF set, CF/OF cleared), BEXTR (start/len from op2[7:0]/[15:8], len==0 or start>=width gives 0 with ZF=1), TZCNT (src==0 gives width with CF=1/ZF=0). All correct, 32-bit results zero-extend.
  • Undefined flag reads (PF/AF, SF for BEXTR, SF/OF for TZCNT) are refused. BLSI/BLSMSK/BLSR stay with the existing cpu_bmi1_aot lifter, so no overlap. No other open or merged PR covers ANDN/BEXTR/TZCNT.
  • The check file has mutants (check_altered_and_mutants) that must diverge, plus a discarded-read test for /O2.
  • Win2025 receipt for this head is in the next comment: dev and release both 3/3, including cpu_aot_bmi1_logic.

Commands run locally:

  • git diff --check origin/main...HEAD
  • python3 tests/docs_links_check.py
  • python3 tests/bidi_controls_check.py

The helper semantics were reviewed by reading; the differential check needs MSVC and ran on Win2025 only.

No blocking findings.

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6067970276 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2668#pullrequestreview-6067970276). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T03:00:52Z. **Review:** COMMENTED --- Review of head e41d90f5 (main merged in), comment only. Checked: - Lifter helpers in `tools/cpu_bmi1_logic_aot.py` against the SDM: ANDN (SF/ZF set, CF/OF cleared), BEXTR (start/len from op2[7:0]/[15:8], len==0 or start>=width gives 0 with ZF=1), TZCNT (src==0 gives width with CF=1/ZF=0). All correct, 32-bit results zero-extend. - Undefined flag reads (PF/AF, SF for BEXTR, SF/OF for TZCNT) are refused. BLSI/BLSMSK/BLSR stay with the existing `cpu_bmi1_aot` lifter, so no overlap. No other open or merged PR covers ANDN/BEXTR/TZCNT. - The check file has mutants (`check_altered_and_mutants`) that must diverge, plus a discarded-read test for `/O2`. - Win2025 receipt for this head is in the next comment: dev and release both 3/3, including `cpu_aot_bmi1_logic`. Commands run locally: - `git diff --check origin/main...HEAD` - `python3 tests/docs_links_check.py` - `python3 tests/bidi_controls_check.py` The helper semantics were reviewed by reading; the differential check needs MSVC and ran on Win2025 only. No blocking findings. AI-assisted; commands listed were executed locally.
tedin7 commented 2026-11-26 03:03:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-26T03:00:53Z.


Win2025 WARP receipt for head e41d90f5 after updating with main.

Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit e41d90f519a5c8d58a00bbac7950d33e102331f8:

  • Debug (--preset dev):
    • Build (cmake --build --preset dev --target cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted -- -j 2): 1.82s (configure 3.77s)
    • Test (ctest --preset dev -R 'bmi1' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (2.61s: cpu_aot_bmi1 0.44s, cpu_aot_bmi1_bit 1.54s, cpu_aot_bmi1_logic 1.98s)
  • Release /O2 (--preset release):
    • Build (cmake --build --preset release --target cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted -- -j 2): 2.63s (configure 3.45s)
    • Test (ctest --preset release -R 'bmi1' --parallel 2 --no-tests=error --output-on-failure): 3/3 passed (2.91s: cpu_aot_bmi1 0.52s, cpu_aot_bmi1_bit 1.62s, cpu_aot_bmi1_logic 2.14s)
  • sccache: 11/29 hits; wall 22.5s

AI-assisted; commands listed were executed locally.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7181318060 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/2668#issuecomment-7181318060). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-26T03:00:53Z. --- Win2025 WARP receipt for head e41d90f5 after updating with main. Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207 + D3D12 WARP) at commit `e41d90f519a5c8d58a00bbac7950d33e102331f8`: - **Debug (`--preset dev`)**: - Build (`cmake --build --preset dev --target cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted -- -j 2`): `1.82s` (configure `3.77s`) - Test (`ctest --preset dev -R 'bmi1' --parallel 2 --no-tests=error --output-on-failure`): **3/3 passed** (`2.61s`: `cpu_aot_bmi1` `0.44s`, `cpu_aot_bmi1_bit` `1.54s`, `cpu_aot_bmi1_logic` `1.98s`) - **Release `/O2` (`--preset release`)**: - Build (`cmake --build --preset release --target cpu_bmi1_logic_aot_altered cpu_bmi1_logic_aot_altered_lifted cpu_bmi1_logic_aot_original cpu_bmi1_logic_aot_original_lifted -- -j 2`): `2.63s` (configure `3.45s`) - Test (`ctest --preset release -R 'bmi1' --parallel 2 --no-tests=error --output-on-failure`): **3/3 passed** (`2.91s`: `cpu_aot_bmi1` `0.52s`, `cpu_aot_bmi1_bit` `1.62s`, `cpu_aot_bmi1_logic` `2.14s`) - `sccache`: 11/29 hits; wall `22.5s` AI-assisted; commands listed were executed locally.
upstream-sync closed this pull request 2026-11-26 04:04:12 +00:00
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!2660
No description provided.