fix(libc): compute acosf with FreeBSD msun's algorithm, as the console's libc.prx does #9668
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/9668"
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 rooklz on 2026-10-22T07:36:31Z.
What
acosfreturned the host library'sacosffor|x| <= 1, so its last bit depended on the host. Over the 2,130,706,434 floats in[-1, 1]the console's algorithm (below) differs from glibc 2.39'sacosfon 1,046,180 of them and from Apple's x86-64 libm on 17,127,560. On macOSacosf(0.5f)is0x3f860a91, one below the0x3f860a92thatguest_mathexpects, so the test fails there.The console's
acosfis FreeBSD msun'se_acosf.c. I checked thelibc.prxbundled with PPSA28319 (catalog zip sha2566426468091b4…, the binary thesinhf,asinlandatanhfdebt entries cite): exportQI-x0SL8jhwat0x40980has msun's branch points (0x3f800000,0x3effffff,0x32800000), its constantspS0..qS1(0x3e2aaa75,0xbd2f13ba,0xbc0dd36b,0xbf34e5ae),pio2_hi0x3fc90fdaandpi0x40490fda,pio2_lo0x33a22168read from.data(msun'svolatile), the0xfffff000mask onsqrtf(z), and the same order of single-precision operations (vmulss/vaddss/vdivss/vsqrtss, no FMA).acosfnow computes|x| <= 1the way that code does: the four ranges, the rationalAcosfRational, andpio2_lokeptvolatileso it's read at run time as there. The build targets baseline x86-64, so nothing gets contracted into FMA. The NaN and|x| > 1paths are unchanged.The console's code for
|x| > 1and NaN also storesEDOMand raisesFE_INVALIDbefore returning(x - x) / (x - x), whichmaindoesn't do (#3688 chose to leaveerrnoalone from FreeBSD's source). That is the gap the new debt entry records. It isn't changed here, and the details are on #9643, which edits those lines. @JJenkx measured it on this PR (comment): the bundledlibc.prxof eleven titles, FMA-contracted builds included, storesEDOMfromacosffor ±2, infinity and a quiet NaN and leaveserrnoalone for 1 and 0.5, so the store is not build-specific.Tested
main61fc572ceewith the open macOS build PRs:guest_mathstops atacosf(0.5f)without this PR and passes 3 of 3 with it, together with #10345 and the test fix in #10907.guest_mathpasses. Withmain'sMath.cppand the new checks it aborts. The three checks for inputs inside the ranges (0x3d58d031,0x3f00b99a,0xbf00b99a) are ones where glibc'sacosfis one off, so they fail on Linux too, not only on macOS.ci/macos-series-2at53db0badbwith #7524, the code of #7937 and the libSceSsl change of #7332:guest_mathpasses 3 of 3. It also needsconfigure_macos_unwind(#8311) onguest_math_testsfor itsrand_r(nullptr)case, as noted on #1.e_acosf.cwith the hostacosfover every float in[-1, 1]on both hosts.check_conventions.pypasses.pr_overlap.pyreports #8658, whose macOS port computesacosfin double under__APPLE__on the same lines; this replaces that with the console's algorithm on every host. #9643 changes the NaN and|x| > 1lines above these and its own test function; the two don't overlap. #9771 (asinf) adds its debt entry on the line after this one's; @JJenkx and I agreed that either landing order works and that whichever lands second keeps both entries,acosfthenasinf.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-22T09:03:54Z.
Review: DISMISSED
The implementation has a strong evidence trail: the reported console disassembly matches the FreeBSD msun path, targeted bit-pattern regressions cover the main branches and endpoints, and the Linux/macOS reports exercise the host-specific discrepancy. Exact-head
git diff --checkandpython3 tools/check_conventions.py --base upstream/main --head refs/remotes/review/9668both pass; all hosted checks pass, including Linux/Windows/macOS relinker and ASan jobs. I did not rerun the runtime fixture locally.There are two follow-ups before this can proceed:
acosfimplementation. The two changes have different goals, but whichever merges second needs to preserve/rebase the other change. Please coordinate an order with that author and update the dependency/overlap notes accordingly. The separate #9643 domain-flag change appears distinct as described.I’ve noticed that holding this PR because of #5239 isn’t necessary. It’s fine for PRs to keep coming in; they’ll be handled in due course. Please disregard my earlier request to hold or defer it.
Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T09:24:43Z.
Windows check of a2924348a8, with an exhaustive comparison against titles' own
libc.prxrun natively. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 r7 (UCRT), Ninja, Release.guest_mathpasses three times out of three with this PR.libc.prxof three titles into a scratch harness (it maps the ELF and applies its relocations). Then I called itsacosf(QI-x0SL8jhw) and this PR'sacosf_nid_postfixfrom the Windows build on all 2^32 inputs and compared the result bits:libc.prxacosfd6eafa54d0(UCRTacosf)vfmadd0x3c439be0:0x3fc788a1there,0x3fc788a0here)Outside [-1, 1], including the NaNs, all three agree with this PR bit for bit. Inside it, the PR's msun port matches the two non-FMA copies exactly, which confirms the reading from PPSA28319 on two more titles.
But PPSA13934's copy is a different build: its
acosfis FMA-contracted. So the last bit ofacosfdepends on whichlibc.prxa title ships, and no single implementation matches every title. Maybe worth a sentence in the new debt entry. The PR is still a large improvement on Windows for all three titles.One more observation: with
__errorsupplied to the harness, PPSA13934's copy did not seterrnoforacosf(2.0f)(I did not try the other two). If the console storesEDOMthere, it doesn't go through the__errorimport, so I couldn't confirm theEDOMpart this way.Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T11:31:00Z.
Thanks, that is the comparison I could not run. I added a sentence to the debt entry (now
c771f45ff2): titles ship different builds of thatlibc.prx, PPSA13934's is FMA-contracted and differs by 1 ulp on 129,170 inputs, so no single port matches every title's last bit.On
errno: what I read is PPSA28319's helper at0x11740storing through the__errorimport. Your PPSA13934 copy not reaching__errorforacosf(2.0f)means that build differs there too, or stores it another way. I have noted on #9643 that theEDOMreading is confirmed for PPSA28319's build only.Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T12:11:35Z.
On the two points:
acosffromlibSceLibcInternalwith one call site (0xBD698in its eboot; the relinker registry classes itR_X86_64_GLOB_DAT), so it resolves to this function rather than to a bundledlibc.prx. I can't show a blocker or a log line, because a last-bit difference doesn't stop anything, and I won't claim one. So this stays a correctness change withUnblocks: none, and under the #5239 direction I'm fine holding it until the queue drops or a maintainer wants it; I'd rather hold than close, since the evidence trail is in the thread.acosfhunk (mine replaces the__APPLE__double computation on the same lines). The description's overlap note says the same.Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:34:38Z.
Rebased onto current
main(8b3e39832a): head4951eee5ed, same diff.Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:41:23Z.
While reviewing #9771, I compared the current heads. Its
asinfdebt entry and this PR'sacosfentry produce an adjacent-hunk conflict indocs/dev/debt/libc.md; both entries can be retained.CONTRIBUTING.mdasks authors to agree merge order for overlapping PRs and record a dependency when one must land first. Could you coordinate with the #9771 author whether either order is acceptable, and note that agreement on the PR checklist? No functional conflict is apparent from the pairwise merge.