fix(libc): compute acosf with FreeBSD msun's algorithm, as the console's libc.prx does #9668

Merged
rooklz merged 1 commit from upstream-pr/9668 into main 2026-10-25 23:14:55 +00:00
rooklz commented 2026-10-22 07:36:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T07:36:31Z.


What

acosf returned the host library's acosf for |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's acosf on 1,046,180 of them and from Apple's x86-64 libm on 17,127,560. On macOS acosf(0.5f) is 0x3f860a91, one below the 0x3f860a92 that guest_math expects, so the test fails there.

The console's acosf is FreeBSD msun's e_acosf.c. I checked the libc.prx bundled with PPSA28319 (catalog zip sha256 6426468091b4…, the binary the sinhf, asinl and atanhf debt entries cite): export QI-x0SL8jhw at 0x40980 has msun's branch points (0x3f800000, 0x3effffff, 0x32800000), its constants pS0..qS1 (0x3e2aaa75, 0xbd2f13ba, 0xbc0dd36b, 0xbf34e5ae), pio2_hi 0x3fc90fda and pi 0x40490fda, pio2_lo 0x33a22168 read from .data (msun's volatile), the 0xfffff000 mask on sqrtf(z), and the same order of single-precision operations (vmulss/vaddss/vdivss/vsqrtss, no FMA).

acosf now computes |x| <= 1 the way that code does: the four ranges, the rational AcosfRational, and pio2_lo kept volatile so it's read at run time as there. The build targets baseline x86-64, so nothing gets contracted into FMA. The NaN and |x| > 1 paths are unchanged.

