Skip to content

cpu: lower all 60 VEX FMA3 scalar and packed instructions - #503

Merged
R0Y00 merged 2 commits into
R0Y00:mainfrom
PrivacyOnion:cpu/lower-fma3-all
Nov 23, 2026
Merged

R0Y00 merged 2 commits into
R0Y00:mainfrom
PrivacyOnion:cpu/lower-fma3-all

Conversation

@PrivacyOnion

Copy link
Copy Markdown
Contributor

Summary

Adds AOT lowering in tools/cpu_avx2_aot.py for all 60 VEX-encoded x64 FMA3 instructions listed in docs/coverage/x64_zen2.tsv group FMA (96 iced_x86.Code variants across scalar ss/sd and packed 128-bit/256-bit ps/pd, supporting both register and flat memory op2 operands):

  • Families: vfmadd, vfmsub, vfnmadd, vfnmsub (ss, sd, ps, pd) and vfmaddsub, vfmsubadd (ps, pd)
  • Operand orders: 132, 213, 231
  • Hardware-exact semantics: single-rounding std::fmaf/std::fma, x86 semantic (a, b, c) NaN priority with QNaN quietening and sign preservation on negated-product forms, scalar upper-lane preservation (op0[127:32] / op0[127:64]), and VEX 128-bit upper-lane zeroing ([255:128]).

Fixture & Coverage

  • Added source-built MASM fixture tests/cpu/fma3_leaf.asm (; coverage: source cpu_aot_fma3) + tests/cpu/fma3_leaf.def with 102 exports (all 96 register variants + 6 memory-operand variants covering m32, m64, m128, m256) and an ALTER_FMA mutation variant.
  • Added tests/cpu/fma3_leaf_check.py running 714 bit-exact differential comparisons (native hardware DLL vs lifted C++ DLL) covering single-rounding witnesses, signed zeros, infinities, subnormals, QNaN/SNaN priority, randomized inputs, mutation control, and 5 rejection controls (undefined op0, nonvolatile ymm6, EVEX FMA3, unsupported RSP memory base, and AMD FMA4).
  • Added check_fma3_leaf() to tests/form_coverage_check.py verifying 60/60 FMA coverage.

Test Commands & Results

  • ctest --preset dev --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_avx2_regs)$" -V -> 2/2 passed (cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations))
  • ctest --preset release --no-tests=error -R "^cpu_aot_fma3$" -V -> 1/1 passed at /O2
  • .\out\pe-env\Scripts\python.exe -B tests/form_coverage_check.py -> passed (FMA 60/60)
  • .\out\pe-env\Scripts\python.exe -B tests/bidi_controls_check.py -> passed
  • .\out\pe-env\Scripts\python.exe -B tests/docs_links_check.py -> passed

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Verification of c5626ec on an Intel Core i7-6820HQ (Skylake; FMA3 and AVX2, no AVX-512). Windows 11 26100.9168, MSVC 19.52.37029, dev preset:

ctest --preset dev --no-tests=error -R "^cpu_aot_(fma|avx2)"
cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations)
1/2 Test #103: cpu_aot_avx2_regs ................   Passed    0.51 sec
2/2 Test #107: cpu_aot_fma3 .....................   Passed    0.20 sec

python -B tests/form_coverage_check.py also reports "form coverage checks passed". The FMA results come from a CPU generation different from the Zen 2 coverage table, and all 714 evaluations match bit for bit.

Two notes for coordination:

  • c5626ec no longer merges cleanly with main at f8d11ad. git merge-tree reports a conflict in CMakeLists.txt. tests/form_coverage_check.py merges automatically.
  • cpu: lower all 36 VEX-encoded packed FMA3 instructions (ps/pd) #349 also lowers the 36 packed VEX FMA3 forms, in the other translator (tools/ffx_version_query.py). This PR covers them in tools/cpu_avx2_aot.py. The code does not overlap. Both PRs do claim the same FMA rows of docs/coverage/x64_zen2.tsv, so whichever lands second may want to state which translator its coverage entry refers to.

AI-assisted: run and summarized with Claude Code; outputs copied from the local runs.

@PrivacyOnion

Copy link
Copy Markdown
Contributor Author

Rebased onto current main (c8a3fd9); the only conflict was in CMakeLists.txt (both sides appended independent CPU test blocks, both kept). No code change otherwise.

Thanks for the independent Skylake verification.

Coordination with #349: that PR lowers the 36 packed VEX FMA3 forms in tools/ffx_version_query.py. This PR covers all 60 FMA forms in tools/cpu_avx2_aot.py. The code does not overlap. Both claim the same FMA rows of docs/coverage/x64_zen2.tsv; this PR's coverage entry (cpu_aot_fma3) refers to the cpu_avx2_aot translator.

Re-run after rebase (dev preset, Windows 11, MSVC):

  • ctest --preset dev --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_avx2_regs)$" -> 2/2 passed
  • tests/form_coverage_check.py, tests/bidi_controls_check.py -> passed

@legacy-code-archaeologist

Copy link
Copy Markdown
Contributor

Re-checked the rebased head d9382c3 on the same Skylake i7-6820HQ with a fresh configure and build. The FMA and AVX2 tests already on main pass alongside this PR's test:

ctest --preset dev --no-tests=error -R "^cpu_aot_(fma|avx2)" -V
1/4 Test #103: cpu_aot_avx2_regs ................   Passed
cpu_aot_fma3 passed (102 exports, 714 bit-exact differential evaluations)
2/4 Test #109: cpu_aot_fma3 .....................   Passed
3/4 Test #137: cpu_aot_avx2_shifts ..............   Passed
4/4 Test #142: cpu_aot_fma_scalar ...............   Passed
100% tests passed, 0 tests failed out of 4

AI-assisted (Claude Code).

@R0Y00

R0Y00 commented Nov 23, 2026

Copy link
Copy Markdown
Owner

I resolved the coverage-test conflict on your original branch in 1dce507. The FMA3 and shader coverage checks both remain registered. The native FMA3 test now reuses the existing CPUID/XGETBV probe and reports a capability skip when FMA or OS-enabled XMM/YMM state is unavailable.

On Windows with MSVC 19.51, both the dev and release presets passed ctest --preset <preset> --no-tests=error -R "^(cpu_aot_fma3|cpu_aot_fma_packed)$" -V. The leaf fixture matched 714 comparisons across 102 exports, and the existing packed fixture matched 196. The form coverage check also passed. GitHub CI is running on the updated head.

@R0Y00
R0Y00 merged commit facfdcf into R0Y00:main Nov 23, 2026
5 checks passed
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