feat(libc): add SSE2 paths for trivial search and min helpers #12717

Open
xdCloudy wants to merge 4 commits from upstream-pr/12717 into main
xdCloudy commented 2026-10-29 04:44:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xdCloudy on 2026-10-29T04:44:07Z.


What

Add SSE2 implementations for ten existing libc trivial-algorithm helpers:

  • __std_find_trivial_{1,2,4}
  • __std_find_trivial_unsized_{1,2}
  • __std_count_trivial_{1,2,4}
  • __std_min_element_{1,2}

Preserve the guest ABI, scalar fallbacks, unaligned handling, scalar tails and first-minimum semantics.

This change provides additional SIMD implementations without changing the expected functional results.

Tested

Windows x86_64 / WinLibs GCC / CMake-Ninja Release:

  • guest_vector_algorithms: PASS
  • guest_vector_algorithms_scalar: PASS

The forced-scalar target compiles the production implementation with ANYPS5_LIBC_FORCE_SCALAR. Preprocessing confirms that the SIMD implementation is excluded.

Independent differential testing reported 611,066 passing checks with zero correctness differences, covering randomized inputs, alignment, signedness, range boundaries, page-end buffers and unsized searches.

These results establish functional equivalence for the tested inputs, not performance benefits.

Performance evidence

Earlier standalone host microbenchmarks indicated improvements on selected large-array workloads.

However, those measurements were outside an AnyPS5 title and do not establish gameplay improvements.

  • Dreaming Sarah menu captures showed no demonstrated speedup or confirmed use of the modified helpers.
  • Minecraft 01.008.000 did not establish imports of the affected helpers.
  • Minecraft 01.044.000 has a relevant import supplied by its bundled libc, not demonstrated to reach this implementation.

No qualifying same-title, same-scene before/after performance traces are available.

Current status

The PR remains draft pending qualifying title-level evidence or an explicit maintainer decision to accept the correctness-validated implementation without a demonstrated title performance benefit.

No gameplay performance claim is made.

