fix(libc): match lgamma's signgam for -0 and its errno at poles as measured on a PS5 #7544
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/7544"
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 ionut27001 on 2026-10-18T13:01:55Z.
What
lgamma(-0)now leaves 1 insigngam, as the console'slibSceLibcInternal.sprxdoes when called from a payload process on a PS5 (firmware 12.70); whether a title's ownlibc.prxdoes the same is unverified. Before, it left -1, following FreeBSD 11'se_lgamma_r.c.signgam(libSceLibcInternal.sprx)mainbefore this PRThe return value is unchanged. On the console
lgamma(-0)returns +infinity, likelgamma(0). For ±2^-1074 the console's value differs from the one here: 0x40875427f6acad6f (about 746.52), where this code returns -log(|x|) (about 744.44). p14 ran under the console's starting MXCSR 0x9fc0 (flush-to-zero and denormals-are-zero) and did not measure that input under 0x1f80, so this PR leaves the value alone and records it in the debt entry. The entry also records that, of the other 84 measured inputs, 32 come out 1 or 2 ulps away from the console's results with the hostlgammaof this Windows build,lgamma(-2.5)12 ulps away andlgamma(-2.75)96 ulps away (this sentence said 66 inputs before; the 84 count both probes).Only the special case for zero is removed. Zero now goes through the same rule as every other input, which gives 1 for both zeros. The existing test asserted -1 for -0; it now asserts 1.
errno. The console'slgammaalso setserrnotoERANGE(34) after a pole and leaves it alone otherwise, an overflow included. The Windows build passed on the host'serrno, which differs, as a reviewer noticed:errnoset to 0 before the call)lgammaof this Windows build (WinLibs GCC 15.2.0)ERANGE)EDOM)ERANGE)lgammanow saveserrno, calls the hostlgammafor the value and the floating-point flags as before, and then setsERANGEfor a pole (a zero or a negative integer) or puts the savederrnoback. The host column comes from a small test program built with the same toolchain; the test checks the console'serrnofor each input above.docs/dev/debt/libc.md: thelgamma and signgamentry said the rule was taken from the FreeBSD source and not measured. Since the sign anderrnobehaviour now follows the measurement, the entry is cut to what is still open: the hostlgammavalues that differ by a few ulps, the ±2^-1074 result measured under the console's starting MXCSR 0x9fc0 (0x1f80 not measured), and the assumption that a title's ownlibc.prxbehaves like the console'slibSceLibcInternal.sprx.How the console was measured
Setup:
Method:
resolvelines show that the calledlgammaand thesigngamread are the oneslibSceLibcInternal.sprxexports.signgamto 12345 before each call, sosigngam=1afterlgamma(-0)is a value the call wrote.Console output (excerpt, lines copied unchanged)
The
pNN:prefix names the probe; the rest of each line is copied unchanged.The p14 sweep has 60 lines, from -0.75 to -30.25. Every one matches the floor rule.
Overlaps:
Tested
Build: Windows 11 x86-64, full build with WinLibs MinGW-w64 GCC 15.2.0 (posix-seh, ucrt, r7) and
-DBUILD_TESTING=ON.Tests (earlier base
9f5d4bd92): with this branch rebased ontomain9f5d4bd92,guest_mathand the otherguest_*tests pass: 494 of 495, andguest_video_out_wqhd_detection_no_vulkanis skipped on this machine. That is the second of two full runs; the first, right after the build, timed out in two tests this PR does not touch (guest_pthread_rwlock,guest_fstatfs). With the final test,guest_mathpassed 3 runs in a row. Old code, new test: withMath.cppreverted tomainand the new tests kept,guest_mathaborts. With only the newerrnolines removed fromlgamma, it aborts too.Rebase (2026-10-26): rebased onto
mainf95acfc948. The only conflict was indocs/dev/debt/libc.md, next to the lgamma entry, which main had not changed. The entry is now cut to what is still open (the hostlgammavalues off by a few ulps, the ±2^-1074 result, and the untested MXCSR case), under the 1,000-character limit, because the sign anderrnobehaviour it described is what this PR implements. On that base, Windows 11 with WinLibs GCC 15.2.0:guest_mathpasses twice, and withMath.cpprestored frommainit fails. The branch merges cleanly with the newermainfd0da73ec5, whoseMath.cppandGuestMath.cppchanges it was not re-run with.Console: behaviour measured on a PS5 (firmware 12.70), as described above.
Not tested here: Linux (a reviewer reported a Linux run of head
ac0a04e180, which has theerrnochange, in the comments), and no title.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)🤖 Generated with Claude Code
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-19T01:09:50Z.
Ran this on both hosts (Windows 11 with WinLibs GCC 15.2.0 (full build), and WSL Ubuntu 24.04 with GCC 13.3, base
a373e85e5).guest_mathpasses on both, and aborts on both withMath.cppreverted; the description lists Linux as not tested.I called the guest
lgammawith p11's and p14's inputs on both hosts.signgamnow matches the console log for every input, including -0 (1).0x404bb9d3beb8c86b).0x40874385446d71c3on both hosts, against the console's0x40875427f6acad6f, as the debt entry says.The console's -0 result matches FreeBSD msun from before r271722 (2014-09-17, "lgamma_r(-0, &signgamp) should set signgamp = -1"). 10.0.0's
e_lgamma_r.csets*signgamp = 1and returns for ±0 before any sign handling. 11.x sets*signgamp = 1-2*((uint32_t)hx>>31)first (11.4e_lgamma_r.c#L216). That may be worth one clause in the debt entry.Two things in the attached p11 log that this PR doesn't cover:
lgammafwritessigngam(1 for -0, -1 for -2.5f). #6158'slgammaf(std::lgamma(x)) doesn't touchsigngam.errnois 34 (ERANGE). Here the guesterrnois 34 on Linux but 33 (EDOM) on the Windows build, both before and after this PR.AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by thepoppfish on 2026-10-25T09:37:38Z.
Review: COMMENTED
Reviewed exact head
ac0a04e18012866fb909f718caa18aff76cc358f, including the newer errno change. This adds independent Windows validation beyond the older-version report above.Windows 11 build 26200, MSYS2 UCRT64 GCC 16.2.0-6, CMake 4.3.1, Ninja 1.13.2:
GuestMath.cpppasses in an out-of-tree native harness, then passes three consecutive repeats (0.02 s each), and a final replay (0.03 s).guest_mathfail. The extra probe independently fails in 14, 2 and 14 cases, respectively. In the sign-only control, exactly the two -0 cases retain signgam -1. In the errno-only control, Windows gives EDOM at each pole and ERANGE on positive overflow instead of preserving errno.b2c7a388406614fb3bc6723a6717a37196d617b8) reports zero errors and one debt-length warning: the changed lgamma entry is 2445 characters. Whitespace check passes.The source change matches the published p11/p14 excerpt for the tested sign/errno cases. I did not independently measure a PS5, execute the attached payloads, verify retail libc equivalence, or resolve the explicitly documented subnormal return-value and host-ULP differences. This is target-level Windows validation, not a full shared-libc/product build, Linux/macOS run, sanitizer run, title run or cross-PR composition check.
The final harness compiles the actual Math.cpp, FloatBuiltins.cpp, Strings.cpp, IntegerBuiltins.cpp, RuntimeSupport.cpp and HostEntropy.cpp plus the complete actual GuestMath.cpp, with no LTO. Support.cpp supplies only __error returning the host errno pointer as Process.cpp does, a throwing NotImplemented helper, and Windows abort-report UI suppression. It does not replace math, conversion, floating-point environment or errno behavior.
Leaving this comment-only because
git merge-tree --write-tree --name-only origin/main HEADagainst mainb2c7a388406614fb3bc6723a6717a37196d617b8conflicts indocs/dev/debt/libc.md; the C++ source and test merge automatically. Please rebase and trim the changed debt entry to the remaining gaps, keeping the detailed measurement report here in the PR. No implementation defect found in this focused review.AI-assisted: yes, OpenAI Codex. Commands and complete local harness follow so the validation can be replayed without modifying the PR checkout.
Replay on Windows with a MinGW GCC toolchain on PATH
Use the exact PR checkout as REPO; place these files in a separate directory. CMake locates g++, Ninja and objcopy through PATH. Python is needed only for the negative controls, which also need Git on PATH. The script uses the checkout's HEAD parent for the original function and verifies that each executable fails for all three controls.
For
negative_controls.py, put the exact checkout at../review-7544relative to the harness, as in the default CMake setting (or change the script's repo assignment). Runpython negative_controls.py.CMakeLists.txtSupport.cppLgammaProbe.cppnegative_controls.pyImported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:41:16Z.
Review: COMMENTED
Reviewed — no blocking findings
The changed
lgammalogic and its tests were reviewed at head92365e35a4cac04a651731dc1fb0cdd4fb52851b; no concrete defect was found, but tests were not run and the complete changed files were not read.What I ran
Read the prepared description, commits, manifest, diff, and targeted ranges from all three changed files using
repo_read.py’s review reader. No build or tests were run.Results
Negative control
Not run; no test execution was available.
Findings
Blocking
None found in the changed hunks reviewed.
Non-blocking
None.
Sources
Reviewed the changed hunks at
core/libs/prx/libc/src/Math.cpp:725-730,core/libs/tests/GuestMath.cpp:1805-1817, anddocs/dev/debt/libc.md:117in the prepared diff and pinned-head reader output.Not verified
Build and test results, including the author-reported runs, were not independently verified. The full contents of the three changed files were not read; only relevant ranges were inspected. The manifest lists all three as included, and no changed files were excluded.
To get this to a good state
Nothing is required.
Pull request closed