feat(libc): implement erand48, nrand48 and jrand48 #3424

Merged
reyani-dev merged 1 commit from upstream-pr/3424 into main 2026-10-11 10:21:04 +00:00
reyani-dev commented 2026-10-11 10:09:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T10:09:04Z.


What

erand48 (Fncgcl1tnXg), nrand48 (3wcYIMz8LUo) and jrand48 (M7KmRg9CERk) from libSceLibcInternal. Nothing in the tree implements the rand48 family yet.

VLC (PPSA85300) stops on erand48 on main c6ca798f, as @babyhuey reported in #1578 (Linux, catalog build with sce_module/libc.prx unwrapped as #2605 requires):

app.elf: symbol lookup error: app.elf: undefined symbol: Fncgcl1tnXg

With this branch, VLC goes on to sigtimedwait (D2-dIoJ0ZtE). VLC imports all three.

They implement the POSIX 48-bit generator X = (0x5DEECE66D * X + 0xB) mod 2^48 on the caller's unsigned short[3], which is updated in place (FreeBSD's _dorand48 arithmetic):

  • erand48 returns X / 2^48;
  • nrand48 returns bits 47..17;
  • jrand48 returns bits 47..16 as a signed 32-bit value.

A null seed throws.

guest_random_state checks:

  • six values and seeds from the srand48(0x1234ABCD) state, including a negative jrand48 (a mutant without the sign cast fails);
  • the zero seed;
  • an all-ones seed staying below 1.
  • On Linux it also compares 64 rounds of each function against the host's glibc erand48/nrand48/jrand48.

Tested

Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release, BUILD_TESTING=ON, on main 24cb0d24:

  • guest_random_state passes 5/5. Without the change it doesn't link, and the sign-cast mutant fails it.
  • ctest -E "^agc_|video_out": 442/445. The three failures fail on main too: windows_exceptions (#3411), guest_process_time_scaled under -j8 (#3414) and guest_ampr_markers. None touch the code changed here.
  • Earlier fork CI of the same commit on Linux and Windows: run 38113162752. Windows passed 765/765. Linux passed 760/761, including the glibc comparison; its one failure, agc_unit_shadow, also fails on main (#3215).
  • babyhuey ran guest_random_state 5/5 on Linux with VLC (#1578).
  • check_conventions.py passes, and pr_overlap.py reports no overlap.

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 in TechnicalDebt
  • 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
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4818278122 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3424). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T10:09:04Z. --- ### What `erand48` (`Fncgcl1tnXg`), `nrand48` (`3wcYIMz8LUo`) and `jrand48` (`M7KmRg9CERk`) from `libSceLibcInternal`. Nothing in the tree implements the rand48 family yet. VLC (PPSA85300) stops on `erand48` on `main` c6ca798f, as @babyhuey reported in #1578 (Linux, catalog build with `sce_module/libc.prx` unwrapped as #2605 requires): ``` app.elf: symbol lookup error: app.elf: undefined symbol: Fncgcl1tnXg ``` With this branch, VLC goes on to `sigtimedwait` (`D2-dIoJ0ZtE`). VLC imports all three. They implement the POSIX 48-bit generator `X = (0x5DEECE66D * X + 0xB) mod 2^48` on the caller's `unsigned short[3]`, which is updated in place (FreeBSD's `_dorand48` arithmetic): - `erand48` returns `X / 2^48`; - `nrand48` returns bits 47..17; - `jrand48` returns bits 47..16 as a signed 32-bit value. A null seed throws. `guest_random_state` checks: - six values and seeds from the `srand48(0x1234ABCD)` state, including a negative `jrand48` (a mutant without the sign cast fails); - the zero seed; - an all-ones seed staying below 1. - On Linux it also compares 64 rounds of each function against the host's glibc `erand48`/`nrand48`/`jrand48`. ### Tested Windows 11 (10.0.26200), WinLibs GCC 15.2.0 r7, Release, `BUILD_TESTING=ON`, on `main` `24cb0d24`: - `guest_random_state` passes 5/5. Without the change it doesn't link, and the sign-cast mutant fails it. - `ctest -E "^agc_|video_out"`: 442/445. The three failures fail on `main` too: `windows_exceptions` (#3411), `guest_process_time_scaled` under `-j8` (#3414) and `guest_ampr_markers`. None touch the code changed here. - Earlier fork CI of the same commit on Linux and Windows: [run 38113162752](https://github.com/AchrafReyani/AnyPS5/actions/runs/38113162752). Windows passed 765/765. Linux passed 760/761, including the glibc comparison; its one failure, `agc_unit_shadow`, also fails on `main` (#3215). - babyhuey ran `guest_random_state` 5/5 on Linux with VLC (#1578). - `check_conventions.py` passes, and `pr_overlap.py` reports no overlap. ### Checklist - [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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-11 10:09:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-11T10:09:29Z.


Progress report (3bf63fa → f85ae8d)

📈 System libraries: 85.75% (+0.01%, +3 implemented functions)

✅ 3 implemented
Library Name
libc erand48_nid_postfix
libc jrand48_nid_postfix
libc nrand48_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6107923095 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3424#issuecomment-6107923095). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-11T10:09:29Z. --- <!-- progress-report --> ### Progress report (3bf63fa → f85ae8d) 📈 **System libraries**: 85.75% (+0.01%, +3 implemented functions) <details> <summary>✅ 3 implemented</summary> | Library | Name | | - | - | | libc | `erand48_nid_postfix` | | libc | `jrand48_nid_postfix` | | libc | `nrand48_nid_postfix` | </details>
adriadam10 commented 2026-10-11 10:21:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T10:21:00Z.


Checked the diff, built and ran guest_random_state on Linux: passes, and it fails to link without the change. The arithmetic matches FreeBSD's _dorand48 and the test cross-checks glibc over 64 rounds; merging once CI is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6108019945 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3424#issuecomment-6108019945). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T10:21:00Z. --- Checked the diff, built and ran `guest_random_state` on Linux: passes, and it fails to link without the change. The arithmetic matches FreeBSD's `_dorand48` and the test cross-checks glibc over 64 rounds; merging once CI is green.
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!3424
No description provided.