The console's code for |x| > 1 and NaN also stores EDOM and raises FE_INVALID before returning (x - x) / (x - x), which main doesn't do (#3688 chose to leave errno alone 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 bundled libc.prx of eleven titles, FMA-contracted builds included, stores EDOM from acosf for ±2, infinity and a quiet NaN and leaves errno alone for 1 and 0.5, so the store is not build-specific.

Tested

  • macOS 27.0.1, Apple M4 Pro, x86-64 under Rosetta 2, by @imellag on main 61fc572cee with the open macOS build PRs: guest_math stops at acosf(0.5f) without this PR and passes 3 of 3 with it, together with #10345 and the test fix in #10907.
  • Linux x86-64 container (Ubuntu 24.04, GCC 13.3, glibc 2.39, Release): guest_math passes. With main's Math.cpp and the new checks it aborts. The three checks for inputs inside the ranges (0x3d58d031, 0x3f00b99a, 0xbf00b99a) are ones where glibc's acosf is one off, so they fail on Linux too, not only on macOS.
  • macOS 26.2, Apple M5, Apple clang 21, x86-64 under Rosetta 2, on @mugurc's ci/macos-series-2 at 53db0badb with #7524, the code of #7937 and the libSceSsl change of #7332: guest_math passes 3 of 3. It also needs configure_macos_unwind (#8311) on guest_math_tests for its rand_r(nullptr) case, as noted on #1.
  • The counts above come from comparing a standalone build of e_acosf.c with the host acosf over every float in [-1, 1] on both hosts.
  • check_conventions.py passes. pr_overlap.py reports #8658, whose macOS port computes acosf in double under __APPLE__ on the same lines; this replaces that with the console's algorithm on every host. #9643 changes the NaN and |x| > 1 lines 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, acosf then asinf.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4963811679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T07:36:31Z. --- ### What `acosf` returned the host library's `acosf` for `|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's `acosf` on 1,046,180 of them and from Apple's x86-64 libm on 17,127,560. On macOS `acosf(0.5f)` is `0x3f860a91`, one below the `0x3f860a92` that `guest_math` expects, so the test fails there. The console's `acosf` is FreeBSD msun's [`e_acosf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_acosf.c). I checked the `libc.prx` bundled with PPSA28319 (catalog zip sha256 `6426468091b4…`, the binary the `sinhf`, `asinl` and `atanhf` debt entries cite): export `QI-x0SL8jhw` at `0x40980` has msun's branch points (`0x3f800000`, `0x3effffff`, `0x32800000`), its constants `pS0`..`qS1` (`0x3e2aaa75`, `0xbd2f13ba`, `0xbc0dd36b`, `0xbf34e5ae`), `pio2_hi` `0x3fc90fda` and `pi` `0x40490fda`, `pio2_lo` `0x33a22168` read from `.data` (msun's `volatile`), the `0xfffff000` mask on `sqrtf(z)`, and the same order of single-precision operations (`vmulss`/`vaddss`/`vdivss`/`vsqrtss`, no FMA). `acosf` now computes `|x| <= 1` the way that code does: the four ranges, the rational `AcosfRational`, and `pio2_lo` kept `volatile` so it's read at run time as there. The build targets baseline x86-64, so nothing gets contracted into FMA. The NaN and `|x| > 1` paths are unchanged. The console's code for `|x| > 1` and NaN also stores `EDOM` and raises `FE_INVALID` before returning `(x - x) / (x - x)`, which `main` doesn't do (#3688 chose to leave `errno` alone 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](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6372738093)): the bundled `libc.prx` of eleven titles, FMA-contracted builds included, stores `EDOM` from `acosf` for ±2, infinity and a quiet NaN and leaves `errno` alone for 1 and 0.5, so the store is not build-specific. ### Tested - macOS 27.0.1, Apple M4 Pro, x86-64 under Rosetta 2, by @imellag on `main` `61fc572cee` with the open macOS build PRs: `guest_math` stops at `acosf(0.5f)` without this PR and passes 3 of 3 with it, together with #10345 and the test fix in #10907. - Linux x86-64 container (Ubuntu 24.04, GCC 13.3, glibc 2.39, Release): `guest_math` passes. With `main`'s `Math.cpp` and the new checks it aborts. The three checks for inputs inside the ranges (`0x3d58d031`, `0x3f00b99a`, `0xbf00b99a`) are ones where glibc's `acosf` is one off, so they fail on Linux too, not only on macOS. - macOS 26.2, Apple M5, Apple clang 21, x86-64 under Rosetta 2, on @mugurc's `ci/macos-series-2` at `53db0badb` with #7524, the code of #7937 and the libSceSsl change of #7332: `guest_math` passes 3 of 3. It also needs `configure_macos_unwind` (#8311) on `guest_math_tests` for its `rand_r(nullptr)` case, as noted on #1. - The counts above come from comparing a standalone build of `e_acosf.c` with the host `acosf` over every float in `[-1, 1]` on both hosts. - `check_conventions.py` passes. `pr_overlap.py` reports #8658, whose macOS port computes `acosf` in double under `__APPLE__` on the same lines; this replaces that with the console's algorithm on every host. #9643 changes the NaN and `|x| > 1` lines 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, `acosf` then `asinf`. <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
MarcZX10 commented 2026-10-22 09:03:54 +00:00 (Migrated from github.com)

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 --check and python3 tools/check_conventions.py --base upstream/main --head refs/remotes/review/9668 both 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:

  1. The overlap bot reports open #8658 touching the same acosf implementation. 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5611731471 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668#pullrequestreview-5611731471). Originally posted by [MarcZX10](https://github.com/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 --check` and `python3 tools/check_conventions.py --base upstream/main --head refs/remotes/review/9668` both 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: 1. The overlap bot reports open #8658 touching the same `acosf` implementation. 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.
JJenkx commented 2026-10-22 09:24:43 +00:00 (Migrated from github.com)

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.prx run natively. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 r7 (UCRT), Ninja, Release.

  • guest_math passes three times out of three with this PR.
  • I loaded the decrypted libc.prx of three titles into a scratch harness (it maps the ELF and applies its relocations). Then I called its acosf (QI-x0SL8jhw) and this PR's acosf_nid_postfix from the Windows build on all 2^32 inputs and compared the result bits:
Title's libc.prx acosf Base d6eafa54d0 (UCRT acosf) This PR
PPSA07923 467 bytes, no FMA 5,678,739 of the 2,130,706,434 inputs in [-1, 1] differ 0 differ
PPSA05916 466 bytes, no FMA not run 0 differ
PPSA13934 441 bytes, 11 vfmadd 5,685,011 differ 129,170 differ, each by 1 ulp (e.g. 0x3c439be0: 0x3fc788a1 there, 0x3fc788a0 here)

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 acosf is FMA-contracted. So the last bit of acosf depends on which libc.prx a 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 __error supplied to the harness, PPSA13934's copy did not set errno for acosf(2.0f) (I did not try the other two). If the console stores EDOM there, it doesn't go through the __error import, so I couldn't confirm the EDOM part this way.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337374610 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6337374610). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T09:24:43Z. --- Windows check of a2924348a8, with an exhaustive comparison against titles' own `libc.prx` run natively. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 r7 (UCRT), Ninja, Release. - `guest_math` passes three times out of three with this PR. - I loaded the decrypted `libc.prx` of three titles into a scratch harness (it maps the ELF and applies its relocations). Then I called its `acosf` (`QI-x0SL8jhw`) and this PR's `acosf_nid_postfix` from the Windows build on all 2^32 inputs and compared the result bits: | Title's `libc.prx` | `acosf` | Base d6eafa54d0 (UCRT `acosf`) | This PR | |---|---|---|---| | PPSA07923 | 467 bytes, no FMA | 5,678,739 of the 2,130,706,434 inputs in [-1, 1] differ | **0** differ | | PPSA05916 | 466 bytes, no FMA | not run | **0** differ | | PPSA13934 | 441 bytes, 11 `vfmadd` | 5,685,011 differ | 129,170 differ, each by 1 ulp (e.g. `0x3c439be0`: `0x3fc788a1` there, `0x3fc788a0` here) | 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 `acosf` is FMA-contracted. So the last bit of `acosf` depends on which `libc.prx` a 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 `__error` supplied to the harness, PPSA13934's copy did not set `errno` for `acosf(2.0f)` (I did not try the other two). If the console stores `EDOM` there, it doesn't go through the `__error` import, so I couldn't confirm the `EDOM` part this way.
rooklz commented 2026-10-22 11:31:00 +00:00 (Migrated from github.com)

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 that libc.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 at 0x11740 storing through the __error import. Your PPSA13934 copy not reaching __error for acosf(2.0f) means that build differs there too, or stores it another way. I have noted on #9643 that the EDOM reading is confirmed for PPSA28319's build only.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6339675463 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6339675463). Originally posted by [rooklz](https://github.com/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 that `libc.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 at `0x11740` storing through the `__error` import. Your PPSA13934 copy not reaching `__error` for `acosf(2.0f)` means that build differs there too, or stores it another way. I have noted on #9643 that the `EDOM` reading is confirmed for PPSA28319's build only.
rooklz commented 2026-10-22 12:11:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T12:11:35Z.


On the two points:

  1. A title that runs this code: PPSA94002 (Raptor) imports acosf from libSceLibcInternal with one call site (0xBD698 in its eboot; the relinker registry classes it R_X86_64_GLOB_DAT), so it resolves to this function rather than to a bundled libc.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 with Unblocks: 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.
  2. Merge order with #8658 is proposed on that PR: whichever lands first, the other drops its acosf hunk (mine replaces the __APPLE__ double computation on the same lines). The description's overlap note says the same.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6340417423 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6340417423). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T12:11:35Z. --- On the two points: 1. A title that runs this code: PPSA94002 (Raptor) imports `acosf` from `libSceLibcInternal` with one call site (`0xBD698` in its eboot; the relinker registry classes it `R_X86_64_GLOB_DAT`), so it resolves to this function rather than to a bundled `libc.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 with `Unblocks: 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. 2. Merge order with #8658 is proposed on that PR: whichever lands first, the other drops its `acosf` hunk (mine replaces the `__APPLE__` double computation on the same lines). The description's overlap note says the same.
rooklz commented 2026-10-22 13:34:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rooklz on 2026-10-22T13:34:38Z.


Rebased onto current main (8b3e39832a): head 4951eee5ed, same diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342081774 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6342081774). Originally posted by [rooklz](https://github.com/rooklz) on 2026-10-22T13:34:38Z. --- Rebased onto current `main` (`8b3e39832a`): head `4951eee5ed`, same diff.
MarcZX10 commented 2026-10-22 14:41:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T14:41:23Z.


While reviewing #9771, I compared the current heads. Its asinf debt entry and this PR's acosf entry produce an adjacent-hunk conflict in docs/dev/debt/libc.md; both entries can be retained. CONTRIBUTING.md asks 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6343511119 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6343511119). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T14:41:23Z. --- While reviewing #9771, I compared the current heads. Its `asinf` debt entry and this PR's `acosf` entry produce an adjacent-hunk conflict in `docs/dev/debt/libc.md`; both entries can be retained. `CONTRIBUTING.md` asks 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!9668
No description provided.