feat(libc): implement reverse trivial element searches #15098

Merged
Danigamer05 merged 2 commits from upstream-pr/15098 into main 2026-11-03 05:14:09 +00:00
Danigamer05 commented 2026-11-02 16:10:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-02T16:10:12Z.


What

Implement libc's __std_find_last_trivial_1/2/4/8 (NIDs eaLVyLMql6I, -ciYO9tOTVQ, fPGFDSCERGM, akVoBLxgReY). Given a valid half-open range and a value of the corresponding unsigned width, return the last equal element, or the supplied end pointer if absent. Reads support unaligned elements and leave input and errno unchanged. Equal endpoints return without accessing memory, including null and inaccessible endpoints. Malformed reversed, partial-element or nonempty null-ended ranges throw; their native faults or nontermination are not reproduced.

The ABI and behavior come from the bundled libc.prx in PPSA03671 v01.001.005, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6, at RVAs 0x658D0, 0x659B0, 0x65A90, 0x65B70. These leaf exports have no calls or RIP-relative data accesses. The implementation uses a scalar element search and the existing unaligned read helper; no SIMD code or original bytes are copied.

#13273 originally declared the same four names as throwing inventory placeholders in libSceLibcInternal, where an import from that library would have been resolved before this libc implementation. Its author removed them in bb4d312e54ddfb0b5e7471271d37ff7cd3afe767 after we agreed on the order in that PR, so the two changes no longer share exports.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 5e85609d0b0152d14ae34d708d3237005ed311ca. Exact tested commit: 9527343a948295e74bc6787a90ab7865ead17770.

The change was first tested on main 0cc3899f6992e86d217a623abc6990feb2ce27b3 at 480c7de6c93d7e0c30a3e439e1f8085bc6e19f48, including the negative control below, then rebased onto the main above without conflicts; the diff of the change is the same apart from context lines. All 18 selected CTests and all 1,703,708 differential vectors were run again at the tested commit and pass.

  • Built libc, its patched production library, guest_find_last_trivial_tests and the neighboring guest_vector_algorithms_tests.
  • All 18 selected CTests pass: 16 width/mode cases, guest_vector_algorithms and public_headers. Cases cover empty/inaccessible ranges, every matching position, duplicate matches, absent values, all 8/16-bit values, high bits and deterministic wider values, 32 byte alignments, unchanged inputs/errno and read-only pages between inaccessible guards. The longest new test took 0.99 seconds locally.
  • Before the implementation, the original leaf functions were executed on 1,703,708 vectors against independently computed expectations. At the tested commit the patched production library agreed with the original and with those expectations on all of them, including 100,000 deterministic random ranges per width. There were no return-pointer or input differences, and the candidate left errno unchanged. Original initializers are not run.
  • Negative control, at the pre-rebase commit: last-match search was temporarily replaced with the existing first-match search. All 12 positions, values and guard-page cases failed, as expected. The production source was then restored byte for byte (SHA-256 checked) and rebuilt, and all 18 CTests and all 1,703,708 differential vectors passed again. No test asserts that an unimplemented function throws.
  • The progress report lists the four exports as declared stubs. Reversed ranges, ranges ending in a partial element and nonempty ranges with a null end call NotImplemented_nid_no_patch, where the original faults or does not terminate; tools/progress.py counts any export that can reach that call as a stub. Every valid range is implemented. On main, __std_max_4i/4u/8i/8u and __std_min_4i/4u/8i/8u use the same kind of guard and are counted the same way.
  • Code conventions and git diff --check pass.
  • tools/pr_overlap.py read 1,747 open PRs at the tested commit and reports no confirmed overlap. #13273 no longer shares exports. It keeps exit 2 for 58 branches that already conflict with main (#5021, #5303, #6819, #7081, #7386, #7447, #7715, #8150, #8965, #9087, #9340, #9371, #9447, #9526, #9929, #10113, #10226, #10245, #10250, #10253, #10265, #10274, #10325, #10328, #10345, #10349, #10405, #10506, #10510, #10512, #10565, #10987, #11260, #11262, #11266, #11332, #11632, #11634, #11724, #11726, #11831, #11907, #12122, #12397, #12514, #12560, #12574, #12593, #12622, #12627, #12682, #12688, #12700, #12965, #13119, #13390, #13804, #14212). None of their added lines against their merge base contains the four names or NIDs. Merging each with this branch and with main using git merge-tree gives the same conflict blocks, and no conflict block contains this change.

