cpu: lower 32-bit and 64-bit MUL, DIV, IDIV, XADD, and CMPXCHG instructions #331

Manually merged
github merged 5 commits from gh-pr/331/head into gh-pr/331/base 2026-11-26 21:43:38 +00:00
Owner

@⁠Vonsek on GitHub · 2026-11-21 22:24 UTC · original
Vonsek:ag/cpu-lower-mul-div → main · +459 −0 · 6 files · 5 commits · merged 2026-11-22 08:56 UTC

Change

  • Lower 32-bit and 64-bit mul, div, idiv, xadd, and cmpxchg in tools/ffx_version_query.py using portable 128-bit arithmetic for 64-bit widening multiply/divide, zero-extending 32-bit writes, and keeping LOCK prefix rejection intact.
  • Add a source-built MASM fixture (tests/cpu/mul_div.asm, tests/cpu/mul_div.def) and differential test (tests/cpu/mul_div.py) wired into CMakeLists.txt as cpu_aot_mul_div, verifying all 10 exports (467 cases including 128/64 unsigned and signed division and ZF/CF flag updates) against both the native DLL and a reference model.
  • Classify mul_div -> cpu_aot_mul_div in tools/form_coverage.py (+5 baseline x64 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_mul_div_lifted failed with:
    ValueError: unsupported query operation at 0x100d: mul r10d
    
  • After the change:
    • cmake --build --preset dev --target cpu_mul_div_lifted succeeded.
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_mul_div|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V: 4/4 passed (467 MUL, DIV, IDIV, XADD, and CMPXCHG cases matched native and translated DLLs).
    • python -B tests/form_coverage_check.py: form coverage checks passed.

Evidence and follow-up

  • Adds five uncovered baseline x64 Integer mnemonics (cmpxchg, div, idiv, mul, xadd) to docs/coverage/x64_zen2.tsv coverage; all are baseline x86-64 instructions requiring no optional CPUID feature gate.
  • 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 22:24 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/331) `Vonsek:ag/cpu-lower-mul-div` → `main` · +459 −0 · 6 files · 5 commits · merged 2026-11-22 08:56 UTC ## Change - Lower 32-bit and 64-bit `mul`, `div`, `idiv`, `xadd`, and `cmpxchg` in `tools/ffx_version_query.py` using portable 128-bit arithmetic for 64-bit widening multiply/divide, zero-extending 32-bit writes, and keeping `LOCK` prefix rejection intact. - Add a source-built MASM fixture (`tests/cpu/mul_div.asm`, `tests/cpu/mul_div.def`) and differential test (`tests/cpu/mul_div.py`) wired into `CMakeLists.txt` as `cpu_aot_mul_div`, verifying all 10 exports (467 cases including 128/64 unsigned and signed division and ZF/CF flag updates) against both the native DLL and a reference model. - Classify `mul_div` -> `cpu_aot_mul_div` in `tools/form_coverage.py` (+5 baseline x64 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_mul_div_lifted` failed with: ```text ValueError: unsupported query operation at 0x100d: mul r10d ``` - After the change: - `cmake --build --preset dev --target cpu_mul_div_lifted` succeeded. - `ctest --preset dev --no-tests=error -R "^(cpu_aot_mul_div|cpu_aot_shift_count|cpu_aot_version_query|cpu_aot_address_size)$" -V`: 4/4 passed (`467 MUL, DIV, IDIV, XADD, and CMPXCHG cases matched native and translated DLLs`). - `python -B tests/form_coverage_check.py`: `form coverage checks passed`. ## Evidence and follow-up - Adds five uncovered baseline x64 Integer mnemonics (`cmpxchg`, `div`, `idiv`, `mul`, `xadd`) to `docs/coverage/x64_zen2.tsv` coverage; all are baseline x86-64 instructions requiring no optional CPUID feature gate. - 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 mul, div, idiv, xadd, and cmpxchg in
tools/ffx_version_query.py using portable 128-bit arithmetic for 64-bit
widening multiply and divide, preserving existing LOCK prefix rejection.

Add a source-built MASM differential fixture (tests/cpu/mul_div.asm,
tests/cpu/mul_div.def, tests/cpu/mul_div.py) registered as
cpu_aot_mul_div 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:43:36 +00:00
github manually merged commit 835b66da92 into gh-pr/331/base 2026-11-26 21:43:38 +00:00
Author
Owner

