Skip to content

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

Open
evilclanker9000 wants to merge 2 commits into
R0Y00:mainfrom
evilclanker9000:ag/cpu-lower-sse3-4-fp
Open

evilclanker9000 wants to merge 2 commits into
R0Y00:mainfrom
evilclanker9000:ag/cpu-lower-sse3-4-fp

Conversation

@evilclanker9000

Copy link
Copy Markdown
Contributor

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.

@evilclanker9000
evilclanker9000 marked this pull request as ready for review November 21, 2026 23:47
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 21, 2026 •

Copy link
Copy Markdown
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.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 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".

Comment on lines +295 to +297
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]; }} "

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍 / 👎.

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);"

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍 / 👎.

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;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 👍 / 👎.

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

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 cpu: lower baseline 128-bit SSE3 and SSE4.1 floating-point instructions #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

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants