fix(libc): compute asinf with FreeBSD msun's algorithm, as titles' libc.prx does #9771

Merged
JJenkx merged 2 commits from upstream-pr/9771 into main 2026-10-27 01:58:26 +00:00
JJenkx commented 2026-10-22 14:05:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T14:05:37Z.


What

asinf returned the host's asin, so its last bit depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 5,413,834 of the floats in (-1, 1). Examples, console then main:

  • 0x3b3a82da → 0x3b3a82ea, main gives 0x3b3a82eb;
  • 0x3f0008fb → 0x3f0614f1, main gives 0x3f0614f0.

The titles' asinf (NID GZWjF-YIFFk) is FreeBSD msun's e_asinf.c. asinf now computes it the same way:

  • the three ranges;
  • the single-precision rational pS0..pS2/qS1 (AsinfRational);
  • for 0.5 <= |x| < 1, sqrt and the final step in double, as msun has them.

The build targets baseline x86-64, so nothing is contracted into FMA. errno: for |x| > 1, the infinities and NaN this stores EDOM before returning (x - x) / (x - x), as the libc.prx bundled with eleven titles does, PPSA13934's included. That reverses #3693's errno == 0 expectation in CheckAsinfErrno, which now checks EDOM for ±2, ±inf and a NaN, and an untouched errno for ±1, 0.5 and 0 (cc @CouchRiv). Tiny arguments return x without raising FE_INEXACT.

asinf is imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630, PPSA04158 and PPSA09614. This is the asinf counterpart of #9668 (acosf).

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 8b3e39832a.

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923 and PPSA05916 natively and supplies __error. Their asinf and this build's asinf_nid_postfix give the same bits for all 2^32 arguments, NaN payloads included. This build stores EDOM exactly where they do: for those eleven titles, asinf stores EDOM for ±2, inf and a quiet NaN and leaves errno alone for 1 and 0.5, and the -O3 libc.prx DLL of this branch matches PPSA07923's asinf bits and errno on 400,012 sampled inputs.
  • FMA builds: the copies bundled with PPSA13934 and PPSA09614 are FMA-contracted. They differ from msun's code by at most 1 ulp, on 391,714 arguments, so no single port matches every title's last bit (as for acosf in #9668).
  • guest_math: the new CheckAsinfConsoleBits has 13 cases with the console's outputs: both ranges, ±1, the last float below 1, tiny, -0, the smallest subnormal, 2.0 and a NaN payload. guest_math and the libc tests (8) pass in three runs out of three. With main's Math.cpp and this test, guest_math aborts in a Require.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • tools/pr_overlap.py --branch: adjacency conflicts only. #9643 rewrites fmodf, the line above asinf; #9668 adds its acosf debt entry near this one. Keeping both sides resolves each.
  • Independent Linux review: Dom3005's report tested head e4b95a92d merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports five passing guest_math runs (--repeat until-fail:5). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.
  • Not tested: macOS, a console.

Overlap coordination

#9643 changes the adjacent fmodf wrapper; #9668 adds the adjacent acosf debt entry. From this PR's side either landing order is acceptable: retain both functions, both test additions and both debt entries, then rerun guest_math after resolving any adjacency conflict. No functional dependency is proposed. Both authors have confirmed either order: @Noxy-HNR for #9643 and @rooklz for #9668. Whichever lands second retains both implementations and test additions, plus both debt entries (acosf then asinf), and reruns the focused math tests. Depends on remains none.

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: none; the 20-line msun function is rewritten as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4968717475 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9771). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T14:05:37Z. --- ### What `asinf` returned the host's `asin`, so its last bit depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 5,413,834 of the floats in (-1, 1). Examples, console then `main`: - `0x3b3a82da` → `0x3b3a82ea`, `main` gives `0x3b3a82eb`; - `0x3f0008fb` → `0x3f0614f1`, `main` gives `0x3f0614f0`. The titles' `asinf` (NID `GZWjF-YIFFk`) is FreeBSD msun's [`e_asinf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_asinf.c). `asinf` now computes it the same way: - the three ranges; - the single-precision rational `pS0..pS2`/`qS1` (`AsinfRational`); - for `0.5 <= |x| < 1`, `sqrt` and the final step in double, as msun has them. The build targets baseline x86-64, so nothing is contracted into FMA. **`errno`:** for `|x| > 1`, the infinities and NaN this stores `EDOM` before returning `(x - x) / (x - x)`, as the `libc.prx` bundled with eleven titles does, PPSA13934's included. That reverses #3693's `errno == 0` expectation in `CheckAsinfErrno`, which now checks `EDOM` for ±2, ±`inf` and a NaN, and an untouched `errno` for ±1, 0.5 and 0 (cc @CouchRiv). Tiny arguments return `x` without raising `FE_INEXACT`. `asinf` is imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630, PPSA04158 and PPSA09614. This is the `asinf` counterpart of #9668 (`acosf`). ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 8b3e39832a. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923 and PPSA05916 natively and supplies `__error`. Their `asinf` and this build's `asinf_nid_postfix` give the same bits for all 2^32 arguments, NaN payloads included. This build stores `EDOM` exactly where they do: for those eleven titles, `asinf` stores `EDOM` for ±2, `inf` and a quiet NaN and leaves `errno` alone for 1 and 0.5, and the `-O3` `libc.prx` DLL of this branch matches PPSA07923's `asinf` bits and `errno` on 400,012 sampled inputs. - **FMA builds:** the copies bundled with PPSA13934 and PPSA09614 are FMA-contracted. They differ from msun's code by at most 1 ulp, on 391,714 arguments, so no single port matches every title's last bit (as for `acosf` in #9668). - **`guest_math`:** the new `CheckAsinfConsoleBits` has 13 cases with the console's outputs: both ranges, ±1, the last float below 1, tiny, -0, the smallest subnormal, `2.0` and a NaN payload. `guest_math` and the libc tests (8) pass in three runs out of three. With `main`'s `Math.cpp` and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - `tools/pr_overlap.py --branch`: adjacency conflicts only. #9643 rewrites `fmodf`, the line above `asinf`; #9668 adds its `acosf` debt entry near this one. Keeping both sides resolves each. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9771#issuecomment-6353486841) tested head `e4b95a92d` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports five passing `guest_math` runs (`--repeat until-fail:5`). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim. - Not tested: macOS, a console. ### Overlap coordination #9643 changes the adjacent `fmodf` wrapper; #9668 adds the adjacent `acosf` debt entry. From this PR's side either landing order is acceptable: retain both functions, both test additions and both debt entries, then rerun `guest_math` after resolving any adjacency conflict. No functional dependency is proposed. Both authors have confirmed either order: [@Noxy-HNR for #9643](https://github.com/boykopovar/AnyPS5/pull/9771#issuecomment-6347647155) and [@rooklz for #9668](https://github.com/boykopovar/AnyPS5/pull/9668#issuecomment-6350210631). Whichever lands second retains both implementations and test additions, plus both debt entries (`acosf` then `asinf`), and reruns the focused math tests. `Depends on` remains none. ### Checklist - [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: none; the 20-line msun function is rewritten as #9668, #5036 and the `sinhf` entry did - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-22 14:41:35 +00:00 (Migrated from github.com)

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

