runtime: define PMADDWD pair-sum wraparound #3340

Open
Khayoon wants to merge 1 commit from upstream-pr/3340 into main
Khayoon commented 2026-12-03 14:23:50 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Khayoon on 2026-12-03T14:23:50Z.


Claimed in the coordination issue as: RT-CPU-PMADDWD-WRAP

What and why

PMADDWD's all-INT16_MIN pair produces two individually representable products whose sum is 2147483648. The existing signed addition has undefined C++ behavior. Sum the products as uint32_t so the required 0x80000000 result is defined, and keep that arithmetic in a constexpr helper used by the emulation path.

Add a constant-evaluation regression and a synthetic full-YMM execution test covering the overflowing pair, near-boundary positive and negative sums, mixed signs and ordinary inputs in both 128-bit halves.

Tested

  • MSVC 19.44.35207, x64 Windows, Intel Core i7-3930K (AVX1; test CPUID reports no AVX2/FMA/F16C), C++20, /O2 /arch:AVX /W4 /permissive- /utf-8. Direct standalone test builds avoid the repository-wide /arch:AVX2 default on this AVX1 host.
  • Red: put the exact previous signed pair arithmetic in the new production constexpr helper, leaving the regression unchanged. MSVC rejects the minimum/minimum static_assert with C2131 and specifically diagnoses signed overflow at +.
  • Green: unsigned pair addition compiles; avx2_emulation_test passes all 44 checks, including the new full-YMM regression.
  • isa_jit_patch_test: passes 51 single-instruction families, two fused pairs and the synthetic PE32+ image. bmi_emulation_test: 66 checks pass. cpu_emulation_test: all checks pass, including the 16-thread x 2048-site CLWB rewrite test. ISA test retains its existing C4324 alignment-padding warnings.
  • Changed-file and full tracked clean-room checks and git diff --check pass.

The red result proves the original C++ expression is invalid under constant evaluation; no incorrect native output or measured gameplay impact is claimed. No game files, GPU runs or full runtime build were used.

GPUs tested: n/a (CPU-only synthetic tests; profile Z).

AI-assisted: yes. Implementation and separate read-only review: GPT-6.1 Sol in Codex. The reviewer found no blocking correctness issue.

