cpu: lower baseline 128-bit SSE/SSE2 packed floating-point instructions #368

Closed
Vonsek wants to merge 2 commits from upstream-pr/374 into main
Vonsek commented 2026-11-21 23:41:37 +00:00 (Migrated from github.com)

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


Change

  • Lower legacy 128-bit movups/movupd and 30 baseline SSE/SSE2 packed floating-point instructions in tools/ffx_version_query.py:
    • Packed arithmetic and square root: addps, subps, mulps, divps, sqrtps, addpd, subpd, mulpd, divpd, sqrtpd
    • Packed min/max with exact x86 second-operand tie/NaN selection: minps, maxps, minpd, maxpd
    • Packed bitwise ops: andps, andnps, orps, xorps, andpd, andnpd, orpd, xorpd
    • Packed shuffle, unpack, and sign-bit mask: shufps, unpcklps, unpckhps, movmskps, shufpd, unpcklpd, unpckhpd, movmskpd
  • Restrict lowering to EncodingKind.LEGACY XMM forms, preserving upper YMM bytes (16..31) and rejecting VEX encodings.
  • Add source-built MASM differential fixture tests/cpu/sse_packed_fp.{asm,def,py}, CMake target cpu_sse_packed_fp_lifted, CTest cpu_aot_sse_packed_fp, coverage mapping in tools/form_coverage.py, and documentation in docs/experiments.md.

Verification

  • Confirmed pre-fix failure on cmake --build --preset dev --target cpu_sse_packed_fp_lifted:
    ValueError: unsupported query operation at 0x1000: movups xmm0,[rcx]
  • Built and tested on Windows 11 x64 (VS 2022 Build Tools):
    • cmake --build --preset dev --target cpu_sse_packed_fp_lifted succeeded with 0 warnings
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_sse_packed_fp|cpu_aot_bmi1)$" -V: 2/2 passed (34 baseline 128-bit SSE/SSE2 packed FP cases matched native and translated DLLs, including ±0.0 and qNaN second-operand selection in min/max + VEX vaddps rejection control)
    • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: form coverage checks passed

Evidence and follow-up

  • All 32 instructions are baseline x86-64 SSE/SSE2 and require no CPUID capability gate.
  • Covers 16 SSE-grouped mnemonics and 16 SSE2-grouped mnemonics in docs/coverage/x64_zen2.tsv using source-built fixtures.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535893814 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/374). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T23:41:37Z. --- ## Change - Lower legacy 128-bit `movups`/`movupd` and 30 baseline SSE/SSE2 packed floating-point instructions in `tools/ffx_version_query.py`: - Packed arithmetic and square root: `addps`, `subps`, `mulps`, `divps`, `sqrtps`, `addpd`, `subpd`, `mulpd`, `divpd`, `sqrtpd` - Packed min/max with exact x86 second-operand tie/NaN selection: `minps`, `maxps`, `minpd`, `maxpd` - Packed bitwise ops: `andps`, `andnps`, `orps`, `xorps`, `andpd`, `andnpd`, `orpd`, `xorpd` - Packed shuffle, unpack, and sign-bit mask: `shufps`, `unpcklps`, `unpckhps`, `movmskps`, `shufpd`, `unpcklpd`, `unpckhpd`, `movmskpd` - Restrict lowering to `EncodingKind.LEGACY` XMM forms, preserving upper YMM bytes (16..31) and rejecting VEX encodings. - Add source-built MASM differential fixture `tests/cpu/sse_packed_fp.{asm,def,py}`, CMake target `cpu_sse_packed_fp_lifted`, CTest `cpu_aot_sse_packed_fp`, coverage mapping in `tools/form_coverage.py`, and documentation in `docs/experiments.md`. ## Verification - Confirmed pre-fix failure on `cmake --build --preset dev --target cpu_sse_packed_fp_lifted`: `ValueError: unsupported query operation at 0x1000: movups xmm0,[rcx]` - Built and tested on Windows 11 x64 (VS 2022 Build Tools): - `cmake --build --preset dev --target cpu_sse_packed_fp_lifted` succeeded with 0 warnings - `ctest --preset dev --no-tests=error -R "^(cpu_aot_sse_packed_fp|cpu_aot_bmi1)$" -V`: 2/2 passed (`34 baseline 128-bit SSE/SSE2 packed FP cases matched native and translated DLLs`, including ±0.0 and qNaN second-operand selection in `min`/`max` + VEX `vaddps` rejection control) - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py`: `form coverage checks passed` ## Evidence and follow-up - All 32 instructions are baseline x86-64 SSE/SSE2 and require no CPUID capability gate. - Covers 16 `SSE`-grouped mnemonics and 16 `SSE2`-grouped mnemonics in `docs/coverage/x64_zen2.tsv` using source-built fixtures.
upstream-sync closed this pull request 2026-11-24 06:42:42 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:42:42 +00:00 (Migrated from github.com)

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

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: f35be10b50

ℹ️ 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:6008966043 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/374#pullrequestreview-6008966043). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:51:42Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `f35be10b50` <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:13 +00:00 (Migrated from github.com)

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


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:51:44.279955Z f35be10 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:7071079035 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/374#issuecomment-7071079035). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:45:01Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"f35be10b50f8f6a03c45e7f194b77ef01d71cdd2","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:51:44.279955Z">2026-11-21T23:51:44.279955Z</relative-time> | `f35be10` | 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>
R0Y00 commented 2026-11-24 06:49:04 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T07:19:16Z.


Closing as superseded. I assembled the 30 MASM exports from this PR at 64b60df and translated those exports with main at 66669bb. All 30 lowered, and an /O2 build of the translated DLL matched the native DLL in all 34 data cases, including the MIN/MAX NaN and signed-zero cases and the MOVMSK references. The old VEX refusal control was omitted from that comparison because main supports VEX.128 through the shared lowering. Thanks for the independent fixture and verification.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7094205174 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/374#issuecomment-7094205174). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T07:19:16Z. --- Closing as superseded. I assembled the 30 MASM exports from this PR at 64b60df and translated those exports with main at 66669bb. All 30 lowered, and an /O2 build of the translated DLL matched the native DLL in all 34 data cases, including the MIN/MAX NaN and signed-zero cases and the MOVMSK references. The old VEX refusal control was omitted from that comparison because main supports VEX.128 through the shared lowering. Thanks for the independent fixture and verification.
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:51:42Z.

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