Review: DISMISSED


The asinf implementation follows the cited FreeBSD msun three-range algorithm, and the reported exhaustive comparison against two console libc builds is strong evidence. I compiled a temporary harness from the exact AsinfRational/asinf_nid_postfix source in this head and ran the 13 added bit-pattern cases; all passed and a preset errno remained unchanged. This was not the full guest_math target. Current-main merge-tree, diff-check and conventions are clean, and the intervening main commits do not touch the changed Math.cpp, GuestMath.cpp or debt paths. Hosted product Linux/Windows tests are skipped; the register-context CI job is a separate suite.

Please resolve the repository's required overlap coordination before merge. Pairwise merges of this head with open #9643 conflict in Math.cpp at the adjacent fmodf/asinf hunk, and with #9668 in the adjacent libc debt entries. Both pairs compose by retaining both changes, but CONTRIBUTING.md requires the authors to agree an order and record any needed Depends on entry. I asked each author to confirm whether either order works. Please record the agreements on this PR and update the checklist if one must land first. I found no duplicate asinf implementation.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5616024411 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9771#pullrequestreview-5616024411). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T14:41:35Z. **Review:** DISMISSED --- The `asinf` implementation follows the cited FreeBSD msun three-range algorithm, and the reported exhaustive comparison against two console libc builds is strong evidence. I compiled a temporary harness from the exact `AsinfRational`/`asinf_nid_postfix` source in this head and ran the 13 added bit-pattern cases; all passed and a preset `errno` remained unchanged. This was not the full `guest_math` target. Current-main merge-tree, diff-check and conventions are clean, and the intervening main commits do not touch the changed Math.cpp, GuestMath.cpp or debt paths. Hosted product Linux/Windows tests are skipped; the register-context CI job is a separate suite. Please resolve the repository's required overlap coordination before merge. Pairwise merges of this head with open #9643 conflict in `Math.cpp` at the adjacent `fmodf`/`asinf` hunk, and with #9668 in the adjacent libc debt entries. Both pairs compose by retaining both changes, but `CONTRIBUTING.md` requires the authors to agree an order and record any needed `Depends on` entry. I asked each author to confirm whether either order works. Please record the agreements on this PR and update the checklist if one must land first. I found no duplicate `asinf` implementation.
JJenkx commented 2026-10-22 14:45:23 +00:00 (Migrated from github.com)

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


