cpu: lower SSE3 and SSE4.1 floating-point and data-movement instructions #369

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

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


Change

  • Lower 21 SSE3 and SSE4.1 floating-point and data-movement instructions (plus legacy movups/movupd) in tools/ffx_version_query.py:
    • SSE3: addsubps, addsubpd, haddps, haddpd, hsubps, hsubpd, movddup, movsldup, movshdup, lddqu
    • SSE4.1: blendps, blendpd, blendvps, blendvpd, dpps, dppd, roundps, roundpd, roundss, roundsd, extractps
  • Restrict lowering to EncodingKind.LEGACY XMM forms, implement exact host-independent ties-to-even/floor/ceil/trunc rounding in round_x86, and reject MXCSR rounding mode (imm8 & 4 != 0).
  • Probe CPUID leaf 1 ECX bits 0 (SSE3) and 19 (SSE4.1) before invoking the native fixture (exiting 77 with SKIP_RETURN_CODE 77 on hosts without SSE3/SSE4.1).
  • Add source-built MASM differential fixture tests/cpu/sse3_4_fp.{asm,def,py}, CMake target cpu_sse3_4_fp_lifted, CTest cpu_aot_sse3_4_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_sse3_4_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_sse3_4_fp_lifted succeeded with 0 warnings
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_sse3_4_fp|cpu_aot_bmi1)$" -V: 2/2 passed (23 SSE3/SSE4.1 floating-point and data-movement cases matched native and translated DLLs + MXCSR-mode roundps rejection control)
    • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: form coverage checks passed

Evidence and follow-up

  • Covers 21 SSE3-4-grouped mnemonics in docs/coverage/x64_zen2.tsv using source-built fixtures, gated by a runtime CPUID check.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6535922257 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-21T23:44:38Z. --- ## Change - Lower 21 SSE3 and SSE4.1 floating-point and data-movement instructions (plus legacy `movups`/`movupd`) in `tools/ffx_version_query.py`: - SSE3: `addsubps`, `addsubpd`, `haddps`, `haddpd`, `hsubps`, `hsubpd`, `movddup`, `movsldup`, `movshdup`, `lddqu` - SSE4.1: `blendps`, `blendpd`, `blendvps`, `blendvpd`, `dpps`, `dppd`, `roundps`, `roundpd`, `roundss`, `roundsd`, `extractps` - Restrict lowering to `EncodingKind.LEGACY` XMM forms, implement exact host-independent ties-to-even/floor/ceil/trunc rounding in `round_x86`, and reject MXCSR rounding mode (imm8 & 4 != 0). - Probe CPUID leaf 1 ECX bits 0 (`SSE3`) and 19 (`SSE4.1`) before invoking the native fixture (exiting 77 with `SKIP_RETURN_CODE 77` on hosts without SSE3/SSE4.1). - Add source-built MASM differential fixture `tests/cpu/sse3_4_fp.{asm,def,py}`, CMake target `cpu_sse3_4_fp_lifted`, CTest `cpu_aot_sse3_4_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_sse3_4_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_sse3_4_fp_lifted` succeeded with 0 warnings - `ctest --preset dev --no-tests=error -R "^(cpu_aot_sse3_4_fp|cpu_aot_bmi1)$" -V`: 2/2 passed (`23 SSE3/SSE4.1 floating-point and data-movement cases matched native and translated DLLs` + MXCSR-mode `roundps` rejection control) - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py`: `form coverage checks passed` ## Evidence and follow-up - Covers 21 `SSE3-4`-grouped mnemonics in `docs/coverage/x64_zen2.tsv` using source-built fixtures, gated by a runtime CPUID check.
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:54:25Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9236fdc991

ℹ️ 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:6008982250 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#pullrequestreview-6008982250). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:54:25Z. **Review:** COMMENTED --- <!-- codex-review-receipt:v1 --> ### 💡 Codex Review Here are some automated review suggestions for this pull request. **Reviewed commit:** `9236fdc991` <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:47:50Z.


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:54:28.332853Z 9236fdc 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:7071110774 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#issuecomment-7071110774). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:47:50Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"9236fdc991f01026513340625c4321b53e6d601a","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:54:28.332853Z">2026-11-21T23:54:28.332853Z</relative-time> | `9236fdc` | 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>
legacy-code-archaeologist commented 2026-11-24 06:48:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by legacy-code-archaeologist on 2026-11-22T19:26:02Z.


