fix(libc): compute lgamma with FreeBSD msun's algorithm, as titles' libc.prx does #18787

Merged
JJenkx merged 4 commits from upstream-pr/18787 into main 2026-11-15 10:04:15 +00:00
JJenkx commented 2026-11-12 08:43:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-12T08:43:43Z.


What

lgamma returned the host's lgamma. On glibc that result differs in the last bit from the libc.prx that titles bundle, on about 40% of inputs in every range:

range of x inputs that differ, PPSA08194 vs main
x < −1e6 17096 of 40220
−1e6 ≤ x < −30 25669 of 59775
−30 ≤ x < −1 9626 of 19424
−1 ≤ x < 0 41823 of 80124
0 ≤ x < 1 21025 of 80200
1 ≤ x < 30 5612 of 19539
30 ≤ x < 1e6 22558 of 60167
x ≥ 1e6 15035 of 40551

FreeBSD 11.4's e_lgamma_r.c, compiled without contraction, matches the module bit for bit on all 400000 of these inputs. So lgamma now runs a port of it, LgammaMsun, which uses this libc's existing KernelSin, KernelCos and log, as the other msun ports here do. It is placed after KernelCos with the Sun notice, matching the file's other ports.

Two things are unchanged:

  • signgam keeps its current rules. #7544, #17026 and #8417 deal with it. The module stores 1 for -0, as #17026 says.
  • ERANGE is now set only at the poles (0 and the negative integers), as the module does. It is no longer set when a large argument overflows (lgamma(1e308)), where the host's lgamma set it.

The debt entry now names the algorithm and the fused-multiply-add caveat.

Tested

Linux 7.2.14, glibc 2.44, GCC 16.2.1, Release.

I called each title's relinked bundled libc.prx and this build's libc.prx side by side on the same 400000 inputs, spread over the ranges above.

  • PPSA08194 (built without fused multiply-adds): 0 differences.
  • PPSA34008 (fused multiply-adds): about 14% still differ in the last bit. That matches this libc's policy of following the non-fused builds.
  • Across 99 titles (100000 inputs each): 55 match exactly; the rest are fused builds.

guest_math_tests gained seven exact-bit cases taken from PPSA08194. It aborts on main and passes here. guest_math_elementary, guest_math_exact and guest_sce_v2_log_gamma pass. python3 tools/check_conventions.py --base origin/main passes. Not run on Windows; the port uses only this libc's own kernels.

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:5274306770 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18787). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-12T08:43:43Z. --- ### What `lgamma` returned the host's `lgamma`. On glibc that result differs in the last bit from the `libc.prx` that titles bundle, on about 40% of inputs in every range: | range of x | inputs that differ, PPSA08194 vs `main` | |---|---| | x < −1e6 | 17096 of 40220 | | −1e6 ≤ x < −30 | 25669 of 59775 | | −30 ≤ x < −1 | 9626 of 19424 | | −1 ≤ x < 0 | 41823 of 80124 | | 0 ≤ x < 1 | 21025 of 80200 | | 1 ≤ x < 30 | 5612 of 19539 | | 30 ≤ x < 1e6 | 22558 of 60167 | | x ≥ 1e6 | 15035 of 40551 | FreeBSD 11.4's `e_lgamma_r.c`, compiled without contraction, matches the module bit for bit on all 400000 of these inputs. So `lgamma` now runs a port of it, `LgammaMsun`, which uses this libc's existing `KernelSin`, `KernelCos` and `log`, as the other msun ports here do. It is placed after `KernelCos` with the Sun notice, matching the file's other ports. Two things are unchanged: - `signgam` keeps its current rules. #7544, #17026 and #8417 deal with it. The module stores 1 for `-0`, as #17026 says. - `ERANGE` is now set only at the poles (0 and the negative integers), as the module does. It is no longer set when a large argument overflows (`lgamma(1e308)`), where the host's `lgamma` set it. The debt entry now names the algorithm and the fused-multiply-add caveat. ### Tested Linux 7.2.14, glibc 2.44, GCC 16.2.1, Release. I called each title's relinked bundled `libc.prx` and this build's `libc.prx` side by side on the same 400000 inputs, spread over the ranges above. - **PPSA08194** (built without fused multiply-adds): 0 differences. - **PPSA34008** (fused multiply-adds): about 14% still differ in the last bit. That matches this libc's policy of following the non-fused builds. - **Across 99 titles** (100000 inputs each): 55 match exactly; the rest are fused builds. `guest_math_tests` gained seven exact-bit cases taken from PPSA08194. It aborts on `main` and passes here. `guest_math_elementary`, `guest_math_exact` and `guest_sce_v2_log_gamma` pass. `python3 tools/check_conventions.py --base origin/main` passes. Not run on Windows; the port uses only this libc's own kernels. ### 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
PrivacyOnion commented 2026-11-12 08:45:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T08:45:40Z.

