cpu: lower scalar CVTSS2SI and CVTSD2SI by the MXCSR rounding control #1378

Merged
tedin7 merged 1 commit from upstream-pr/1385 into main 2026-11-24 10:32:58 +00:00
tedin7 commented 2026-11-24 10:09:08 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:09:08Z.


Change

tools/cpu_sse_aot.py now lowers the rounding scalar float-to-integer conversions CVTSS2SI and CVTSD2SI in bounded leaf functions: 32-bit (upper half zeroed) and 64-bit (REX.W) general-register destination, XMM or m32/m64 source. Before this change the translator refused them with unsupported instruction; the truncating CVTTSS2SI/CVTTSD2SI were already lifted and are untouched.

  • Rounding: the RC field of the MXCSR decides (nearest even, down, up, toward zero). A NaN, an infinity or a result that does not fit gives the integer indefinite (0x80000000, 0x8000000000000000). DAZ reads a denormal source as zero. FTZ plays no part in a conversion to an integer. Note a value that rounds up to 2^31 is out of range for a 32-bit destination where it truncates in range (for example 2147483647.5 gives 0x7fffffff toward zero and the indefinite to nearest).
  • Why not std::lrint / std::llrint with a guard for the indefinite: the Windows C runtime (UCRT/MSVC) does not follow the instruction for NaN, out-of-range values, DAZ or a non-default rounding control (the same family of defect as the SQRTSS with DAZ case in #1168), and a guard would only restore the indefinite while the rounding still came from the library. The generated code calls _mm_cvtss_si32, _mm_cvtss_si64, _mm_cvtsd_si32 and _mm_cvtsd_si64 (<xmmintrin.h>/<emmintrin.h>, available with MSVC x64, clang and gcc) on an operand read through a volatile, so the compiler cannot fold a constant with the default environment and the hardware decides every case.
  • MXCSR: as in #1168 and #1213, a guest MXCSR write is not lifted, so the rounding control, DAZ and FTZ are the host's. The invalid and precision sticky flags are not observed. Declared in the docstring and in docs/experiments.md.

Advances the scalar SSE translator coverage in docs/experiments.md. No overlapping open PR touches tools/cpu_sse_aot.py or tests/cpu/scalar_float_check.py (the one shared file is tests/form_coverage_check.py, two lines).

Verification

Linux, assembled with llvm-ml-19, linked with lld-link-19, original machine code run natively through a Windows-ABI trampoline, translation built with clang++-19 at -O0, -O2 and -O3:

  • python3 -B tests/cpu/scalar_float_check.py <original.dll> <lifted> <swapped.dll> <swapped lifted> <mxcsr harness>: 84 fixed results and 1,890 new round_cvt calls (and the 24 other exports) match on result and guarded buffer for the original and the control build; round_cvt matches the processor and an independent reference under all 16 MXCSR words (4 rounding controls x DAZ x FTZ) with 32 further fixed answers; 33 patched-instruction rejections and 14 signature rejections pass. Same output at -O0, -O2 and -O3.
  • python3 -B tests/form_coverage_check.py, python3 -B tests/docs_links_check.py, python3 -B tests/bidi_controls_check.py, python3 -m py_compile: pass.
  • MinGW (x86_64-w64-mingw32-g++, -O0/-O2/-O3) build of the translation under Wine against the fixture DLL: 8,400 calls over the 16 MXCSR words, 0 mismatches.
  • Negative control, fails before: with tools/cpu_sse_aot.py from origin/main the new fixture fails at translation (unsupported instruction at RVA 0x1370: cvtss2si eax,xmm0). With the conversion lowered as a truncation, or with a 32-bit destination that keeps the upper half, the check fails at round_cvt(0, 0x3ff0000000000000) and round_cvt(0, 0) respectively.
  • The reference is exact rational arithmetic in Python (no host instruction, none of the translator's code). Eight wrong rules (truncation, ties away from zero, zero indefinite, nearest-only, DAZ ignored, 32-bit result keeping or sign-extending the upper half, a 64-bit result narrowed to 32 bits) must each disagree with the processor on exactly the words of their destination size and on no other word.
  • Not run by me: the Windows MASM/MSVC ctest. PR outside --preset pr: ctest --preset dev -R '^cpu_aot_scalar_float$' (and --preset release). The scalar-fp fuzz area does not generate these instructions and is unchanged.

Evidence and follow-up

  • New fixture code is original MASM in tests/cpu/scalar_float.asm, one new export round_cvt(float, double, uint8_t*): 32- and 64-bit destinations from a register and from memory, each 32-bit destination starting from all ones. All stores are inside an 80-byte window of the guarded buffer, and the check compares the whole buffer with 8 guard bytes either side, so nothing outside the eight result words, the float and the double may change.
  • Operands: ties at 0.5, 1.5, 2.5 and their negatives, one step either side of a tie, the limits around 2^31 and 2^63 and their negatives, 2147483647.5, denormals of both signs, infinities and quiet and signalling NaNs.
  • Refused as before: VEX forms (vcvtss2si), CVTPS2PI/CVTPD2PI (MMX destination), an unwritten source register, a nonvolatile destination, a RIP-relative source. The old rejection control that used CVTSS2SI as the unsupported example now uses CVTPS2PI.
  • Not covered: the exception flags, a guest MXCSR write, CVTPS2PI/CVTPD2PI, EVEX forms.
  • AI-assisted.

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:6579407662 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1385). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T10:09:08Z. --- ## Change `tools/cpu_sse_aot.py` now lowers the rounding scalar float-to-integer conversions `CVTSS2SI` and `CVTSD2SI` in bounded leaf functions: 32-bit (upper half zeroed) and 64-bit (`REX.W`) general-register destination, XMM or `m32`/`m64` source. Before this change the translator refused them with `unsupported instruction`; the truncating `CVTTSS2SI`/`CVTTSD2SI` were already lifted and are untouched. - Rounding: the RC field of the MXCSR decides (nearest even, down, up, toward zero). A NaN, an infinity or a result that does not fit gives the integer indefinite (`0x80000000`, `0x8000000000000000`). DAZ reads a denormal source as zero. FTZ plays no part in a conversion to an integer. Note a value that rounds up to 2^31 is out of range for a 32-bit destination where it truncates in range (for example 2147483647.5 gives `0x7fffffff` toward zero and the indefinite to nearest). - Why not `std::lrint` / `std::llrint` with a guard for the indefinite: the Windows C runtime (UCRT/MSVC) does not follow the instruction for NaN, out-of-range values, DAZ or a non-default rounding control (the same family of defect as the `SQRTSS` with DAZ case in #1168), and a guard would only restore the indefinite while the rounding still came from the library. The generated code calls `_mm_cvtss_si32`, `_mm_cvtss_si64`, `_mm_cvtsd_si32` and `_mm_cvtsd_si64` (`<xmmintrin.h>`/`<emmintrin.h>`, available with MSVC x64, clang and gcc) on an operand read through a volatile, so the compiler cannot fold a constant with the default environment and the hardware decides every case. - MXCSR: as in #1168 and #1213, a guest MXCSR write is not lifted, so the rounding control, DAZ and FTZ are the host's. The invalid and precision sticky flags are not observed. Declared in the docstring and in `docs/experiments.md`. Advances the scalar SSE translator coverage in `docs/experiments.md`. No overlapping open PR touches `tools/cpu_sse_aot.py` or `tests/cpu/scalar_float_check.py` (the one shared file is `tests/form_coverage_check.py`, two lines). ## Verification Linux, assembled with `llvm-ml-19`, linked with `lld-link-19`, original machine code run natively through a Windows-ABI trampoline, translation built with clang++-19 at -O0, -O2 and -O3: - `python3 -B tests/cpu/scalar_float_check.py <original.dll> <lifted> <swapped.dll> <swapped lifted> <mxcsr harness>`: 84 fixed results and 1,890 new `round_cvt` calls (and the 24 other exports) match on result and guarded buffer for the original and the control build; `round_cvt` matches the processor and an independent reference under all 16 MXCSR words (4 rounding controls x DAZ x FTZ) with 32 further fixed answers; 33 patched-instruction rejections and 14 signature rejections pass. Same output at -O0, -O2 and -O3. - `python3 -B tests/form_coverage_check.py`, `python3 -B tests/docs_links_check.py`, `python3 -B tests/bidi_controls_check.py`, `python3 -m py_compile`: pass. - MinGW (`x86_64-w64-mingw32-g++`, -O0/-O2/-O3) build of the translation under Wine against the fixture DLL: 8,400 calls over the 16 MXCSR words, 0 mismatches. - Negative control, fails before: with `tools/cpu_sse_aot.py` from `origin/main` the new fixture fails at translation (`unsupported instruction at RVA 0x1370: cvtss2si eax,xmm0`). With the conversion lowered as a truncation, or with a 32-bit destination that keeps the upper half, the check fails at `round_cvt(0, 0x3ff0000000000000)` and `round_cvt(0, 0)` respectively. - The reference is exact rational arithmetic in Python (no host instruction, none of the translator's code). Eight wrong rules (truncation, ties away from zero, zero indefinite, nearest-only, DAZ ignored, 32-bit result keeping or sign-extending the upper half, a 64-bit result narrowed to 32 bits) must each disagree with the processor on exactly the words of their destination size and on no other word. - Not run by me: the Windows MASM/MSVC `ctest`. PR outside `--preset pr`: `ctest --preset dev -R '^cpu_aot_scalar_float$'` (and `--preset release`). The scalar-fp fuzz area does not generate these instructions and is unchanged. ## Evidence and follow-up - New fixture code is original MASM in `tests/cpu/scalar_float.asm`, one new export `round_cvt(float, double, uint8_t*)`: 32- and 64-bit destinations from a register and from memory, each 32-bit destination starting from all ones. All stores are inside an 80-byte window of the guarded buffer, and the check compares the whole buffer with 8 guard bytes either side, so nothing outside the eight result words, the float and the double may change. - Operands: ties at 0.5, 1.5, 2.5 and their negatives, one step either side of a tie, the limits around 2^31 and 2^63 and their negatives, 2147483647.5, denormals of both signs, infinities and quiet and signalling NaNs. - Refused as before: VEX forms (`vcvtss2si`), `CVTPS2PI`/`CVTPD2PI` (MMX destination), an unwritten source register, a nonvolatile destination, a RIP-relative source. The old rejection control that used `CVTSS2SI` as the unsupported example now uses `CVTPS2PI`. - Not covered: the exception flags, a guest MXCSR write, `CVTPS2PI`/`CVTPD2PI`, EVEX forms. - AI-assisted. ## 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 11:02:44 +00:00
tedin7 commented 2026-11-24 11:02:58 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-24T10:12:23Z.


Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit 166a6a9a62987acc1004d5e4ee76558dc2dfa68f:

  • Windows Python checks: tests/form_coverage_check.py ok (39.8s)
  • Debug (--preset dev):
    • Build (cpu_float_*, cpu_mxcsr_harness, cpu_fuzz_scalar_fp*): 3.76s (configure 1.65s)
    • Test (ctest --preset dev -R '^(cpu_aot_scalar_float|cpu_aot_fuzz_scalar_fp)$' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (cpu_aot_fuzz_scalar_fp 1.19s, cpu_aot_scalar_float 3.79s)
  • Release /O2 (--preset release):
    • Build (cpu_float_*, cpu_mxcsr_harness, cpu_fuzz_scalar_fp*): 1.92s (configure 1.59s)
    • Test (ctest --preset release -R '^(cpu_aot_scalar_float|cpu_aot_fuzz_scalar_fp)$' --parallel 4 --no-tests=error --output-on-failure): 2/2 passed (cpu_aot_fuzz_scalar_fp 1.38s, cpu_aot_scalar_float 3.70s)
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7128460412 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1385#issuecomment-7128460412). Originally posted by [tedin7](https://github.com/tedin7) on 2026-11-24T10:12:23Z. --- Verified on Windows Server 2025 (Build 26100, MSVC 14.44.35207) at commit `166a6a9a62987acc1004d5e4ee76558dc2dfa68f`: - **Windows Python checks**: `tests/form_coverage_check.py` ok (`39.8s`) - **Debug (`--preset dev`)**: - Build (`cpu_float_*`, `cpu_mxcsr_harness`, `cpu_fuzz_scalar_fp*`): `3.76s` (configure `1.65s`) - Test (`ctest --preset dev -R '^(cpu_aot_scalar_float|cpu_aot_fuzz_scalar_fp)$' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`cpu_aot_fuzz_scalar_fp` `1.19s`, `cpu_aot_scalar_float` `3.79s`) - **Release `/O2` (`--preset release`)**: - Build (`cpu_float_*`, `cpu_mxcsr_harness`, `cpu_fuzz_scalar_fp*`): `1.92s` (configure `1.59s`) - Test (`ctest --preset release -R '^(cpu_aot_scalar_float|cpu_aot_fuzz_scalar_fp)$' --parallel 4 --no-tests=error --output-on-failure`): **2/2 passed** (`cpu_aot_fuzz_scalar_fp` `1.38s`, `cpu_aot_scalar_float` `3.70s`)
chatgpt-codex-connector[bot commented 2026-11-24 11:02:59 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-24T10:12:32Z.


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-24T10:18:03.332209Z 166a6a9 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.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7128463824 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/1385#issuecomment-7128463824). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-24T10:12:32Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"166a6a9a62987acc1004d5e4ee76558dc2dfa68f","has_any_findings":false,"has_unresolved_findings":false,"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-24T10:18:03.332209Z">2026-11-24T10:18:03.332209Z</relative-time> | `166a6a9` | 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>
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!1378
No description provided.