@⁠chatgpt-codex-connector[bot] on GitHub · 2026-11-21 22:27 UTC · original · edited 2026-11-21 22:33 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:33:48.759860Z 85491d3 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:27 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/331#issuecomment-7070197422) · edited 2026-11-21 22:33 UTC <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"85491d3932e4eb7490ca69fc0ca40fb976f2a2bd","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:33:48.759860Z">2026-11-21T22:33:48.759860Z</relative-time> | `85491d3` | 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:33 UTC · original

P1 Badge Snapshot the effective address before updating the XADD source

When the memory operand uses the source register as its base or index (e.g. xadd qword ptr [rcx], rcx), this assignment updates rcx before write('v') reevaluates address(). The store therefore goes to an address derived from the old memory contents rather than the original effective address, potentially corrupting unrelated memory or faulting. This reproduced in a native-versus-emitted Linux/x86-64 probe. Capture the effective address before updating the source register and reuse it for the load and store.

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 22:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/331#discussion_r4655335491) **<sub><sub>![P1 Badge](https://img.shields.io/badge/P1-orange?style=flat)</sub></sub> Snapshot the effective address before updating the XADD source** When the memory operand uses the source register as its base or index (e.g. `xadd qword ptr [rcx], rcx`), this assignment updates `rcx` before `write('v')` reevaluates `address()`. The store therefore goes to an address derived from the old memory contents rather than the original effective address, potentially corrupting unrelated memory or faulting. This reproduced in a native-versus-emitted Linux/x86-64 probe. Capture the effective address before updating the source register and reuse it for the load and store. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
@ -438,0 +466,4 @@
f"if (den == 0ull || hi >= den) raise_de(den == 0); "
f"for (int b = 63; b >= 0; --b) {{ const bool top = (hi >> 63) != 0; "
f"hi = (hi << 1) | ((lo >> b) & 1u); if (top || hi >= den) {{ hi -= den; q |= 1ull << b; }} }} "
f"rax = q; rdx = hi; }}")
Author
Owner

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

P2 Badge Preserve divide errors for zero divisors and quotient overflow

For a zero divisor or an out-of-range quotient, x86 DIV/IDIV must raise #DE, but these paths continue with fabricated results. In a native-versus-emitted Linux/x86-64 probe, div r64 with RDX:RAX=0:7 and divisor 0 returned UINT64_MAX here, and 32-bit div with EDX:EAX=1:0 and divisor 1 returned 0; both native instructions faulted. The signed branches likewise omit quotient-range checks. Check the divisor and quotient bounds before updating guest registers, and add the corresponding failure cases to the new fixture.

AGENTS.md reference: AGENTS.md:L133-L133

Useful? React with 👍 / 👎.

**@⁠chatgpt-codex-connector[bot]** on GitHub · 2026-11-21 22:33 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/331#discussion_r4655335499) **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve divide errors for zero divisors and quotient overflow** For a zero divisor or an out-of-range quotient, x86 `DIV`/`IDIV` must raise `#DE`, but these paths continue with fabricated results. In a native-versus-emitted Linux/x86-64 probe, `div r64` with `RDX:RAX=0:7` and divisor 0 returned `UINT64_MAX` here, and 32-bit `div` with `EDX:EAX=1:0` and divisor 1 returned 0; both native instructions faulted. The signed branches likewise omit quotient-range checks. Check the divisor and quotient bounds before updating guest registers, and add the corresponding failure cases to the new fixture. AGENTS.md reference: [AGENTS.md:L133-L133](https://github.com/R0Y00/Xbox2Windows/blob/85491d3932e4eb7490ca69fc0ca40fb976f2a2bd/AGENTS.md#L133-L133) Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
Author
Owner

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

Addressed both findings:

  • Snapshot the effective address once before updating the source register in memory-operand XADD (and CMPXCHG), and added xadd64_mem (xadd qword ptr [rcx], rcx) to tests/cpu/mul_div.asm.
  • Added zero-divisor and quotient-overflow #DE checks (raise_de()) to 32-bit and 64-bit DIV/IDIV before writing guest registers, and verified 8 #DE fault cases against both the native and translated DLLs in tests/cpu/mul_div.py.
**@⁠Vonsek** on GitHub · 2026-11-21 22:38 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/331#issuecomment-7070324851) Addressed both findings: - Snapshot the effective address once before updating the source register in memory-operand `XADD` (and `CMPXCHG`), and added `xadd64_mem` (`xadd qword ptr [rcx], rcx`) to `tests/cpu/mul_div.asm`. - Added zero-divisor and quotient-overflow `#DE` checks (`raise_de()`) to 32-bit and 64-bit `DIV`/`IDIV` before writing guest registers, and verified 8 `#DE` fault cases against both the native and translated DLLs in `tests/cpu/mul_div.py`.
Author
Owner

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 85491d3932

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