feat(libc): implement lgammaf, tgamma, and tgammaf #6158
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6158"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by freshstart2066-create on 2026-10-14T23:50:03Z.
What
Guest game engines, procedural generation systems, and audio synthesis runtimes require the true gamma function (
tgamma,tgammaf) and single-precision log-gamma (lgammaf). While double-precisionlgammawas previously supported, missingtgamma,tgammaf, andlgammafexports cause dynamic linker resolution failures at guest boot.This PR implements:
lgammaf(lgammaf_nid_postfix)tgamma(tgamma_nid_postfix)tgammaf(tgammaf_nid_postfix)Implementation Details
std::lgammaandstd::tgammaadhering to C99 standards and factorial/pole behaviors for integers and negative values.core/libs/tests/GuestMath.cppcovering standard factorial values (e.g.tgamma(4) == 6), half-integers (tgamma(0.5) == sqrt(pi)), negative integer pole NaNs, and single/double precision log-gamma.Tested
core/libs/tests/GuestMath.cpp(guest_math).upstream/mainwith 0 conflicts (tree:290489a8ffbf8ea7f85a891a13b1e526931fcac7).python tools/check_conventions.py --base 584cc4c57:All conventions checks passed.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)Co-authored-by: Nawasfadili [email protected]
Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-14T23:50:39Z.
Progress report (
ab9a476→fdbe668)📈 System libraries: 88.72% (+0.01%, +3 implemented functions)
✅ 3 implemented
lgammaf_nid_postfixtgamma_nid_postfixtgammaf_nid_postfixImported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-15T20:05:35Z.
On Windows,
lgammaf_nid_postfixgives wrong results for small negative arguments, because it returns the host'sstd::lgamma(float). I checked at headfdbe66845with a standalone program that callsstd::lgamma(float)from a non-inlined function, the way this PR does, with the inputs passed throughvolatileso GCC can't fold them at compile time. I compared WinLibs GCC 15.2.0 (MinGW-w64, Windows 11) against glibc 2.39 (Ubuntu 24.04):x-1e-20f-2.63e-23f-2.58e-23finf-1e-24finf-1e-30finf-1e-40finf-1e-21down, the Windows result drifts from the double-precision reference. The relative error reaches 1.3% near-2.67e-23.-2.6e-23down, the result isinf.-0.5fare correct.This is the float counterpart of #5775 (merged), which fixed the same MinGW behaviour for
lgammaby returning-log|x|below a threshold, following FreeBSD'se_lgamma_r.c; FreeBSD'se_lgammaf_r.chas the same tiny-argument branch. A check such asstd::abs(lgammaf_nid_postfix(-1e-30f) - 69.07755f) < 1e-4fwould catch it in the Windows build. Linux passes either way, because glibc is correct here.Also, the
lgamma_nid_postfixcontext line in this branch predates #5775, so it will conflict with currentmainthere.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-18T16:41:48Z.
Review: CHANGES_REQUESTED
Thanks for the PR. Changes needed:
core/libs/prx/libc/src/Math.cpp:251lgammaf_nid_postfixreturns the hoststd::lgamma(float), which is wrong on MinGW for tiny negative arguments (-2.6e-23fgivesinf, see @wachi35676's measurements above).lgamma_nid_postfixonmainalready handles this (#5775:-log|x|below a threshold, following FreeBSDe_lgamma_r.c; FreeBSD'se_lgammaf_r.chas the same branch). Please do the same for the float version.lgamma_nid_postfixonmainalso setssigngam_nid_postfix;lgammafshould set it the same way (FreeBSDlgammaf_rdoes).±1e-30f,±denorm_min) and asigngamcheck forlgammaf; with the current rows the Windows bug is invisible. Fortgamma/tgammaf, exact== 6style checks depend on the host libm (MinGW'stgammais not guaranteed exact for integers); either use a tolerance or implement it from FreeBSD msun, and add a pole row (0,-0) next to the-1NaN row.maininMath.cppandGuestMath.cpp(lgammaarea changed by #5775); please rebase.tgamma/tgammaf; this PR came first, so that one should drop them.Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-19T00:11:33Z.
Measurement for the open review point on tgamma exactness: how MinGW's host tgamma/tgammaf does on integer arguments.
Windows 11, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), -O0, arguments passed through volatile so nothing is folded at compile time. A standalone program, not this PR's code; it calls std::tgamma(double) and std::tgamma(float) and compares tgamma(n) with (n-1)!.
So an exact tgamma(4) == 6 style check passes here only for small n; tolerances or an own implementation are needed for the rest, as the review says. I did not build this PR; only the host functions were measured. Not measured: Linux/glibc, other MinGW versions.
AI-assisted: yes (Claude Code).
Imported from boykopovar/AnyPS5. Originally posted by ionut27001 on 2026-10-19T06:35:18Z.
Console data for
lgammaf,tgammaandtgammaf, in case it is useful here. p14 called the functions thatlibSceLibcInternal.sprxexports, from a payload process on a PS5 (firmware 12.70), witherrnoset to 0 before each call andsigngamset to 777 before eachlgammafcall:Compared with the host functions this PR calls, from a test program built with WinLibs GCC 15.2.0 on Windows:
lgammafwritessigngam: 1 for -0 and -1, -1 for -2.5. This PR'slgammafreturnsstd::lgamma(x)and leavessigngamalone.lgammafsetserrnoto 34 (ERANGE); the host sets 33 (EDOM).tgamma(-0)andtgammaf(-0)return -infinity witherrno0 on the console; the host sets 34.tgammaf(171.5f)overflows to +infinity witherrno0 on the console; the host sets 34.tgamma(-1)andtgammaf(-1)return a NaN with the sign bit set (0xfff8000000000000, 0xffc00000) anderrno33 on the console; the host's NaN has the sign bit clear.tgamma(-2.5),tgammaf(-2.5f)andlgammaf(171.5f)differ from the console by 1 ulp on this host.The probe source and the full log are in lgamma-probes.zip (p14). For
lgammaitself, #7544 now setserrnotoERANGEafter a pole and otherwise leaves it as it was; in p11 (same zip) the console'slgamma, called witherrnoset to 0, set 34 for 0, -0, -1, -2 and -1e306 and left it at 0 for 1e306, whose result overflows, both infinities and NaN.AI-assisted: yes; the host results above were run on this machine.
Pull request closed