@@ -210,6 +219,76 @@ 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 op in (M.MOVUPS, M.MOVUPD) and i.encoding == EncodingKind.LEGACY and has_wide:
+            if i.op0_kind == K.REGISTER and i.op0_register in wide_xmm and i.op1_kind == K.MEMORY:
+                stmt = f"memcpy({wide_xmm[i.op0_register]}.data(), reinterpret_cast<const void*>({address()}), 16);"
+            elif i.op0_kind == K.MEMORY and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm:
+                stmt = f"memcpy(reinterpret_cast<void*>({address()}), {wide_xmm[i.op1_register]}.data(), 16);"
+            elif (i.op0_kind == K.REGISTER and i.op0_register in wide_xmm
+                  and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm):
+                stmt = f"memcpy({wide_xmm[i.op0_register]}.data(), {wide_xmm[i.op1_register]}.data(), 16);"
+            else:
+                raise ValueError(f"unsupported {op} form at {i.ip:#x}: {i}")
+        elif (op in (M.MOVMSKPS, M.MOVMSKPD) and i.encoding == EncodingKind.LEGACY and has_wide
+              and i.op0_kind == K.REGISTER and RegisterExt.size(i.op0_register) in (4, 8)
+              and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm):
+            src = wide_xmm[i.op1_register]
+            lane_size = 4 if op == M.MOVMSKPS else 8
+            lanes = 16 // lane_size
+            stmt = (f"{{ uint32_t m = 0; for (size_t k=0; k<{lanes}; ++k) {{ "
+                    f"if ({src}[(k + 1) * {lane_size} - 1] & 0x80u) m |= (1u << k); }} {write('m')} }}")
+        elif (op in sse_packed_fp_ops and op not in (M.MOVUPS, M.MOVUPD, M.MOVMSKPS, M.MOVMSKPD)
+              and i.encoding == EncodingKind.LEGACY and i.op0_kind == K.REGISTER and i.op0_register in wide_xmm and has_wide):
+            dst = wide_xmm[i.op0_register]
+            if i.op1_kind == K.REGISTER and i.op1_register in wide_xmm:
+                src_init = f"std::array<uint8_t,16> rhs; memcpy(rhs.data(), {wide_xmm[i.op1_register]}.data(), 16);"
+            elif i.op1_kind == K.MEMORY:
+                src_init = f"std::array<uint8_t,16> rhs; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), 16);"
+            else:
+                raise ValueError(f"unsupported SSE packed FP source at {i.ip:#x}: {i}")
+            if op in (M.ANDPS, M.ANDPD, M.ANDNPS, M.ANDNPD, M.ORPS, M.ORPD, M.XORPS, M.XORPD):
+                expr = {M.ANDPS: "a & b", M.ANDPD: "a & b", M.ANDNPS: "(~a) & b", M.ANDNPD: "(~a) & b",
+                        M.ORPS: "a | b", M.ORPD: "a | b", M.XORPS: "a ^ b", M.XORPD: "a ^ b"}[op]
+                stmt = (f"{{ {src_init} for (size_t n=0; n<16; ++n) {{ "
+                        f"const uint8_t a = {dst}[n], b = rhs[n]; {dst}[n] = static_cast<uint8_t>({expr}); }} }}")
+            elif op in (M.SQRTPS, M.SQRTPD):
+                ftype, lane_size = ("float", 4) if op == M.SQRTPS else ("double", 8)
+                stmt = (f"{{ {src_init} for (size_t n=0; n<16; n+={lane_size}) {{ "
+                        f"{ftype} b; memcpy(&b, rhs.data()+n, {lane_size}); "
+                        f"const {ftype} v = std::sqrt(b); memcpy({dst}.data()+n, &v, {lane_size}); }} }}")
+            elif op in (M.ADDPS, M.SUBPS, M.MULPS, M.DIVPS, M.MINPS, M.MAXPS,
+                        M.ADDPD, M.SUBPD, M.MULPD, M.DIVPD, M.MINPD, M.MAXPD):
+                is_ps = op in (M.ADDPS, M.SUBPS, M.MULPS, M.DIVPS, M.MINPS, M.MAXPS)
+                ftype, lane_size = ("float", 4) if is_ps else ("double", 8)
+                expr = {
+                    M.ADDPS: "a + b", M.ADDPD: "a + b", M.SUBPS: "a - b", M.SUBPD: "a - b",
+                    M.MULPS: "a * b", M.MULPD: "a * b", M.DIVPS: "a / b", M.DIVPD: "a / b",
+                    M.MINPS: "(a < b) ? a : b", M.MINPD: "(a < b) ? a : b",
+                    M.MAXPS: "(a > b) ? a : b", M.MAXPD: "(a > b) ? a : b",

P2 Badge Preserve DAZ semantics in packed min/max lowering

When MXCSR.DAZ is enabled, these comparisons treat subnormal inputs as zero, but selecting and copying the original a or b preserves its subnormal bits. For example, minps with a=1.0f and b bits 0x00000001 produces 0x00000000 natively, whereas the emitted unoptimized loop produces 0x00000001. This mismatch reproduces for all four min/max variants using the generated statements in a Linux x64 probe. Callers using DAZ therefore receive different results from native execution, and the current fixture does not exercise this mode. Use the corresponding SSE intrinsics or explicitly apply DAZ normalization, and add a differential case with DAZ enabled.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655609679 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/374#discussion_r4655609679). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:51:42Z. **Code review location:** `tools/ffx_version_query.py`; line 267 ~~~~diff @@ -210,6 +219,76 @@ 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 op in (M.MOVUPS, M.MOVUPD) and i.encoding == EncodingKind.LEGACY and has_wide: + if i.op0_kind == K.REGISTER and i.op0_register in wide_xmm and i.op1_kind == K.MEMORY: + stmt = f"memcpy({wide_xmm[i.op0_register]}.data(), reinterpret_cast<const void*>({address()}), 16);" + elif i.op0_kind == K.MEMORY and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm: + stmt = f"memcpy(reinterpret_cast<void*>({address()}), {wide_xmm[i.op1_register]}.data(), 16);" + elif (i.op0_kind == K.REGISTER and i.op0_register in wide_xmm + and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm): + stmt = f"memcpy({wide_xmm[i.op0_register]}.data(), {wide_xmm[i.op1_register]}.data(), 16);" + else: + raise ValueError(f"unsupported {op} form at {i.ip:#x}: {i}") + elif (op in (M.MOVMSKPS, M.MOVMSKPD) and i.encoding == EncodingKind.LEGACY and has_wide + and i.op0_kind == K.REGISTER and RegisterExt.size(i.op0_register) in (4, 8) + and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm): + src = wide_xmm[i.op1_register] + lane_size = 4 if op == M.MOVMSKPS else 8 + lanes = 16 // lane_size + stmt = (f"{{ uint32_t m = 0; for (size_t k=0; k<{lanes}; ++k) {{ " + f"if ({src}[(k + 1) * {lane_size} - 1] & 0x80u) m |= (1u << k); }} {write('m')} }}") + elif (op in sse_packed_fp_ops and op not in (M.MOVUPS, M.MOVUPD, M.MOVMSKPS, M.MOVMSKPD) + and i.encoding == EncodingKind.LEGACY and i.op0_kind == K.REGISTER and i.op0_register in wide_xmm and has_wide): + dst = wide_xmm[i.op0_register] + if i.op1_kind == K.REGISTER and i.op1_register in wide_xmm: + src_init = f"std::array<uint8_t,16> rhs; memcpy(rhs.data(), {wide_xmm[i.op1_register]}.data(), 16);" + elif i.op1_kind == K.MEMORY: + src_init = f"std::array<uint8_t,16> rhs; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), 16);" + else: + raise ValueError(f"unsupported SSE packed FP source at {i.ip:#x}: {i}") + if op in (M.ANDPS, M.ANDPD, M.ANDNPS, M.ANDNPD, M.ORPS, M.ORPD, M.XORPS, M.XORPD): + expr = {M.ANDPS: "a & b", M.ANDPD: "a & b", M.ANDNPS: "(~a) & b", M.ANDNPD: "(~a) & b", + M.ORPS: "a | b", M.ORPD: "a | b", M.XORPS: "a ^ b", M.XORPD: "a ^ b"}[op] + stmt = (f"{{ {src_init} for (size_t n=0; n<16; ++n) {{ " + f"const uint8_t a = {dst}[n], b = rhs[n]; {dst}[n] = static_cast<uint8_t>({expr}); }} }}") + elif op in (M.SQRTPS, M.SQRTPD): + ftype, lane_size = ("float", 4) if op == M.SQRTPS else ("double", 8) + stmt = (f"{{ {src_init} for (size_t n=0; n<16; n+={lane_size}) {{ " + f"{ftype} b; memcpy(&b, rhs.data()+n, {lane_size}); " + f"const {ftype} v = std::sqrt(b); memcpy({dst}.data()+n, &v, {lane_size}); }} }}") + elif op in (M.ADDPS, M.SUBPS, M.MULPS, M.DIVPS, M.MINPS, M.MAXPS, + M.ADDPD, M.SUBPD, M.MULPD, M.DIVPD, M.MINPD, M.MAXPD): + is_ps = op in (M.ADDPS, M.SUBPS, M.MULPS, M.DIVPS, M.MINPS, M.MAXPS) + ftype, lane_size = ("float", 4) if is_ps else ("double", 8) + expr = { + M.ADDPS: "a + b", M.ADDPD: "a + b", M.SUBPS: "a - b", M.SUBPD: "a - b", + M.MULPS: "a * b", M.MULPD: "a * b", M.DIVPS: "a / b", M.DIVPD: "a / b", + M.MINPS: "(a < b) ? a : b", M.MINPD: "(a < b) ? a : b", + M.MAXPS: "(a > b) ? a : b", M.MAXPD: "(a > b) ? a : b", ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve DAZ semantics in packed min/max lowering** When MXCSR.DAZ is enabled, these comparisons treat subnormal inputs as zero, but selecting and copying the original `a` or `b` preserves its subnormal bits. For example, `minps` with `a=1.0f` and `b` bits `0x00000001` produces `0x00000000` natively, whereas the emitted unoptimized loop produces `0x00000001`. This mismatch reproduces for all four min/max variants using the generated statements in a Linux x64 probe. Callers using DAZ therefore receive different results from native execution, and the current fixture does not exercise this mode. Use the corresponding SSE intrinsics or explicitly apply DAZ normalization, and add a differential case with DAZ enabled. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->

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