Checklist

  • One topic per PR
  • Preserve existing ABI and behavior
  • No new unimplemented paths
  • No new third-party dependency
  • Generic, not title-specific
  • Tested SIMD and forced-scalar implementations
  • Document limitations of current performance evidence
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5062339696 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717). Originally posted by [xdCloudy](https://github.com/xdCloudy) on 2026-10-29T04:44:07Z. --- ### What Add SSE2 implementations for ten existing libc trivial-algorithm helpers: - `__std_find_trivial_{1,2,4}` - `__std_find_trivial_unsized_{1,2}` - `__std_count_trivial_{1,2,4}` - `__std_min_element_{1,2}` Preserve the guest ABI, scalar fallbacks, unaligned handling, scalar tails and first-minimum semantics. This change provides additional SIMD implementations without changing the expected functional results. ### Tested Windows x86_64 / WinLibs GCC / CMake-Ninja Release: - `guest_vector_algorithms`: PASS - `guest_vector_algorithms_scalar`: PASS The forced-scalar target compiles the production implementation with `ANYPS5_LIBC_FORCE_SCALAR`. Preprocessing confirms that the SIMD implementation is excluded. Independent differential testing reported 611,066 passing checks with zero correctness differences, covering randomized inputs, alignment, signedness, range boundaries, page-end buffers and unsized searches. These results establish functional equivalence for the tested inputs, not performance benefits. ### Performance evidence Earlier standalone host microbenchmarks indicated improvements on selected large-array workloads. However, those measurements were outside an AnyPS5 title and do not establish gameplay improvements. - Dreaming Sarah menu captures showed no demonstrated speedup or confirmed use of the modified helpers. - Minecraft 01.008.000 did not establish imports of the affected helpers. - Minecraft 01.044.000 has a relevant import supplied by its bundled libc, not demonstrated to reach this implementation. No qualifying same-title, same-scene before/after performance traces are available. ### Current status The PR remains draft pending qualifying title-level evidence or an explicit maintainer decision to accept the correctness-validated implementation without a demonstrated title performance benefit. No gameplay performance claim is made. ### Checklist - [x] One topic per PR - [x] Preserve existing ABI and behavior - [x] No new unimplemented paths - [x] No new third-party dependency - [x] Generic, not title-specific - [x] Tested SIMD and forced-scalar implementations - [x] Document limitations of current performance evidence - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
rooklz commented 2026-10-29 04:45:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T04:45:49Z.

Review: COMMENTED


The branch doesn't contain the change this PR describes. Checked on macOS (host-side only):

  • Head 496f97a1c2 has one commit, feat(libkernel_sys): implement getfsstat. It changes only libkernel_sys/libc path-alias files (7 files, +57/-6). VectorAlgorithms.cpp and GuestVectorAlgorithms.cpp aren't modified, and the PR head has no SSE2 code in the __std_find_trivial_*/__std_count_trivial_*/__std_min_element_* helpers.
  • 496f97a1c24f84c17bae952cbbfd4bdb38e2072d is also the head of your closed #12695, so the getfsstat branch seems to have been pushed to perf/libc-vector-algorithms-sse2-10 by mistake.
  • That commit wouldn't link on its own. Export.cpp L56 declares extern "C" int PosixStatfs_nid_no_patch(...) and calls it at L469, but git grep PosixStatfs finds no definition anywhere in the head tree or on main 52c09d567d.
  • getfsstat is also implemented by the older open #11031, and the head conflicts with it (git merge-tree: Export.cpp, GuestKernelSys.cpp, debt/libkernel_sys.md).
  • The failing conventions job is the body template: it uses ## What/Tested/Checklist where the template needs ### headings. The diff itself passes check_conventions.py --base upstream/main locally.

Could you force-push the vector-algorithms commit to this branch and fix the headings? I'm happy to run a differential check of the ten helpers against the scalar versions once it's there.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5692972266 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717#pullrequestreview-5692972266). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T04:45:49Z. **Review:** COMMENTED --- The branch doesn't contain the change this PR describes. Checked on macOS (host-side only): - Head `496f97a1c2` has one commit, `feat(libkernel_sys): implement getfsstat`. It changes only libkernel_sys/libc path-alias files (7 files, +57/-6). `VectorAlgorithms.cpp` and `GuestVectorAlgorithms.cpp` aren't modified, and the PR head has no SSE2 code in the `__std_find_trivial_*`/`__std_count_trivial_*`/`__std_min_element_*` helpers. - `496f97a1c24f84c17bae952cbbfd4bdb38e2072d` is also the head of your closed #12695, so the getfsstat branch seems to have been pushed to `perf/libc-vector-algorithms-sse2-10` by mistake. - That commit wouldn't link on its own. `Export.cpp` L56 declares `extern "C" int PosixStatfs_nid_no_patch(...)` and calls it at L469, but `git grep PosixStatfs` finds no definition anywhere in the head tree or on main 52c09d567d. - getfsstat is also implemented by the older open #11031, and the head conflicts with it (`git merge-tree`: Export.cpp, GuestKernelSys.cpp, debt/libkernel_sys.md). - The failing `conventions` job is the body template: it uses `## What/Tested/Checklist` where the template needs `###` headings. The diff itself passes `check_conventions.py --base upstream/main` locally. Could you force-push the vector-algorithms commit to this branch and fix the headings? I'm happy to run a differential check of the ten helpers against the scalar versions once it's there.
rooklz commented 2026-10-29 05:11:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-29T05:11:07Z.

Review: COMMENTED


Thanks for pushing the vector commit. I tested 7bd7edae5e on the host only (macOS 26, Apple clang, built -arch x86_64 so __SSE2__ is on, ASan+UBSan, -fno-sanitize-recover=undefined). I did not run it on a PS5 or under Windows.

Differential vs main. I linked VectorAlgorithms.cpp from this PR and from main 52c09d567d (main's symbols renamed) into one binary. A seeded random harness then compared find / count / find_unsized / min_element (signed and unsigned) for element sizes 1/2/4/8. Inputs: lengths 0–599, start offsets 0–63 bytes (so unaligned starts are covered), random data or 4-value pools with 0, all-ones and the sign bit, and targets taken from the range or from the pool. For find_unsized, the target is always present at or after the start.

  • 11,984,195 calls, 0 differences, no ASan/UBSan reports, 9.8 s.
  • arm64 (scalar fallback): the PR test also passes.

Mutants. 13 mutants, each run against the PR test and against the differential:

  • Killed by both (7): count divisor, lane index without / sizeof, skipping the first-block scan in unsized, signed int8 min treated as unsigned, unsigned uint16 min treated as signed, sign-flip constant, returning the last minimum instead of the first.
  • Equivalent (5): dropping bits >> 1 (16-bit) or >> 3 (32-bit) in MatchPositions, because cmpeq sets every byte of a lane together; < 64 → < 16; >= 16 → > 16; starting minValue at lanes[1]. All five give 0 differences.
  • Missed by the PR test (1): removing the scalar tail fold in MinElementSse2 (for (; current != end; ...) if (value < minValue) ...). The PR test still passes because element 35 is already the minimum before the tail. The differential catches it (19,746 differences). This 2-row addition kills it, and passes on this PR and on main:
+template<typename TValue>
+static void CheckMinElementTail(MinElementFunction function, bool isSigned, TValue fill, TValue minimum) {
+    std::vector<TValue> values(64 / sizeof(TValue) + 3, fill);
+    values.back() = minimum;
+    Require(function(values.data(), values.data() + values.size(), isSigned) == &values.back());
+}
+
 static void CheckVectorizedHelpers() {
@@
     CheckMinElementVector<std::uint16_t>(__std_min_element_2_nid_postfix, false, UINT16_MAX, 2, Length);
+    CheckMinElementTail<std::int8_t>(__std_min_element_1_nid_postfix, true, 20, -5);
+    CheckMinElementTail<std::uint16_t>(__std_min_element_2_nid_postfix, false, UINT16_MAX, 2);

Other checks

  • git merge-tree against main 52c09d567d is clean, and check_conventions --base upstream/main --head passes.
  • This conflicts with #12605 (KernelPanicNT, which changes the __std_min/max range-check lines in the same file plus docs/dev/debt/libc.md). Whichever lands second needs a small rebase.
  • Optional: the PR body could include a benchmark number. The change is perf-only and has no behaviour change (per the above), so a speed-up figure would be the evidence for merging it.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5693134945 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717#pullrequestreview-5693134945). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-29T05:11:07Z. **Review:** COMMENTED --- Thanks for pushing the vector commit. I tested 7bd7edae5e on the host only (macOS 26, Apple clang, built `-arch x86_64` so `__SSE2__` is on, ASan+UBSan, `-fno-sanitize-recover=undefined`). I did not run it on a PS5 or under Windows. **Differential vs main.** I linked `VectorAlgorithms.cpp` from this PR and from main 52c09d567d (main's symbols renamed) into one binary. A seeded random harness then compared find / count / find_unsized / min_element (signed and unsigned) for element sizes 1/2/4/8. Inputs: lengths 0–599, start offsets 0–63 bytes (so unaligned starts are covered), random data or 4-value pools with 0, all-ones and the sign bit, and targets taken from the range or from the pool. For find_unsized, the target is always present at or after the start. - 11,984,195 calls, **0 differences**, no ASan/UBSan reports, 9.8 s. - arm64 (scalar fallback): the PR test also passes. **Mutants.** 13 mutants, each run against the PR test and against the differential: - **Killed by both (7):** count divisor, lane index without `/ sizeof`, skipping the first-block scan in unsized, signed int8 min treated as unsigned, unsigned uint16 min treated as signed, sign-flip constant, returning the last minimum instead of the first. - **Equivalent (5):** dropping `bits >> 1` (16-bit) or `>> 3` (32-bit) in `MatchPositions`, because `cmpeq` sets every byte of a lane together; `< 64` → `< 16`; `>= 16` → `> 16`; starting `minValue` at `lanes[1]`. All five give 0 differences. - **Missed by the PR test (1):** removing the scalar tail fold in `MinElementSse2` (`for (; current != end; ...) if (value < minValue) ...`). The PR test still passes because element 35 is already the minimum before the tail. The differential catches it (19,746 differences). This 2-row addition kills it, and passes on this PR and on main: ```diff +template<typename TValue> +static void CheckMinElementTail(MinElementFunction function, bool isSigned, TValue fill, TValue minimum) { + std::vector<TValue> values(64 / sizeof(TValue) + 3, fill); + values.back() = minimum; + Require(function(values.data(), values.data() + values.size(), isSigned) == &values.back()); +} + static void CheckVectorizedHelpers() { @@ CheckMinElementVector<std::uint16_t>(__std_min_element_2_nid_postfix, false, UINT16_MAX, 2, Length); + CheckMinElementTail<std::int8_t>(__std_min_element_1_nid_postfix, true, 20, -5); + CheckMinElementTail<std::uint16_t>(__std_min_element_2_nid_postfix, false, UINT16_MAX, 2); ``` **Other checks** - `git merge-tree` against main 52c09d567d is clean, and `check_conventions --base upstream/main --head` passes. - This conflicts with #12605 (KernelPanicNT, which changes the `__std_min/max` range-check lines in the same file plus `docs/dev/debt/libc.md`). Whichever lands second needs a small rebase. - Optional: the PR body could include a benchmark number. The change is perf-only and has no behaviour change (per the above), so a speed-up figure would be the evidence for merging it. AI-assisted: yes
CnRJay commented 2026-11-01 19:54:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:54:37Z.

Review: COMMENTED


  1. The speed claim is a host microbenchmark. The figures (find u8 10x, min u8 17.6x) are for 1 MiB arrays at -O3, 2,000 calls per sample, outside AnyPS5. The project's rule for a speed claim is tools/trace_digest.py summaries of main and the change from the same machine and scene. Nothing shows these __std_find_trivial, __std_count_trivial or __std_min_element helpers appear in any title's trace at all, or at what sizes. Typical std::find over a short range is not 1 MiB, and for short inputs the SSE2 prologue and scalar tail can be slower than the loop they replace.
  2. Please name a title and a trace where these calls show up with a non-trivial share, or drop the perf framing.
  3. Portability: the vector path uses _mm_cvtsi128_si64 and is guarded by __SSE2__ || _M_X64. Which configurations take the scalar path, and does the new test run on one of them? The description lists only Linux x86-64 GCC 13.3.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5729939051 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717#pullrequestreview-5729939051). Originally posted by [CnRJay](https://github.com/CnRJay) on 2026-11-01T19:54:37Z. **Review:** COMMENTED --- 1. **The speed claim is a host microbenchmark.** The figures (find u8 10x, min u8 17.6x) are for 1 MiB arrays at `-O3`, 2,000 calls per sample, outside AnyPS5. The project's rule for a speed claim is `tools/trace_digest.py` summaries of `main` and the change from the same machine and scene. Nothing shows these `__std_find_trivial`, `__std_count_trivial` or `__std_min_element` helpers appear in any title's trace at all, or at what sizes. Typical `std::find` over a short range is not 1 MiB, and for short inputs the SSE2 prologue and scalar tail can be slower than the loop they replace. 2. **Please name a title and a trace** where these calls show up with a non-trivial share, or drop the perf framing. 3. Portability: the vector path uses `_mm_cvtsi128_si64` and is guarded by `__SSE2__ || _M_X64`. Which configurations take the scalar path, and does the new test run on one of them? The description lists only Linux x86-64 GCC 13.3.
THEMANnt commented 2026-11-02 19:43:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-11-02T19:43:53Z.

Review: APPROVED


Reviewed e0ca5fec4f379bb00b60e6a632c5a761a8400582 against d8b60b46ed425b117b6fae18900fe6ab81ed6661 for correctness of the SSE2 implementation and scalar fallback. No correctness blocker found in the reviewed diff.

Windows x64 / WinLibs GCC 15.2 UCRT Release: registered guest_vector_algorithms and guest_vector_algorithms_scalar both pass (2/2). Preprocessing confirms the production SSE2 guard is selected normally and excluded with ANYPS5_LIBC_FORCE_SCALAR; this is scalar-source testing on x86-64, not an ARM/Emscripten runtime result.

An independent -O3 harness compiles the original base, current SSE2 source and forced-scalar source separately with distinct symbol names and the real guest ABI. All 611,066 checks pass, covering deterministic randomized values/lengths/byte offsets, long all-match counts, empty ranges, signed/unsigned minima and first-tie behavior, guarded page-end buffers, and byte/word unsized searches. Bounded operations are also checked against independently computed expected results. These are functional checks, not benchmark iterations or proof of a gameplay speedup. Diff whitespace passes.

The revised correctness-only framing is reasonable. The PR description still says to keep it draft pending title traces, so that text should be aligned with the intended scope and maintainers' requirements. This review does not establish a title-level performance benefit or waive repository performance policy.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5742356227 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717#pullrequestreview-5742356227). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-11-02T19:43:53Z. **Review:** APPROVED --- Reviewed `e0ca5fec4f379bb00b60e6a632c5a761a8400582` against `d8b60b46ed425b117b6fae18900fe6ab81ed6661` for correctness of the SSE2 implementation and scalar fallback. No correctness blocker found in the reviewed diff. Windows x64 / WinLibs GCC 15.2 UCRT Release: registered `guest_vector_algorithms` and `guest_vector_algorithms_scalar` both pass (2/2). Preprocessing confirms the production SSE2 guard is selected normally and excluded with `ANYPS5_LIBC_FORCE_SCALAR`; this is scalar-source testing on x86-64, not an ARM/Emscripten runtime result. An independent `-O3` harness compiles the original base, current SSE2 source and forced-scalar source separately with distinct symbol names and the real guest ABI. All **611,066 checks pass**, covering deterministic randomized values/lengths/byte offsets, long all-match counts, empty ranges, signed/unsigned minima and first-tie behavior, guarded page-end buffers, and byte/word unsized searches. Bounded operations are also checked against independently computed expected results. These are functional checks, not benchmark iterations or proof of a gameplay speedup. Diff whitespace passes. The revised correctness-only framing is reasonable. The PR description still says to keep it draft pending title traces, so that text should be aligned with the intended scope and maintainers' requirements. This review does not establish a title-level performance benefit or waive repository performance policy.
MarcZX10 commented 2026-11-20 08:49:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T08:49:30Z.

Review: COMMENTED


Reviewed — no blocking findings

The implementation and its added tests show no verified blocking defect; the PR’s title and description also make clear that title level performance benefits have not been demonstrated.

What I ran

Reviewed head e0ca5fec4f379bb00b60e6a632c5a761a8400582. Read the prepared description, commit list, manifest, diff, and the changed implementation, test, and CMake sections through repo_read.py. No build or tests were run during this review.

Results

Check Result Note
Changed implementation review passed No verified blocking defect
Added tests and build configuration passed Inspected targeted changes
Build and test execution not run No local execution performed
Excluded files inventory not run files.json was unavailable through the reader

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author reports Windows x86_64 test runs and differential checks in the PR description; these were not independently run or verified. The files.json inventory was unavailable, so I could not confirm whether any changed files were excluded from the prepared bundle. The prepared manifest lists three changed files, and all three were inspected in relevant sections; I did not read their full text.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5989837818 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717#pullrequestreview-5989837818). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T08:49:30Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The implementation and its added tests show no verified blocking defect; the PR’s title and description also make clear that title level performance benefits have not been demonstrated. ## What I ran Reviewed head `e0ca5fec4f379bb00b60e6a632c5a761a8400582`. Read the prepared description, commit list, manifest, diff, and the changed implementation, test, and CMake sections through `repo_read.py`. No build or tests were run during this review. ## Results | Check | Result | Note | |---|---|---| | Changed implementation review | passed | No verified blocking defect | | Added tests and build configuration | passed | Inspected targeted changes | | Build and test execution | not run | No local execution performed | | Excluded files inventory | not run | `files.json` was unavailable through the reader | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author reports Windows x86_64 test runs and differential checks in the PR description; these were not independently run or verified. The `files.json` inventory was unavailable, so I could not confirm whether any changed files were excluded from the prepared bundle. The prepared manifest lists three changed files, and all three were inspected in relevant sections; I did not read their full text. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 13:24:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:24:54Z.

Review: COMMENTED


Built the focused checks against the existing Linux runtime. GuestVectorAlgorithms passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170649835 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12717#pullrequestreview-6170649835). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:24:54Z. **Review:** COMMENTED --- Built the focused checks against the existing Linux runtime. `GuestVectorAlgorithms` passed. AI-assisted: yes.
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/12717:upstream-pr/12717'
git switch 'upstream-pr/12717'

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/12717'
git switch 'upstream-pr/12717'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/12717'
git switch 'upstream-pr/12717'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/12717'
git switch 'main'
git merge --squash 'upstream-pr/12717'
git switch 'main'
git merge --ff-only 'upstream-pr/12717'
git switch 'main'
git merge 'upstream-pr/12717'
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
boykopovar/AnyPS5!12717
No description provided.