@MarcZX10 thanks. On order: either works from my side, and Depends on stays none unless one of the other authors needs a fixed order.

  • #9643 (@Noxy-HNR): the hunks only touch. #9643 rewrites the fmodf one-liner just above asinf, and this PR replaces asinf and adds AsinfRational before it. The resolution keeps #9643's fmodf block, then AsinfRational and this asinf. In GuestMath.cpp, #9643's RestoreFloatingPointState and its acosf checks sit away from CheckAsinfConsoleBits.
  • #9668 (@rooklz): both add a debt entry at the same spot in docs/dev/debt/libc.md. The resolution keeps both entries, acosf and then asinf, with neither text changed. In Math.cpp the two functions are separate, so there is no overlap there.

If either of you merges first, I'll rebase this onto it with that resolution and rerun guest_math and the exhaustive asinf comparison. @Noxy-HNR, @rooklz, please say whether either order works for you too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6343596758 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9771#issuecomment-6343596758). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T14:45:23Z. --- @MarcZX10 thanks. On order: either works from my side, and `Depends on` stays none unless one of the other authors needs a fixed order. - **#9643 (@Noxy-HNR):** the hunks only touch. #9643 rewrites the `fmodf` one-liner just above `asinf`, and this PR replaces `asinf` and adds `AsinfRational` before it. The resolution keeps #9643's `fmodf` block, then `AsinfRational` and this `asinf`. In `GuestMath.cpp`, #9643's `RestoreFloatingPointState` and its `acosf` checks sit away from `CheckAsinfConsoleBits`. - **#9668 (@rooklz):** both add a debt entry at the same spot in `docs/dev/debt/libc.md`. The resolution keeps both entries, `acosf` and then `asinf`, with neither text changed. In `Math.cpp` the two functions are separate, so there is no overlap there. If either of you merges first, I'll rebase this onto it with that resolution and rerun `guest_math` and the exhaustive `asinf` comparison. @Noxy-HNR, @rooklz, please say whether either order works for you too.
JJenkx commented 2026-10-22 15:01:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T15:01:05Z.


@Noxy-HNR @rooklz — following the overlap review, either landing order is acceptable from #9771's side.

I checked heads 3cd4f5cac270 (#9771), 43c42031a0cf (#9643) and 4951eee5ed87 (#9668). #9643 changes the adjacent fmodf wrapper, while #9771 inserts AsinfRational and replaces asinf; the resolution must retain both implementations and both test additions. #9668 implements acosf; its adjacent debt entry and #9771's asinf entry must both remain.

Neither is a functional prerequisite for this change. If either lands first, I will refresh #9771, retain both changes and rerun guest_math before it lands. Please confirm whether either order works for you, or name a preferred order. The description records this as a proposal pending your confirmations; it does not claim an agreement yet.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6343936665 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9771#issuecomment-6343936665). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T15:01:05Z. --- @Noxy-HNR @rooklz — following the overlap review, either landing order is acceptable from #9771's side. I checked heads `3cd4f5cac270` (#9771), `43c42031a0cf` (#9643) and `4951eee5ed87` (#9668). #9643 changes the adjacent `fmodf` wrapper, while #9771 inserts `AsinfRational` and replaces `asinf`; the resolution must retain both implementations and both test additions. #9668 implements `acosf`; its adjacent debt entry and #9771's `asinf` entry must both remain. Neither is a functional prerequisite for this change. If either lands first, I will refresh #9771, retain both changes and rerun `guest_math` before it lands. Please confirm whether either order works for you, or name a preferred order. The description records this as a proposal pending your confirmations; it does not claim an agreement yet.
MarcZX10 commented 2026-10-22 16:59:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:59:09Z.

Review: COMMENTED


The author has now rebased #9771 onto 499b1cf9fc and reports three passing Windows guest_math/libc runs, including the exhaustive comparison against the two cited title libc.prx builds. The head has a clean merge tree with current main; conventions and diff-check pass. The earlier overlap note remains explicit: merge resolution must retain adjacent #9643 fmodf and this asinf implementation/tests, and keep both #9668 acosf and this debt entry. The author has offered either order but the other authors have not confirmed in the visible conversation, so please record their confirmation or coordinate before merging. The branch is eight commits behind current main; please refresh the stack and rerun guest_math on the new head. I withdraw the queue-based request to keep this in draft. It is fine to keep submitting PRs; maintainers will handle them in due course. The branch-freshness and overlap requests above remain separate.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5617886911 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9771#pullrequestreview-5617886911). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:59:09Z. **Review:** COMMENTED --- The author has now rebased #9771 onto `499b1cf9fc` and reports three passing Windows `guest_math`/libc runs, including the exhaustive comparison against the two cited title libc.prx builds. The head has a clean merge tree with current `main`; conventions and diff-check pass. The earlier overlap note remains explicit: merge resolution must retain adjacent #9643 `fmodf` and this `asinf` implementation/tests, and keep both #9668 `acosf` and this debt entry. The author has offered either order but the other authors have not confirmed in the visible conversation, so please record their confirmation or coordinate before merging. The branch is eight commits behind current main; please refresh the stack and rerun `guest_math` on the new head. I withdraw the queue-based request to keep this in draft. It is fine to keep submitting PRs; maintainers will handle them in due course. The branch-freshness and overlap requests above remain separate.
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!9771
No description provided.