fix(libc): store the gamma sign in an exported signgam #5495

Closed
Roojool wants to merge 1 commit from upstream-pr/5495 into main
Roojool commented 2026-10-13 21:06:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Roojool on 2026-10-13T21:06:58Z.


What

lgamma now fills the guest signgam, which libc also exports.

FreeBSD 11's e_lgamma_r.c derives the sign stored into signgam from the argument alone, and the Windows CRT has no lgamma_r, so that rule is evaluated here: +1 at a pole, at the infinities and for NaN, -1 for a negative zero and any tiny negative argument, and otherwise, for x < 0, the sign of sin(pi*x), which is negative exactly where ceil(x) is even. The magnitude still comes from the host's lgamma (last-bit differences from the console's msun code are possible). lgammaf and lgammal are not exported in this tree, so no other call fills signgam.

The export follows the library's own convention — every external global is exported and renamed to its NID, here 5gOOC0kzW0c. The libc.prx bundled with PPSA59322 and PPSA76677 exports that NID as a 4-byte STB_GLOBAL/STT_OBJECT, so the console does too. No title available here imports it today; this fixes the libc contract, and the debt entry keeps the caveats: signgam is a plain global, so concurrent lgamma calls and a guest reading it race exactly as they do on FreeBSD, and console behaviour of the whole pair is unverified.

Tested

  • guest_math gains signgam assertions around lgamma calls with both signs (-0.5 → -1, 1.5 → +1), poles and non-finite inputs. On unmodified main the extended test fails to link (undefined reference to 'signgam_nid_postfix'); with the change it passes (Windows 4 runs including reruns; WSL Ubuntu 3/3).
  • Export verified in the built artifacts: the patched libc.prx export table contains 5gOOC0kzW0c on Windows (objdump) and the ELF shows a 4-byte OBJECT GLOBAL on Linux (readelf); the helper that computes the sign stays internal.
  • Neighbouring guest_math cases and the nid_* patcher tests (6/6) pass; the full libs target with the real NID patcher builds on both platforms.
  • python tools/check_conventions.py --base upstream/main passes.

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; silent stubs are listed in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • No new third-party code
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4849482643 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5495). Originally posted by [Roojool](https://github.com/Roojool) on 2026-10-13T21:06:58Z. --- ### What `lgamma` now fills the guest `signgam`, which libc also exports. FreeBSD 11's [`e_lgamma_r.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_lgamma_r.c) derives the sign stored into `signgam` from the argument alone, and the Windows CRT has no `lgamma_r`, so that rule is evaluated here: `+1` at a pole, at the infinities and for NaN, `-1` for a negative zero and any tiny negative argument, and otherwise, for `x < 0`, the sign of `sin(pi*x)`, which is negative exactly where `ceil(x)` is even. The magnitude still comes from the host's `lgamma` (last-bit differences from the console's msun code are possible). `lgammaf` and `lgammal` are not exported in this tree, so no other call fills `signgam`. The export follows the library's own convention — every external global is exported and renamed to its NID, here `5gOOC0kzW0c`. The `libc.prx` bundled with PPSA59322 and PPSA76677 exports that NID as a 4-byte `STB_GLOBAL`/`STT_OBJECT`, so the console does too. No title available here imports it today; this fixes the libc contract, and the debt entry keeps the caveats: `signgam` is a plain global, so concurrent `lgamma` calls and a guest reading it race exactly as they do on FreeBSD, and console behaviour of the whole pair is unverified. ### Tested - `guest_math` gains `signgam` assertions around `lgamma` calls with both signs (`-0.5` → `-1`, `1.5` → `+1`), poles and non-finite inputs. On unmodified `main` the extended test fails to link (`undefined reference to 'signgam_nid_postfix'`); with the change it passes (Windows 4 runs including reruns; WSL Ubuntu 3/3). - Export verified in the built artifacts: the patched `libc.prx` export table contains `5gOOC0kzW0c` on Windows (`objdump`) and the ELF shows a 4-byte `OBJECT GLOBAL` on Linux (`readelf`); the helper that computes the sign stays internal. - Neighbouring `guest_math` cases and the `nid_*` patcher tests (6/6) pass; the full `libs` target with the real NID patcher builds on both platforms. - `python tools/check_conventions.py --base upstream/main` passes. ### 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 - [x] Unimplemented paths throw; silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] No new third-party code - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-10-13 21:07:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-13T21:07:07Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5509964625 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5495#pullrequestreview-5509964625). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-13T21:07:07Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
oneandonlydean commented 2026-10-14 04:42:26 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-14T04:42:26Z.


Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6161061066 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5495#issuecomment-6161061066). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-14T04:42:26Z. --- Closing: no title is known to reach this. With the queue at 480+ open PRs, please hold off on new PRs that don't name a title needing the change until it's down. If a title stops on this on current main, reopen with the title ID and the log line.

Pull request closed

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!5495
No description provided.