Verified 8510482 on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, dev preset. ctest --preset dev --no-tests=error -R "^cpu_" -j 4 passes 81 of 81 on this branch's base.

Two notes:

  • tests/cpu/sse3_4_fp.py exercises dpps_op/dppd_op only with finite inputs; there are no NaN or infinity lanes. In #602 a DPPS case with several NaN products failed on this Intel host because the reference model encodes one vendor's NaN choice, and the translation runs the host's own dot-product instruction. The same case does not arise here yet, but a multi-NaN dot product would need the same vendor-tolerant check.
  • The branch no longer merges cleanly with main at ff7f68a (git merge-tree).

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7084398076 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#issuecomment-7084398076). Originally posted by [legacy-code-archaeologist](https://github.com/legacy-code-archaeologist) on 2026-11-22T19:26:02Z. --- Verified `8510482` on an Intel Core i7-6820HQ (Skylake), Windows 11 26100.9168, MSVC/ML64 19.52.37029, `dev` preset. `ctest --preset dev --no-tests=error -R "^cpu_" -j 4` passes 81 of 81 on this branch's base. Two notes: - `tests/cpu/sse3_4_fp.py` exercises `dpps_op/dppd_op` only with finite inputs; there are no NaN or infinity lanes. In #602 a DPPS case with several NaN products failed on this Intel host because the reference model encodes one vendor's NaN choice, and the translation runs the host's own dot-product instruction. The same case does not arise here yet, but a multi-NaN dot product would need the same vendor-tolerant check. - The branch no longer merges cleanly with `main` at `ff7f68a` (`git merge-tree`). AI-assisted (Claude Code).
R0Y00 commented 2026-11-24 06:49:10 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T13:43:10Z.


I rebuilt your original 23 exports at 8510482 and translated them with the current #602 candidate (3007320). All 23 positive cases matched the native DLL in an independent MSVC /O2 build, including the legacy MOVUPS/MOVUPD transport and rounding cases. The old MXCSR-selected rejection check was outside that positive comparison because #602 explicitly tests that mode as supported.

I am keeping these in the same merge queue. #602 is still open while its Windows check reruns for a separate WARP failure. Your original fixture and the independent Skylake report remain useful cross-checks for the overlapping implementation.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7102931769 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#issuecomment-7102931769). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T13:43:10Z. --- I rebuilt your original 23 exports at 8510482 and translated them with the current #602 candidate (3007320). All 23 positive cases matched the native DLL in an independent MSVC /O2 build, including the legacy MOVUPS/MOVUPD transport and rounding cases. The old MXCSR-selected rejection check was outside that positive comparison because #602 explicitly tests that mode as supported. I am keeping these in the same merge queue. #602 is still open while its Windows check reruns for a separate WARP failure. Your original fixture and the independent Skylake report remain useful cross-checks for the overlapping implementation.
R0Y00 commented 2026-11-24 06:49:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T14:37:13Z.


#602 is now merged. As recorded in the independent comparison above, all 23 positive exports from this branch matched its shared lowering. The Skylake report remains linked here, including the DPPS NaN-selection observation. Closing this overlapping implementation as a duplicate of #602.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7104276920 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#issuecomment-7104276920). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T14:37:13Z. --- #602 is now merged. As recorded in the independent comparison above, all 23 positive exports from this branch matched its shared lowering. The Skylake report remains linked here, including the DPPS NaN-selection observation. Closing this overlapping implementation as a duplicate of #602.
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:54:25Z.

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

