feat(libc): implement _LExp #10780
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10780"
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 00xDisaster on 2026-10-25T00:06:02Z.
What
Add
_LExp, the three-argument Dinkumware helper that computesy * exp(x) * 2^scale. The finite path uses the Dinkumware rational sequence, multiplies byybefore the final scaling, and raises the libc error and floating-point exception state. The implementation follows the_LExpof firmware 13.42's systemlibSceLibcInternal.sprxas measured natively off-console by @BunnyFl:xalone, before the offset is used.x > 29490storesy * inf, returnsInfinite, and raises overflow withERANGEandFE_INEXACT.x < -29490stores0 * y, returnsZero, and raises noerrnoor flags. A30000operand with offset-32768therefore still overflows, and a-60000operand with offset0still returns zero.errno, no flags).y == 0storesyitself, preserving-0.x = +inf, y = 0andx = -inf, y = +infstore NaN, seterrnotoEDOM, and raiseFE_INVALID; the remaining infinity combinations follow the module's table.Finiteand preserves the incomingerrno/flags except for raisingFE_INEXACT. An underflow to zero stores+0(even with a negative multiplier), returnsZero, and raisesERANGEwithFE_UNDERFLOW|FE_INEXACT. Overflow always stores signed infinity, including in directed rounding modes.short. The debt entry records that the published declaration is narrower than the module's read; signaling NaNs and a title's bundledlibc.prxremain unverified.The tests assert the measured operands from @BunnyFl's run, including the exact bits of
_LExp(-0.5, 1, -16400)(0x0.0001368b2fc6f96p-16385, kind -1,errno == 0,FE_INEXACTonly), the four rounding modes for the±29490cutoffs, the two-inf/+infinvalid cases, the+0underflow sign withy = -1, and thex = -30000,x = -60000andx = 30000cases. The 401-pointexp(i/16)sweep remains within the1e-17relative tolerance.Tested
Linux CachyOS 7.3.0-4-cachyos, GCC 16.2.1. Base
upstream/main0836dfdf4fecb1e0326100c490476e15d3a74da5; headd0a02abde419c60a39b94c5134e29d309b9d5530.cmake --build build --target guest_math_exact_tests guest_math_tests -j$(nproc): passed.ctest --test-dir build -R 'guest_math(_exact)?$' --output-on-failure: 2/2 passed in 0.05 s._LExp(-0.5, 1, -16400)subnormal matches @BunnyFl's module bits exactly. The measured rows for+inf, 0, 0;1, -0, 0;1, NaN, 0;+inf, 1, 0;-inf, 1, 0;-inf, +inf, 0;30000, 1, -32768;-60000, 1, 0;-60000, +inf, 0; and-5, +inf, 0in toward-zero mode all assert the stored value, kind,errnoand flags from that run.LDBL_MAX-on-directed-overflow path, the restored flags on subnormal results, the NaN multiplier, and the-inftoward-zero case. Those paths are replaced by the module behavior above and now have direct assertions.tools/check_conventions.py --base upstream/main --head HEADandgit diff --check upstream/main..HEAD: clean.guest_math_exactpassed in a joint 160/160 run of the libc, CesCs, Font, FreeType, LibreSsl3 and NpUniversalDataSystem suites. Not run on a PS5.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-25T00:08:06Z.
Review: COMMENTED
The positive path has focused coverage and the patch is carefully documented. I found an ordering edge case in the new front-end special cases:
y == 0is handled before classifying NaNs. Forx = NaN, y = 0, the current code returns zero, while the subsequent NaN branch suggests NaNs should propagate; please add an explicit test and establish the intended console behavior for this combination. Likewise, thex > 29490fast path runs before checkingy: withx = +infinity, y = 0, it raises overflow and returns infinity even though the zero multiplier branch otherwise returns zero. These special-value combinations need an explicit ordering backed by the target helper behavior, especially since the PR states exception behavior is unverified. I have not approved pending that clarification.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:08:09Z.
Review: CHANGES_REQUESTED
The finite path has focused tests, and the PR documents what remains unverified. I found a special-value ordering issue that changes return values and exception state. The
y == 0shortcut precedes NaN classification, sox = NaN, y = 0returns zero. Thex > 29490shortcut also precedes multiplier classification, sox = +infinity, y = 0returns infinity and raises overflow even though the zero-multiplier shortcut otherwise returns zero. Please add tests for both combinations, plusx = NaN, y = 0with alternate scale if relevant, and establish the intended Dinkumware/console semantics before choosing precedence. Because this helper explicitly updates errno and floating-point exceptions, those cases should assert both outputs and state. I did not run the full build locally; the PR reports its focused Linux suites and hosted checks are underway.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T00:27:17Z.
Review: CHANGES_REQUESTED
I compiled the exact
MathExact.cppfrom head30ec7bea29e1349d9efa3f6297435f02436c86c9with a small harness and confirmed an overflow-rounding mismatch: withFE_TOWARDZERO,_LExp(&value, 1.0L, 16384)stores +infinity and returnsInfinite, while hostscalbnl(1.0L, 16384)storesLDBL_MAXunder the same mode. Both setFE_OVERFLOW|FE_INEXACT; the_LExpresult is observably different. The implementation handles the subnormal rounding direction but the exponent-overflow branch always constructs infinity. Please make overflow honor the active rounding direction (including sign) and add tests for positive and negative overflow underFE_TOWARDZERO/directed rounding, asserting both stored value and return classification. I did not run the full project test suite; this was an isolated compile/run of the PR translation unit and harness.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T01:12:24Z.
Review: CHANGES_REQUESTED
Thanks for addressing the two special-value cases and overflow rounding. A further directed-rounding case remains in
ScaleExtended: whenrightShift >= 65, it unconditionally stores zero. For a positive nonzero exact value below half the least subnormal,FE_UPWARDmust round todenorm_min(); for a negative value,FE_DOWNWARDmust round to-denorm_min(). I confirmed the hostscalbnl(1.0L, -20000)behavior with-frounding-math: nearest/toward-zero/downward produce zero, while upward produces0x0.000000000000001p-16385(the 80-bit extended minimum subnormal). The current early return skips the mode/sign check, so_LExploses that directed rounding result even though the PR otherwise handles rounding modes explicitly. Please preserve the sign and return the least subnormal in the directed-away-from-zero cases, then add positive and negative tests for both sufficiently tiny and just-below-half-minsub inputs, including return classification,errno, and exception flags. I only ran this focused hostscalbnlcontrol; I have not run the updated project target locally.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T02:45:43Z.
Review: COMMENTED
The current head addresses the earlier special-value ordering, directed overflow, and tiny directed-rounding findings. Focused host tests reported by the author pass, and my isolated exact-head compile/run confirmed the large-magnitude branches behave as described; I left one non-blocking follow-up question about signed zero in the negative-exponent early-underflow path. I am leaving a comment rather than approving while console-specific edge semantics remain unverified.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-25T03:05:28Z.
Review: CHANGES_REQUESTED
The current head addresses the special-value ordering, directed overflow and directed tiny-result issues from my earlier reviews. My exact-head host harness confirmed the large-exponent cases; the author reports
guest_math_exactandguest_mathpass, and hosted conventions, eligibility, labels, relinker, sanitizer, register-context and write-watch checks pass (product Linux/Windows jobs are skipped). A remaining repository-rule blocker is the overlap report: open #8397 changes the samecore/libs/prx/libc/include/Api.hpplines, and open #10569 changes the samedocs/dev/debt/libc.mdlines. All three current checklists sayDepends on: none. CONTRIBUTING.md requires the authors to agree on merge order and record it withDepends on. Please coordinate with #8397 and #10569 and update the affected checklists. I will update #10569 after we agree; I have not modified your branch.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T22:09:54Z.
Review: APPROVED
Reviewing exact head 70a0b39dcef4a4c3786982841b965ccbbe50a46c.
What I ran
ca0e9557aa.Results
Findings
Blocking
None.
Non-blocking
Sources
Not verified
To get this to a good state
No blocking issue remains on this head. Land after #10569 and #11203, preserving their shared debt and test changes.
Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-10-29T11:38:23Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The finite path builds and the focused tests pass, but
_LExp's special-value and range handling contradicts the console-measured behaviour of itsdouble/floatsiblings_Expand_FExpalready onmain, and the new tests pin those differences.What I ran
1147f76d012a7aa0133c49a88fc5440a5e77f92cmain:ba5b5330b37724bca4b99f30faab504653635a04BUILD_TESTING=ON, in a clean worktree. Build and test commands ran in a sandbox with no network access.Results
maingit diff --checktools/check_conventions.pyAll conventions checks passed.guest_math_exact_tests,guest_math_tests,libsguest_math,guest_math_exact_LExpvs_Expon special operands_LExp(x, 1, 0)accuracy vsexpqSame operands through
_Exp(main) and_LExp(this PR), round to nearestInfinite multiplier beyond
|x| > 50000(_LExponly):Accuracy of
_LExp(x, 1, 0)against libquadmathexpqNegative control
MathExact.cppandApi.hppfrommain.guest_math_exact_testsno longer compiles because_LExp_nid_postfixis not declared. This is expected for a new export, and matches the Windows control already posted in this thread.x == 1.0Lbranch (MathExact.cpp:175-180).guest_math_exactthen aborts atGuestMathExact.cpp:216, the exact-bits check inCheckLongDoubleExpCompensatingRange. So the_LExp(1, LDBL_MAX, -2)vector passes only because of that branch (see the second non-blocking finding).After both controls I restored the merged files, rebuilt, and both tests passed again.
Findings
Blocking
Special operands and range cutoffs contradict the measured
_Exp/_FExponmain._Expand_FExpare thedoubleandfloatforms of the same Dinkumware helper. Commits35c38ce8d5and79d65956a1changed them onmainwhile this PR was in review. Their debt entry (docs/dev/debt/libc.md:215) records what PRAGMATA PPSA02530's bundledlibc.prxdoes:0.0*y, above ity*infwith overflow;*pxunchanged;+infwith zero, and-infwith infinity, are invalid.The code is at DinkumwareInternals.cpp:219-249 and :265-273. Subnormal results there return
Finite(-1) and restore the entry flags anderrno._LExpdoes something different in each of these cases (probe output above):y == -0stores+0(MathExact.cpp:412-415). This is also wrong for the stated contracty * exp(x) * 2^scale.*valuewith the NaN (:408-411).x = +inf, y = 0: returns 0 with no exception.x = -inf, y = inf: returns 0 with underflow._ExpraisesFE_INVALID/EDOMand returns NaN for both.x = ±infwith a finite multiplier:errno = ERANGEplusFE_OVERFLOWorFE_UNDERFLOW._Expreturns the exact infinity or zero without either (:160-167).Denormal(-2) withERANGEandFE_UNDERFLOW(:138-143). This includes exact ones such as_LExp(0, denorm_min, 0)._LExp(30000, 1, -32768)is finite, while_Exp(1843, 1, -2000)overflows. Below the cutoff the result underflows withERANGEinstead of returning0.0*ysilently._LExp(-60000, inf, 0)returns 0 while_LExp(-100, inf, 0)returns inf. UnderFE_TOWARDZERO,_LExp(60000, inf, 0)returnsLDBL_MAXwhile_LExp(-5, inf, 0)returns inf._Expreturnsyfor any infinite multiplier with finitex.Several of these are asserted as intended behaviour, for example GuestMathExact.cpp:349 (
+inf,0returns zero), :355 (+inf,1setsERANGE/FE_OVERFLOW) and :363 (a NaN multiplier is stored).I have not measured the console's
_LExp, so I can't say which_LExpresult is right in every case. Thelibc.prxthat served as the oracle for_Exp/_FExpshould also export_LExp, though, and the measured evidence for the family points away from several of the policies chosen here. Many of those policies were added in answer to earlier review requests (directed overflow, compensating offsets). Those requests were about hostscalbnl/mathematical semantics, not the console's helper.Suggested fix: measure
_LExpfrom the samelibc.prxthe way the_Expentry was measured, and follow it. Where that is not possible yet, follow the measured_Expstructure: special operands first,*value = yfor zero and infinite multipliers, x-only cutoffs, no range state for subnormal or infinite-operand results. Any long-double-specific choice that stays unknown, such as the cutoff value, should throw or be recorded in the debt entry rather than pinned by tests. At minimum, fix the signed zero and the infinite-multiplier ordering, which are wrong under any reading ofy * exp(x) * 2^scale.Non-blocking
_LExp(x, 1, 0)is off by up to 2.1 ulp, and about 7 % of results are off by more than 1 ulp (details above). That may well be the console's own error if these are the titles' coefficients, but the PR doesn't say wherep0..q1,ln2High/ln2Lowand the0x1p-66shortcut come from, and the debt entry says nothing about accuracy. Meanwhile, the sweep in GuestMathExact.cpp:331-336 allows a relative error of1e-17, about 90 ulp for an 80-bit long double, so it would not catch a coefficient regression. Suggestion: name the source of the constants in the debt entry, as the_Expentry does, and tighten the tolerance to a few ulp or assert exact bits for a handful of points.x == 1.0Lshortcut is broader than its precedent. MathExact.cpp:175-180 replaces the general path for every multiplier and scale wheneverx == 1._Exptakes itsEshortcut only foroffset == 0 && x == 1 && y == 1. As negative control 2 shows, the branch exists to make the_LExp(1, LDBL_MAX, -2)vector bit-exact, so it is fitted to a review test case rather than measured behaviour. Either match the measured condition, with evidence, or drop it and assert that vector to a stated ulp bound.Api.hppdeclares the last parameter asoffset, like_Exp/_FExp, but the definition names itscale. Usingoffsetin both would keep the family consistent.Not verified
libc.prxmeasurement of_LExp. The comparison above is against_Exp/_FExpas implemented onmainand their measured debt entry, not against_LExpon hardware.To get this to a good state
_LExp's special operands, cutoffs, return codes anderrno/fenv behaviour in line with the measured_Exp/_FExp, or measure_LExpitself from the samelibc.prx. Update the tests and the debt entry to match.x == 1shortcut.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-02T20:32:48Z.
Review: APPROVED
Re-checked — approved
The nine earlier commits are replaced by one commit,
d0a02abde4. It rebuilds_LExparound BunnyFl's native run of firmware 13.42's systemlibSceLibcInternal.sprx, which resolves my blocking finding. It builds,guest_mathandguest_math_exactpass, and a mutation of the new cutoff makesguest_math_exactfail.What changed since my last review
main, and its nine commits are replaced by one:d0a02abde4 fix(libc): match measured _LExp overflow and cutoff behavior.+infwith a zero multiplier, and-infwith an infinite one, give NaN withEDOMandFE_INVALID.-0keeps its sign.xalone, at ±29490, before the offset is used:y*inf,Infinite,ERANGE, overflow;0*y,Zero, noerrnoand no flags.+0withERANGE. A subnormal result returnsFinite, restores the entry state and raises onlyFE_INEXACT.x == 1shortcut now applies only toy == 1, offset == 0. The parameter is namedoffsetin both the declaration and the definition.What I ran
d0a02abde419c60a39b94c5134e29d309b9d5530,main096c45570f516de76a43bbe1b97781381c84f55f, merged tree97cef9af053238e3f053784369bc26fe6d1ab0f5.git diff --checkcheck_conventions.pyAll conventions checks passed.libsguest_math,guest_math_exactmain.guest_math_exactabortsgit status --porcelainemptyOn the negative control: if the production files are restored from
main, the test does not compile, because the export is new (the same result as my earlier control). The cutoff mutation shows that the new range assertions actually check the 29490 cutoff.Previous findings
_Exp/_FExp: Resolved. The PR now follows a measurement of_LExpitself, which is what I asked for.MathExact.cpp:560-633and the cutoffs at:208-213match each row of BunnyFl's table.0x0.0001368b2fc6f96p-16385(GuestMathExact.cpp:204-208), the cutoff cases (:217-243) and the NaN, zero and infinity rows (:376-438). All of them pass here.p0..q1,ln2High/ln2Lowandtiny.GuestMathExact.cpp:361-366still allows1e-17relative error, about 90 ulp. BunnyFl reports the 401 points as within that tolerance, not as identical bits, so the sweep does not show bit equality with the module.x == 1shortcut: Resolved. It is now limited tooffset == 0 && x == 1 && y == 1(MathExact.cpp:625-628), as in_Exp. The constant0xa.df85458a2bb4a9bp-2Lisecorrectly rounded: the 64-bit mantissa0xadf85458a2bb4a9ahas a remainder of 0.687, so it rounds up.offsetinApi.hppand in the definition.New findings
Blocking
None.
Non-blocking
OverflowToInfinity,OverflowScaled,FinishExpScaleandUnderflowScaled(core/libs/prx/libc/src/MathExact.cpp:70-140) are never called.ExpScaled(:160-207: infinitex, infinite multiplier,x == 0,x == 1) cannot be reached, because_LExp_nid_postfixhandles those cases before it callsExpScaled.-Wunused-functionis off.ScaleExtendeddepend on the shift.rightShift >= 65, it stores+0in every rounding mode (:105-108), as measured.rightShiftis 64 or less, it keeps the sign (a negative product that rounds to zero stores-0) and rounds up to±denorm_minin the directed modes (:110-127).denorm_min.x == 0, nonzero-offset path.ScaleExtendedraises no flag, and:611-616restore the entry flags, so a subnormal result that lost bits there raises noFE_INEXACT, unlike theExpScaledpath.core/libs/tests/GuestMathExact.cpp:11adds#include <limits>, which line 13 already has.docs/dev/debt/libc.md:162opens with what matches. CONTRIBUTING asks entries to state the gap first: the unverified titlelibc.prx, console and signaling NaNs, theshort/64-bit offset difference, and the unnamed coefficient source.Not verified
libc.prx, and there was no console run. The agreement with the module comes from BunnyFl's report, which the tests encode.