cpu: lower signed condition codes, Jcc, CMOVcc, SETcc, NEG and CWD #545

Closed
agilesh08 wants to merge 2 commits from upstream-pr/551 into main
agilesh08 commented 2026-11-22 11:02:25 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by agilesh08 on 2026-11-22T11:02:25Z.


## Change

Lower signed relational condition codes (l, ge, le, g), sign-flag conditions (s, ns), overflow conditions (o, no), two's complement integer negation (neg), and AX sign extension (cwd) in tools/ffx_version_query.py.

  • Models the sign flag SF across comparison (CMP, TEST), arithmetic (ADD, ADC, SUB, SBB, INC, DEC), logical/shift operations (AND, OR, XOR, SHL, SHR, SAR), and SAHF.
  • Lowers signed relational predicates (l as SF != OF, ge as SF == OF, le as ZF || SF != OF, and g as !ZF && SF == OF), sign tests (s and ns), and overflow tests (o and no) across Jcc, CMOVcc, and SETcc.
  • Unconditionally zero-extends 32-bit CMOVcc destination registers on both true and false paths, preserving upper 32-bit semantics.
  • Preserves upper 56 bits on SETcc byte destinations.
  • Lowers 32-bit and 64-bit NEG, updating CF, OF, SF, and ZF per Intel SDM specifications.
  • Lowers CWD, sign-extending AX into DX:AX.
  • Adds synthetic source-built MASM fixture tests/cpu/signed_conds.asm, export definition tests/cpu/signed_conds.def, and differential harness tests/cpu/signed_conds.py covering 35 exports, wired to cpu_aot_signed_conds in CMakeLists.txt and documented in docs/experiments.md.

Verification

  1. Verified all 20 platform-independent checks from .github/workflows/windows.yml:
    • tests/script_state_check.py (13 tests) OK
    • tests/resource_root_check.py (4 tests) OK
    • tests/bcpack_check.py (8 tests) OK
    • tests/bc7_blocks_check.py (10 tests, 2 skipped) OK
    • tests/bc7_tiles_check.py (6 tests) OK
    • tests/form_coverage_check.py OK
    • tests/resource_ranges_check.py (19 tests) OK
    • tests/awc_metadata_check.py (4 tests) OK
    • tests/sgd5_effects_check.py (3 tests) OK
    • tests/dxcontainer_part_offsets_check.py (7 tests) OK
    • tests/layout_chunks_check.py OK
    • tests/dependency_paths_check.py OK
    • tests/intake/paths_check.py OK
    • tests/shader_probe_check.py OK
    • tests/dxil_retarget_check.py OK
    • tests/rdna2_tools_check.py OK
    • tests/pso_readiness_check.py (36 tests, 1 skipped) OK
    • tests/xmem_attribute_scan_check.py OK
    • tests/docs_links_check.py (10 files, 116 links checked, 0 broken) OK
    • tests/bidi_controls_check.py (532 files checked, 0 bidirectional controls) OK
  2. Verified clean git diff without whitespace errors via git -c core.whitespace=cr-at-eol diff --check upstream/main.
  3. Verified differential numerical correctness: compiled lowered C++20 with MinGW-w64 g++ -std=c++20 and evaluated 12,860 test vectors across extreme bounds, signed-overflow boundary pairs, and negative invalid-opcode controls; 100% bit-for-bit match against reference model.
  4. Note: Full MSVC/MASM build and CTest execution (cpu_signed_conds_lifted, cpu_aot_signed_conds) run in GitHub Actions windows.yml CI upon PR submission.