@@ -210,6 +228,89 @@ 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 == M.LDDQU and i.encoding == EncodingKind.LEGACY and has_wide and 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 op == M.EXTRACTPS and i.encoding == EncodingKind.LEGACY and has_wide and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm:
+            lane = i.immediate(2) & 3
+            src = wide_xmm[i.op1_register]
+            stmt = f"{{ uint32_t v; memcpy(&v, {src}.data()+{4 * lane}, 4); {write('v')} }}"
+        elif (op in sse3_4_fp_ops and op not in (M.MOVUPS, M.MOVUPD, M.LDDQU, M.EXTRACTPS)
+              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 SSE3/SSE4.1 source at {i.ip:#x}: {i}")
+            if op in (M.ADDSUBPS, M.ADDSUBPD):
+                ftype, lane_size, lanes = ("float", 4, 4) if op == M.ADDSUBPS else ("double", 8, 2)
+                stmt = (f"{{ {src_init} for (size_t k=0; k<{lanes}; ++k) {{ "
+                        f"{ftype} a, b; memcpy(&a, {dst}.data()+k*{lane_size}, {lane_size}); memcpy(&b, rhs.data()+k*{lane_size}, {lane_size}); "
+                        f"const {ftype} v = (k & 1u) ? (a + b) : (a - b); memcpy({dst}.data()+k*{lane_size}, &v, {lane_size}); }} }}")
+            elif op in (M.HADDPS, M.HSUBPS):
+                sign = "+" if op == M.HADDPS else "-"
+                stmt = (f"{{ std::array<uint8_t,16> lhs; memcpy(lhs.data(), {dst}.data(), 16); {src_init} "
+                        f"float a[4], b[4]; memcpy(a, lhs.data(), 16); memcpy(b, rhs.data(), 16); "
+                        f"const float r[4] = {{a[0] {sign} a[1], a[2] {sign} a[3], b[0] {sign} b[1], b[2] {sign} b[3]}}; "
+                        f"memcpy({dst}.data(), r, 16); }}")
+            elif op in (M.HADDPD, M.HSUBPD):
+                sign = "+" if op == M.HADDPD else "-"
+                stmt = (f"{{ std::array<uint8_t,16> lhs; memcpy(lhs.data(), {dst}.data(), 16); {src_init} "
+                        f"double a[2], b[2]; memcpy(a, lhs.data(), 16); memcpy(b, rhs.data(), 16); "
+                        f"const double r[2] = {{a[0] {sign} a[1], b[0] {sign} b[1]}}; "
+                        f"memcpy({dst}.data(), r, 16); }}")
+            elif op == M.MOVDDUP:
+                stmt = f"{{ {src_init} memcpy({dst}.data(), rhs.data(), 8); memcpy({dst}.data()+8, rhs.data(), 8); }}"
+            elif op == M.MOVSLDUP:
+                stmt = (f"{{ {src_init} memcpy({dst}.data()+0, rhs.data()+0, 4); memcpy({dst}.data()+4, rhs.data()+0, 4); "
+                        f"memcpy({dst}.data()+8, rhs.data()+8, 4); memcpy({dst}.data()+12, rhs.data()+8, 4); }}")
+            elif op == M.MOVSHDUP:
+                stmt = (f"{{ {src_init} memcpy({dst}.data()+0, rhs.data()+4, 4); memcpy({dst}.data()+4, rhs.data()+4, 4); "
+                        f"memcpy({dst}.data()+8, rhs.data()+12, 4); memcpy({dst}.data()+12, rhs.data()+12, 4); }}")
+            elif op in (M.BLENDPS, M.BLENDPD):
+                imm = i.immediate(2)
+                lane_size, lanes = (4, 4) if op == M.BLENDPS else (8, 2)
+                copies = " ".join(f"memcpy({dst}.data()+{k*lane_size}, rhs.data()+{k*lane_size}, {lane_size});"
+                                  for k in range(lanes) if (imm >> k) & 1)
+                stmt = f"{{ {src_init} {copies} }}"
+            elif op in (M.BLENDVPS, M.BLENDVPD):
+                lane_size, lanes = (4, 4) if op == M.BLENDVPS else (8, 2)
+                stmt = (f"{{ {src_init} std::array<uint8_t,16> m; memcpy(m.data(), wide0.data(), 16); "
+                        f"for (size_t k=0; k<{lanes}; ++k) {{ if (m[(k + 1) * {lane_size} - 1] & 0x80u) "
+                        f"memcpy({dst}.data()+k*{lane_size}, rhs.data()+k*{lane_size}, {lane_size}); }} }}")
+            elif op in (M.DPPS, M.DPPD):
+                imm = i.immediate(2)
+                ftype, lane_size, lanes = ("float", 4, 4) if op == M.DPPS else ("double", 8, 2)
+                stmt = (f"{{ {src_init} {ftype} a[{lanes}], b[{lanes}], s = {ftype}(0); "
+                        f"memcpy(a, {dst}.data(), 16); memcpy(b, rhs.data(), 16); "
+                        f"for (size_t k=0; k<{lanes}; ++k) {{ if (({imm}u >> (4u + k)) & 1u) s += a[k] * b[k]; }} "

P2 Badge Preserve the architectural dot-product reduction order

For inputs with cancellation, the sequential accumulator does not implement DPPS's pairwise reduction (p0+p1)+(p2+p3). With the fixture's 0xD5 mask, a=(1e20f,0,-1e20f,1) and b=(1,1,1,1), native DPPS returns (0,0,0,0) while the emitted C++ returns (1,0,1,0); this reproduces in both unoptimized and optimized comparisons on Linux x86-64. Initializing the accumulator to positive zero also makes DPPD lose negative zero when both selected products are negative zero. Emit masked products followed by the architectural reduction tree, rather than accumulating from zero, and add cancellation and signed-zero cases.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655617483 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#discussion_r4655617483). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:54:25Z. **Code review location:** `tools/ffx_version_query.py`; line 297 ~~~~diff @@ -210,6 +228,89 @@ 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 == M.LDDQU and i.encoding == EncodingKind.LEGACY and has_wide and 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 op == M.EXTRACTPS and i.encoding == EncodingKind.LEGACY and has_wide and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm: + lane = i.immediate(2) & 3 + src = wide_xmm[i.op1_register] + stmt = f"{{ uint32_t v; memcpy(&v, {src}.data()+{4 * lane}, 4); {write('v')} }}" + elif (op in sse3_4_fp_ops and op not in (M.MOVUPS, M.MOVUPD, M.LDDQU, M.EXTRACTPS) + 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 SSE3/SSE4.1 source at {i.ip:#x}: {i}") + if op in (M.ADDSUBPS, M.ADDSUBPD): + ftype, lane_size, lanes = ("float", 4, 4) if op == M.ADDSUBPS else ("double", 8, 2) + stmt = (f"{{ {src_init} for (size_t k=0; k<{lanes}; ++k) {{ " + f"{ftype} a, b; memcpy(&a, {dst}.data()+k*{lane_size}, {lane_size}); memcpy(&b, rhs.data()+k*{lane_size}, {lane_size}); " + f"const {ftype} v = (k & 1u) ? (a + b) : (a - b); memcpy({dst}.data()+k*{lane_size}, &v, {lane_size}); }} }}") + elif op in (M.HADDPS, M.HSUBPS): + sign = "+" if op == M.HADDPS else "-" + stmt = (f"{{ std::array<uint8_t,16> lhs; memcpy(lhs.data(), {dst}.data(), 16); {src_init} " + f"float a[4], b[4]; memcpy(a, lhs.data(), 16); memcpy(b, rhs.data(), 16); " + f"const float r[4] = {{a[0] {sign} a[1], a[2] {sign} a[3], b[0] {sign} b[1], b[2] {sign} b[3]}}; " + f"memcpy({dst}.data(), r, 16); }}") + elif op in (M.HADDPD, M.HSUBPD): + sign = "+" if op == M.HADDPD else "-" + stmt = (f"{{ std::array<uint8_t,16> lhs; memcpy(lhs.data(), {dst}.data(), 16); {src_init} " + f"double a[2], b[2]; memcpy(a, lhs.data(), 16); memcpy(b, rhs.data(), 16); " + f"const double r[2] = {{a[0] {sign} a[1], b[0] {sign} b[1]}}; " + f"memcpy({dst}.data(), r, 16); }}") + elif op == M.MOVDDUP: + stmt = f"{{ {src_init} memcpy({dst}.data(), rhs.data(), 8); memcpy({dst}.data()+8, rhs.data(), 8); }}" + elif op == M.MOVSLDUP: + stmt = (f"{{ {src_init} memcpy({dst}.data()+0, rhs.data()+0, 4); memcpy({dst}.data()+4, rhs.data()+0, 4); " + f"memcpy({dst}.data()+8, rhs.data()+8, 4); memcpy({dst}.data()+12, rhs.data()+8, 4); }}") + elif op == M.MOVSHDUP: + stmt = (f"{{ {src_init} memcpy({dst}.data()+0, rhs.data()+4, 4); memcpy({dst}.data()+4, rhs.data()+4, 4); " + f"memcpy({dst}.data()+8, rhs.data()+12, 4); memcpy({dst}.data()+12, rhs.data()+12, 4); }}") + elif op in (M.BLENDPS, M.BLENDPD): + imm = i.immediate(2) + lane_size, lanes = (4, 4) if op == M.BLENDPS else (8, 2) + copies = " ".join(f"memcpy({dst}.data()+{k*lane_size}, rhs.data()+{k*lane_size}, {lane_size});" + for k in range(lanes) if (imm >> k) & 1) + stmt = f"{{ {src_init} {copies} }}" + elif op in (M.BLENDVPS, M.BLENDVPD): + lane_size, lanes = (4, 4) if op == M.BLENDVPS else (8, 2) + stmt = (f"{{ {src_init} std::array<uint8_t,16> m; memcpy(m.data(), wide0.data(), 16); " + f"for (size_t k=0; k<{lanes}; ++k) {{ if (m[(k + 1) * {lane_size} - 1] & 0x80u) " + f"memcpy({dst}.data()+k*{lane_size}, rhs.data()+k*{lane_size}, {lane_size}); }} }}") + elif op in (M.DPPS, M.DPPD): + imm = i.immediate(2) + ftype, lane_size, lanes = ("float", 4, 4) if op == M.DPPS else ("double", 8, 2) + stmt = (f"{{ {src_init} {ftype} a[{lanes}], b[{lanes}], s = {ftype}(0); " + f"memcpy(a, {dst}.data(), 16); memcpy(b, rhs.data(), 16); " + f"for (size_t k=0; k<{lanes}; ++k) {{ if (({imm}u >> (4u + k)) & 1u) s += a[k] * b[k]; }} " ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Preserve the architectural dot-product reduction order** For inputs with cancellation, the sequential accumulator does not implement DPPS's pairwise reduction `(p0+p1)+(p2+p3)`. With the fixture's `0xD5` mask, `a=(1e20f,0,-1e20f,1)` and `b=(1,1,1,1)`, native DPPS returns `(0,0,0,0)` while the emitted C++ returns `(1,0,1,0)`; this reproduces in both unoptimized and optimized comparisons on Linux x86-64. Initializing the accumulator to positive zero also makes DPPD lose negative zero when both selected products are negative zero. Emit masked products followed by the architectural reduction tree, rather than accumulating from zero, and add cancellation and signed-zero cases. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
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:54:25Z.

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

@@ -210,6 +228,89 @@ 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 == M.LDDQU and i.encoding == EncodingKind.LEGACY and has_wide and 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 op == M.EXTRACTPS and i.encoding == EncodingKind.LEGACY and has_wide and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm:
+            lane = i.immediate(2) & 3
+            src = wide_xmm[i.op1_register]
+            stmt = f"{{ uint32_t v; memcpy(&v, {src}.data()+{4 * lane}, 4); {write('v')} }}"
+        elif (op in sse3_4_fp_ops and op not in (M.MOVUPS, M.MOVUPD, M.LDDQU, M.EXTRACTPS)
+              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);"

P2 Badge Limit scalar memory reads to the architectural operand width

When ROUNDSS, ROUNDSD, or MOVDDUP uses a memory source, this emits a 16-byte read even though their memory operands are only 4, 8, and 8 bytes respectively. A scalar occupying the last valid bytes before an inaccessible page therefore succeeds natively but can fault in the unoptimized emitted C++; a Linux x86-64 page-boundary comparison reproduces this for all three instructions. Read only the decoded memory operand's width instead of always copying 16 bytes. The new fixture misses this because it uses register sources for these operations.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655617486 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#discussion_r4655617486). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:54:25Z. **Code review location:** `tools/ffx_version_query.py`; line 253 ~~~~diff @@ -210,6 +228,89 @@ 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 == M.LDDQU and i.encoding == EncodingKind.LEGACY and has_wide and 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 op == M.EXTRACTPS and i.encoding == EncodingKind.LEGACY and has_wide and i.op1_kind == K.REGISTER and i.op1_register in wide_xmm: + lane = i.immediate(2) & 3 + src = wide_xmm[i.op1_register] + stmt = f"{{ uint32_t v; memcpy(&v, {src}.data()+{4 * lane}, 4); {write('v')} }}" + elif (op in sse3_4_fp_ops and op not in (M.MOVUPS, M.MOVUPD, M.LDDQU, M.EXTRACTPS) + 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);" ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Limit scalar memory reads to the architectural operand width** When `ROUNDSS`, `ROUNDSD`, or `MOVDDUP` uses a memory source, this emits a 16-byte read even though their memory operands are only 4, 8, and 8 bytes respectively. A scalar occupying the last valid bytes before an inaccessible page therefore succeeds natively but can fault in the unoptimized emitted C++; a Linux x86-64 page-boundary comparison reproduces this for all three instructions. Read only the decoded memory operand's width instead of always copying 16 bytes. The new fixture misses this because it uses register sources for these operations. Useful? React with 👍 / 👎. <!-- codex-code-review-finding:v1 -->
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:54:25Z.

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

@@ -30,6 +31,15 @@
 template<class T> void store(word address, T v) {
     memcpy(reinterpret_cast<void*>(address), &v, sizeof v);
 }
+template<class T> T round_x86(T x, unsigned mode) {
+    if (!std::isfinite(x) || x == T(0)) return x;

P2 Badge Quiet signaling NaNs before returning non-finite inputs

For a signaling-NaN source with the normal masked-invalid MXCSR setting, this fast path returns the original signaling bit pattern, whereas SSE4.1 rounding instructions quiet it. For example, native ROUNDPS with immediate 0x08 maps 0x7f800001 to 0x7fc00001, but the emitted helper returns 0x7f800001; this reproduces in both unoptimized and optimized comparisons on Linux x86-64. Consequently, the accepted ROUNDPS/PD/SS/SD forms produce incorrect output bytes for these inputs. Handle signaling NaNs explicitly before returning non-finite values and include exceptional-value comparisons in the fixture.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655617490 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/375#discussion_r4655617490). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-21T23:54:25Z. **Code review location:** `tools/ffx_version_query.py`; line 35 ~~~~diff @@ -30,6 +31,15 @@ template<class T> void store(word address, T v) { memcpy(reinterpret_cast<void*>(address), &v, sizeof v); } +template<class T> T round_x86(T x, unsigned mode) { + if (!std::isfinite(x) || x == T(0)) return x; ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Quiet signaling NaNs before returning non-finite inputs** For a signaling-NaN source with the normal masked-invalid MXCSR setting, this fast path returns the original signaling bit pattern, whereas SSE4.1 rounding instructions quiet it. For example, native `ROUNDPS` with immediate `0x08` maps `0x7f800001` to `0x7fc00001`, but the emitted helper returns `0x7f800001`; this reproduces in both unoptimized and optimized comparisons on Linux x86-64. Consequently, the accepted ROUNDPS/PD/SS/SD forms produce incorrect output bytes for these inputs. Handle signaling NaNs explicitly before returning non-finite values and include exceptional-value comparisons in the fixture. 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!369
No description provided.