fix(libc): compute exp2l with FreeBSD msun's ld80 code as the bundled libc.prx does #11266
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/11266"
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 AlexFigas on 2026-10-25T23:07:51Z.
What
exp2lcalled the hoststd::exp2, which differed from the console's result on 7,539,279 of 20 million random arguments, about 38%. This PR replaces it with FreeBSD msun's ld80s_exp2l.c, theexp2lof thelibc.prxthat PPSA21564 bundles (0x46a50, the library's own code). The binary followss_exp2l.cstep by step:1 << 63) andx + xotherwise. From 0x400d,x ≥ 16384returns +∞.x ≤ -16446returnstwom10000 * twom10000, withtwom10000read from.dataat run time.0x3fbdand below, the result is1 + x.x + reduxusesredux=0x1.8p63/128(the float0x5bc00000). The low mantissa word plus 64 gives the 7-bit table index andk = (int)i0 >> 7. Thenz = x - ((x + redux) - redux).r = t_lo + (t_hi + t_lo)*z*(P1 + z*(P2 + … + z*P6)) + t_hi, wheret_hi/t_locome from the 256-doubletblat 0x1622a0 andP1–P6aredoubles. It is scaled by2^k, byr*2*2^16383fork == 16384, or by2^(k+10000) * twom10000belowk == -16381.The table and coefficients are taken from the binary as exact hex-float literals. The table and
P2–P6are thes_exp2l.cvalues;P1is not. The source has the long doubleP1 = LD80C(0xb17217f7d1cf79ac, -1, …), while the binary holds thedouble0x1.62e42fefa39efp-1, and the port uses the binary's. Two behaviours follow the binary rather than the source's intent:huge * huge, soFE_OVERFLOWis not raised.errno.x87 precision.
s_exp2l.cwraps the computation inENTERI()/RETURNI(), but those set the precision only on i386 and are empty on amd64 (math_private.h). The binary'sexp2laccordingly has no instruction that reads or sets the x87 control word, so it computes at the caller's precision, as the port already did: it needed no change for 53 bits.exp2lgets a debt entry likeacosl's andatanl's. The remaining host-computed entry now records what I found in that binary:expl,logl,log10l,sinhlandcoshlthere are thin wrappers over Dinkumware's_LExp,_LLog,_LSinhand_LCosh, not msun code.Licence.
ld80/s_exp2l.c's notice is copied verbatim directly above its table, which moved next toexp2lso that one notice covers both, asMath.cppalready does for the msun code ofsinh.Tested
Head
1d907ed73bmerges currentmaina221a742da, which has #11262 (and #11260), into the approved branch without a conflict, so this PR is now only theexp2lcommit. The PR's diff ofMath.cpp,GuestMath.cppandlibc.mdagainstmainis line for line the same as 3d2cd44d81's against #11262's head1dfac78f90.BUILD_TESTING=ON, Ninja:guest_math,guest_math_elementaryandguest_math_exactpass 3 runs each.python3 tools/check_conventions.py --base origin/mainpasses, andgit diff --checkis clean.The runs below are on Ubuntu 24.04 under WSL2 (GCC 13.3, Release), head
3d2cd44d81, which merges #11262's head1dfac78f90(withmain813f46cd68) into the previous head4c1383e264, without a conflict.4c1383e264had merged #11262's head0ecffe5ca5(withmaina4eca2a056) intof8a43f4245, without a conflict either.f8a43f4245had merged #11262's head6479c72ac1(withmaine0caf13600) into7116579fc3:libc.prx. Against the binary, 5,000,000 arguments of every kind (overflow, the subnormal range, ±∞, NaNs and non-canonical encodings included) at each of 64, 53 and 24 bits, under each rounding mode, give the same result, NaN bits,errnoand flags, and the control word is unchanged after every call (60,000,000 in all)..datasegment, becausetwom10000lives there. It ran 20,000,000 random arguments of either sign with magnitudes from 2^-79 to 2^15, weighted towards the overflow and denormal ranges, plus the exponent edges0x3fbc–0x3fbe,0x3ffe–0x4000,0x400b–0x400e,0x7ffeand0x7fff. The port gives the same 80 bits as that binary on all of them (0 mismatches) at each of 64-, 53- and 24-bit precision (control words 0x37f, 0x27f and 0x07f). wachi35676 found that swapping onlyP1for the source's long double changes 1,442,132 of 10,000,000 random results, so this run is also what shows the binary'sP1is the double.guest_mathpasses (3 runs). It gainsCheckExp2l, 24 known-answer pairs from that binary. They cover ±0, the1 + xcutoff, both table halves,2^16383.99…, overflow, the subnormal tail down to 0, ±∞, NaN, and four arguments the host gets wrong, for example0xf30567547a34c162:3ffe → 0xf728f2c7a229ee69:3fffwhere the host gives…67. Nine arguments, includingexp2l(0.75), π,2^16383.99…and a subnormal result, also run at 53 bits through #11260'sExtendedBitsAt53Bits; each must give the binary's 53-bit result and leave the control word unchanged. At 53 bits the reduction is coarser and the binary's results move far from the 64-bit ones (exp2l(0.75)is0xd744ea8956fc0000:3fffinstead of0xd744fccad69d6af4:3fff), which the port reproduces. The existingexp2lchecks still pass.python3 tools/check_conventions.py --base origin/mainpasses, andgit diff --checkis clean.1d907ed73b,python3 tools/pr_overlap.py --branch fix/libc-exp2l-msun-ld80reports only #18110 (opened on November 10, after this PR), which replaces the hostloglandlog10llines next to theexp2lline this PR replaces inMath.cppand edits the same host-computed entry inlibc.md(L89). The two do not change the same functions; whichever lands second keeps the other's lines. This PR was opened and approved first, so I have not added a dependency. oberonlift's #15802 stack has been closed. The other pull requests the tool could not compare conflict withmainalready.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 MarcZX10 on 2026-10-25T23:14:53Z.
Review: CHANGES_REQUESTED
The implementation follows the cited
s_exp2l.creduction and polynomial, but it omits the source'sENTERI()/RETURNI()floating-point environment scope around the computation. In FreeBSD's ld80 implementation those macros ensure the calculation runs with the expected x87 precision control and restores the caller's state. This matters here: the repository's libc debt entry documents that guest threads can enter with 53-bit x87 precision (0x27f), while the algorithm needs 64-bit precision for its advertised bit-for-bit match; inputs such asexp2l(0.75)can therefore diverge even though the reported native comparison ran under the harness's default environment. The newCheckExp2lcases do not set a non-default control word or verify it is restored. Please add a scoped precision-control implementation with a regression test that runs representative cases under 53-bit precision and checks the original control word is restored, or document evidence that all callers are guaranteed 64-bit precision. The hosted checks are passing, but the macOS relinker, Windows relinker and macOS Intel write-watch jobs are still pending; Linux and Windows product jobs are skipped. I have not independently run the author's local comparison.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T11:00:48Z.
Review: CHANGES_REQUESTED
The PR includes a broad comparison against the bundled libc.prx, with overflow/denormal edges, multiple precision settings and known-answer test cases. I am not approving this submitted head yet: its merge-base is
68cc9d4e75, whereas current main is3fea2a937b. Main has changed Math.cpp, GuestMath.cpp and docs/dev/debt/libc.md since that base, and the Tested section names main5b5d24cb22rather than the submitted head's base.This PR is stacked on #11262 and declares that dependency. Please rebase the stack onto current main after #11262 (and its dependencies) is integrated, rerun guest_math on the updated head, and align the test report with that exact commit. I found no independent arithmetic regression in the reviewed portion; current-tree integration is still unverified.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:24:03Z.
Review: CHANGES_REQUESTED
I reviewed the current head d85d265eb824dec162c8b92eab3bf47d272c8d89. The reduction/table path and documented differences from FreeBSD source are explicit and are backed by the bundled-binary comparisons in the PR. I found no separate arithmetic defect.
Independent focused check: with #11260, #11262, and #11266 changes applied to the clean #11213-based Release checkout, guest_math_tests built and guest_math passed three runs. This is focused Linux validation on the stack's
1089f2bbase plus unrelated #11213 exception changes, not a build of the current integrated main tree.The Windows relinker check is red. Its CTest log reports five timeouts: check_compile_modes_test_library_directory_values_can_look_like_modes, conventions_bom_shebang, and three conventions_env_reference tests. The relinker-specific tests in that same job passed; these failures are in unrelated tooling/conventions tests, but the check still needs a clean rerun. The stack is also based on
1089f2bwhile current main is2ee8e97(30 commits ahead), with #11260/#11262 and their declared dependencies still open. Please rerun the failed Windows job and update/retest the stack after its dependencies land before approval.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T15:26:06Z.
Review: CHANGES_REQUESTED
I reviewed the current head d85d265eb824dec162c8b92eab3bf47d272c8d89. The reduction/table path and the documented differences from the FreeBSD source are explicit and supported by the bundled-binary evidence. I found no arithmetic defect.
Independent focused check: applied the #11260, #11262, and #11266 Math.cpp, GuestMath.cpp, and debt changes to a clean #11213-based Release checkout, built guest_math_tests, and ran guest_math three times; all passed. I did not rerun the author's differential harness. Current main merges cleanly, and the paths this stack edits have not changed since its
1089f2bbase; the earlier branch-currency concern is resolved.The Windows relinker check remains red. Its completed CTest log shows five timeouts in check_compile_modes_test_library_directory_values_can_look_like_modes and unrelated conventions tests (conventions_bom_shebang plus three conventions_env_reference cases); the relinker tests passed. Please rerun this Windows job and confirm the timeout failures clear before approval. The PR's declared stack/dependencies should land in the stated order.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:10:37Z.
Review: APPROVED
The Windows rerun on the unchanged head d85d265eb824dec162c8b92eab3bf47d272c8d89 resolves the prior CI blocker: the relinker job now passes 370/370 tests, including the earlier timeouts, and the Windows job passes. The macOS and Ubuntu test jobs also pass; Linux product is skipped by this PR's job filters. My focused local guest_math build and repeated runs passed in the earlier review, and the current-main merge preview is clean.
I found no arithmetic defect in the exp2l implementation. Approved; keep the declared dependency on #11262 and land this stack in order.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:51:54Z.
Review: APPROVED
Re-reviewed exact current head
394be8ce3e9042cfd9ea42729e083839e8decb8d. The rebase moves the already-reviewed atan/exp2 tables next to their functions and adds the FreeBSDinvtrig.c,s_atanl.cands_exp2l.ccopyright/license notices; the notices match release/11.4.0 and the table values/algorithm remain intact. The declared #11262 dependency is preserved. Hosted Ubuntu, Windows, macOS, ASan, register-context and write-watch checks pass; Linux product is skipped by workflow filters. The author reports three exact-headguest_mathruns plus title-binary differential results. No PS5 title run is claimed.