feat(libc): implement heapsort #11400

Merged
iAbdulrrahman merged 2 commits from upstream-pr/11400 into main 2026-10-27 08:25:10 +00:00
iAbdulrrahman commented 2026-10-26 10:53:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by iAbdulrrahman on 2026-10-26T10:53:12Z.


What

Implement heapsort in libc matching FreeBSD 11 behavior, and add test coverage in GuestQsort.cpp.

Tested

Windows 11, core/libs/tests/GuestQsort.cpp with arrays of various lengths (0, 1, 2, 3, 7, 16, 33, 1000), zero element size argument error checking (EINVAL), and null base when count is zero.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5016209813 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11400). Originally posted by [iAbdulrrahman](https://github.com/iAbdulrrahman) on 2026-10-26T10:53:12Z. --- ### What Implement `heapsort` in `libc` matching FreeBSD 11 behavior, and add test coverage in `GuestQsort.cpp`. ### 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 11, `core/libs/tests/GuestQsort.cpp` with arrays of various lengths (`0`, `1`, `2`, `3`, `7`, `16`, `33`, `1000`), zero element size argument error checking (`EINVAL`), and null base when count is zero. ### 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
MarcZX10 commented 2026-10-26 10:54:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T10:54:00Z.

Review: CHANGES_REQUESTED


Reviewed exact head d91c3b0de966d48f2243c21e42668b9b08524a22. There is a behavior mismatch with the stated FreeBSD 11 reference: FreeBSD 11.4 heapsort returns success for nmemb <= 1 before checking whether size is zero. This implementation checks size == 0 first, so zero elements or one element with zero size incorrectly returns -1 and sets errno = EINVAL instead of returning 0 without inspecting the comparator/base.

Please put the count <= 1 fast path before the size check and add tests for both count == 0 and count == 1 with size == 0 (including a null base for the zero-count case). The hosted eligibility, conventions, intake, labels and report checks are still queued; I did not run the product test locally.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5650900509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11400#pullrequestreview-5650900509). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T10:54:00Z. **Review:** CHANGES_REQUESTED --- Reviewed exact head `d91c3b0de966d48f2243c21e42668b9b08524a22`. There is a behavior mismatch with the stated FreeBSD 11 reference: [FreeBSD 11.4 heapsort](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/libc/stdlib/heapsort.c#L887-L895) returns success for `nmemb <= 1` before checking whether `size` is zero. This implementation checks `size == 0` first, so zero elements or one element with zero size incorrectly returns `-1` and sets `errno = EINVAL` instead of returning `0` without inspecting the comparator/base. Please put the `count <= 1` fast path before the size check and add tests for both `count == 0` and `count == 1` with `size == 0` (including a null base for the zero-count case). The hosted eligibility, conventions, intake, labels and report checks are still queued; I did not run the product test locally.
MarcZX10 commented 2026-10-26 11:25:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T11:25:14Z.

Review: CHANGES_REQUESTED


The original size/count ordering issue is fixed. Changes requested only to sync the current main base and rerun the focused test as required by CONTRIBUTING.md; see my comment.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5651228695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11400#pullrequestreview-5651228695). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T11:25:14Z. **Review:** CHANGES_REQUESTED --- The original size/count ordering issue is fixed. Changes requested only to sync the current main base and rerun the focused test as required by CONTRIBUTING.md; see my comment.
MarcZX10 commented 2026-10-26 12:15:06 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T12:15:06Z.

Review: APPROVED


Reviewed exact head b999ea3c4348e40e0fa738331a250eb6fa4e4ea8. The implementation builds a max heap bottom-up, then extracts the maximum with in-place byte swaps; the count <= 1 early return now precedes size, base, and comparator checks as requested and matches FreeBSD behavior. The zero-size case sets EINVAL and leaves the input unchanged. The test covers duplicates, varied lengths, sorted keys, zero/one element no-op cases, and the zero-size error. The author reports rerunning the focused GuestQsort tests after rebasing onto 4b1a9a3b2f; intervening main changes do not touch these three files. The hosted relinker matrix, ASan, register-context and write-watch checks are green across Linux, Windows and macOS; product jobs are skipped by path policy. I found no remaining blocker.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5651753240 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11400#pullrequestreview-5651753240). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T12:15:06Z. **Review:** APPROVED --- Reviewed exact head `b999ea3c4348e40e0fa738331a250eb6fa4e4ea8`. The implementation builds a max heap bottom-up, then extracts the maximum with in-place byte swaps; the `count <= 1` early return now precedes size, base, and comparator checks as requested and matches FreeBSD behavior. The zero-size case sets `EINVAL` and leaves the input unchanged. The test covers duplicates, varied lengths, sorted keys, zero/one element no-op cases, and the zero-size error. The author reports rerunning the focused GuestQsort tests after rebasing onto `4b1a9a3b2f`; intervening `main` changes do not touch these three files. The hosted relinker matrix, ASan, register-context and write-watch checks are green across Linux, Windows and macOS; product jobs are skipped by path policy. I found no remaining blocker.
MarcZX10 commented 2026-10-26 12:15:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T12:15:29Z.

Review: APPROVED


Reviewed exact head b999ea3c4348e40e0fa738331a250eb6fa4e4ea8. The implementation builds a max heap bottom-up, then extracts the maximum with in-place byte swaps; the count <= 1 early return now precedes size, base, and comparator checks as requested and matches FreeBSD behavior. The zero-size case sets EINVAL and leaves the input unchanged. The tests cover repeated keys, varied lengths, one/zero-element no-op cases, and the zero-size error. The author reports rerunning the focused GuestQsort tests after syncing to 4b1a9a3b2f; subsequent main changes do not touch these files, and the hosted relinker matrix, ASan, register-context and write-watch checks pass across Linux, Windows and macOS. Product jobs are skipped by path policy. I found no remaining blocker.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5651757684 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11400#pullrequestreview-5651757684). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T12:15:29Z. **Review:** APPROVED --- Reviewed exact head `b999ea3c4348e40e0fa738331a250eb6fa4e4ea8`. The implementation builds a max heap bottom-up, then extracts the maximum with in-place byte swaps; the `count <= 1` early return now precedes size, base, and comparator checks as requested and matches FreeBSD behavior. The zero-size case sets `EINVAL` and leaves the input unchanged. The tests cover repeated keys, varied lengths, one/zero-element no-op cases, and the zero-size error. The author reports rerunning the focused GuestQsort tests after syncing to `4b1a9a3b2f`; subsequent `main` changes do not touch these files, and the hosted relinker matrix, ASan, register-context and write-watch checks pass across Linux, Windows and macOS. Product jobs are skipped by path policy. I found no remaining blocker.
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!11400
No description provided.