cpu: zero-extend 32-bit destinations of zero-count shifts #239

Merged
Spaghetih merged 2 commits from upstream-pr/242 into main 2026-11-21 19:55:33 +00:00
Spaghetih commented 2026-11-21 19:00:52 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Spaghetih on 2026-11-21T19:00:52Z.


## Change

Independent problem: a silent mistranslation in lower() (tools/ffx_version_query.py), the shared integer lowering behind the ffx_* translators, bink_heap_aot and the heap_layout/version_query fixtures.

An immediate SHL, SHR or SAR whose masked count is zero was lowered to ;. The flags are indeed unchanged in that case, but a 32-bit destination is still written, so the upper half of the 64-bit register clears. Measured on Windows with a MASM fixture, for RAX = 0xdeadbeef12345678 before shl eax, 0:

Encoding Hardware Before this change
c1 e0 00 shl eax, 0 0x12345678 0xdeadbeef12345678
c1 f8 00 sar eax, 0 0x12345678 0xdeadbeef12345678
c1 e0 20 shl eax, 32 (count masked to 0) 0x12345678 0xdeadbeef12345678

The zero-count case now emits the zero-extending write of the destination and leaves zero/carry untouched. A 64-bit destination is unchanged, as before.

Shared interface: lower(). #233, now merged, edited address() in the same function. This PR touches only the shift branch.

The second commit adds shift_count → cpu_aot_shift_count to CPU_FIXTURES in tools/form_coverage.py. Without it the coverage report lists the new fixture as unclassified. #252 classifies the fixtures already on main at another position in the table. All three branches merge cleanly in sequence, and form_coverage_check passes on the combination.

Verification

Commit 8ffdf5c, rebased on 1485f17 after #233 merged. The only conflict was the adjacent paragraph in docs/experiments.md, and both paragraphs are kept. The first figures below were measured at 7320a70 on 3911995; the rebased head was re-run as noted. Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, Python 3.14.6, iced-x86 1.21.0, pefile 2024.8.26.

ctest --preset dev --no-tests=error -R '^cpu_aot_shift_count$' -V
  • With this change: 132 native and translated zero-count shift cases matched the reference. Passed.
  • Negative control, the same test with tools/ffx_version_query.py from 3911995: AssertionError: lifted shl_zero(0xdeadbeef12345678) = 0xdeadbeef12345678, expected 0x12345678. Failed as expected.

The native image runs against the same Python reference before the translation, so the expected values are checked on hardware and not only assumed.

ctest --preset dev --no-tests=error
./out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py

ctest gave 113 passed, 0 failed and 3 capability skips: bvh_wave64_hardware, bvh_wave64_warp (lane range excludes 64) and wave_shuffle_warp. The hardware tests ran on an NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088. form_coverage_check printed form coverage checks passed.

Local package check. I used a local copy of the loader and upscaler whose SHA-256 values match the LOADER and UPSCALER profiles. ffx_version_query.py, ffx_device_init.py and ffx_create_context.py generated byte-identical C++ before and after this change (171, 1290 and 109 lines). None of the selected package intervals uses a zero-count shift. In a separate build configured with -DFFX_UPSCALER_IMAGE, the 8 ffx_* tests passed and the full suite gave 114 passed with the same 3 skips.

Rebased head 8ffdf5c: cpu_aot_shift_count, cpu_aot_address_size and cpu_lower_prefixes pass together. The full ctest gave 117 passed, 0 failed and the same 3 capability skips. form_coverage_check passed. The three package translations are still byte-identical to 1485f17.

A local differential sweep of 539 single-instruction forms through lower() (ALU, SBB, INC/DEC, shifts, IMUL, MOVZX/MOVSXD, CMOVcc, SETE, LEA and CDQ, at 8/16/32/64 bits, including AH/DH) found no value mismatch other than the six zero-count forms fixed here: shl/shr/sar eax with counts 0 and 32. The only other difference was ZF after IMUL, which Intel documents as undefined. The sweep is not part of this PR.

Evidence and follow-up

tests/cpu/shift_count.asm, .def and .py are original source written for this repository under its MIT license. They need no game files. The fixture covers:

  • shl, shr and sar eax, 0;
  • shl eax, 32, whose count is masked to 0;
  • shl rax, 64, a 64-bit destination that must stay unchanged;
  • shl eax, 3, a nonzero control through the existing path;
  • flags_zero, where cmp ecx, edx; shl eax, 0; sete al must still observe the compare. A zero low half in the initial RAX would expose a lowering that recomputed ZF from the shift result.

Inputs include 0xdeadbeef12345678, 0xffffffff00000000 and 0x8000000000000001. docs/experiments.md describes the behavior and gives the command.

