feat(libc): answer the remaining gamma and lgamma entry points #16438

Closed
Opensource55 wants to merge 1 commit from upstream-pr/16438 into main
Opensource55 commented 2026-11-06 17:46:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Opensource55 on 2026-11-06T17:46:03Z.


What

The libc bundled with PPSA01341 v01.007.000 exports eight names this tree did not:

export NID address
lgamma_r EjL+gY1G2lk 0x496b0
lgammaf i-ifjh3SLBU 0x49d40
lgammaf_r RlGUiqyKf9I 0x49d60
tgamma b7J3q7-UABY 0x50560
gamma BD-xV2fLe2M 0x51880
gamma_r q+AdV-EHiKc 0x518a0
gammaf sZ93QMbGRJY 0x518b0
gammaf_r E3RYvWbYLgk 0x518d0

Each NID is the hash its name carries under tools/nid_names.py.

Seven of the eight are the same two routines, and the module says so itself:

49690 (lgamma):   lea signgam,%rdi ; call 0x496b0        ; lgamma_r
51880 (gamma):    lea signgam,%rdi ; call 0x518a0        ; gamma_r
518b0 (gammaf):   lea signgam,%rdi ; call 0x518d0        ; gammaf_r
518a0 (gamma_r):  call 0x496b0                           ; lgamma_r
518d0 (gammaf_r): call 0x49d60                           ; lgammaf_r

So lgamma is rewritten to forward through lgamma_r — which is all it ever was — and the six names above become forwards of those two. signgam is the one this tree already exports.

lgammaf_r takes the same shape as the double routine one size down, including the shortcut the module takes at 0x49d9c: at or below 0x35000000 the answer is -log|x| rather than a series. The magnitude is computed in double and narrowed, exactly as tgammaf and lgammal in this tree already do.

tgamma is the one real implementation — the module carries msun's e_gamma.c. This tree takes it from the host, as tgammaf already does, keeping the three things the module's tail does differently:

  • a pole answers a negative quiet NaN (0xfff8000000000000) rather than whatever payload the host produces;
  • a negative integer sets EDOM;
  • an underflow keeps the sign of the gamma at that point rather than the sign of the zero the host returns.

One deliberate difference from the module

lgammaf_r stores 1 into the sign at entry (49d71: movl $0x1,(%rdi)) and returns for a zero or a non-finite argument before touching it again, so the module answers 1 for -0.0. This tree's lgamma has always answered -1 there, and an existing check in guest_math pins that:

Require(gammaSign(0.) == 1 && gammaSign(-0.) == -1);

The new names follow the tree rather than answering a third way, so all six agree with each other. If a maintainer would rather match the module, the change is in LgammaSign plus that one existing assertion.

Tested

Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), head 351f177d5b on top of main 5779a4ea51; no title was run.