Review: CHANGES_REQUESTED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 809ea49d12:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1017: guest_math .......................   Passed    0.01 sec
    2/3 Test #1018: guest_math_elementary ............   Passed    0.01 sec
    3/3 Test #1091: guest_math_exact .................Subprocess aborted***Exception:   0.17 sec
    67% tests passed, 1 tests failed out of 3
    
    The following tests FAILED:
    
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Requesting changes for the points above.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5871556949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18787#pullrequestreview-5871556949). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T08:45:40Z. **Review:** CHANGES_REQUESTED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `809ea49d12`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1017: guest_math ....................... Passed 0.01 sec 2/3 Test #1018: guest_math_elementary ............ Passed 0.01 sec 3/3 Test #1091: guest_math_exact .................Subprocess aborted***Exception: 0.17 sec 67% tests passed, 1 tests failed out of 3 ``` ``` The following tests FAILED: ``` - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Requesting changes for the points above.
PrivacyOnion commented 2026-11-12 11:07:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T11:07:09Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 8730c7fa24:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1091: guest_math_exact .................   Passed    0.00 sec
    2/3 Test #1017: guest_math .......................   Passed    0.01 sec
    3/3 Test #1018: guest_math_elementary ............   Passed    0.00 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #1091: guest_math_exact .................   Passed    0.00 sec
    2/3 Test #1018: guest_math_elementary ............   Passed    0.00 sec
    3/3 Test #1017: guest_math .......................Subprocess aborted***Exception:   0.12 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/Math.cpp (LgammaMsun): Verified polynomial and rational approximation coefficients, constants, and interval partition thresholds against FreeBSD 11.4 e_lgamma_r.c across all ranges (|x| < 2^-56, (0, 0.9], [0.9, 1.1], [1.1, 2.0], [2.0, 8.0], [8.0, 2^56], and asymptotic x >= 2^56).
    • core/libs/prx/libc/src/Math.cpp (LgammaSinPi): Verified argument reduction mod 2 using 2^52 and 2^50 bias steps, quadrant decomposition, and delegation to this libc's KernelSin and KernelCos for negative non-integer reflection.
    • core/libs/prx/libc/src/Math.cpp (lgamma_nid_postfix): Verified ERANGE is set exclusively at non-positive integer poles (0.0, -0.0, negative integers, and negative values with magnitude >= 2^52 such as -1e308) and is preserved/unset on positive overflow (1e308), infinities (+inf, -inf), and NaN.
    • core/libs/prx/libc/src/Math.cpp (lgamma_nid_postfix): Verified signgam_nid_postfix assignment logic and global symbol contract are preserved across all positive inputs, negative intervals, poles, and -0.0.
    • core/libs/tests/GuestMath.cpp: Verified test coverage exercises exact bit-level results across small, medium, and large positive and negative ranges, as well as pole and non-pole errno transitions, without leaving global state dirty.
    • docs/dev/debt/libc.md: Verified the debt entry accurately documents the FreeBSD 11.4 algorithm provenance, FMA-contracted vs non-contracted differences, signgam rules, and pole-specific ERANGE behavior.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5873312062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18787#pullrequestreview-5873312062). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T11:07:09Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `8730c7fa24`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1091: guest_math_exact ................. Passed 0.00 sec 2/3 Test #1017: guest_math ....................... Passed 0.01 sec 3/3 Test #1018: guest_math_elementary ............ Passed 0.00 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #1091: guest_math_exact ................. Passed 0.00 sec 2/3 Test #1018: guest_math_elementary ............ Passed 0.00 sec 3/3 Test #1017: guest_math .......................Subprocess aborted***Exception: 0.12 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/Math.cpp (LgammaMsun): Verified polynomial and rational approximation coefficients, constants, and interval partition thresholds against FreeBSD 11.4 e_lgamma_r.c across all ranges (|x| < 2^-56, (0, 0.9], [0.9, 1.1], [1.1, 2.0], [2.0, 8.0], [8.0, 2^56], and asymptotic x >= 2^56). - core/libs/prx/libc/src/Math.cpp (LgammaSinPi): Verified argument reduction mod 2 using 2^52 and 2^50 bias steps, quadrant decomposition, and delegation to this libc's KernelSin and KernelCos for negative non-integer reflection. - core/libs/prx/libc/src/Math.cpp (lgamma_nid_postfix): Verified ERANGE is set exclusively at non-positive integer poles (0.0, -0.0, negative integers, and negative values with magnitude >= 2^52 such as -1e308) and is preserved/unset on positive overflow (1e308), infinities (+inf, -inf), and NaN. - core/libs/prx/libc/src/Math.cpp (lgamma_nid_postfix): Verified signgam_nid_postfix assignment logic and global symbol contract are preserved across all positive inputs, negative intervals, poles, and -0.0. - core/libs/tests/GuestMath.cpp: Verified test coverage exercises exact bit-level results across small, medium, and large positive and negative ranges, as well as pole and non-pole errno transitions, without leaving global state dirty. - docs/dev/debt/libc.md: Verified the debt entry accurately documents the FreeBSD 11.4 algorithm provenance, FMA-contracted vs non-contracted differences, signgam rules, and pole-specific ERANGE behavior. - Note: the AI-assisted checkbox can stay. Looks Good To Me
