fix(libc): lgamma stores 1 in signgam for -0 #17026

Closed
xXPBSXx wants to merge 4 commits from upstream-pr/17026 into main
xXPBSXx commented 2026-11-08 08:27:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-11-08T08:27:29Z.


What

lgamma stored -1 in signgam for an argument of -0, following FreeBSD's e_lgamma_r.c. On a PS5 lgamma(-0.0) stores 1, as lgamma(+0.0) does. This stores 1 for both zeros. The result, errno and the exception flags still come from the host's lgamma and are not changed. A test pins the sign for -0, and the debt entry for lgamma and signgam in docs/dev/debt/libc.md states the measurement. A title process was not measured.

Tested

Measured on a PS5 with firmware 11.60 (kernel_get_fw_version 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-08 at 03:58 UTC. A title process may differ. signgam was set to 7 before each call; the cases are 2.14 and 2.15 of the raw output in the first comment. The other 25 value cases of the run (positive values, negative non-integers, the poles -1 and -2, tiny and huge values, both infinities and NaN) store the same sign as main does; only -0 differs.

Call PS5, payload process, firmware 11.60 AnyPS5 main before here
lgamma(0.0), then signgam 1 (result +infinity, errno 34) 1 1
lgamma(-0.0), then signgam 1 (result +infinity, errno 34) -1 1

On this branch, Linux (WSL, Release) on main 62e7ca86763c: with only the test commit guest_math fails in 3 of 3 runs (GuestMath.cpp:3677), and with the fix it passes in 3 of 3 runs. tools/check_conventions.py --base upstream/main passes. The probe source and its raw output are in the first comment. Not tested: Windows, macOS, the CI runner, a title process and any other firmware.

Overlap (P2, 2026-11-08, list of open pull requests from 07:40 UTC, main 4b37092bef, checked 07:40-07:46 UTC, head 78dc9f8bf733): of the 119 open pull requests that change core/libs/prx/libc/src/Math.cpp, core/libs/tests/GuestMath.cpp or docs/dev/debt/libc.md, each was merged with and without this branch. Two conflict only with this branch: #8417 (implement tgamma, lgamma, and gamma math functions), which rewrites the same lgamma function in Math.cpp (as lgamma_r, built on #7544) and keeps the sign -1 for -0 that FreeBSD's source gives and this measurement did not find, so whichever lands second must take the measured sign; and #16613 (ours, a documentation change in docs/dev/debt/libc.md). 40 conflict with main without this branch as well; the other 77 merge cleanly with and without it.

Checklist

  • Based on current main; no other open PR implements the same functions (based on current main, but #8417 also rewrites lgamma; see Overlap)
  • 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 (a host-independent math check only)
  • AI-assisted: yes

AI-assisted (Claude Code).

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5209864894 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17026). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-11-08T08:27:29Z. --- ### What `lgamma` stored -1 in `signgam` for an argument of -0, following FreeBSD's `e_lgamma_r.c`. On a PS5 `lgamma(-0.0)` stores 1, as `lgamma(+0.0)` does. This stores 1 for both zeros. The result, `errno` and the exception flags still come from the host's `lgamma` and are not changed. A test pins the sign for -0, and the debt entry for `lgamma` and `signgam` in `docs/dev/debt/libc.md` states the measurement. A title process was not measured. ### Tested Measured on a PS5 with firmware 11.60 (`kernel_get_fw_version` 0x11600005), from a homebrew payload process (ps5-payload-sdk v0.45, elfldr), on 2026-11-08 at 03:58 UTC. A title process may differ. `signgam` was set to 7 before each call; the cases are 2.14 and 2.15 of the raw output in the first comment. The other 25 value cases of the run (positive values, negative non-integers, the poles -1 and -2, tiny and huge values, both infinities and NaN) store the same sign as `main` does; only -0 differs. | Call | PS5, payload process, firmware 11.60 | AnyPS5 `main` before | here | |---|---|---|---| | `lgamma(0.0)`, then `signgam` | 1 (result +infinity, `errno` 34) | 1 | 1 | | `lgamma(-0.0)`, then `signgam` | 1 (result +infinity, `errno` 34) | -1 | 1 | On this branch, Linux (WSL, Release) on `main` `62e7ca86763c`: with only the test commit `guest_math` fails in 3 of 3 runs (`GuestMath.cpp:3677`), and with the fix it passes in 3 of 3 runs. `tools/check_conventions.py --base upstream/main` passes. The probe source and its raw output are in the first comment. Not tested: Windows, macOS, the CI runner, a title process and any other firmware. Overlap (P2, 2026-11-08, list of open pull requests from 07:40 UTC, `main` `4b37092bef`, checked 07:40-07:46 UTC, head `78dc9f8bf733`): of the 119 open pull requests that change `core/libs/prx/libc/src/Math.cpp`, `core/libs/tests/GuestMath.cpp` or `docs/dev/debt/libc.md`, each was merged with and without this branch. Two conflict only with this branch: #8417 (`implement tgamma, lgamma, and gamma math functions`), which rewrites the same `lgamma` function in `Math.cpp` (as `lgamma_r`, built on #7544) and keeps the sign -1 for -0 that FreeBSD's source gives and this measurement did not find, so whichever lands second must take the measured sign; and #16613 (ours, a documentation change in `docs/dev/debt/libc.md`). 40 conflict with `main` without this branch as well; the other 77 merge cleanly with and without it. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [ ] Based on current `main`; no other open PR implements the same functions (based on current `main`, but #8417 also rewrites `lgamma`; see Overlap) - [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 <!-- #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 (a host-independent math check only) <!-- 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 --> AI-assisted (Claude Code). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-08 16:42:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-08T16:42:12Z.

Review: COMMENTED


Windows run of head 78dc9f8bf7, merged onto main 828e44310e. To save builds, this ran in one local tree together with 13 other open PRs that touch different files. All 14 merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (x86_64-ucrt-posix-seh), Release, BUILD_TESTING=ON.

  • guest_math with --repeat until-fail:10: 10 / 10 pass.
  • Negative control, with main's libc/src/Math.cpp and this PR's test: 5 / 5 runs fail. With the PR's file put back, it passes again.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5818701361 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17026#pullrequestreview-5818701361). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-08T16:42:12Z. **Review:** COMMENTED --- Windows run of head `78dc9f8bf7`, merged onto `main` `828e44310e`. To save builds, this ran in one local tree together with 13 other open PRs that touch different files. All 14 merged cleanly. Setup: Windows 11, WinLibs GCC 15.2 (`x86_64-ucrt-posix-seh`), Release, `BUILD_TESTING=ON`. - `guest_math` with `--repeat until-fail:10`: 10 / 10 pass. - **Negative control,** with `main`'s `libc/src/Math.cpp` and this PR's test: 5 / 5 runs fail. With the PR's file put back, it passes again.
adriadam10 commented 2026-11-12 04:39:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-11-12T04:39:48Z.

Review: CHANGES_REQUESTED


Thanks for the PS5 measurement. Two things block this as it stands:

  • conventions fails: the checklist line Based on current main; no other open PR implements the same functions is unchecked. The change to the signgam line for -0 in lgamma_nid_postfix (core/libs/prx/libc/src/Math.cpp:1897) and the matching GuestMath.cpp:4019 pin are the same one #7544 already makes (measured on firmware 12.70), and #7544 also covers the errno at poles. Please list #7544 under Depends on and tick the line, or drop the code and test change.
  • Since both PRs rewrite the same lgamma debt entry (docs/dev/debt/libc.md:218), the useful part left here is your firmware 11.60 measurement: once #7544 lands, rebase on it and keep only the debt text that adds the 11.60 result (and the 25 other value cases), so the two measurements sit in one entry.

The tests themselves look right: the -0 pin fails on main and passes with the fix, as the Windows runs by @JJenkx and @gabichuuuuu show.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869220949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/17026#pullrequestreview-5869220949). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-11-12T04:39:48Z. **Review:** CHANGES_REQUESTED --- Thanks for the PS5 measurement. Two things block this as it stands: - `conventions` fails: the checklist line `Based on current main; no other open PR implements the same functions` is unchecked. The change to the `signgam` line for `-0` in `lgamma_nid_postfix` (`core/libs/prx/libc/src/Math.cpp:1897`) and the matching `GuestMath.cpp:4019` pin are the same one #7544 already makes (measured on firmware 12.70), and #7544 also covers the `errno` at poles. Please list #7544 under **Depends on** and tick the line, or drop the code and test change. - Since both PRs rewrite the same `lgamma` debt entry (`docs/dev/debt/libc.md:218`), the useful part left here is your firmware 11.60 measurement: once #7544 lands, rebase on it and keep only the debt text that adds the 11.60 result (and the 25 other value cases), so the two measurements sit in one entry. The tests themselves look right: the `-0` pin fails on `main` and passes with the fix, as the Windows runs by @JJenkx and @gabichuuuuu show.

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