fix(libc): set expf's range error and tgammaf's underflow without relying on the host libm #21714
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/21714"
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 XIIYAZ on 2026-11-16T16:34:01Z.
What
On macOS,
guest_mathandguest_math_exactabort:Both functions relied on the host math library for that part of the result:
expfreturnedstd::exp(x)and lefterrnoto the host.tgammaftakes the underflow to a signed zero (for -190 < x < 0, not an integer) from the host'stgamma, which raisesFE_UNDERFLOWon Linux.Apple's libm does neither. A standalone x86-64 program on macOS shows it:
math_errhandlingisMATH_ERREXCEPTonly, so no math function setserrno.tgammareturns 0 with onlyFE_INEXACT(0x20).Now:
expfsetsERANGEwhen a finite argument overflows to infinity or underflows to zero, asexpalready does in its own implementation.tgammafraisesFE_UNDERFLOW | FE_INEXACTin the branch that returns that signed zero.glibc and the UCRT already set both, so Linux and Windows behave the same as before.
Tested
Setup:
mainwith-DBUILD_TESTING=ON, x86_64 under RosettaResults:
guest_mathandguest_math_exactfail before this change and pass after it, all of each test, not only the line that failed first.python3 tools/check_conventions.py --base origin/mainpasses.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)Summary by CodeRabbit
Imported from boykopovar/AnyPS5. Originally posted by kurexw on 2026-11-17T04:45:47Z.
Review: COMMENTED
Tested on an Apple M4 (macOS 27.2, Apple clang 21.0.0, x86-64 build under Rosetta).
Base:
mainata72186d3cfplus #22056 and #22074, two build fixes thatmaincurrently needs on macOS. Each binary was run 3 times.guest_math_exactguest_mathGuestMathExact.cpp:687(expf(89.0f):errno == ERANGE)GuestMath.cpp:4971(unrelated, see #22081)Apple's libm never sets
errno, so on macOSexpfrelied on the host to report the range error. Setting it here fixes that. The other math tests I ran (guest_sce_v2_exp,guest_sce_v2_log_gamma,guest_libc_internal_log_gamma) pass with and without this PR.Overlap: the older #10113 replaces
expfwith msun's algorithm. It fixes the same test on this machine; see my review there.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T11:32:23Z.
Review: APPROVED
Reviewed — looks good
Nothing in the reviewed changes blocks merging; one optional edge case is noted below.
What I ran
Reviewed head
4f8002edd1d2d9ff9b03870ec286b7368987ccd5against the prepared diff. No tests or other commands were run.Results
Findings
Blocking
None.
Non-blocking
core/libs/prx/libc/src/Math.cpp:4025: Theexpfwrapper setsERANGEwhen a finite input produces infinity or zero, but does not set it for a nonzero subnormal result. If the intended behavior is the full C range-error behavior for underflow, consider detecting that case too and add a focused test.Not verified
review/description.md,review/commits.md,review/manifest.json,review/diff.patch, and the changedMath.cppranges 240–280 and 4000–4040.review/files.jsonwas unavailable through the reader; the review listing confirms the one changed file,Math.cpp, is included. No other changed files were listed.To get this to a good state
Nothing is required. Optional: clarify whether nonzero subnormal
expfresults should setERANGE, and test the chosen behavior.Imported from boykopovar/AnyPS5. Originally posted by coderabbitai[bot] on 2026-11-30T01:59:39Z.
Review: COMMENTED
Actionable comments posted: 1
Architecture findings (1)
Advisory architecture findings (1)
c52ddd81-0847-48db-83b5-12a08b9aab5cReviewing files that changed from the base of the PR and between
0ed2560d37andf35e676275.core/libs/prx/libc/src/Math.cppIncluded review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-30T02:02:08Z.
Review: APPROVED
Reviewed — looks good
Nothing blocks this change; the behavior and code inspected support merging.
What I ran
Reviewed head
f35e676275e41cc398a5e38a556fea3e3019c2bc. No build or tests were run in this review. Readdescription.md,commits.md,manifest.json,diff.patch, and the changedMath.cppranges around both hunks; searched its callers and related errno handling. The description reports macOS tests and a conventions check; those were not run here.Results
Findings
Blocking
None.
Non-blocking
None.
Not verified
No build, tests, or conventions check were run. No files were excluded from review; the manifest lists one changed file, and its relevant changed code was read. Other portions of
Math.cppand unrelated repository files were not reviewed.To get this to a good state
Nothing is required.