feat(libc): add libc++ clock and prime helpers for converted cores #23862

Merged
mihai1859 merged 3 commits from upstream-pr/23862 into main 2026-11-19 21:35:17 +00:00
mihai1859 commented 2026-11-19 20:03:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by mihai1859 on 2026-11-19T20:03:55Z.


What

Expose three named libc++ imports used by converted RetroArch cores:

  • __next_prime finds the next prime at or above the requested size and raises the existing guest overflow exception if no 64-bit result exists.
  • steady_clock::now returns monotonic nanoseconds. On Windows it uses the registered guest kernel clock, including APS5_TIME_SCALE.
  • system_clock::now returns Unix microseconds using the same precise wall-clock source as _Xtime_get_ticks.

The three exports use scalar guest ABI values and resolve through the existing libc export lookup.

Tested

  • Windows x86-64 with MinGW. Built and patched libc and libkernel. guest_overflow_error, guest_kernel_clock and guest_time_extras passed. guest_kernel_clock also passed with APS5_TIME_SCALE=2.
  • Converted Windows callers resolved the exact named imports. The prime caller returned 29 for 24. The steady-clock caller read positive, non-decreasing nanoseconds. The system-clock caller read the current Unix time in microseconds.
  • With the separately converted Azahar module from RetroArch PPSA99169 (#5927), the unresolved import count went from 247 to 244. The three new exports account for the entire change. Startup still stops on the remaining imports.
  • Runtime validation covered Windows. Linux and macOS runtime validation is pending.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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:5402063640 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23862). Originally posted by [mihai1859](https://github.com/mihai1859) on 2026-11-19T20:03:55Z. --- ### What Expose three named libc++ imports used by converted RetroArch cores: - __next_prime finds the next prime at or above the requested size and raises the existing guest overflow exception if no 64-bit result exists. - steady_clock::now returns monotonic nanoseconds. On Windows it uses the registered guest kernel clock, including APS5_TIME_SCALE. - system_clock::now returns Unix microseconds using the same precise wall-clock source as _Xtime_get_ticks. The three exports use scalar guest ABI values and resolve through the existing libc export lookup. ### 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 with MinGW. Built and patched libc and libkernel. guest_overflow_error, guest_kernel_clock and guest_time_extras passed. guest_kernel_clock also passed with APS5_TIME_SCALE=2. - Converted Windows callers resolved the exact named imports. The prime caller returned 29 for 24. The steady-clock caller read positive, non-decreasing nanoseconds. The system-clock caller read the current Unix time in microseconds. - With the separately converted Azahar module from RetroArch PPSA99169 (#5927), the unresolved import count went from 247 to 244. The three new exports account for the entire change. Startup still stops on the remaining imports. - Runtime validation covered Windows. Linux and macOS runtime validation is pending. ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 -->
PrivacyOnion commented 2026-11-19 20:12:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-19T20:12:44Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 5dc68eefbd:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_kernel_clock_tests, guest_overflow_error_tests built cleanly.
  • CTest (guest_overflow_error, guest_kernel_clock):
    1/2 Test #1401: guest_overflow_error .............   Passed    0.01 sec
    2/2 Test #5643: guest_kernel_clock ...............   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 2
    
  • Negative control: with core/libs/prx/libc/include/Api.hpp, core/libs/prx/libc/include/GuestTime.hpp, core/libs/prx/libc/src/Time.cpp, core/libs/prx/libkernel/Time/Time.cpp, core/libs/prx/libc/src/LibcxxNextPrime.cpp reverted to main, the new tests do not build/link without the change; restoring it makes them build and pass.
  • Code reading:
    • Verified _ZNSt3__112__next_primeEm_nid_postfix correctly implements Miller-Rabin with prime bases {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37} (valid for all 64-bit integers up to 2^64-1 per Sorenson-Webster bound), handles minimum <= 2 and minimum == 0 correctly, and throws guest overflow on minimum > 18446744073709551557ull without overflow on candidate increments.
    • Verified _ZNSt3__16chrono12system_clock3nowEv_nid_postfix correctly calls ReadRealtimeMicroseconds, returning Unix epoch microseconds with proper handling of clock failure via std::terminate(), matching upstream libc++ POSIX behavior.
    • Verified _ZNSt3__16chrono12steady_clock3nowEv_nid_postfix loads the atomic monotonic time source with acquire semantics, falling back to clock_gettime(CLOCK_MONOTONIC) on Linux/macOS or std::chrono on Windows, returning signed 64-bit nanoseconds noexcept.
    • Verified LibcSetMonotonicTimeSource_nid_no_patch properly registers LibcMonotonicNanos in libkernel on Windows and is thread-safe using release semantics.
    • Verified technical debt fragments (feat-libc-libcxx-next-prime.md, feat-libc-libcxx-steady-clock.md, feat-libc-libcxx-system-clock.md) and header declarations in Api.hpp match AGENTS.md conventions, VABI calling conventions, and the lack of illegal comments.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5982552856 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/23862#pullrequestreview-5982552856). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-19T20:12:44Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `5dc68eefbd`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_kernel_clock_tests`, `guest_overflow_error_tests` built cleanly. - **CTest** (`guest_overflow_error`, `guest_kernel_clock`): ``` 1/2 Test #1401: guest_overflow_error ............. Passed 0.01 sec 2/2 Test #5643: guest_kernel_clock ............... Passed 0.01 sec 100% tests passed, 0 tests failed out of 2 ``` - **Negative control**: with `core/libs/prx/libc/include/Api.hpp`, `core/libs/prx/libc/include/GuestTime.hpp`, `core/libs/prx/libc/src/Time.cpp`, `core/libs/prx/libkernel/Time/Time.cpp`, `core/libs/prx/libc/src/LibcxxNextPrime.cpp` reverted to `main`, the new tests do not build/link without the change; restoring it makes them build and pass. - **Code reading**: - Verified _ZNSt3__112__next_primeEm_nid_postfix correctly implements Miller-Rabin with prime bases {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37} (valid for all 64-bit integers up to 2^64-1 per Sorenson-Webster bound), handles minimum <= 2 and minimum == 0 correctly, and throws guest overflow on minimum > 18446744073709551557ull without overflow on candidate increments. - Verified _ZNSt3__16chrono12system_clock3nowEv_nid_postfix correctly calls ReadRealtimeMicroseconds, returning Unix epoch microseconds with proper handling of clock failure via std::terminate(), matching upstream libc++ POSIX behavior. - Verified _ZNSt3__16chrono12steady_clock3nowEv_nid_postfix loads the atomic monotonic time source with acquire semantics, falling back to clock_gettime(CLOCK_MONOTONIC) on Linux/macOS or std::chrono on Windows, returning signed 64-bit nanoseconds noexcept. - Verified LibcSetMonotonicTimeSource_nid_no_patch properly registers LibcMonotonicNanos in libkernel on Windows and is thread-safe using release semantics. - Verified technical debt fragments (feat-libc-libcxx-next-prime.md, feat-libc-libcxx-steady-clock.md, feat-libc-libcxx-system-clock.md) and header declarations in Api.hpp match AGENTS.md conventions, VABI calling conventions, and the lack of illegal comments. Looks Good To Me
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!23862
No description provided.