cpu: lower all 24 VEX-encoded scalar FMA3 instructions (ss/sd) #342

Merged
Vonsek merged 5 commits from upstream-pr/347 into main 2026-11-22 11:48:47 +00:00
Vonsek commented 2026-11-21 22:56:53 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T22:56:53Z.


Change

  • Lower all 24 VEX-encoded scalar FMA3 instructions (vfmadd/vfmsub/vfnmadd/vfnmsub × 132/213/231 × ss/sd) in tools/ffx_version_query.py using single-rounding std::fmaf/std::fma, preserving upper XMM bytes of dst, zeroing the upper 128 bits of YMM, and rejecting EVEX forms.
  • Add a source-built MASM fixture (tests/cpu/fma_scalar.asm, tests/cpu/fma_scalar.def) and differential test (tests/cpu/fma_scalar.py) wired into CMakeLists.txt as cpu_aot_fma_scalar with a host_has_fma() check (OSXSAVE, XCR0 YMM state, and CPUID leaf 1 ECX bit 12 FMA) and SKIP_RETURN_CODE 77 on unsupported hosts.
  • Verify 120 bit-for-bit cases across all 24 exports (including exact-cancellation triples where single-rounding FMA differs from unfused multiply-add, upper-XMM preservation, upper-YMM zeroing, and an EVEX rejection control).
  • Classify fma_scalar -> cpu_aot_fma_scalar in tools/form_coverage.py (+24 FMA mnemonics, raising FMA from 0/60 to 24/60 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_fma_scalar_lifted failed with:
    ValueError: unsupported query operation at 0x100d: vfmadd132ss xmm0,xmm1,xmm2
    
  • After the change:
    • cmake --build --preset dev --target cpu_fma_scalar_lifted succeeded.
    • ctest --preset dev --no-tests=error -R "^cpu_aot_fma_scalar$" -V: passed (120 scalar FMA3 cases across all 24 ss/sd forms matched native and translated DLLs bit-for-bit).
    • python -B tests/form_coverage_check.py: form coverage checks passed.

Evidence and follow-up

  • Adds all 24 scalar FMA3 mnemonics (0/60 -> 24/60) in docs/coverage/x64_zen2.tsv; the remaining 36 packed FMA3 forms (ps/pd and maddsub/msubadd) can follow in a separate focused PR.
  • 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
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535494862 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/347). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T22:56:53Z. --- ## Change - Lower all 24 VEX-encoded scalar FMA3 instructions (`vfmadd`/`vfmsub`/`vfnmadd`/`vfnmsub` × `132`/`213`/`231` × `ss`/`sd`) in `tools/ffx_version_query.py` using single-rounding `std::fmaf`/`std::fma`, preserving upper XMM bytes of `dst`, zeroing the upper 128 bits of YMM, and rejecting EVEX forms. - Add a source-built MASM fixture (`tests/cpu/fma_scalar.asm`, `tests/cpu/fma_scalar.def`) and differential test (`tests/cpu/fma_scalar.py`) wired into `CMakeLists.txt` as `cpu_aot_fma_scalar` with a `host_has_fma()` check (`OSXSAVE`, `XCR0` YMM state, and CPUID leaf 1 ECX bit 12 `FMA`) and `SKIP_RETURN_CODE 77` on unsupported hosts. - Verify 120 bit-for-bit cases across all 24 exports (including exact-cancellation triples where single-rounding FMA differs from unfused multiply-add, upper-XMM preservation, upper-YMM zeroing, and an EVEX rejection control). - Classify `fma_scalar` -> `cpu_aot_fma_scalar` in `tools/form_coverage.py` (+24 FMA mnemonics, raising FMA from 0/60 to 24/60 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_fma_scalar_lifted` failed with: ```text ValueError: unsupported query operation at 0x100d: vfmadd132ss xmm0,xmm1,xmm2 ``` - After the change: - `cmake --build --preset dev --target cpu_fma_scalar_lifted` succeeded. - `ctest --preset dev --no-tests=error -R "^cpu_aot_fma_scalar$" -V`: passed (`120 scalar FMA3 cases across all 24 ss/sd forms matched native and translated DLLs bit-for-bit`). - `python -B tests/form_coverage_check.py`: `form coverage checks passed`. ## Evidence and follow-up - Adds all 24 scalar FMA3 mnemonics (`0/60 -> 24/60`) in `docs/coverage/x64_zen2.tsv`; the remaining 36 packed FMA3 forms (`ps`/`pd` and `maddsub`/`msubadd`) can follow in a separate focused PR. - 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
upstream-sync closed this pull request 2026-11-24 06:42:33 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:42:34 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T23:06:35Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 609b97b613

