cpu: lower CLZERO in counted x64 loops #1223

Merged
R0Y00 merged 1 commit from upstream-pr/1230 into main 2026-11-24 04:40:25 +00:00
R0Y00 commented 2026-11-24 04:32:20 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-24T04:32:20Z.


Refs #870.

Teach the shared x64 static lifter to lower the plain three-byte CLZERO through its normal exported-function entry. It requires a defined 64-bit RAX, rounds down to a 64-byte cache line, emits observable stores, and leaves the cursor and flags unchanged. Existing arithmetic and control flow implement the surrounding counted loop. The code uses no fixed executable addresses, image rewriter, profile lookup or exception handler.

The Windows Release test compares six range and alignment cases with an independently assembled integer-store reference and literal expected bytes. The 24 compact loop comparisons rotate the bound through RDX, R8, R9 and R10. Six additional instrumented comparisons check CF before/after CLZERO, the final equality/above results and the returned cursor. Negative controls reject address-size overrides and uninitialized RAX. The measured domain is single-threaded, writable ordinary memory.

. ./tools/dev.ps1
cmake --preset release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release
cmake --build --preset release --target cpu_clzero_lifted
ctest --preset release --no-tests=error -R '^cpu_counted_clzero$' -V

Windows x64, Intel Core i9-14900HX, MSVC 19.51.36244, Release: 1/1 passed, zero skips. This fixture measures architectural results, not application frame rate.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6574665210 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1230). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-24T04:32:20Z. --- Refs #870. Teach the shared x64 static lifter to lower the plain three-byte CLZERO through its normal exported-function entry. It requires a defined 64-bit RAX, rounds down to a 64-byte cache line, emits observable stores, and leaves the cursor and flags unchanged. Existing arithmetic and control flow implement the surrounding counted loop. The code uses no fixed executable addresses, image rewriter, profile lookup or exception handler. The Windows Release test compares six range and alignment cases with an independently assembled integer-store reference and literal expected bytes. The 24 compact loop comparisons rotate the bound through RDX, R8, R9 and R10. Six additional instrumented comparisons check CF before/after CLZERO, the final equality/above results and the returned cursor. Negative controls reject address-size overrides and uninitialized RAX. The measured domain is single-threaded, writable ordinary memory. ```powershell . ./tools/dev.ps1 cmake --preset release -DCMAKE_TRY_COMPILE_CONFIGURATION=Release cmake --build --preset release --target cpu_clzero_lifted ctest --preset release --no-tests=error -R '^cpu_counted_clzero$' -V ``` Windows x64, Intel Core i9-14900HX, MSVC 19.51.36244, Release: 1/1 passed, zero skips. This fixture measures architectural results, not application frame rate.
tedin7 commented 2026-11-24 04:38:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T04:38:46Z.

Review: COMMENTED


Reviewed commit f39021c6 on Linux (x86_64, Python 3.12 with iced-x86 and pefile). Windows build/tests (cpu_clzero_lifted, ctest --preset release --no-tests=error -R '^cpu_counted_clzero$' -V) were not run locally.

Code & Spec Audit

  • tools/cpu_switch_aot.py: adds Mnemonic.CLZERO to FLAG_NEUTRAL, rejects ins.len != 3 (address-size override 67h / prefixes), requires defined 64-bit RAX (self.reg(R.RAX) with {R.RAX: 8} read, {} write), aligns down with ({address} & ~63ull), and emits a 64-byte volatile unsigned char* zeroing loop without modifying RAX or flags.
  • tests/cpu/counted_clzero.{asm,def,py}: tests 6 aligned/unaligned/equal/reversed/multi-line bounds against reference_counted and literal expected bytes/RAX/flags, 4 compact loop variants (loop_dx, loop_r8, loop_r9, loop_r10), and negative controls (prefix_addr32, unknown_rax).
  • README.md and docs/experiments.md are title-neutral and match the test scope.

Checks Run Locally (Linux)

  • /root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py: passed (self-test passed; 16 Markdown files, 278 links)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py: passed (self-test passed; 1037 tracked files)
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py: passed
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/hardware_reports_check.py: 12/12 passed, 0 skipped, 0 failed
  • /root/.local/share/x2w-rev-venv/bin/python -B tests/gpu_report_format_check.py: 10/10 passed, 0 skipped, 0 failed
  • Inline tools/cpu_switch_aot.py + iced_x86 check (0f 01 fc len 3, 67 0f 01 fc len 4, Mnemonic.CLZERO in FLAG_NEUTRAL): passed
  • git diff --check HEAD~1..HEAD: passed
<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6033725304 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1230#pullrequestreview-6033725304). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T04:38:46Z. **Review:** COMMENTED --- Reviewed commit `f39021c6` on Linux (`x86_64`, Python 3.12 with `iced-x86` and `pefile`). Windows build/tests (`cpu_clzero_lifted`, `ctest --preset release --no-tests=error -R '^cpu_counted_clzero$' -V`) were not run locally. ### Code & Spec Audit - `tools/cpu_switch_aot.py`: adds `Mnemonic.CLZERO` to `FLAG_NEUTRAL`, rejects `ins.len != 3` (address-size override `67h` / prefixes), requires defined 64-bit `RAX` (`self.reg(R.RAX)` with `{R.RAX: 8}` read, `{}` write), aligns down with `({address} & ~63ull)`, and emits a 64-byte `volatile unsigned char*` zeroing loop without modifying `RAX` or flags. - `tests/cpu/counted_clzero.{asm,def,py}`: tests 6 aligned/unaligned/equal/reversed/multi-line bounds against `reference_counted` and literal expected bytes/RAX/flags, 4 compact loop variants (`loop_dx`, `loop_r8`, `loop_r9`, `loop_r10`), and negative controls (`prefix_addr32`, `unknown_rax`). - `README.md` and `docs/experiments.md` are title-neutral and match the test scope. ### Checks Run Locally (Linux) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/docs_links_check.py`: passed (self-test passed; 16 Markdown files, 278 links) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/bidi_controls_check.py`: passed (self-test passed; 1037 tracked files) - `/root/.local/share/x2w-rev-venv/bin/python -B tests/form_coverage_check.py`: passed - `/root/.local/share/x2w-rev-venv/bin/python -B tests/hardware_reports_check.py`: 12/12 passed, 0 skipped, 0 failed - `/root/.local/share/x2w-rev-venv/bin/python -B tests/gpu_report_format_check.py`: 10/10 passed, 0 skipped, 0 failed - Inline `tools/cpu_switch_aot.py` + `iced_x86` check (`0f 01 fc` len 3, `67 0f 01 fc` len 4, `Mnemonic.CLZERO in FLAG_NEUTRAL`): passed - `git diff --check HEAD~1..HEAD`: passed
upstream-sync closed this pull request 2026-11-24 06:47:07 +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!1223
No description provided.