fix(libc): sort with FreeBSD 11.4's heapsort.c in heapsort #16861

Merged
mnoori11 merged 1 commit from upstream-pr/16861 into main 2026-11-11 23:40:18 +00:00
mnoori11 commented 2026-11-07 19:10:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mnoori11 on 2026-11-07T19:10:14Z.


What

heapsort was a generic top-down heap sort. It now runs FreeBSD 11.4's heapsort.c (identical in release/11.4.0): CREATE builds the heap with COMPAR(child, child + size) and COMPAR(child, par), and SELECT walks the hole to a leaf without comparing, then sifts the displaced element, kept in a malloc'd scratch block, back up with COMPAR(k, par). The sorted result is the same for a consistent comparator, but the comparator is called in a different order, a different number of times and with a pointer outside the array. A failed malloc returns -1 as in FreeBSD. Null base or comparator and size overflow still throw; the console's heapsort is unverified, so the debt entry says so. Same approach as #12271 (mergesort); #11629 asked for CREATE and SELECT to be ported.

Tested

Linux, WSL2 Ubuntu, kernel 6.6.87.2, GCC 13.3, full build.

  • guest_qsort fails on main with the new CheckHeapsortComparisons: the 5-key case makes 12 calls in a different order, the all-equal case 9 calls (10 expected), the 16-key case 75 (64 expected), and the added allocation-failure check returns 0 where -1 is expected. It passes with this change.
  • The expected call sequences come from FreeBSD's heapsort.c at the pinned commit compiled unmodified on the host with a recording comparator, not from this implementation. A throwaway host build of Heapsort.cpp against the same file made identical comparator calls, results and bytes over 200,000 random arrays (0 to 301 elements; element sizes 8, 9, 12, 16, 24).
  • Full ctest: 4,347 of 4,406 pass. The 59 failures are 58 agc_driver_*/agc_storage_* tests and log_file, which fail the same way on main on this machine (WSL2: no GPU, so Vulkan falls back to lavapipe, and the 6.6 kernel lacks asynchronous userfaultfd write protection). tools/check_conventions.py: all checks passed.

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:5204972509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16861). Originally posted by [mnoori11](https://github.com/mnoori11) on 2026-11-07T19:10:14Z. --- ### What `heapsort` was a generic top-down heap sort. It now runs FreeBSD 11.4's [`heapsort.c`](https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/libc/stdlib/heapsort.c) (identical in `release/11.4.0`): `CREATE` builds the heap with `COMPAR(child, child + size)` and `COMPAR(child, par)`, and `SELECT` walks the hole to a leaf without comparing, then sifts the displaced element, kept in a `malloc`'d scratch block, back up with `COMPAR(k, par)`. The sorted result is the same for a consistent comparator, but the comparator is called in a different order, a different number of times and with a pointer outside the array. A failed `malloc` returns -1 as in FreeBSD. Null base or comparator and size overflow still throw; the console's `heapsort` is unverified, so the debt entry says so. Same approach as #12271 (`mergesort`); #11629 asked for `CREATE` and `SELECT` to be ported. ### 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. --> Linux, WSL2 Ubuntu, kernel 6.6.87.2, GCC 13.3, full build. - `guest_qsort` fails on `main` with the new `CheckHeapsortComparisons`: the 5-key case makes 12 calls in a different order, the all-equal case 9 calls (10 expected), the 16-key case 75 (64 expected), and the added allocation-failure check returns 0 where -1 is expected. It passes with this change. - The expected call sequences come from FreeBSD's `heapsort.c` at the pinned commit compiled unmodified on the host with a recording comparator, not from this implementation. A throwaway host build of `Heapsort.cpp` against the same file made identical comparator calls, results and bytes over 200,000 random arrays (0 to 301 elements; element sizes 8, 9, 12, 16, 24). - Full `ctest`: 4,347 of 4,406 pass. The 59 failures are 58 `agc_driver_*`/`agc_storage_*` tests and `log_file`, which fail the same way on `main` on this machine (WSL2: no GPU, so Vulkan falls back to lavapipe, and the 6.6 kernel lacks asynchronous userfaultfd write protection). `tools/check_conventions.py`: all checks passed. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> 🤖 Generated with [Claude Code](https://claude.com/claude-code)
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!16861
No description provided.