PrivacyOnion commented 2026-11-15 00:37:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-15T00:37:47Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 293c9dd56f:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math, guest_math_elementary, guest_math_exact):
    1/3 Test #1065: guest_math_elementary ............   Passed    0.03 sec
    2/3 Test #1138: guest_math_exact .................   Passed    0.02 sec
    3/3 Test #1064: guest_math .......................   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 3
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/3 Test #1138: guest_math_exact .................   Passed    0.02 sec
    2/3 Test #1065: guest_math_elementary ............   Passed    0.03 sec
    3/3 Test #1064: guest_math .......................Subprocess aborted***Exception:   0.34 sec
    67% tests passed, 1 tests failed out of 3
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified pointer validation in core/libs/prx/libc/src/Math.cpp (lgamma_r_nid_postfix): confirmed signgamp == nullptr throws std::invalid_argument prior to dereference.
    • Verified errno semantics in core/libs/prx/libc/src/Math.cpp (lgamma_r_nid_postfix): confirmed ERANGE is set strictly for poles (0.0, -0.0, and finite negative integers where std::floor(x) == x) and remains unchanged on overflow (e.g. 1e308) or infinities, matching console measurements.
    • Verified signgam calculation in core/libs/prx/libc/src/Math.cpp (lgamma_r_nid_postfix): confirmed *signgamp stores -1 for negative non-integers with odd floor and 1 otherwise (including -0.0 and poles).
    • Verified msun algorithm fidelity in core/libs/prx/libc/src/Math.cpp (LgammaMsun and LgammaSinPi): confirmed argument reduction, octant selection, rational approximations, and reflection formula accurately port FreeBSD 11.4 e_lgamma_r.c using in-tree KernelSin, KernelCos, and log.
    • Verified edge-case and exceptional value handling in core/libs/prx/libc/src/Math.cpp (LgammaMsun): confirmed NaNs, positive/negative infinities, exact zeroes, and subnormal inputs (|x| < 2^-56 returning -log(|x|)) behave as specified.
    • Verified test coverage in core/libs/tests/GuestMath.cpp: confirmed inclusion of exact bit-level test cases from PPSA08194 across all 7 input intervals, pole/overflow errno checks, and denormal minimum handling.
    • Verified AGENTS.md compliance: confirmed no comments were added to code aside from the verbatim Sun Microsystems license notice, and debt documentation in docs/dev/debt/libc.md is correctly maintained.
  • Note: the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5908189059 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18787#pullrequestreview-5908189059). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-15T00:37:47Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `293c9dd56f`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_math_tests` built cleanly. - **CTest** (`guest_math`, `guest_math_elementary`, `guest_math_exact`): ``` 1/3 Test #1065: guest_math_elementary ............ Passed 0.03 sec 2/3 Test #1138: guest_math_exact ................. Passed 0.02 sec 3/3 Test #1064: guest_math ....................... Passed 0.02 sec 100% tests passed, 0 tests failed out of 3 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/3 Test #1138: guest_math_exact ................. Passed 0.02 sec 2/3 Test #1065: guest_math_elementary ............ Passed 0.03 sec 3/3 Test #1064: guest_math .......................Subprocess aborted***Exception: 0.34 sec 67% tests passed, 1 tests failed out of 3 ``` Restoring the change makes it pass again. - **Code reading**: - Verified pointer validation in core/libs/prx/libc/src/Math.cpp (lgamma_r_nid_postfix): confirmed signgamp == nullptr throws std::invalid_argument prior to dereference. - Verified errno semantics in core/libs/prx/libc/src/Math.cpp (lgamma_r_nid_postfix): confirmed ERANGE is set strictly for poles (0.0, -0.0, and finite negative integers where std::floor(x) == x) and remains unchanged on overflow (e.g. 1e308) or infinities, matching console measurements. - Verified signgam calculation in core/libs/prx/libc/src/Math.cpp (lgamma_r_nid_postfix): confirmed *signgamp stores -1 for negative non-integers with odd floor and 1 otherwise (including -0.0 and poles). - Verified msun algorithm fidelity in core/libs/prx/libc/src/Math.cpp (LgammaMsun and LgammaSinPi): confirmed argument reduction, octant selection, rational approximations, and reflection formula accurately port FreeBSD 11.4 e_lgamma_r.c using in-tree KernelSin, KernelCos, and log. - Verified edge-case and exceptional value handling in core/libs/prx/libc/src/Math.cpp (LgammaMsun): confirmed NaNs, positive/negative infinities, exact zeroes, and subnormal inputs (|x| < 2^-56 returning -log(|x|)) behave as specified. - Verified test coverage in core/libs/tests/GuestMath.cpp: confirmed inclusion of exact bit-level test cases from PPSA08194 across all 7 input intervals, pole/overflow errno checks, and denormal minimum handling. - Verified AGENTS.md compliance: confirmed no comments were added to code aside from the verbatim Sun Microsystems license notice, and debt documentation in docs/dev/debt/libc.md is correctly maintained. - Note: the AI-assisted checkbox can stay. Looks Good To Me
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!18787
No description provided.