feat(libc): implement tgamma, lgamma, and gamma math functions #8417

Merged
SwetabhSingh17 merged 7 commits from upstream-pr/8417 into main 2026-11-14 11:30:56 +00:00
SwetabhSingh17 commented 2026-10-20 12:29:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SwetabhSingh17 on 2026-10-20T12:29:04Z.


What

Implement tgamma, lgammaf, lgamma_r, lgammaf_r, gamma, gammaf, gamma_r, and gammaf_r in core/libs/prx/libc/src/Math.cpp, add unit test coverage in core/libs/tests/GuestMath.cpp, and update docs/dev/debt/libc.md. Built on #7544 to inherit PS5 lgamma(-0) sign and pole errno behavior in lgamma_r.

Tested

python3 tools/check_conventions.py --base upstream/main, python3 tools/pr_overlap.py --branch feat/libc-math-gamma.

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: #7544
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935722697 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8417). Originally posted by [SwetabhSingh17](https://github.com/SwetabhSingh17) on 2026-10-20T12:29:04Z. --- ### What Implement `tgamma`, `lgammaf`, `lgamma_r`, `lgammaf_r`, `gamma`, `gammaf`, `gamma_r`, and `gammaf_r` in `core/libs/prx/libc/src/Math.cpp`, add unit test coverage in `core/libs/tests/GuestMath.cpp`, and update `docs/dev/debt/libc.md`. Built on #7544 to inherit PS5 `lgamma(-0)` sign and pole `errno` behavior in `lgamma_r`. ### Tested `python3 tools/check_conventions.py --base upstream/main`, `python3 tools/pr_overlap.py --branch feat/libc-math-gamma`. ### 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: #7544 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
SwetabhSingh17 commented 2026-10-20 12:55:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by SwetabhSingh17 on 2026-10-20T12:55:35Z.


Windows Test Verification

System Specifications:

  • OS: Windows 11 Home (x86_64, Build 10.0.26300)
  • CPU: AMD Ryzen 9 7900X (12 Cores, 24 Threads)
  • RAM: 32 GB
  • GPU: NVIDIA GeForce RTX 4070 SUPER
  • Toolchain: MinGW-w64 GCC 15.2.0 (15.2.0posix-14.0.0-ucrt-r7), Ninja, C++20

Test Results:

  • python3 tools/check_conventions.py --base origin/main: passed with 0 errors.
  • ctest --test-dir build -R "^guest_math$" -V: guest_math_tests.exe passed (0.20 s) with all assertions verified ( gamma, lgamma, lgamma_r, lgammaf, lgammaf_r, gamma, gammaf, gamma_r, gammaf_r).
  • ctest --test-dir build -R "^patched_libraries$" -V: passed (352 patched libraries loaded, 0 failed).
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6291338342 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8417#issuecomment-6291338342). Originally posted by [SwetabhSingh17](https://github.com/SwetabhSingh17) on 2026-10-20T12:55:35Z. --- ### Windows Test Verification **System Specifications**: - **OS**: Windows 11 Home (x86_64, Build 10.0.26300) - **CPU**: AMD Ryzen 9 7900X (12 Cores, 24 Threads) - **RAM**: 32 GB - **GPU**: NVIDIA GeForce RTX 4070 SUPER - **Toolchain**: MinGW-w64 GCC 15.2.0 (15.2.0posix-14.0.0-ucrt-r7), Ninja, C++20 **Test Results**: - python3 tools/check_conventions.py --base origin/main: passed with 0 errors. - ctest --test-dir build -R "^guest_math$" -V: guest_math_tests.exe passed (0.20 s) with all assertions verified ( gamma, lgamma, lgamma_r, lgammaf, lgammaf_r, gamma, gammaf, gamma_r, gammaf_r). - ctest --test-dir build -R "^patched_libraries$" -V: passed (352 patched libraries loaded, 0 failed).
nikowolf123 commented 2026-10-20 20:41:00 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T20:41:00Z.


Null signgamp (Math.cpp L419): if (signgamp) lets a null pointer through. lgamma_r(0.5, NULL) returns 0.5723649429247001 and stores nothing, and lgammaf_r, gamma_r and gammaf_r forward to it. FreeBSD's e_lgamma_r.c, which the debt entry cites, writes *signgamp = 1; before anything else, and so does e_lgammaf_r.c, so the same call faults there. CONTRIBUTING asks that a function do exactly what it should or throw. This file already throws on a null out-pointer in rand_r (L691), _Dtest and _FDtest. I would store the sign unconditionally and start the function with:

if (signgamp == nullptr) throw std::invalid_argument("lgamma_r: null signgamp");

Sign checks in the new tests (GuestMath.cpp L1192): the lgammaf_r(1.f, &rSign) line already leaves rSign at 1. The rSign == 1 checks for gamma_r and gammaf_r on lines 1192-1193 therefore pass even if neither function writes it. No new line reads signgam_nid_postfix after lgammaf, gamma or gammaf. For example, if gamma_r_nid_postfix returned lgamma_nid_postfix(x), the sign would go to the global instead of the caller's int, and every test would still pass. Setting a sentinel and using an argument whose Γ is negative catches this:

rSign = 7;
Require(gamma_r_nid_postfix(-0.5, &rSign) > 0. && rSign == -1);
rSign = 7;
Require(gammaf_r_nid_postfix(-0.5f, &rSign) > 0.f && rSign == -1);
signgam_nid_postfix = 7;
Require(gammaf_nid_postfix(-0.5f) > 0.f && signgam_nid_postfix == -1);

The same signgam_nid_postfix check belongs after lgammaf_nid_postfix(-0.5f) and after gamma_nid_postfix(-0.5).

Dead FreeBSD link (libc.md L78): the s_gamma.c link returns 404, because FreeBSD has no such file on stable/11 or on main. The wrappers are in e_gamma.c (gamma_r(x, &signgam)), e_gamma_r.c (lgamma_r(x, signgamp)), e_gammaf.c and e_gammaf_r.c, and the entry could link those instead. The new entry also drops the old one's s_signgam.c link, which was the source for signgam starting at 0.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6301005214 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8417#issuecomment-6301005214). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T20:41:00Z. --- **Null `signgamp`** ([Math.cpp L419](https://github.com/boykopovar/AnyPS5/blob/aaa50b87b0caec8747b0cd462b534f74546d2a85/core/libs/prx/libc/src/Math.cpp#L419)): `if (signgamp)` lets a null pointer through. `lgamma_r(0.5, NULL)` returns 0.5723649429247001 and stores nothing, and `lgammaf_r`, `gamma_r` and `gammaf_r` forward to it. FreeBSD's [`e_lgamma_r.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_lgamma_r.c#L211), which the debt entry cites, writes `*signgamp = 1;` before anything else, and so does [`e_lgammaf_r.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_lgammaf_r.c#L132), so the same call faults there. CONTRIBUTING asks that a function do exactly what it should or throw. This file already throws on a null out-pointer in `rand_r` ([L691](https://github.com/boykopovar/AnyPS5/blob/aaa50b87b0caec8747b0cd462b534f74546d2a85/core/libs/prx/libc/src/Math.cpp#L691)), `_Dtest` and `_FDtest`. I would store the sign unconditionally and start the function with: ```cpp if (signgamp == nullptr) throw std::invalid_argument("lgamma_r: null signgamp"); ``` **Sign checks in the new tests** ([GuestMath.cpp L1192](https://github.com/boykopovar/AnyPS5/blob/aaa50b87b0caec8747b0cd462b534f74546d2a85/core/libs/tests/GuestMath.cpp#L1192)): the `lgammaf_r(1.f, &rSign)` line already leaves `rSign` at 1. The `rSign == 1` checks for `gamma_r` and `gammaf_r` on lines 1192-1193 therefore pass even if neither function writes it. No new line reads `signgam_nid_postfix` after `lgammaf`, `gamma` or `gammaf`. For example, if `gamma_r_nid_postfix` returned `lgamma_nid_postfix(x)`, the sign would go to the global instead of the caller's int, and every test would still pass. Setting a sentinel and using an argument whose Γ is negative catches this: ```cpp rSign = 7; Require(gamma_r_nid_postfix(-0.5, &rSign) > 0. && rSign == -1); rSign = 7; Require(gammaf_r_nid_postfix(-0.5f, &rSign) > 0.f && rSign == -1); signgam_nid_postfix = 7; Require(gammaf_nid_postfix(-0.5f) > 0.f && signgam_nid_postfix == -1); ``` The same `signgam_nid_postfix` check belongs after `lgammaf_nid_postfix(-0.5f)` and after `gamma_nid_postfix(-0.5)`. **Dead FreeBSD link** ([libc.md L78](https://github.com/boykopovar/AnyPS5/blob/aaa50b87b0caec8747b0cd462b534f74546d2a85/docs/dev/debt/libc.md#L78)): the [`s_gamma.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/s_gamma.c) link returns 404, because FreeBSD has no such file on stable/11 or on main. The wrappers are in [`e_gamma.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_gamma.c) (`gamma_r(x, &signgam)`), [`e_gamma_r.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_gamma_r.c) (`lgamma_r(x, signgamp)`), [`e_gammaf.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_gammaf.c) and [`e_gammaf_r.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/e_gammaf_r.c), and the entry could link those instead. The new entry also drops the old one's [`s_signgam.c`](https://github.com/freebsd/freebsd-src/blob/stable/11/lib/msun/src/s_signgam.c) link, which was the source for `signgam` starting at 0.
oneandonlydean commented 2026-10-20 22:59:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T22:59:59Z.


@SwetabhSingh17 Agreed with nikowolf123's three points. To keep them in one place:

  • lgamma_r (Math.cpp:419) should not skip a null signgamp: FreeBSD's e_lgamma_r.c stores the sign first, so throw std::invalid_argument there (as rand_r and _Dtest in this file do) and store unconditionally.
  • The sign checks for gamma_r, gammaf_r and the signgam writers can't fail as written; please set a sentinel first and use -0.5 (where Γ is negative), as in nikowolf123's snippet.
  • The debt entry's s_gamma.c link is a 404 on stable/11; e_gamma.c, e_gamma_r.c, e_gammaf.c and e_gammaf_r.c are the wrappers, and the s_signgam.c link from the old entry is worth keeping.

After that, the bar is a full CI run with guest_math passing on Linux and Windows. These tests haven't been run yet, so that will be their first run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303687741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8417#issuecomment-6303687741). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T22:59:59Z. --- @SwetabhSingh17 Agreed with nikowolf123's three points. To keep them in one place: - `lgamma_r` (Math.cpp:419) should not skip a null `signgamp`: FreeBSD's `e_lgamma_r.c` stores the sign first, so throw `std::invalid_argument` there (as `rand_r` and `_Dtest` in this file do) and store unconditionally. - The sign checks for `gamma_r`, `gammaf_r` and the `signgam` writers can't fail as written; please set a sentinel first and use -0.5 (where Γ is negative), as in nikowolf123's snippet. - The debt entry's `s_gamma.c` link is a 404 on stable/11; `e_gamma.c`, `e_gamma_r.c`, `e_gammaf.c` and `e_gammaf_r.c` are the wrappers, and the `s_signgam.c` link from the old entry is worth keeping. After that, the bar is a full CI run with `guest_math` passing on Linux and Windows. These tests haven't been run yet, so that will be their first run.
adriadam10 commented 2026-10-21 11:27:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-21T11:27:07Z.

Review: DISMISSED


Thanks. Agreeing with the points already raised by @nikowolf123 and @oneandonlydean, plus an overlap that needs sorting out first:

  • Overlap: #6158 (older) already adds lgammaf and tgamma, and #7544 rewrites lgamma's signgam and pole errno behaviour (measured on a PS5) in the same lines of Math.cpp this PR replaces. Please rebase on whichever of them merges first, drop the exports it already provides, and list the others under Depends on.
  • core/libs/prx/libc/src/Math.cpp:472: if (signgamp) lets a null pointer through, while FreeBSD's e_lgamma_r.c / e_lgammaf_r.c store the sign first. Throw std::invalid_argument on null (as rand_r does in this file) and store unconditionally.
  • core/libs/tests/GuestMath.cpp:1409-1415: the rSign == 1 checks for gamma_r and gammaf_r pass even if nothing writes it (rSign is already 1), and nothing reads signgam_nid_postfix after lgammaf, gamma or gammaf. Set a sentinel (7) before each call and use -0.5, where Γ is negative, expecting -1, for gamma_r, gammaf_r, and via signgam_nid_postfix for lgammaf, gamma, gammaf.
  • docs/dev/debt/libc.md:83: the s_gamma.c link is a 404; link e_gamma.c, e_gamma_r.c, e_gammaf.c, e_gammaf_r.c instead, and keep the s_signgam.c link the old entry had.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5598945843 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8417#pullrequestreview-5598945843). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-21T11:27:07Z. **Review:** DISMISSED --- Thanks. Agreeing with the points already raised by @nikowolf123 and @oneandonlydean, plus an overlap that needs sorting out first: - Overlap: #6158 (older) already adds `lgammaf` and `tgamma`, and #7544 rewrites `lgamma`'s `signgam` and pole `errno` behaviour (measured on a PS5) in the same lines of `Math.cpp` this PR replaces. Please rebase on whichever of them merges first, drop the exports it already provides, and list the others under Depends on. - `core/libs/prx/libc/src/Math.cpp:472`: `if (signgamp)` lets a null pointer through, while FreeBSD's `e_lgamma_r.c` / `e_lgammaf_r.c` store the sign first. Throw `std::invalid_argument` on null (as `rand_r` does in this file) and store unconditionally. - `core/libs/tests/GuestMath.cpp:1409-1415`: the `rSign == 1` checks for `gamma_r` and `gammaf_r` pass even if nothing writes it (`rSign` is already 1), and nothing reads `signgam_nid_postfix` after `lgammaf`, `gamma` or `gammaf`. Set a sentinel (7) before each call and use -0.5, where Γ is negative, expecting -1, for `gamma_r`, `gammaf_r`, and via `signgam_nid_postfix` for `lgammaf`, `gamma`, `gammaf`. - `docs/dev/debt/libc.md:83`: the `s_gamma.c` link is a 404; link `e_gamma.c`, `e_gamma_r.c`, `e_gammaf.c`, `e_gammaf_r.c` instead, and keep the `s_signgam.c` link the old entry had.
adriadam10 commented 2026-10-26 12:35:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T12:35:55Z.

Review: DISMISSED


Thanks, the null signgamp, sentinel tests and e_gamma*.c links are all fixed. One point left, the overlap:

  • #6158 was closed without merging, but #7544 (open) rewrites lgamma in the same lines of Math.cpp with behaviour measured on a PS5: lgamma(-0) stores signgam = 1 (this PR keeps FreeBSD 11's -1 in lgamma_r_nid_postfix, core/libs/prx/libc/src/Math.cpp:727) and a pole (0, -0, negative integers) sets errno to ERANGE (not set here). Since lgamma, lgammaf, gamma* and *_r all go through lgamma_r_nid_postfix, they would inherit the wrong -0 sign and no pole errno. Please rebase onto #7544 once it merges (or build on its branch), keep its sign/errno rules inside lgamma_r_nid_postfix, adapt the lgamma_r(-0) expectations in GuestMath.cpp, and list #7544 under Depends on.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5652016383 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8417#pullrequestreview-5652016383). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-26T12:35:55Z. **Review:** DISMISSED --- Thanks, the null `signgamp`, sentinel tests and `e_gamma*.c` links are all fixed. One point left, the overlap: - #6158 was closed without merging, but #7544 (open) rewrites `lgamma` in the same lines of `Math.cpp` with behaviour measured on a PS5: `lgamma(-0)` stores `signgam = 1` (this PR keeps FreeBSD 11's -1 in `lgamma_r_nid_postfix`, `core/libs/prx/libc/src/Math.cpp:727`) and a pole (0, -0, negative integers) sets `errno` to `ERANGE` (not set here). Since `lgamma`, `lgammaf`, `gamma*` and `*_r` all go through `lgamma_r_nid_postfix`, they would inherit the wrong -0 sign and no pole `errno`. Please rebase onto #7544 once it merges (or build on its branch), keep its sign/`errno` rules inside `lgamma_r_nid_postfix`, adapt the `lgamma_r(-0)` expectations in `GuestMath.cpp`, and list #7544 under Depends on.
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!8417
No description provided.