feat(libSceLibcInternal): implement __kernel_rem_pio2 #14158
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/14158"
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 CodingBreadd on 2026-10-31T21:25:26Z.
What
Implements
__kernel_rem_pio2(libSceLibcInternal, NIDGLNDoAYNlLQ, which hashes from that name). Until now the export only threw throughNotImplemented_nid_no_patch.k_rem_pio2.cin the newRemPio2.cpp, with themath_private.hprototypeint (double*, double*, int, int, int).LDBL_MAX_EXPis 16384 (float.h). The table was generated from the upstream file, not copied by hand.RemPio2.cppis built with-ffp-contract=off, as libc is, so FMA hosts give the same results.int32_t, overflow its exponent arithmetic, or read an unset term. Here they throw instead:20 - jke0past the tablexoryMath.cpphas a private copy specialised to float, one chunk and precision 0. It lives in a different library, so this PR leaves it alone.Tested
Windows 11, WinLibs GCC 15.2.0, Release.
ctest -R guest_kernel_rem_pio2passes in about 1 s.Linux (Arch, GCC 16.2.1, Release), head
1e9f8de014onmain4f41f4c375:ctest -R '^guest_kernel_rem_pio2$'passes. The rebase only resolved thedocs/dev/debt/libSceLibcInternal.mdconflict with the new_ZNSt4_PadD1Eventry; both entries are kept.The test (
GuestKernelRemPio2.cpp) runs 52 known-answer rows, covering all four precisions, 1 to 5 chunks, and exponents from -199 to 16360:It checks four things for each row:
xand a nullyNegative control for the null
ycase: with|| y == nullptrremoved from the check, the test fails with a segfault.How the expected values were produced and checked:
-ffp-contract=off.ih1 and 2, the carry path,q0of 0, 1 and 2, the 24-bit split and the zero-term chop.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.pyfinds no PR that shares these exports. It could not compare #5021, #11750 and #11751 because they conflict with main, so I compared them by hand: none declaresGLNDoAYNlLQ, and their CMake and debt edits are in other regions of those files. #13778 (__kernel_sin/__kernel_cos) touches the same files in separate hunks.Landing order: this PR goes before #13782, #14212 and #14837. Each of them changes
core/libs/prx/libSceLibcInternal/CMakeLists.txtordocs/dev/debt/libSceLibcInternal.mdnext to this PR's lines, and each already conflicts withmainin those files, so it needs a rebase anyway. When it rebases onto this PR, it keeps both sides' added lines. I have proposed this order on each of the three PRs.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 MarcZX10 on 2026-10-31T21:32:39Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The new input guard still permits signed integer overflow when
e0is nearINT_MAX, before the routine can reject an exponent beyond the 2/π table.What I ran
Reviewed PR head
18c9fad67c03147ad9c96d5689163c969298cca1against its base422367d61a4dd1264df0cb71a0d3cab9ba5fd5d6. The live merge preview is clean against currentmaindaa8d64e456287fc97e1c4cc3920ffdb9cfa4025. Environment: Linux x86-64, Python 3.11.2, GCC 12.2.0. I did not build or run CTest. Commands:Results
GLNDoAYNlLQ, matching the removed export.guest_kernel_rem_pio2passes on Windows; I did not independently run it.Relevant raw output
Findings
Blocking
At
RemPio2.cpp:128,q0 = e0 - 24 * (jv + 1)multiplies as signedint32_t. Fore0 = INT_MAXon the supported 32-bit-inttargets,jvis 89,478,485 and24 * (jv + 1)is 2,147,483,664, exceedingINT_MAXbefore the laterIpio2Attable check can throw. This is undefined behavior on an out-of-range exponent, despite the implementation and tests intending invalid table inputs to throw. Compute the expression in a checked wider type and reject out-of-table exponents before narrowing; add a regression usingstd::numeric_limits<int>::max()that asserts a defined exception.Non-blocking
None found.
Sources
Not verified
I did not build or run the C++ test locally. Windows CI was queued; Linux and specialized jobs were skipped. The PR author also states that the console module and PS5 ABI behavior have not been verified.
To get this to a good state
Use checked wide arithmetic (or reject excessive positive
e0before this expression), add theINT_MAXregression, and rerun the focused Windows test. I will repeat the relevant checks on the new head.Imported from boykopovar/AnyPS5. Originally posted by CnRJay on 2026-11-01T19:49:35Z.
Review: COMMENTED
Withdrawn. This review applied a title-ID/log requirement that is not in CONTRIBUTING (
Unblocks: noneis allowed), and it contained no code review.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-03T12:41:00Z.
Review: CHANGES_REQUESTED
Reviewed — changes requested
The reported overflow fix is sound, but the current head still has unresolved merge conflicts with other open PRs and no recorded landing order as CONTRIBUTING requires.
What I ran
Reviewed all six changed files at head
9c3f189e8c2eef528a275d5785281d9fa8472f3dagainst base4f41f4c375f5abb487dbd184fbc150f31994d762. The stack checker previews this head cleanly against current main. Environment: Debian 12, Linux x86_64, Python 3.11.2, Git 2.39.5, GCC 12.2.0. I did not build or run CTest.I also fetched the pinned FreeBSD 11.4 source named in the PR and compared the
ipio2,PIo2andinit_jkconstants with this head. The prior review's signed-overflow issue is addressed: the table-bound check now rejects a large positivee0beforeq0computes24 * (jv + 1); the lower-bound guard leaves room for the laterq0decrements.Results
PIo2values and fourinit_jkvalues match.Full precheck table and relevant raw output
The direct ABI audit and NID lookup returned:
The removed export is the stub this PR replaces; the new declaration and definition compute the same NID. The direct test-quality helper flags the test path because CMake names it through
${LIBS_INCLUDE_DIR}/tests/GuestKernelRemPio2.cpp; inspection confirms the registration at lines 98–103 of the changed CMake file.The pairwise merge previews returned:
Negative control
Findings
Blocking
Depends on. Please coordinate the order with the affected authors and record it in the relevant PR descriptions before approval. This does not imply a rebase is needed solely because a branch is behind.Non-blocking
xand nully, but the new test only calls the function with nullx. Add a null-ycase or narrow the Tested claim that the null-pointer case is covered.Sources
Not verified
I did not run a local build, CTest, the PR's known-answer suite, the reported independent high-precision calculation, a negative control, or a PS5 hardware test. The hosted Windows check passed; Linux and specialized jobs were skipped. The selector's actual console signature and behavior are not established by this PR.
To get this to a good state
Agree and document the landing order for the conflicting open PRs; add the null-
yregression or correct the coverage claim. I will refresh the heads and rerun the no-build checks on the resulting head.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T12:58:25Z.
Review: APPROVED
Reviewed — approved
The PR merges and builds cleanly. Its known-answer test passes in the normal build and in a standalone build with UBSan and ASan. Mutating the carry path, the table-bound guard (caught only under UBSan) and the null-
ycheck makes the test fail, and nothing is blocking.What I ran
Head
e9ad50598506713a7988df2b3460d47f979205bd,main4f41f4c375f5abb487dbd184fbc150f31994d762, merged tree4c44c68ac9ac458239e9ab17c9c8a5b366d0ee94. Machine: Debian 13 VM, 32 vCPU (Intel Xeon Gold 6136), GCC 14.2.0, Release, Ninja.Results
git diff --checkcheck_conventions.py__kernel_rem_pio2hashes toGLNDoAYNlLQ, the NID of the removed stublibsOKguest_kernel_rem_pio2main.guest_net_ctlwas skipped for an unexpected reason, and it is skipped the same way onmain.__kernel_rem_pio2: ok__kernel_rem_pio2: okgit status --porcelainempty, test passes againUBSan with the table guard deleted
Negative control
If the production files are restored from
main, the function and the test registration both disappear, so the test cannot even be built. I mutated the implementation instead:Carry correction removed: I replaced
z -= std::scalbn(one, q0)in theih == 2carry path withz -= 0.0.guest_kernel_rem_pio2then aborts ("Subprocess aborted"), so the known-answer rows reach that branch.Table guard removed: I deleted the
jv + jkguard added after MarcZX10's first review, so theINT_MAXinput reachesq0 = e0 - 24 * (jv + 1)again.Ipio2Atcheck throwsout_of_rangeanyway.INT_MAXregression reports the signed overflow at line 128 (the guard's line, now holding theq0computation).So the regression detects the overflow only in a sanitizer build (see Non-blocking). This confirms MarcZX10's original finding and shows the fix removes it.
Null-
ycheck removed: I removed|| y == nullptr. The test segfaults, as the author reported.Findings
Blocking
None.
Non-blocking
INT_MAXregression proves nothing without a sanitizer. At RemPio2.cpp:128-129, removing the guard still passes in a normal build, becauseIpio2Atthrows the same exception type afterwards. The fix itself is correct, and a UBSan build of the test confirms it. If a stronger check is wanted, the guard could throw a message the test can tell apart fromIpio2At's, though the test only compares exception types today. Optional.jz < 1throw cannot be reached. Atcore/libs/prx/libSceLibcInternal/RemPio2.cpp:243,jzstarts atjk, which is 6 for precision 3, and only grows on recompute. The zero-chop loop runs only when someiq[i]withi >= jkis non-zero, so it stops atjz >= jk. The check is harmless. The "read an unset term" case listed in the description and the debt entry is not an input that can occur. It is fine to keep as a defensive check.core/libs/tests/GuestKernelRemPio2.cpp:30and:31are the same row (prec 1, e0 826), so the suite has 52 distinct rows, not 53. Drop one row or replace it with another case.Not verified
PIo2orInitJkwith the FreeBSD source, because I had no network. MarcZX10 reports that all of them match. I did read the algorithm against the structure ofk_rem_pio2.c(f/q setup, recompute, chop, theprecswitch) and found no divergence apart from the added throws.libSceLibcInternaluses this code, signature and table is unverified, as the debt entry states.-ffp-contract=offand thePATHtest environment).Depends online, and I did not check the other PRs.Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T13:12:08Z.
Review: APPROVED
Re-checked — approved
The only new commit,
1e9f8de014, removes the duplicatedprec 1, e0 826row from the test. That resolves the open finding from my last review. The production code is unchanged, and the checks and the focused test pass.What changed since my last review
1e9f8de014deletes one line fromcore/libs/tests/GuestKernelRemPio2.cpp: the second copy of the{1, 826, 3, 5, ...}row. The suite now has 52 rows. I counted them, and the description also says 52.core/libs/prxchanged betweene9ad505985(my last review) and1e9f8de014.INT_MAXregression only detects the overflow under UBSan, and thejz < 1throw cannot be reached). They are keeping both as defensive checks. That is fine: I had marked both as optional.What I ran
Head
1e9f8de014e80e88e2afe035ee7904be1969b488,main4f41f4c375f5abb487dbd184fbc150f31994d762, merged tree1b96cf7a858db9975520d86cdfd4a280ba534273. Machine: Debian 13 VM, GCC 14.2.0, Release, Ninja.git diff --checkcheck_conventions.pylibsOKguest_kernel_rem_pio2main.guest_net_ctlwas skipped for an unexpected reason, and that skip is the same onmain.Negative control: I did not run it again. This push only removes a duplicate test row, and the production files are byte-identical to the ones I mutation-tested last time. Those results still hold: removing the carry correction aborts the test, removing
|| y == nullptrmakes it segfault, and removing the table guard is caught under UBSan.Previous findings
1e9f8de014removes the secondprec 1, e0 826row, and 52 rows remain.INT_MAXregression only proves the fix under a sanitizer (non-blocking,core/libs/prx/libSceLibcInternal/RemPio2.cpp:128): Still open. The author chose to keep it as it is. It was optional and does not block.jz < 1throw cannot be reached (non-blocking,core/libs/prx/libSceLibcInternal/RemPio2.cpp:243): Still open. The author keeps it as a defensive check. It is harmless and does not block.New findings
Blocking
None.
Non-blocking
None.
Not verified
PIo2/InitJkconstants with FreeBSD myself, because I had no network. MarcZX10 reports that they match.1e9f8de014(Arch, GCC 16.2.1) is reported by the author. I did not run it.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-05T00:25:13Z.
Review: COMMENTED
Re-checked — new head, no blocking findings
Since my review of
9c3f189e8c2ethe PR adds two test-only commits and now records its landing order, so both points of my earlier change request are addressed; nothing was built or run here.What I ran
Head
1e9f8de014e80e88e2afe035ee7904be1969b488, current base4f41f4c375f5abb487dbd184fbc150f31994d762. I compared this head with9c3f189e8c2eef528a275d5785281d9fa8472f3d, the head of my earlier full review: only the guest test file changed, in two commits. The implementation, CMake registration, exports and debt entry are unchanged, so my earlier read of them (including the FreeBSD constant comparison and the overflow fix) still applies. Environment: Debian 12, Linux x86_64. No AnyPS5 build was run.Results
mainprec 1, e0 826row remainsguest_kernel_rem_pio2Precheck report for this head, unedited table
git diff --check 4f41f4c375f5abb487dbd184fbc150f31994d762...1e9f8de014e80e88e2afe035ee7904be1969b488/marc/DeusMachina_0x1914/.venv/bin/python3 tools/check_conventions.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 --head 1e9f8de014e80e88e2afe035ee7904be1969b488 --title feat(libSceLibcInternal): implement __kernel_rem_pio2 --body /tmp/anyps5-pr-precheck/final/14158/pr-body.md/marc/DeusMachina_0x1914/.venv/bin/python3 /marc/AnyPS5/tools/pr_abi_audit.py --base 4f41f4c375f5abb487dbd184fbc150f31994d762 --head 1e9f8de014e80e88e2afe035ee7904be1969b488 --json/marc/DeusMachina_0x1914/.venv/bin/python3 /marc/AnyPS5/tools/pr_stack_check.py 14158 --repo boykopovar/AnyPS5 --output-dir /tmp/anyps5-pr-precheck/final/14158/stack/marc/DeusMachina_0x1914/.venv/bin/python3 /marc/AnyPS5/tools/pr_test_quality.py --root /tmp/anyps5-pr-precheck/final/14158/head-1e9f8de014e8-47526a13 --json core/libs/prx/libSceLibcInternal/CMakeLists.txt core/libs/prx/libSceLibcInternal/InventoryExports6.cpp core/libs/prx/libSceLibcInternal/RemPio2.cpp core/libs/prx/libSceLibcInternal/include/Api.hpp core/libs/tests/GuestKernelRemPio2.cpp docs/dev/debt/libSceLibcInternal.mdNegative control
Not run. The new null-output assertion is satisfied by the guard at line 114 of the implementation; I did not mutate it.
Findings
Blocking
None.
Non-blocking
mainin those files.Sources
github.com/boykopovar/AnyPS5@1e9f8de014/core/libs/prx/libSceLibcInternal/RemPio2.cpp (L114)github.com/boykopovar/AnyPS5@1e9f8de014/core/libs/prx/libSceLibcInternal/RemPio2.cpp (L117)Not verified
guest_kernel_rem_pio2result on any platform. A CI comment on this head reports a passing GPU run with 0 new failures (19 failures that also occur onmainor are unstable there); that is another contributor's report, not my run.To get this to a good state
guest_kernel_rem_pio2output on Linux for this head.