Checklist

  • Tested against main b053a1813c. The publication commit uses current main; intervening upstream changes do not touch either modified runtime file.
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • No new shader translator rule.
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5628376378 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3340). Originally posted by [Khayoon](https://github.com/Khayoon) on 2026-12-03T14:23:50Z. --- Claimed in the coordination issue as: [RT-CPU-PMADDWD-WRAP](https://github.com/ProjectLeonida/mandy/issues/3312#issuecomment-7380971261) ### What and why PMADDWD's all-INT16_MIN pair produces two individually representable products whose sum is 2147483648. The existing signed addition has undefined C++ behavior. Sum the products as uint32_t so the required 0x80000000 result is defined, and keep that arithmetic in a constexpr helper used by the emulation path. Add a constant-evaluation regression and a synthetic full-YMM execution test covering the overflowing pair, near-boundary positive and negative sums, mixed signs and ordinary inputs in both 128-bit halves. ### Tested - MSVC 19.44.35207, x64 Windows, Intel Core i7-3930K (AVX1; test CPUID reports no AVX2/FMA/F16C), C++20, `/O2 /arch:AVX /W4 /permissive- /utf-8`. Direct standalone test builds avoid the repository-wide `/arch:AVX2` default on this AVX1 host. - Red: put the exact previous signed pair arithmetic in the new production constexpr helper, leaving the regression unchanged. MSVC rejects the minimum/minimum static_assert with C2131 and specifically diagnoses signed overflow at `+`. - Green: unsigned pair addition compiles; `avx2_emulation_test` passes all 44 checks, including the new full-YMM regression. - `isa_jit_patch_test`: passes 51 single-instruction families, two fused pairs and the synthetic PE32+ image. `bmi_emulation_test`: 66 checks pass. `cpu_emulation_test`: all checks pass, including the 16-thread x 2048-site CLWB rewrite test. ISA test retains its existing C4324 alignment-padding warnings. - Changed-file and full tracked clean-room checks and `git diff --check` pass. The red result proves the original C++ expression is invalid under constant evaluation; no incorrect native output or measured gameplay impact is claimed. No game files, GPU runs or full runtime build were used. GPUs tested: n/a (CPU-only synthetic tests; profile Z). AI-assisted: yes. Implementation and separate read-only review: GPT-6.1 Sol in Codex. The reviewer found no blocking correctness issue. ### Checklist - [x] Tested against main b053a1813c8980c4357d51bbe6e69a35dc8c476c. The publication commit uses current main; intervening upstream changes do not touch either modified runtime file. - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [x] No new shader translator rule.
merzleckeier commented 2026-12-03 14:27:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by merzleckeier on 2026-12-03T14:27:48Z.


Independent CPU-only check on d3890375 merged onto 9039f6a3 (7e53dc20).

  • The unchanged avx2_emulation_test.cpp passed all 42 applicable checks in a Clang 23.1.5 MSVC-target build (/O2 /arch:AVX) under Wine 11.22 on Ryzen 7 7800X3D. That includes the new synthetic YMM-context test. The two live AVX1-only checks are conditional on features absent from this host.
  • I also compiled the exact production lane function and helper with Clang 23.1.5 and GCC 16.2.1, with AVX2 disabled for the emulation object and undefined-behavior checks enabled. For each compiler, all 268,705 pair results matched both a separate native vpmaddwd and a widened 64-bit integer reference. This covers all 6,561 combinations of nine boundary values per operand plus 262,144 seeded pairs.

No findings in the arithmetic change. AI-assisted (Codex).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381487572 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3340#issuecomment-7381487572). Originally posted by [merzleckeier](https://github.com/merzleckeier) on 2026-12-03T14:27:48Z. --- Independent CPU-only check on `d3890375` merged onto `9039f6a3` (`7e53dc20`). - The unchanged `avx2_emulation_test.cpp` passed all 42 applicable checks in a Clang 23.1.5 MSVC-target build (`/O2 /arch:AVX`) under Wine 11.22 on Ryzen 7 7800X3D. That includes the new synthetic YMM-context test. The two live AVX1-only checks are conditional on features absent from this host. - I also compiled the exact production lane function and helper with Clang 23.1.5 and GCC 16.2.1, with AVX2 disabled for the emulation object and undefined-behavior checks enabled. For each compiler, all 268,705 pair results matched both a separate native `vpmaddwd` and a widened 64-bit integer reference. This covers all 6,561 combinations of nine boundary values per operand plus 262,144 seeded pairs. No findings in the arithmetic change. AI-assisted (Codex).
Kal0s-design commented 2026-12-03 14:28:03 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Kal0s-design on 2026-12-03T14:28:03Z.


Ran natively on Windows 11, head d38903759 merged into current main (9039f6a31, clean merge).

No findings. The fix is correct and the emulation matches the hardware bit for bit. This adds native MSVC on an AVX2-capable CPU to the AVX1 run in the PR body and the Clang/GCC run above.

Tests (standalone cl builds with the same flags CMake gives these two targets, plus the repo's /arch:AVX2, C++20, /O2)

result
avx2_emulation_test, merge OK, 42 checks
isa_jit_patch_test, merge OK, 51 families + 2 fused pairs + synthetic PE32+ image (only the C4324 padding warnings already on main)

42 here vs 44 in the PR body is expected. The two extra checks in test_live_256bit_execution only run when the host lacks FMA/AVX2 (read). On this CPU that live test takes the native path. The new test_pmaddwd_wrap doesn't depend on the host: it calls ae::apply on a synthetic CONTEXT, so it always runs the emulation code, including the 0xF5 case (read, and it passes here).

Red/green (measured)

setup result
main header + new test file compile error C2039/C3861, pmaddwd_pair is not a member of detail (the helper doesn't exist on main)
PR header with the old signed sum put back inside pmaddwd_pair, new test C2131, "the '+' operation causing signed overflow during constant evaluation", same as the PR body
main header + new test with the static_assert line removed builds and passes, 42 checks

So the runtime test alone is not a red on MSVC: /O2 codegen wraps the signed add and gives 0x80000000 anyway. The static_assert is what catches the UB. That fits the PR body, which doesn't claim a wrong native result.

Emulation vs real _mm256_madd_epi16 (throwaway .cpp, /arch:AVX2, each case through ae::apply of vpmaddwd ymm2, ymm0, ymm1 and compared per dword)

input pairs mismatches, merge mismatches, main /O2 mismatches, main /Od
all 4-operand combinations of {-32768, -32767, -2, -1, 0, 1, 2, 32766, 32767} 6,568 (6,561 + padding) 0 0 0
one vector of all INT16_MIN 8 0 0 0
random, mt19937 seed 3340 10,000,000 0 0 0

apply succeeded on every call. Same story as the red table: MSVC happens to produce the right bits today, the PR makes it defined.

Other sites of the same shape (read, avx2_emulation.h on main). I didn't find another one. PMULHW (628), PMADDUBSW (675-676), PMULHRSW (700) and PMULDQ (706-707) all stay inside their int32/int64 range. PSIGNW/B (691, 694) and PABS (702-703) negate after widening to int32. PADDD/PSUBD/PMULLD, PHADDD/PHSUBD and PSIGND use u32/u64. The 16-bit shifts and the narrowing conversions to int16_t are defined in C++20. Line 635 is the one this PR fixes.

Conflicts: no other open PR touches avx2_emulation.h or its test (checked all 223 open PRs). git merge-tree with main is clean.

Not checked: the CMake/ctest path (I built the two targets with cl directly), an AVX1-only host. Clang/GCC with UB checks are covered in the comment above.

GPUs tested: n/a (CPU-only tests).
AI-assisted: yes (Claude Code).

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7381494867 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/3340#issuecomment-7381494867). Originally posted by [Kal0s-design](https://github.com/Kal0s-design) on 2026-12-03T14:28:03Z. --- Ran natively on Windows 11, head `d38903759` merged into current `main` (`9039f6a31`, clean merge). No findings. The fix is correct and the emulation matches the hardware bit for bit. This adds native MSVC on an AVX2-capable CPU to the AVX1 run in the PR body and the Clang/GCC run above. **Tests** (standalone `cl` builds with the same flags CMake gives these two targets, plus the repo's `/arch:AVX2`, C++20, `/O2`) | | result | |---|---| | `avx2_emulation_test`, merge | OK, 42 checks | | `isa_jit_patch_test`, merge | OK, 51 families + 2 fused pairs + synthetic PE32+ image (only the C4324 padding warnings already on `main`) | 42 here vs 44 in the PR body is expected. The two extra checks in `test_live_256bit_execution` only run when the host lacks FMA/AVX2 (read). On this CPU that live test takes the native path. The new `test_pmaddwd_wrap` doesn't depend on the host: it calls `ae::apply` on a synthetic CONTEXT, so it always runs the emulation code, including the `0xF5` case (read, and it passes here). **Red/green** (measured) | setup | result | |---|---| | `main` header + new test file | compile error C2039/C3861, `pmaddwd_pair` is not a member of `detail` (the helper doesn't exist on `main`) | | PR header with the old signed sum put back inside `pmaddwd_pair`, new test | C2131, "the '+' operation causing signed overflow during constant evaluation", same as the PR body | | `main` header + new test with the `static_assert` line removed | builds and passes, 42 checks | So the runtime test alone is not a red on MSVC: `/O2` codegen wraps the signed add and gives `0x80000000` anyway. The `static_assert` is what catches the UB. That fits the PR body, which doesn't claim a wrong native result. **Emulation vs real `_mm256_madd_epi16`** (throwaway `.cpp`, `/arch:AVX2`, each case through `ae::apply` of `vpmaddwd ymm2, ymm0, ymm1` and compared per dword) | input | pairs | mismatches, merge | mismatches, `main` `/O2` | mismatches, `main` `/Od` | |---|---|---|---|---| | all 4-operand combinations of {-32768, -32767, -2, -1, 0, 1, 2, 32766, 32767} | 6,568 (6,561 + padding) | 0 | 0 | 0 | | one vector of all INT16_MIN | 8 | 0 | 0 | 0 | | random, `mt19937` seed 3340 | 10,000,000 | 0 | 0 | 0 | `apply` succeeded on every call. Same story as the red table: MSVC happens to produce the right bits today, the PR makes it defined. **Other sites of the same shape** (read, `avx2_emulation.h` on `main`). I didn't find another one. PMULHW (628), PMADDUBSW (675-676), PMULHRSW (700) and PMULDQ (706-707) all stay inside their int32/int64 range. PSIGNW/B (691, 694) and PABS (702-703) negate after widening to int32. PADDD/PSUBD/PMULLD, PHADDD/PHSUBD and PSIGND use `u32`/`u64`. The 16-bit shifts and the narrowing conversions to `int16_t` are defined in C++20. Line 635 is the one this PR fixes. Conflicts: no other open PR touches `avx2_emulation.h` or its test (checked all 223 open PRs). `git merge-tree` with `main` is clean. Not checked: the CMake/ctest path (I built the two targets with `cl` directly), an AVX1-only host. Clang/GCC with UB checks are covered in the comment above. GPUs tested: n/a (CPU-only tests). AI-assisted: yes (Claude Code).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/3340:upstream-pr/3340'
git switch 'upstream-pr/3340'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/3340'
git switch 'upstream-pr/3340'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/3340'
git switch 'upstream-pr/3340'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/3340'
git switch 'main'
git merge --squash 'upstream-pr/3340'
git switch 'main'
git merge --ff-only 'upstream-pr/3340'
git switch 'main'
git merge 'upstream-pr/3340'
git push origin 'main'
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
ProjectLeonida/mandy!3340
No description provided.