feat(libc): implement tgamma, lgamma, and gamma math functions #8417
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8417"
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 SwetabhSingh17 on 2026-10-20T12:29:04Z.
What
Implement
tgamma,lgammaf,lgamma_r,lgammaf_r,gamma,gammaf,gamma_r, andgammaf_rincore/libs/prx/libc/src/Math.cpp, add unit test coverage incore/libs/tests/GuestMath.cpp, and updatedocs/dev/debt/libc.md. Built on #7544 to inherit PS5lgamma(-0)sign and poleerrnobehavior inlgamma_r.Tested
python3 tools/check_conventions.py --base upstream/main,python3 tools/pr_overlap.py --branch feat/libc-math-gamma.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)Imported from boykopovar/AnyPS5. Originally posted by SwetabhSingh17 on 2026-10-20T12:55:35Z.
Windows Test Verification
System Specifications:
Test Results:
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, andlgammaf_r,gamma_randgammaf_rforward to it. FreeBSD'se_lgamma_r.c, which the debt entry cites, writes*signgamp = 1;before anything else, and so doese_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 inrand_r(L691),_Dtestand_FDtest. I would store the sign unconditionally and start the function with:Sign checks in the new tests (GuestMath.cpp L1192): the
lgammaf_r(1.f, &rSign)line already leavesrSignat 1. TherSign == 1checks forgamma_randgammaf_ron lines 1192-1193 therefore pass even if neither function writes it. No new line readssigngam_nid_postfixafterlgammaf,gammaorgammaf. For example, ifgamma_r_nid_postfixreturnedlgamma_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:The same
signgam_nid_postfixcheck belongs afterlgammaf_nid_postfix(-0.5f)and aftergamma_nid_postfix(-0.5).Dead FreeBSD link (libc.md L78): the
s_gamma.clink returns 404, because FreeBSD has no such file on stable/11 or on main. The wrappers are ine_gamma.c(gamma_r(x, &signgam)),e_gamma_r.c(lgamma_r(x, signgamp)),e_gammaf.cande_gammaf_r.c, and the entry could link those instead. The new entry also drops the old one'ss_signgam.clink, which was the source forsigngamstarting at 0.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 nullsigngamp: FreeBSD'se_lgamma_r.cstores the sign first, so throwstd::invalid_argumentthere (asrand_rand_Dtestin this file do) and store unconditionally.gamma_r,gammaf_rand thesigngamwriters can't fail as written; please set a sentinel first and use -0.5 (where Γ is negative), as in nikowolf123's snippet.s_gamma.clink is a 404 on stable/11;e_gamma.c,e_gamma_r.c,e_gammaf.cande_gammaf_r.care the wrappers, and thes_signgam.clink from the old entry is worth keeping.After that, the bar is a full CI run with
guest_mathpassing on Linux and Windows. These tests haven't been run yet, so that will be their first run.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:
lgammafandtgamma, and #7544 rewriteslgamma'ssigngamand poleerrnobehaviour (measured on a PS5) in the same lines ofMath.cppthis 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'se_lgamma_r.c/e_lgammaf_r.cstore the sign first. Throwstd::invalid_argumenton null (asrand_rdoes in this file) and store unconditionally.core/libs/tests/GuestMath.cpp:1409-1415: therSign == 1checks forgamma_randgammaf_rpass even if nothing writes it (rSignis already 1), and nothing readssigngam_nid_postfixafterlgammaf,gammaorgammaf. Set a sentinel (7) before each call and use -0.5, where Γ is negative, expecting -1, forgamma_r,gammaf_r, and viasigngam_nid_postfixforlgammaf,gamma,gammaf.docs/dev/debt/libc.md:83: thes_gamma.clink is a 404; linke_gamma.c,e_gamma_r.c,e_gammaf.c,e_gammaf_r.cinstead, and keep thes_signgam.clink the old entry had.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-26T12:35:55Z.
Review: DISMISSED
Thanks, the null
signgamp, sentinel tests ande_gamma*.clinks are all fixed. One point left, the overlap:lgammain the same lines ofMath.cppwith behaviour measured on a PS5:lgamma(-0)storessigngam = 1(this PR keeps FreeBSD 11's -1 inlgamma_r_nid_postfix,core/libs/prx/libc/src/Math.cpp:727) and a pole (0, -0, negative integers) setserrnotoERANGE(not set here). Sincelgamma,lgammaf,gamma*and*_rall go throughlgamma_r_nid_postfix, they would inherit the wrong -0 sign and no poleerrno. Please rebase onto #7544 once it merges (or build on its branch), keep its sign/errnorules insidelgamma_r_nid_postfix, adapt thelgamma_r(-0)expectations inGuestMath.cpp, and list #7544 under Depends on.