Compilers rarely emit a literal zero count, so this is a correctness fix for the lowering rather than a change observed in a package interval.

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 (Claude Code). Every result above was run locally on the configuration listed.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6533100327 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/242). Originally posted by [Spaghetih](https://github.com/Spaghetih) on 2026-11-21T19:00:52Z. --- ## Change Independent problem: a silent mistranslation in `lower()` (`tools/ffx_version_query.py`), the shared integer lowering behind the `ffx_*` translators, `bink_heap_aot` and the `heap_layout`/`version_query` fixtures. An immediate SHL, SHR or SAR whose masked count is zero was lowered to `;`. The flags are indeed unchanged in that case, but a 32-bit destination is still written, so the upper half of the 64-bit register clears. Measured on Windows with a MASM fixture, for RAX = `0xdeadbeef12345678` before `shl eax, 0`: | Encoding | Hardware | Before this change | |---|---|---| | `c1 e0 00` `shl eax, 0` | `0x12345678` | `0xdeadbeef12345678` | | `c1 f8 00` `sar eax, 0` | `0x12345678` | `0xdeadbeef12345678` | | `c1 e0 20` `shl eax, 32` (count masked to 0) | `0x12345678` | `0xdeadbeef12345678` | The zero-count case now emits the zero-extending write of the destination and leaves `zero`/`carry` untouched. A 64-bit destination is unchanged, as before. Shared interface: `lower()`. #233, now merged, edited `address()` in the same function. This PR touches only the shift branch. The second commit adds `shift_count` → `cpu_aot_shift_count` to `CPU_FIXTURES` in `tools/form_coverage.py`. Without it the coverage report lists the new fixture as unclassified. #252 classifies the fixtures already on `main` at another position in the table. All three branches merge cleanly in sequence, and `form_coverage_check` passes on the combination. ## Verification Commit `8ffdf5c`, rebased on `1485f17` after #233 merged. The only conflict was the adjacent paragraph in `docs/experiments.md`, and both paragraphs are kept. The first figures below were measured at `7320a70` on `3911995`; the rebased head was re-run as noted. Windows 11 build 26200.9974, VS Build Tools 2022 17.14 (MSVC 14.44.35207), Windows SDK 10.0.26100, Python 3.14.6, `iced-x86` 1.21.0, `pefile` 2024.8.26. ```powershell ctest --preset dev --no-tests=error -R '^cpu_aot_shift_count$' -V ``` - With this change: `132 native and translated zero-count shift cases matched the reference`. Passed. - Negative control, the same test with `tools/ffx_version_query.py` from `3911995`: `AssertionError: lifted shl_zero(0xdeadbeef12345678) = 0xdeadbeef12345678, expected 0x12345678`. Failed as expected. The native image runs against the same Python reference before the translation, so the expected values are checked on hardware and not only assumed. ```powershell ctest --preset dev --no-tests=error ./out/pe-env/Scripts/python.exe -B tests/form_coverage_check.py ``` `ctest` gave 113 passed, 0 failed and 3 capability skips: `bvh_wave64_hardware`, `bvh_wave64_warp` (lane range excludes 64) and `wave_shuffle_warp`. The hardware tests ran on an NVIDIA GeForce RTX 4070 Ti, driver 32.0.16.1088. `form_coverage_check` printed `form coverage checks passed`. **Local package check.** I used a local copy of the loader and upscaler whose SHA-256 values match the `LOADER` and `UPSCALER` profiles. `ffx_version_query.py`, `ffx_device_init.py` and `ffx_create_context.py` generated byte-identical C++ before and after this change (171, 1290 and 109 lines). None of the selected package intervals uses a zero-count shift. In a separate build configured with `-DFFX_UPSCALER_IMAGE`, the 8 `ffx_*` tests passed and the full suite gave 114 passed with the same 3 skips. **Rebased head `8ffdf5c`:** `cpu_aot_shift_count`, `cpu_aot_address_size` and `cpu_lower_prefixes` pass together. The full `ctest` gave 117 passed, 0 failed and the same 3 capability skips. `form_coverage_check` passed. The three package translations are still byte-identical to `1485f17`. A local differential sweep of 539 single-instruction forms through `lower()` (ALU, SBB, INC/DEC, shifts, IMUL, MOVZX/MOVSXD, CMOVcc, SETE, LEA and CDQ, at 8/16/32/64 bits, including AH/DH) found no value mismatch other than the six zero-count forms fixed here: `shl`/`shr`/`sar eax` with counts 0 and 32. The only other difference was ZF after IMUL, which Intel documents as undefined. The sweep is not part of this PR. ## Evidence and follow-up `tests/cpu/shift_count.asm`, `.def` and `.py` are original source written for this repository under its MIT license. They need no game files. The fixture covers: - `shl`, `shr` and `sar eax, 0`; - `shl eax, 32`, whose count is masked to 0; - `shl rax, 64`, a 64-bit destination that must stay unchanged; - `shl eax, 3`, a nonzero control through the existing path; - `flags_zero`, where `cmp ecx, edx; shl eax, 0; sete al` must still observe the compare. A zero low half in the initial RAX would expose a lowering that recomputed ZF from the shift result. Inputs include `0xdeadbeef12345678`, `0xffffffff00000000` and `0x8000000000000001`. `docs/experiments.md` describes the behavior and gives the command. Compilers rarely emit a literal zero count, so this is a correctness fix for the lowering rather than a change observed in a package interval. ## 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 (Claude Code). Every result above was run locally on the configuration listed._
upstream-sync closed this pull request 2026-11-24 06:41:59 +00:00
R0Y00 commented 2026-11-24 06:47:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T19:14:48Z.


At 7320a706, Git reports a conflict only in docs/experiments.md. The shared lowerer and CMake changes merge automatically. Please retain both the #233 address-size results and the shift-count results, then rerun cpu_aot_shift_count and cpu_aot_address_size on the rebased Windows build.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7067875915 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/242#issuecomment-7067875915). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T19:14:48Z. --- At `7320a706`, Git reports a conflict only in `docs/experiments.md`. The shared lowerer and CMake changes merge automatically. Please retain both the #233 address-size results and the shift-count results, then rerun `cpu_aot_shift_count` and `cpu_aot_address_size` on the rebased Windows build.
Spaghetih commented 2026-11-24 06:47:55 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Spaghetih on 2026-11-21T19:37:05Z.


Done. The branch was rebased onto 1485f17 (8ffdf5c), and ccf1794 adds the shift_count row to CPU_FIXTURES. In docs/experiments.md both paragraphs are kept, in order: the #233 address-size results, then the zero-count shift results.

Re-checked just now against current main 7233cec: ccf1794 merges without conflict (CMakeLists.txt and docs/experiments.md auto-merge), and the merged docs keep the prefix, address-size and shift paragraphs. On that merged Windows build (Windows 11 26200.9974, MSVC 14.44.35207, Python 3.14.6):

ctest --preset dev --no-tests=error -R '^cpu_aot_(shift_count|address_size)$' -V
74: 132 native and translated zero-count shift cases matched the reference
1/2 Test #74: cpu_aot_shift_count ..............   Passed    0.06 sec
77: 72 lea and 42 load/store cases matched the 32-bit reference on the native and translated images
2/2 Test #77: cpu_aot_address_size .............   Passed    0.07 sec

Full ctest --preset dev --no-tests=error: 119 passed, 0 failed, 3 capability skips (bvh_wave64_hardware, bvh_wave64_warp, wave_shuffle_warp). Windows CI on ccf1794 passed all three jobs.

AI-assisted (Claude Code); run locally on the configuration above.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7068152359 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/242#issuecomment-7068152359). Originally posted by [Spaghetih](https://github.com/Spaghetih) on 2026-11-21T19:37:05Z. --- Done. The branch was rebased onto `1485f17` (`8ffdf5c`), and `ccf1794` adds the `shift_count` row to `CPU_FIXTURES`. In `docs/experiments.md` both paragraphs are kept, in order: the #233 address-size results, then the zero-count shift results. Re-checked just now against current `main` `7233cec`: `ccf1794` merges without conflict (`CMakeLists.txt` and `docs/experiments.md` auto-merge), and the merged docs keep the prefix, address-size and shift paragraphs. On that merged Windows build (Windows 11 26200.9974, MSVC 14.44.35207, Python 3.14.6): ```text ctest --preset dev --no-tests=error -R '^cpu_aot_(shift_count|address_size)$' -V 74: 132 native and translated zero-count shift cases matched the reference 1/2 Test #74: cpu_aot_shift_count .............. Passed 0.06 sec 77: 72 lea and 42 load/store cases matched the 32-bit reference on the native and translated images 2/2 Test #77: cpu_aot_address_size ............. Passed 0.07 sec ``` Full `ctest --preset dev --no-tests=error`: 119 passed, 0 failed, 3 capability skips (`bvh_wave64_hardware`, `bvh_wave64_warp`, `wave_shuffle_warp`). Windows CI on `ccf1794` passed all three jobs. _AI-assisted (Claude Code); run locally on the configuration above._
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!239
No description provided.