feat(libc): implement _Erfc, _FErfc and _LErfc Dinkumware math helpers #12365
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/12365"
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 PrivacyOnion on 2026-10-28T11:02:04Z.
What
Implement the PS5 3.20 Dinkumware sub-range complementary error function helpers
_Erfc,_FErfc, and_LErfcinlibc(core/libs/prx/libc/include/Api.hpp,core/libs/prx/libc/src/DinkumwareInternals.cpp), retaining the verbatim P.J. Plauger copyright and licence notice from pinned Dinkumwarexxxerfc.hat the top ofDinkumwareInternals.cpp. Rational approximations evaluateexp(-x * x) * NPOLY(x) / DPOLY(x)with_Exp_nid_postfix,_FExp_nid_postfix, andexpl_nid_postfix, using coefficient tables derived fromxxxerfc.hand propagating NaN on invalid inputs. Provenance: names and NIDs follow the PS5 3.20 export inventorylibSceLibcInternal.c.Record technical debt in
docs/dev/debt/libc.md: contract preconditions restrict inputs to precision sub-ranges ([1.523, 5.84]for double,[1.523, 3.82]for float,[1.523, 6.45]for long double) with caller dispatch performing sign reflection and cutoff handling, note out-of-range evaluation matching 13.42 export measurements, and record unverified console signatures, results, exceptions, and non-FMA ULP deltas. Unit tests incore/libs/tests/GuestDinkumwareInternals.cppcover in-range sub-domain inputs (1.523,3.0), pin unfused polynomial evaluation, and check quiet-NaN propagation.Sequencing: The branch is based on
maindirectly and shares insertion sites with #12363 and #12364 (Api.hpp,DinkumwareInternals.cpp,GuestDinkumwareInternals.cpp, anddocs/dev/debt/libc.md). Coordinated with #11752 on NID exports.Tested
Windows 11 x86_64, ran guest_dinkumware_internals unit test verifying in-domain sub-range rational evaluation, unfused polynomial evaluation, and quiet-NaN propagation for _Erfc, _FErfc, and _LErfc.
Ran locally on
2fd4997231: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 rooklz on 2026-10-28T13:22:49Z.
Review: COMMENTED
macOS check at head
5c11861612(basemain3a40945e62; merges cleanly onto750cf6cf48)Forwarding
_Erfc/_FErfc/_LErfcto the existingerfc/erfcf/erfclexports looks right to me. The NIDs matchtools/nid_names.pyhashing of the three names (aQURHgjHo-w,qwR1gtp-WS0,PG4HVe4X+Oc). The debt entry says correctly that console behaviour is unverified.check_conventions.py --base upstream/main: passes. Relinker-only build + ctest on macOS: 378/378.pr_overlap.py: no conflicting files or same exports among the 153 comparable PRs._Rint): the body lists #12364 under Depends on, but this branch is not stacked on it. It is based onmaindirectly. Both PRs append at the same place inDinkumwareInternals.cpp,GuestDinkumwareInternals.cppanddebt/libc.md, sogit merge-treereports content conflicts in all three files. They are additive and easy to resolve, but whichever lands second needs a rebase. "Depends on: none, conflicts textually with #12364" would describe it more accurately.erfc/erfcfare computed. Because these helpers forward, they pick up that change automatically. That's another reason for the tests to compare againsterfc*_nid_postfixrather thanstd::erfc.Tests don't discriminate. I built the three bodies as a separate TU with
sysv_abiand ran the PR's fourRequirelines unchanged, using Apple clang -O2 on x86_64 (Rosetta) and on arm64. All of these mutants pass the PR's tests:1 - erf(x)for all three (loses the tail)_LErfc→erfc_nid_postfix((double)x)(narrowing)_LErfc→x == 0 ? 1 : 0_Erfc→x == 0 ? 1 : x > 0 ? 0 : 20, ±inf are fixed points that any erfc-shaped function hits exactly. A tail argument, compared to the forwarded export, kills all four:
(
erfc(27)is subnormal in double (0x1.9e0fp-1058, low precision) but a full-precision normal in x87 extended (0xc.f07866fc1d3afd4p-1061), so on x86_64 the third line also catches narrowing to double.) Theerfc*_nid_postfixdeclarations are already inApi.hpp, which the test includes.I didn't build the full
guest_dinkumware_internals_teststarget, because the libc submodules aren't available here. The harness uses the PR's code and test lines verbatim.Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-28T14:34:11Z.
Review: COMMENTED
macOS re-check at head
cd0317aab9(merges cleanly ontomain446bd8776c)My iter109 point no longer applies: the helpers stopped forwarding to
erfc*and now evaluate the sub-range rational form, so the tail lines I posted then don't fit. I checked the new head again.check_conventions.py --base upstream/main: passes.git merge-treeagainst446bd8776c: clean.DinkumwareInternals.cppunchanged with Apple clang-O2 -ffp-contract=offfor x86_64 (Rosetta), linked it to stubs for the imports it doesn't need here, and ran the PR's ten newRequirelines verbatim. All of them pass. (On arm64_LErfc finitefails becauselong doubleis 64-bit there. That's expected, and the libc lane doesn't target it.)isnan(y)early return_Erfc special,_FErfc special)_LErfcthrough doublestd::exp_LErfc finite)_Erfcasstd::exp(z) * yinstead of_Exp(&z, y, 0)_Erfc 3)EvaluatePolynomialwithstd::fmaThe tests don't pin the unfused choice that the debt entry documents. Going by @BunnyFl's numbers, the firmware is the FMA build, so a fused mutant agrees with the 13.42 table to the digits the tests compare. Non-blocking: if unfused evaluation is meant to be kept, a bit-exact input from @BunnyFl's differential run where the two disagree would lock it in.
Provenance question for the maintainers. The coefficient tables are taken verbatim from
xxxerfc.h. It is a file from the Dinkumware C library shipped in Embarcadero's C++Builder RTL source, in a third-party GitHub repo with no licence (bergsteiger/realwork; the header starts/* xxxerfc.h -- common _[FL]Erfc functionality */). There is precedent for citing Dinkumware headers: #9631 (merged) and the_Thrd_sleep/_Fetch_*debt entries cite VC12 CRT sources. But those cite them for types and semantics. This PR copies a data table that was redistributed without a licence. REVERSE_ENGINEERING.md asks contributors to "rebuild data tables from a public source when one exists". I can't tell whether these coefficients have a public origin (for example a published minimax fit), so I'm asking @boykopovar / the maintainers to say whether this source is acceptable before merging. I'm not blocking on it.AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T16:53:13Z.
Review: CHANGES_REQUESTED
Tests run and reviewed — build fails on Linux, changes requested
The new test does not build on Linux with libstdc++ (
std::fabsl,GuestDinkumwareInternals.cpp:411), and CI does not catch it because thelinuxjob is skipped on this PR.What I ran
97272c1877merged onto currentmain(merges cleanly)Results
git diff --checkguest_dinkumware_internals_testsstd::fabsinstead ofstd::fabsl(scratch copy)Build error
Negative control
Run on the scratch copy that has the
std::fabsfix, new tests kept, code under test changed:std::fmainEvaluatePolynomial_Erfc 1.523 unfused_LErfcpolynomial by 1.0001_LErfc finite_FErfc_FErfc specialSo the new assertions do pin the cases they name.
Findings
Blocking
std::fabsldoes not exist in libstdc++ (GuestDinkumwareInternals.cpp:L411)<cmath>in C++ providesstd::fabs(long double);fabslis only the C name. The Windows and macOS libraries happen to declarestd::fabsl, which is probably why those runs pass. Thelinuxjob is skipped on this PR, so nothing in CI compiles this file with libstdc++. Suggested change on that line (both occurrences):Non-blocking
main, not stacked on it. #12363, #12364 and #12365 conflict textually indocs/dev/debt/libc.md,GuestDinkumwareInternals.cppandcore/libs/prx/libc/include/Api.hpp(I confirmed #12365 onto #12363 conflicts). Whichever lands second needs a rebase; saying so in the body would be enough.Sources
GuestDinkumwareInternals.cpp:L411at97272c1877xxxerfc.h, so that one is resolved.Not verified
_Erfcrecorded in the debt entry. My port substitutedstd::exp(z) * yfor the real_Exppath, so my ULP figures are not comparable and I am not treating them as a finding.To get this to a good state
Replace
std::fabslwithstd::fabsand mention the merge order in the body. I will re-run the same commands on the new head.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T17:28:02Z.
Review: APPROVED
Reviewed — looks good\n\nThe Linux portability issue is fixed with std::fabs, and the exact-head Linux/GCC target build and guest_dinkumware_internals CTest passed locally. The Windows hosted job passes; other pending platform jobs and console behavior remain unverified. No blocking issue found.\n\nReviewed head against base ; no blocking finding. This approval covers this exact diff only.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-28T17:29:07Z.
Review: APPROVED
Reviewed — looks good
The Linux portability issue is fixed with std::fabs, and the exact-head Linux/GCC target build and guest_dinkumware_internals CTest passed locally. The Windows hosted job passes; other pending platform jobs and console behavior remain unverified. No blocking issue found.
Reviewed head 9d907a63f8bd62843cdcb2d3e5bb89ed53a08743 against base
7fea87746b; no blocking finding. This approval covers this exact diff.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T05:25:40Z.
Review: CHANGES_REQUESTED
Re-checked — changes requested
The author resolved the provenance, sequencing, and Linux std::fabs feedback, but the implementation, tests, and debt entry still treat a range-reduced rational helper as if its valid input domain were all non-negative values.
What I ran
52c09d567d.Results
Negative control
Findings
Blocking
The cited Dinkumware header labels these coefficient tables as sub-range approximations: all start at x = 1.523, with upper bounds 3.82 for float, 5.84 for double, and 6.45 for long double. It defines Erf_small = 1.523 and each precision's Erf_one cutoff. The implementation here evaluates the rational at any input, without the surrounding dispatcher's small-input/sign handling or large-input cutoff, while the debt entry says the helpers accept x >= 0.
That mismatch is observable in the new test: _Erfc_nid_postfix(0.0) is pinned to 0.99999422623189549, although erfc(0) is exactly 1. The test currently blesses a below-range approximation as a valid result. The infinity-to-NaN assertions likewise test outside the table's finite interval.
Please clarify which contract this exported helper is intended to implement:
Non-blocking
Sources
Not verified
To get this to a good state
Resolve the helper's actual input contract as above, update the assertions and debt wording to match it, then rerun the focused test on the resulting head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-29T09:34:07Z.
Review: APPROVED
Re-checked — approved
The updated contract resolves my range-domain finding. The debt entry now identifies the precision-specific sub-ranges as preconditions and explains that the caller handles sign reflection and cutoffs; the tests no longer treat below-range inputs or infinities as valid outputs. The implementation matches the cited range-reduced Dinkumware helper, and the focused finite assertions remain within the documented intervals.
What I checked
c044ff4f0d15dc5d68cd5ad33f7fc08f145566b0.xxxerfc.hreference and the earlier review findings.mainaaccd0ba39b7d40fdbf19664575a7e567d8542f7: the merge preview is clean, and current main has no changes to the PR's implementation, API, tests or debt paths since the merge base.Test evidence
The author reports
guest_dinkumware_internalspassing on this exact head on Windows. I did not run the test locally or verify behavior on console hardware. Console signatures/results and exception details remain appropriately listed as unverified.No blocking issue remains in this diff. Approval covers this exact head.
Pull request closed