guest_gamma is new (core/libs/tests/GuestGamma.cpp) and covers:

  • lgamma_r at 1, 2, 0.5, 10, -0.5 and 0, and lgammaf_r at the same shapes plus a tiny 1e-30f that has to take the -log shortcut;

  • the sign reported through the pointer, for both widths: 0.5, 10, ±0, -0.5, -1.5, -2.5, the integers, and both infinities and a NaN;

  • each alias agreeing bit for bit with the name it stands for, over ten double and seven float arguments, and reporting the same sign;

  • signgam being written by gamma, gammaf and lgammaf;

  • tgamma's exact small integers, 0.5, -0.5, and both zeroes answering signed infinities;

  • tgamma's poles answering 0xfff8000000000000 with EDOM, and left-alone errno elsewhere.

  • Red side, with tgamma handing straight to the host: guest_gamma aborts on the pole payload at line 110.

  • Red side, with gamma dropping the sign it forwards: guest_gamma aborts at line 86.

  • Green side: guest_gamma passes in 0.01 s, and guest_math_elementary and guest_math_exact — which carry the pre-existing lgamma and signgam checks the rewrite has to keep — still pass.

  • tools/check_conventions.py reports All conventions checks passed; objdump -p libc.prx lists all eight entries.

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:5192158678 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16438). Originally posted by [Opensource55](https://github.com/Opensource55) on 2026-11-06T17:46:03Z. --- ### What The `libc` bundled with PPSA01341 v01.007.000 exports eight names this tree did not: | export | NID | address | |---|---|---| | `lgamma_r` | `EjL+gY1G2lk` | 0x496b0 | | `lgammaf` | `i-ifjh3SLBU` | 0x49d40 | | `lgammaf_r` | `RlGUiqyKf9I` | 0x49d60 | | `tgamma` | `b7J3q7-UABY` | 0x50560 | | `gamma` | `BD-xV2fLe2M` | 0x51880 | | `gamma_r` | `q+AdV-EHiKc` | 0x518a0 | | `gammaf` | `sZ93QMbGRJY` | 0x518b0 | | `gammaf_r` | `E3RYvWbYLgk` | 0x518d0 | Each NID is the hash its name carries under `tools/nid_names.py`. Seven of the eight are the same two routines, and the module says so itself: ``` 49690 (lgamma): lea signgam,%rdi ; call 0x496b0 ; lgamma_r 51880 (gamma): lea signgam,%rdi ; call 0x518a0 ; gamma_r 518b0 (gammaf): lea signgam,%rdi ; call 0x518d0 ; gammaf_r 518a0 (gamma_r): call 0x496b0 ; lgamma_r 518d0 (gammaf_r): call 0x49d60 ; lgammaf_r ``` So `lgamma` is rewritten to forward through `lgamma_r` — which is all it ever was — and the six names above become forwards of those two. `signgam` is the one this tree already exports. `lgammaf_r` takes the same shape as the double routine one size down, including the shortcut the module takes at `0x49d9c`: at or below `0x35000000` the answer is `-log|x|` rather than a series. The magnitude is computed in double and narrowed, exactly as `tgammaf` and `lgammal` in this tree already do. `tgamma` is the one real implementation — the module carries msun's `e_gamma.c`. This tree takes it from the host, as `tgammaf` already does, keeping the three things the module's tail does differently: - a pole answers a **negative quiet NaN** (`0xfff8000000000000`) rather than whatever payload the host produces; - a negative integer sets `EDOM`; - an underflow keeps the sign of the gamma at that point rather than the sign of the zero the host returns. #### One deliberate difference from the module `lgammaf_r` stores `1` into the sign at entry (`49d71: movl $0x1,(%rdi)`) and returns for a zero or a non-finite argument before touching it again, so the module answers `1` for `-0.0`. This tree's `lgamma` has always answered `-1` there, and an existing check in `guest_math` pins that: ```cpp Require(gammaSign(0.) == 1 && gammaSign(-0.) == -1); ``` The new names follow the tree rather than answering a third way, so all six agree with each other. If a maintainer would rather match the module, the change is in `LgammaSign` plus that one existing assertion. ### Tested Windows 10 22H2 (19045.6456), MinGW-W64 GCC 15.2.0 (ucrt, Release build), head `351f177d5b` on top of `main` `5779a4ea51`; no title was run. `guest_gamma` is new (`core/libs/tests/GuestGamma.cpp`) and covers: - `lgamma_r` at `1`, `2`, `0.5`, `10`, `-0.5` and `0`, and `lgammaf_r` at the same shapes plus a tiny `1e-30f` that has to take the `-log` shortcut; - the sign reported through the pointer, for both widths: `0.5`, `10`, `±0`, `-0.5`, `-1.5`, `-2.5`, the integers, and both infinities and a NaN; - each alias agreeing bit for bit with the name it stands for, over ten double and seven float arguments, and reporting the same sign; - `signgam` being written by `gamma`, `gammaf` and `lgammaf`; - `tgamma`'s exact small integers, `0.5`, `-0.5`, and both zeroes answering signed infinities; - `tgamma`'s poles answering `0xfff8000000000000` with `EDOM`, and left-alone `errno` elsewhere. - Red side, with `tgamma` handing straight to the host: `guest_gamma` aborts on the pole payload at line 110. - Red side, with `gamma` dropping the sign it forwards: `guest_gamma` aborts at line 86. - Green side: `guest_gamma` passes in 0.01 s, and `guest_math_elementary` and `guest_math_exact` — which carry the pre-existing `lgamma` and `signgam` checks the rewrite has to keep — still pass. - `tools/check_conventions.py` reports `All conventions checks passed`; `objdump -p libc.prx` lists all eight entries. ### 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

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