Evidence and follow-up

  • All test cases in tests/cpu/signed_conds.asm are 100% synthetic and source-built, annotated with ; coverage: source cpu_aot_signed_conds.
  • Negative controls assert that invalid opcodes (UD2, malformed prefixes) raise exceptions prior to execution, and that memory read semantics for conditional moves occur unconditionally.
  • Follow-up: The branch coverage for signed integer comparison branches can be expanded to multi-way jump tables once the jump-table translator infrastructure is ready.
  • 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:6541602648 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/551). Originally posted by [agilesh08](https://github.com/agilesh08) on 2026-11-22T11:02:25Z. --- ## Change Lower signed relational condition codes (`l`, `ge`, `le`, `g`), sign-flag conditions (`s`, `ns`), overflow conditions (`o`, `no`), two's complement integer negation (`neg`), and AX sign extension (`cwd`) in `tools/ffx_version_query.py`. - Models the sign flag `SF` across comparison (`CMP`, `TEST`), arithmetic (`ADD`, `ADC`, `SUB`, `SBB`, `INC`, `DEC`), logical/shift operations (`AND`, `OR`, `XOR`, `SHL`, `SHR`, `SAR`), and `SAHF`. - Lowers signed relational predicates (`l` as `SF != OF`, `ge` as `SF == OF`, `le` as `ZF || SF != OF`, and `g` as `!ZF && SF == OF`), sign tests (`s` and `ns`), and overflow tests (`o` and `no`) across `Jcc`, `CMOVcc`, and `SETcc`. - Unconditionally zero-extends 32-bit `CMOVcc` destination registers on both true and false paths, preserving upper 32-bit semantics. - Preserves upper 56 bits on `SETcc` byte destinations. - Lowers 32-bit and 64-bit `NEG`, updating CF, OF, SF, and ZF per Intel SDM specifications. - Lowers `CWD`, sign-extending AX into DX:AX. - Adds synthetic source-built MASM fixture `tests/cpu/signed_conds.asm`, export definition `tests/cpu/signed_conds.def`, and differential harness `tests/cpu/signed_conds.py` covering 35 exports, wired to `cpu_aot_signed_conds` in `CMakeLists.txt` and documented in `docs/experiments.md`. ## Verification 1. Verified all 20 platform-independent checks from `.github/workflows/windows.yml`: - `tests/script_state_check.py` (13 tests) OK - `tests/resource_root_check.py` (4 tests) OK - `tests/bcpack_check.py` (8 tests) OK - `tests/bc7_blocks_check.py` (10 tests, 2 skipped) OK - `tests/bc7_tiles_check.py` (6 tests) OK - `tests/form_coverage_check.py` OK - `tests/resource_ranges_check.py` (19 tests) OK - `tests/awc_metadata_check.py` (4 tests) OK - `tests/sgd5_effects_check.py` (3 tests) OK - `tests/dxcontainer_part_offsets_check.py` (7 tests) OK - `tests/layout_chunks_check.py` OK - `tests/dependency_paths_check.py` OK - `tests/intake/paths_check.py` OK - `tests/shader_probe_check.py` OK - `tests/dxil_retarget_check.py` OK - `tests/rdna2_tools_check.py` OK - `tests/pso_readiness_check.py` (36 tests, 1 skipped) OK - `tests/xmem_attribute_scan_check.py` OK - `tests/docs_links_check.py` (10 files, 116 links checked, 0 broken) OK - `tests/bidi_controls_check.py` (532 files checked, 0 bidirectional controls) OK 2. Verified clean git diff without whitespace errors via `git -c core.whitespace=cr-at-eol diff --check upstream/main`. 3. Verified differential numerical correctness: compiled lowered C++20 with MinGW-w64 `g++ -std=c++20` and evaluated 12,860 test vectors across extreme bounds, signed-overflow boundary pairs, and negative invalid-opcode controls; 100% bit-for-bit match against reference model. 4. Note: Full MSVC/MASM build and CTest execution (`cpu_signed_conds_lifted`, `cpu_aot_signed_conds`) run in GitHub Actions `windows.yml` CI upon PR submission. ## Evidence and follow-up - All test cases in `tests/cpu/signed_conds.asm` are 100% synthetic and source-built, annotated with `; coverage: source cpu_aot_signed_conds`. - Negative controls assert that invalid opcodes (`UD2`, malformed prefixes) raise exceptions prior to execution, and that memory read semantics for conditional moves occur unconditionally. - Follow-up: The branch coverage for signed integer comparison branches can be expanded to multi-way jump tables once the jump-table translator infrastructure is ready. - 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:38 +00:00
legacy-code-archaeologist commented 2026-11-24 06:48:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T15:33:03Z.


Verification of 3dd23ea on an Intel Core i7-6820HQ (Skylake). Windows 11 26100.9168, MSVC 19.52.37029, dev preset. The head merges cleanly with main at f8d11ad (git merge-tree).

ctest --preset dev --no-tests=error -R "^cpu_" -j 4
100% tests passed, 0 tests failed out of 107

The only skip is cpu_aot_crypto_sha, which is expected because this CPU has no SHA extensions. The new test reports:

12860 signed condition and flag-op cases (CMOVcc, SETcc, Jcc, NEG, CWD) matched native and translated DLLs

Because this PR changes the shared translator, I also checked whether the fixture detects wrong lowerings. I changed one condition at a time in tools/ffx_version_query.py and rebuilt cpu_signed_conds_lifted. After each run I restored the file, verified its hash, and confirmed the test passed again.

Mutation cpu_aot_signed_conds
CMOVL as sign == overflow fails (detected)
JLE without zero fails (detected)
JS inverted fails (detected)
SAHF takes sign from AH bit 6 instead of bit 7 passes (not detected)

The last row is a coverage gap. This PR makes SAHF set SF from AH bit 7, as the commit title "fix SAHF …" says. However, tests/cpu/signed_conds.asm has no SAHF case, and the existing SAHF users (x87_harness.asm, x87_status.asm) consume only CF, ZF and PF. A case that loads AH with bit 7 set and bit 6 clear (and the reverse), runs sahf, and then uses js/cmovs/setl would cover the new SF path.

AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7081389551 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/551#issuecomment-7081389551). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T15:33:03Z. --- Verification of `3dd23ea` on an Intel Core i7-6820HQ (Skylake). Windows 11 26100.9168, MSVC 19.52.37029, `dev` preset. The head merges cleanly with `main` at `f8d11ad` (`git merge-tree`). ```text ctest --preset dev --no-tests=error -R "^cpu_" -j 4 100% tests passed, 0 tests failed out of 107 ``` The only skip is `cpu_aot_crypto_sha`, which is expected because this CPU has no SHA extensions. The new test reports: ```text 12860 signed condition and flag-op cases (CMOVcc, SETcc, Jcc, NEG, CWD) matched native and translated DLLs ``` Because this PR changes the shared translator, I also checked whether the fixture detects wrong lowerings. I changed one condition at a time in `tools/ffx_version_query.py` and rebuilt `cpu_signed_conds_lifted`. After each run I restored the file, verified its hash, and confirmed the test passed again. | Mutation | `cpu_aot_signed_conds` | |---|---| | `CMOVL` as `sign == overflow` | fails (detected) | | `JLE` without `zero` | fails (detected) | | `JS` inverted | fails (detected) | | `SAHF` takes `sign` from AH bit 6 instead of bit 7 | **passes (not detected)** | The last row is a coverage gap. This PR makes `SAHF` set SF from AH bit 7, as the commit title "fix SAHF …" says. However, `tests/cpu/signed_conds.asm` has no `SAHF` case, and the existing `SAHF` users (`x87_harness.asm`, `x87_status.asm`) consume only CF, ZF and PF. A case that loads AH with bit 7 set and bit 6 clear (and the reverse), runs `sahf`, and then uses `js`/`cmovs`/`setl` would cover the new SF path. AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.
agilesh08 commented 2026-11-24 06:49:00 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by agilesh08 on 2026-11-23T04:49:58Z.


Closing to reset and focus on a fresh, clean core contribution.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7091903150 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/551#issuecomment-7091903150). Originally posted by [agilesh08](https://github.com/agilesh08) on 2026-11-23T04:49:58Z. --- Closing to reset and focus on a fresh, clean core contribution.

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!545
No description provided.