cpu: lower VEX.256 packed single-precision min and max #381

Merged
Vonsek merged 6 commits from upstream-pr/387 into main 2026-11-23 15:42:52 +00:00
Vonsek commented 2026-11-22 00:04:27 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by Vonsek on 2026-11-22T00:04:27Z.


Change

  • Lower 31 VEX 128-bit and 256-bit AVX packed floating-point instructions in tools/ffx_version_query.py:
    • Move: vmovupd
    • Arithmetic and square root: vaddps, vsubps, vmulps, vdivps, vsqrtps, vminps, vmaxps, vaddpd, vsubpd, vmulpd, vdivpd, vsqrtpd, vminpd, vmaxpd
    • Bitwise: vandps, vandnps, vorps, vxorps, vandpd, vandnpd, vorpd, vxorpd
    • Horizontal / alternating: vaddsubps, vhaddps, vhsubps, vaddsubpd, vhaddpd, vhsubpd
    • Sign masks: vmovmskps, vmovmskpd
  • Restrict lowering to EncodingKind.VEX (rejecting EVEX encodings) and zero upper YMM bytes for 128-bit XMM destinations.
  • Probe CPUID leaf 1 ECX bits 27 (OSXSAVE) and 28 (AVX) plus XCR0 XMM/YMM state before invoking the native fixture (exiting 77 with SKIP_RETURN_CODE 77 on hosts without AVX).
  • Add source-built MASM differential fixture tests/cpu/avx_packed_fp.{asm,def,py}, CMake target cpu_avx_packed_fp_lifted, CTest cpu_aot_avx_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_avx_packed_fp_lifted:
    ValueError: unsupported query operation at 0x1008: vaddps ymm0,ymm1,ymm2
  • Built and tested on Windows 11 x64 (VS 2022 Build Tools):
    • cmake --build --preset dev --target cpu_avx_packed_fp_lifted succeeded with 0 warnings
    • ctest --preset dev --no-tests=error -R "^(cpu_aot_avx_packed_fp|cpu_aot_bmi1)$" -V: 2/2 passed (30 AVX 256-bit packed FP cases matched native and translated DLLs + EVEX vaddps rejection control)
    • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py: form coverage checks passed

Evidence and follow-up

  • Covers 31 AVX-grouped mnemonics in docs/coverage/x64_zen2.tsv using source-built fixtures, gated by a runtime CPUID/XCR0 check.