Not tested: Linux, macOS, another SDK, a game, GPU or console. No speed or game compatibility claim. No original module, extracted bytes, oracle, report or log is committed or uploaded.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5123457653 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15098). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-02T16:10:12Z. --- ### What Implement libc's `__std_find_last_trivial_1/2/4/8` (NIDs `eaLVyLMql6I`, `-ciYO9tOTVQ`, `fPGFDSCERGM`, `akVoBLxgReY`). Given a valid half-open range and a value of the corresponding unsigned width, return the last equal element, or the supplied end pointer if absent. Reads support unaligned elements and leave input and errno unchanged. Equal endpoints return without accessing memory, including null and inaccessible endpoints. Malformed reversed, partial-element or nonempty null-ended ranges throw; their native faults or nontermination are not reproduced. The ABI and behavior come from the bundled `libc.prx` in PPSA03671 v01.001.005, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`, at RVAs `0x658D0`, `0x659B0`, `0x65A90`, `0x65B70`. These leaf exports have no calls or RIP-relative data accesses. The implementation uses a scalar element search and the existing unaligned read helper; no SIMD code or original bytes are copied. #13273 originally declared the same four names as throwing inventory placeholders in `libSceLibcInternal`, where an import from that library would have been resolved before this libc implementation. Its author removed them in `bb4d312e54ddfb0b5e7471271d37ff7cd3afe767` after we agreed on the order in that PR, so the two changes no longer share exports. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Windows x86-64, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, main `5e85609d0b0152d14ae34d708d3237005ed311ca`. Exact tested commit: `9527343a948295e74bc6787a90ab7865ead17770`. The change was first tested on main `0cc3899f6992e86d217a623abc6990feb2ce27b3` at `480c7de6c93d7e0c30a3e439e1f8085bc6e19f48`, including the negative control below, then rebased onto the main above without conflicts; the diff of the change is the same apart from context lines. All 18 selected CTests and all 1,703,708 differential vectors were run again at the tested commit and pass. - Built libc, its patched production library, `guest_find_last_trivial_tests` and the neighboring `guest_vector_algorithms_tests`. - All 18 selected CTests pass: 16 width/mode cases, `guest_vector_algorithms` and `public_headers`. Cases cover empty/inaccessible ranges, every matching position, duplicate matches, absent values, all 8/16-bit values, high bits and deterministic wider values, 32 byte alignments, unchanged inputs/errno and read-only pages between inaccessible guards. The longest new test took 0.99 seconds locally. - Before the implementation, the original leaf functions were executed on 1,703,708 vectors against independently computed expectations. At the tested commit the patched production library agreed with the original and with those expectations on all of them, including 100,000 deterministic random ranges per width. There were no return-pointer or input differences, and the candidate left errno unchanged. Original initializers are not run. - Negative control, at the pre-rebase commit: last-match search was temporarily replaced with the existing first-match search. All 12 positions, values and guard-page cases failed, as expected. The production source was then restored byte for byte (SHA-256 checked) and rebuilt, and all 18 CTests and all 1,703,708 differential vectors passed again. No test asserts that an unimplemented function throws. - The progress report lists the four exports as declared stubs. Reversed ranges, ranges ending in a partial element and nonempty ranges with a null end call `NotImplemented_nid_no_patch`, where the original faults or does not terminate; `tools/progress.py` counts any export that can reach that call as a stub. Every valid range is implemented. On main, `__std_max_4i/4u/8i/8u` and `__std_min_4i/4u/8i/8u` use the same kind of guard and are counted the same way. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,747 open PRs at the tested commit and reports no confirmed overlap. #13273 no longer shares exports. It keeps exit 2 for 58 branches that already conflict with main (#5021, #5303, #6819, #7081, #7386, #7447, #7715, #8150, #8965, #9087, #9340, #9371, #9447, #9526, #9929, #10113, #10226, #10245, #10250, #10253, #10265, #10274, #10325, #10328, #10345, #10349, #10405, #10506, #10510, #10512, #10565, #10987, #11260, #11262, #11266, #11332, #11632, #11634, #11724, #11726, #11831, #11907, #12122, #12397, #12514, #12560, #12574, #12593, #12622, #12627, #12682, #12688, #12700, #12965, #13119, #13390, #13804, #14212). None of their added lines against their merge base contains the four names or NIDs. Merging each with this branch and with main using `git merge-tree` gives the same conflict blocks, and no conflict block contains this change. Not tested: Linux, macOS, another SDK, a game, GPU or console. No speed or game compatibility claim. No original module, extracted bytes, oracle, report or log is committed or uploaded. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
BunnyFl commented 2026-11-02 16:43:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T16:43:22Z.

Review: APPROVED


Cross-checked against firmware 13.42's system libSceLibcInternal.sprx (sha256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285). All four NIDs are exported there as leaf functions at 0x40dd0, 0x40eb0, 0x40f90 and 0x41070. The 1-byte form, for example, is a backward vpcmpeqb scan in 32-byte, then 16-byte, blocks, with a final cmp rdi, rcx against the start (read from disassembly only, not measured on a console). That matches a last-match search that returns the end pointer when nothing matches. The scalar implementation computes the same result.

Build and test on Windows (WinLibs GCC, BUILD_TESTING=ON), PR merged into current main: all 16 guest_find_last_trivial_* tests pass. On main the exports don't exist.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5739988716 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15098#pullrequestreview-5739988716). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T16:43:22Z. **Review:** APPROVED --- Cross-checked against firmware 13.42's system `libSceLibcInternal.sprx` (sha256 `a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285`). All four NIDs are exported there as leaf functions at `0x40dd0`, `0x40eb0`, `0x40f90` and `0x41070`. The 1-byte form, for example, is a backward `vpcmpeqb` scan in 32-byte, then 16-byte, blocks, with a final `cmp rdi, rcx` against the start (read from disassembly only, not measured on a console). That matches a last-match search that returns the end pointer when nothing matches. The scalar implementation computes the same result. Build and test on Windows (WinLibs GCC, `BUILD_TESTING=ON`), PR merged into current `main`: all 16 `guest_find_last_trivial_*` tests pass. On `main` the exports don't exist.
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!15098
No description provided.