ℹ️ 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".

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review:6008776097 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/347#pullrequestreview-6008776097). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:06:35Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `609b97b613` <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>
chatgpt-codex-connector[bot commented 2026-11-24 06:48:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T23:00:44Z.


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-21T23:06:38.242218Z 609b97b 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:7070580249 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/347#issuecomment-7070580249). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:00:44Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"609b97b613a6aa7ef570817286f564fae0d4ae30","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-21T23:06:38.242218Z">2026-11-21T23:06:38.242218Z</relative-time> | `609b97b` | 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>
Vonsek commented 2026-11-24 06:48:11 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-21T23:14:56Z.


Added fma_x86 / quiet_nan helpers in tools/ffx_version_query.py to propagate NaNs in x86 (dst, src1, src2) order without flipping their sign bits, and added raw-bit qNaN and sNaN inputs across all three operand positions in tests/cpu/fma_scalar.py (192 bit-for-bit cases passing).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7070745752 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/347#issuecomment-7070745752). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T23:14:56Z. --- Added `fma_x86` / `quiet_nan` helpers in `tools/ffx_version_query.py` to propagate NaNs in x86 `(dst, src1, src2)` order without flipping their sign bits, and added raw-bit qNaN and sNaN inputs across all three operand positions in `tests/cpu/fma_scalar.py` (192 bit-for-bit cases passing).
chatgpt-codex-connector[bot commented 2026-11-24 06:50:37 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-21T23:06:35Z.

Code review location: tools/ffx_version_query.py; line 230

@@ -210,6 +216,19 @@ def write(expr):
             stmt = (f"{{ auto input = {src}; {dst} = {{}}; for (size_t n=0; n<16; n+=4) {{ "
                     f"auto v = load<uint32_t>(reinterpret_cast<word>(input.data()+n)) {operation}; "
                     f"memcpy({dst}.data()+n, &v, 4); }} }}")
+        elif has_wide and i.encoding == EncodingKind.VEX and op in fma_scalar:
+            kind, order, is_f64 = fma_scalar[op]
+            ftype, fn, sz = ("double", "std::fma", 8) if is_f64 else ("float", "std::fmaf", 4)
+            dst, src1 = wide_any[i.op0_register], wide_any[i.op1_register]
+            s0 = f"load<{ftype}>(reinterpret_cast<word>({dst}.data()))"
+            s1 = f"load<{ftype}>(reinterpret_cast<word>({src1}.data()))"
+            s2 = (f"load<{ftype}>({address()})" if i.op2_kind == K.MEMORY
+                  else f"load<{ftype}>(reinterpret_cast<word>({wide_any[i.op2_register]}.data()))")
+            a, b, c = (s0, s2, s1) if order == "132" else (s1, s0, s2) if order == "213" else (s1, s2, s0)
+            neg_a = "-" if kind in ("nmadd", "nmsub") else ""
+            neg_c = "-" if kind in ("msub", "nmsub") else ""
+            stmt = (f"{{ const {ftype} r = {fn}({neg_a}({a}), ({b}), {neg_c}({c})); "

P2 Badge Preserve NaN signs in the negated FMA lowerings

When a negated operand is a NaN, these unary negations flip its sign bit, whereas the corresponding x86 FMA instruction propagates the NaN without applying that arithmetic sign change. For example, with low lanes (1.0f, 1.0f, qNaN(0x7fc12345)), native vfmsub213ss stores 0x7fc12345, but the emitted std::fmaf(1.0f, 1.0f, -qNaN) stores 0xffc12345 (reproduced using the actual emitter on x86-64 Linux/GCC 13); the sd form has the same mismatch. This breaks bit-for-bit translation for NaN inputs. Handle native NaN propagation before applying the sign inversions, and add raw-bit NaN inputs to the differential fixture.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655461931 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/347#discussion_r4655461931). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:06:35Z. **Code review location:** `tools/ffx_version_query.py`; line 230 ~~~~diff @@ -210,6 +216,19 @@ def write(expr): stmt = (f"{{ auto input = {src}; {dst} = {{}}; for (size_t n=0; n<16; n+=4) {{ " f"auto v = load<uint32_t>(reinterpret_cast<word>(input.data()+n)) {operation}; " f"memcpy({dst}.data()+n, &v, 4); }} }}") + elif has_wide and i.encoding == EncodingKind.VEX and op in fma_scalar: + kind, order, is_f64 = fma_scalar[op] + ftype, fn, sz = ("double", "std::fma", 8) if is_f64 else ("float", "std::fmaf", 4) + dst, src1 = wide_any[i.op0_register], wide_any[i.op1_register] + s0 = f"load<{ftype}>(reinterpret_cast<word>({dst}.data()))" + s1 = f"load<{ftype}>(reinterpret_cast<word>({src1}.data()))" + s2 = (f"load<{ftype}>({address()})" if i.op2_kind == K.MEMORY + else f"load<{ftype}>(reinterpret_cast<word>({wide_any[i.op2_register]}.data()))") + a, b, c = (s0, s2, s1) if order == "132" else (s1, s0, s2) if order == "213" else (s1, s2, s0) + neg_a = "-" if kind in ("nmadd", "nmsub") else "" + neg_c = "-" if kind in ("msub", "nmsub") else "" + stmt = (f"{{ const {ftype} r = {fn}({neg_a}({a}), ({b}), {neg_c}({c})); " ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve NaN signs in the negated FMA lowerings** When a negated operand is a NaN, these unary negations flip its sign bit, whereas the corresponding x86 FMA instruction propagates the NaN without applying that arithmetic sign change. For example, with low lanes `(1.0f, 1.0f, qNaN(0x7fc12345))`, native `vfmsub213ss` stores `0x7fc12345`, but the emitted `std::fmaf(1.0f, 1.0f, -qNaN)` stores `0xffc12345` (reproduced using the actual emitter on x86-64 Linux/GCC 13); the `sd` form has the same mismatch. This breaks bit-for-bit translation for NaN inputs. Handle native NaN propagation before applying the sign inversions, and add raw-bit NaN inputs to the differential fixture. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
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!342
No description provided.