<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536113940 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/387). Originally posted by [Vonsek](https://github.com/Vonsek) on 2026-11-22T00:04:27Z. --- ## Change - Lower 31 VEX 128-bit and 256-bit AVX packed floating-point instructions in `tools/ffx_version_query.py`: - Move: `vmovupd` - Arithmetic and square root: `vaddps`, `vsubps`, `vmulps`, `vdivps`, `vsqrtps`, `vminps`, `vmaxps`, `vaddpd`, `vsubpd`, `vmulpd`, `vdivpd`, `vsqrtpd`, `vminpd`, `vmaxpd` - Bitwise: `vandps`, `vandnps`, `vorps`, `vxorps`, `vandpd`, `vandnpd`, `vorpd`, `vxorpd` - Horizontal / alternating: `vaddsubps`, `vhaddps`, `vhsubps`, `vaddsubpd`, `vhaddpd`, `vhsubpd` - Sign masks: `vmovmskps`, `vmovmskpd` - Restrict lowering to `EncodingKind.VEX` (rejecting EVEX encodings) and zero upper YMM bytes for 128-bit XMM destinations. - Probe CPUID leaf 1 ECX bits 27 (`OSXSAVE`) and 28 (`AVX`) plus XCR0 XMM/YMM state before invoking the native fixture (exiting 77 with `SKIP_RETURN_CODE 77` on hosts without AVX). - Add source-built MASM differential fixture `tests/cpu/avx_packed_fp.{asm,def,py}`, CMake target `cpu_avx_packed_fp_lifted`, CTest `cpu_aot_avx_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_avx_packed_fp_lifted`: `ValueError: unsupported query operation at 0x1008: vaddps ymm0,ymm1,ymm2` - Built and tested on Windows 11 x64 (VS 2022 Build Tools): - `cmake --build --preset dev --target cpu_avx_packed_fp_lifted` succeeded with 0 warnings - `ctest --preset dev --no-tests=error -R "^(cpu_aot_avx_packed_fp|cpu_aot_bmi1)$" -V`: 2/2 passed (`30 AVX 256-bit packed FP cases matched native and translated DLLs` + EVEX `vaddps` rejection control) - `.\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py`: `form coverage checks passed` ## Evidence and follow-up - Covers 31 `AVX`-grouped mnemonics in `docs/coverage/x64_zen2.tsv` using source-built fixtures, gated by a runtime CPUID/XCR0 check.
upstream-sync closed this pull request 2026-11-24 06:42:46 +00:00
chatgpt-codex-connector[bot commented 2026-11-24 06:42:46 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-22T00:15:16Z.

Review: COMMENTED


💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: adbc413a36

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

Imported from R0Y00/Xbox2Windows. Originally posted by chatgpt-codex-connector[bot] on 2026-11-22T00:07: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-22T00:15:19.462489Z adbc413 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:7071331111 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/387#issuecomment-7071331111). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-22T00:07:32Z. --- <!-- codex-pull-request-review-summary --> <!-- codex-review-state:v1 {"code_review":{"commit_sha":"adbc413a36376099f6d311b69d738ae44aaf05aa","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-22T00:15:19.462489Z">2026-11-22T00:15:19.462489Z</relative-time> | `adbc413` | 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-23T08:20:08Z.


I assembled the original MASM fixtures from this PR and #382 and tested their exports through cpu_switch_aot.translate_pe at d2ac415. The shared entry rejected all 30 exports here and all 22 in #382. That leaves both sets of fixtures useful. Please start this queue with a narrow packed-min/max slice for #387, using the shared lowering and a native bit comparison for one width. Include the DAZ-on subnormal second-operand case from the existing review and both operand orders. The aligned SSE moves and the broader #382 conversion set can keep their own review.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7095326054 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/387#issuecomment-7095326054). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T08:20:08Z. --- I assembled the original MASM fixtures from this PR and #382 and tested their exports through `cpu_switch_aot.translate_pe` at `d2ac415`. The shared entry rejected all 30 exports here and all 22 in #382. That leaves both sets of fixtures useful. Please start this queue with a narrow packed-min/max slice for #387, using the shared lowering and a native bit comparison for one width. Include the DAZ-on subnormal second-operand case from the existing review and both operand orders. The aligned SSE moves and the broader #382 conversion set can keep their own review.
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:45:06Z.


Independent Windows result on an i9-14900HX with MSVC 19.51 /Od and /O2: the source-built 256-bit VMINPS/VMAXPS returned 0x00000000 for (+0, 0x00000001) with MXCSR 0x1fc0, while the scalar conditional returned 0x00000001. Reversing the operands returned zero in both. Across 64 output words per build (both operations, both orders, DAZ on/off, signed subnormals, zeros and distinct qNaNs), the conditional differed in 10 words. Two 128-bit MINPS/MAXPS intrinsics applied in source order matched all 64. That gives us a Windows oracle for the packed single-precision min/max slice.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7104479281 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/387#issuecomment-7104479281). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T14:45:06Z. --- Independent Windows result on an i9-14900HX with MSVC 19.51 /Od and /O2: the source-built 256-bit VMINPS/VMAXPS returned 0x00000000 for (+0, 0x00000001) with MXCSR 0x1fc0, while the scalar conditional returned 0x00000001. Reversing the operands returned zero in both. Across 64 output words per build (both operations, both orders, DAZ on/off, signed subnormals, zeros and distinct qNaNs), the conditional differed in 10 words. Two 128-bit MINPS/MAXPS intrinsics applied in source order matched all 64. That gives us a Windows oracle for the packed single-precision min/max slice.
R0Y00 commented 2026-11-24 06:49:14 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-23T15:29:28Z.


Maintainer update at cedf64f: resolved the conflict with the now-merged #395 and kept this slice to register-form VEX.256 VMINPS and VMAXPS. The original MASM comparison remains in place. Two SSE halves preserve source order, including when DAZ is enabled. The fixture checks 64 exact word observations covering signed subnormals, signed zero, NaN and both operand orders, and rejects the unsupported memory form.

ctest --preset <dev|release> --no-tests=error -R '^cpu_aot_(avx_packed_fp|avx_packed_int|float_packed|sse34_fp)$' passed 4/4 in each preset. Windows CI is now running on the updated branch. The other floating-point forms can be assessed with their own native evidence.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7105648586 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/387#issuecomment-7105648586). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-23T15:29:28Z. --- Maintainer update at `cedf64f`: resolved the conflict with the now-merged #395 and kept this slice to register-form VEX.256 `VMINPS` and `VMAXPS`. The original MASM comparison remains in place. Two SSE halves preserve source order, including when DAZ is enabled. The fixture checks 64 exact word observations covering signed subnormals, signed zero, NaN and both operand orders, and rejects the unsupported memory form. `ctest --preset <dev|release> --no-tests=error -R '^cpu_aot_(avx_packed_fp|avx_packed_int|float_packed|sse34_fp)$'` passed 4/4 in each preset. Windows CI is now running on the updated branch. The other floating-point forms can be assessed with their own native evidence.
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-22T00:15:16Z.

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

@@ -210,6 +220,92 @@ 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 == M.VMOVUPD and i.encoding == EncodingKind.VEX and has_wide:
+            if i.op0_kind == K.REGISTER and i.op0_register in wide_any and i.op1_kind == K.MEMORY:
+                dst = wide_any[i.op0_register]
+                nbytes = 32 if i.op0_register in wide else 16
+                stmt = f"{dst} = {{}}; memcpy({dst}.data(), reinterpret_cast<const void*>({address()}), {nbytes});"
+            elif i.op0_kind == K.MEMORY and i.op1_kind == K.REGISTER and i.op1_register in wide_any:
+                src = wide_any[i.op1_register]
+                nbytes = 32 if i.op1_register in wide else 16
+                stmt = f"memcpy(reinterpret_cast<void*>({address()}), {src}.data(), {nbytes});"
+            elif i.op0_kind == K.REGISTER and i.op0_register in wide_any and i.op1_kind == K.REGISTER and i.op1_register in wide_any:
+                dst, src = wide_any[i.op0_register], wide_any[i.op1_register]
+                nbytes = 32 if i.op0_register in wide else 16
+                stmt = f"{{ auto t = {src}; {dst} = {{}}; memcpy({dst}.data(), t.data(), {nbytes}); }}"
+            else:
+                raise ValueError(f"unsupported VMOVUPD form at {i.ip:#x}: {i}")
+        elif (op in (M.VMOVMSKPS, M.VMOVMSKPD) and i.encoding == EncodingKind.VEX 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_any):
+            src = wide_any[i.op1_register]
+            nbytes = 32 if i.op1_register in wide else 16
+            lane_size = 4 if op == M.VMOVMSKPS else 8
+            lanes = nbytes // 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 avx_fp_ops and op not in (M.VMOVUPD, M.VMOVMSKPS, M.VMOVMSKPD)
+              and i.encoding == EncodingKind.VEX and has_wide
+              and i.op0_kind == K.REGISTER and i.op0_register in wide_any):
+            dst = wide_any[i.op0_register]
+            nbytes = 32 if i.op0_register in wide else 16
+            zero_hi = f" memset({dst}.data()+16, 0, 16);" if nbytes == 16 else ""
+            if op in (M.VSQRTPS, M.VSQRTPD):
+                if i.op1_kind == K.REGISTER and i.op1_register in wide_any:
+                    s_init = f"std::array<uint8_t,32> rhs = {wide_any[i.op1_register]};"
+                elif i.op1_kind == K.MEMORY:
+                    s_init = f"std::array<uint8_t,32> rhs{{}}; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), {nbytes});"
+                else:
+                    raise ValueError(f"unsupported AVX sqrt source at {i.ip:#x}: {i}")
+                ftype, lane_size = ("float", 4) if op == M.VSQRTPS else ("double", 8)
+                stmt = (f"{{ {s_init} for (size_t n=0; n<{nbytes}; 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}); }}{zero_hi} }}")
+            else:
+                if i.op1_kind != K.REGISTER or i.op1_register not in wide_any:
+                    raise ValueError(f"unsupported AVX src1 at {i.ip:#x}: {i}")
+                src1 = wide_any[i.op1_register]
+                if i.op2_kind == K.REGISTER and i.op2_register in wide_any:
+                    s2_init = f"std::array<uint8_t,32> rhs = {wide_any[i.op2_register]};"
+                elif i.op2_kind == K.MEMORY:
+                    s2_init = f"std::array<uint8_t,32> rhs{{}}; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), {nbytes});"
+                else:
+                    raise ValueError(f"unsupported AVX src2 at {i.ip:#x}: {i}")
+                if op in (M.VANDPS, M.VANDPD, M.VANDNPS, M.VANDNPD, M.VORPS, M.VORPD, M.VXORPS, M.VXORPD):
+                    expr = {M.VANDPS: "a & b", M.VANDPD: "a & b", M.VANDNPS: "(~a) & b", M.VANDNPD: "(~a) & b",
+                            M.VORPS: "a | b", M.VORPD: "a | b", M.VXORPS: "a ^ b", M.VXORPD: "a ^ b"}[op]
+                    stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t n=0; n<{nbytes}; ++n) {{ "
+                            f"const uint8_t a = lhs[n], b = rhs[n]; {dst}[n] = static_cast<uint8_t>({expr}); }}{zero_hi} }}")
+                elif op in (M.VADDSUBPS, M.VADDSUBPD):
+                    ftype, lane_size = ("float", 4) if op == M.VADDSUBPS else ("double", 8)
+                    lanes = nbytes // lane_size
+                    stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t k=0; k<{lanes}; ++k) {{ "
+                            f"{ftype} a, b; memcpy(&a, lhs.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}); }}{zero_hi} }}")
+                elif op in (M.VHADDPS, M.VHSUBPS):
+                    sign = "+" if op == M.VHADDPS else "-"
+                    stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t lane=0; lane<{nbytes}; lane+=16) {{ "
+                            f"float a[4], b[4]; memcpy(a, lhs.data()+lane, 16); memcpy(b, rhs.data()+lane, 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()+lane, r, 16); }}{zero_hi} }}")
+                elif op in (M.VHADDPD, M.VHSUBPD):
+                    sign = "+" if op == M.VHADDPD else "-"
+                    stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t lane=0; lane<{nbytes}; lane+=16) {{ "
+                            f"double a[2], b[2]; memcpy(a, lhs.data()+lane, 16); memcpy(b, rhs.data()+lane, 16); "
+                            f"const double r[2] = {{a[0] {sign} a[1], b[0] {sign} b[1]}}; "
+                            f"memcpy({dst}.data()+lane, r, 16); }}{zero_hi} }}")
+                else:  # VADD/VSUB/VMUL/VDIV/VMIN/VMAX PS/PD
+                    is_ps = op in (M.VADDPS, M.VSUBPS, M.VMULPS, M.VDIVPS, M.VMINPS, M.VMAXPS)
+                    ftype, lane_size = ("float", 4) if is_ps else ("double", 8)
+                    expr = {
+                        M.VADDPS: "a + b", M.VADDPD: "a + b", M.VSUBPS: "a - b", M.VSUBPD: "a - b",
+                        M.VMULPS: "a * b", M.VMULPD: "a * b", M.VDIVPS: "a / b", M.VDIVPD: "a / b",
+                        M.VMINPS: "(a < b) ? a : b", M.VMINPD: "(a < b) ? a : b",
+                        M.VMAXPS: "(a > b) ? a : b", M.VMAXPD: "(a > b) ? a : b",

P2 Badge Honor DAZ when lowering packed min/max

When the caller enables MXCSR.DAZ, these comparisons treat subnormal inputs as zero, but the subsequent selection and memcpy retain the original subnormal bits. Native VMIN/VMAX also apply DAZ to the returned operand: with MXCSR 0x1fc0, lhs +0.0f, and rhs bits 0x00000001, native vminps returns 0x00000000, while the generated loop returns 0x00000001. This mismatch reproduces with the emitted lowering compiled unoptimized using GCC 13 and Clang 17 on x86-64 Linux, and affects both PS/PD and both vector widths. Use min/max intrinsics that preserve the instruction semantics, or explicitly normalize operands according to MXCSR, and add a DAZ-enabled differential case.

Useful? React with 👍 / 👎.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:review-comment:4655716347 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/387#discussion_r4655716347). Originally posted by [chatgpt-codex-connector[bot]](https://github.com/chatgpt-codex-connector%5Bbot%5D) on 2026-11-22T00:15:16Z. **Code review location:** `tools/ffx_version_query.py`; line 304 ~~~~diff @@ -210,6 +220,92 @@ 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 == M.VMOVUPD and i.encoding == EncodingKind.VEX and has_wide: + if i.op0_kind == K.REGISTER and i.op0_register in wide_any and i.op1_kind == K.MEMORY: + dst = wide_any[i.op0_register] + nbytes = 32 if i.op0_register in wide else 16 + stmt = f"{dst} = {{}}; memcpy({dst}.data(), reinterpret_cast<const void*>({address()}), {nbytes});" + elif i.op0_kind == K.MEMORY and i.op1_kind == K.REGISTER and i.op1_register in wide_any: + src = wide_any[i.op1_register] + nbytes = 32 if i.op1_register in wide else 16 + stmt = f"memcpy(reinterpret_cast<void*>({address()}), {src}.data(), {nbytes});" + elif i.op0_kind == K.REGISTER and i.op0_register in wide_any and i.op1_kind == K.REGISTER and i.op1_register in wide_any: + dst, src = wide_any[i.op0_register], wide_any[i.op1_register] + nbytes = 32 if i.op0_register in wide else 16 + stmt = f"{{ auto t = {src}; {dst} = {{}}; memcpy({dst}.data(), t.data(), {nbytes}); }}" + else: + raise ValueError(f"unsupported VMOVUPD form at {i.ip:#x}: {i}") + elif (op in (M.VMOVMSKPS, M.VMOVMSKPD) and i.encoding == EncodingKind.VEX 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_any): + src = wide_any[i.op1_register] + nbytes = 32 if i.op1_register in wide else 16 + lane_size = 4 if op == M.VMOVMSKPS else 8 + lanes = nbytes // 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 avx_fp_ops and op not in (M.VMOVUPD, M.VMOVMSKPS, M.VMOVMSKPD) + and i.encoding == EncodingKind.VEX and has_wide + and i.op0_kind == K.REGISTER and i.op0_register in wide_any): + dst = wide_any[i.op0_register] + nbytes = 32 if i.op0_register in wide else 16 + zero_hi = f" memset({dst}.data()+16, 0, 16);" if nbytes == 16 else "" + if op in (M.VSQRTPS, M.VSQRTPD): + if i.op1_kind == K.REGISTER and i.op1_register in wide_any: + s_init = f"std::array<uint8_t,32> rhs = {wide_any[i.op1_register]};" + elif i.op1_kind == K.MEMORY: + s_init = f"std::array<uint8_t,32> rhs{{}}; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), {nbytes});" + else: + raise ValueError(f"unsupported AVX sqrt source at {i.ip:#x}: {i}") + ftype, lane_size = ("float", 4) if op == M.VSQRTPS else ("double", 8) + stmt = (f"{{ {s_init} for (size_t n=0; n<{nbytes}; 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}); }}{zero_hi} }}") + else: + if i.op1_kind != K.REGISTER or i.op1_register not in wide_any: + raise ValueError(f"unsupported AVX src1 at {i.ip:#x}: {i}") + src1 = wide_any[i.op1_register] + if i.op2_kind == K.REGISTER and i.op2_register in wide_any: + s2_init = f"std::array<uint8_t,32> rhs = {wide_any[i.op2_register]};" + elif i.op2_kind == K.MEMORY: + s2_init = f"std::array<uint8_t,32> rhs{{}}; memcpy(rhs.data(), reinterpret_cast<const void*>({address()}), {nbytes});" + else: + raise ValueError(f"unsupported AVX src2 at {i.ip:#x}: {i}") + if op in (M.VANDPS, M.VANDPD, M.VANDNPS, M.VANDNPD, M.VORPS, M.VORPD, M.VXORPS, M.VXORPD): + expr = {M.VANDPS: "a & b", M.VANDPD: "a & b", M.VANDNPS: "(~a) & b", M.VANDNPD: "(~a) & b", + M.VORPS: "a | b", M.VORPD: "a | b", M.VXORPS: "a ^ b", M.VXORPD: "a ^ b"}[op] + stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t n=0; n<{nbytes}; ++n) {{ " + f"const uint8_t a = lhs[n], b = rhs[n]; {dst}[n] = static_cast<uint8_t>({expr}); }}{zero_hi} }}") + elif op in (M.VADDSUBPS, M.VADDSUBPD): + ftype, lane_size = ("float", 4) if op == M.VADDSUBPS else ("double", 8) + lanes = nbytes // lane_size + stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t k=0; k<{lanes}; ++k) {{ " + f"{ftype} a, b; memcpy(&a, lhs.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}); }}{zero_hi} }}") + elif op in (M.VHADDPS, M.VHSUBPS): + sign = "+" if op == M.VHADDPS else "-" + stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t lane=0; lane<{nbytes}; lane+=16) {{ " + f"float a[4], b[4]; memcpy(a, lhs.data()+lane, 16); memcpy(b, rhs.data()+lane, 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()+lane, r, 16); }}{zero_hi} }}") + elif op in (M.VHADDPD, M.VHSUBPD): + sign = "+" if op == M.VHADDPD else "-" + stmt = (f"{{ auto lhs = {src1}; {s2_init} for (size_t lane=0; lane<{nbytes}; lane+=16) {{ " + f"double a[2], b[2]; memcpy(a, lhs.data()+lane, 16); memcpy(b, rhs.data()+lane, 16); " + f"const double r[2] = {{a[0] {sign} a[1], b[0] {sign} b[1]}}; " + f"memcpy({dst}.data()+lane, r, 16); }}{zero_hi} }}") + else: # VADD/VSUB/VMUL/VDIV/VMIN/VMAX PS/PD + is_ps = op in (M.VADDPS, M.VSUBPS, M.VMULPS, M.VDIVPS, M.VMINPS, M.VMAXPS) + ftype, lane_size = ("float", 4) if is_ps else ("double", 8) + expr = { + M.VADDPS: "a + b", M.VADDPD: "a + b", M.VSUBPS: "a - b", M.VSUBPD: "a - b", + M.VMULPS: "a * b", M.VMULPD: "a * b", M.VDIVPS: "a / b", M.VDIVPD: "a / b", + M.VMINPS: "(a < b) ? a : b", M.VMINPD: "(a < b) ? a : b", + M.VMAXPS: "(a > b) ? a : b", M.VMAXPD: "(a > b) ? a : b", ~~~~ --- **<sub><sub>![P2 Badge](https://img.shields.io/badge/P2-yellow?style=flat)</sub></sub> Honor DAZ when lowering packed min/max** When the caller enables MXCSR.DAZ, these comparisons treat subnormal inputs as zero, but the subsequent selection and `memcpy` retain the original subnormal bits. Native VMIN/VMAX also apply DAZ to the returned operand: with MXCSR `0x1fc0`, lhs `+0.0f`, and rhs bits `0x00000001`, native `vminps` returns `0x00000000`, while the generated loop returns `0x00000001`. This mismatch reproduces with the emitted lowering compiled unoptimized using GCC 13 and Clang 17 on x86-64 Linux, and affects both PS/PD and both vector widths. Use min/max intrinsics that preserve the instruction semantics, or explicitly normalize operands according to MXCSR, and add a DAZ-enabled differential case. 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!381
No description provided.