Skip to content

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

Merged
oneandonlydean merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libc-asinf-msun
Oct 27, 2026
Merged

oneandonlydean merged 2 commits into
boykopovar:mainfrom
JJenkx:windows/libc-asinf-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 8b3e398.

  • 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 fix(libc): compute acosf with FreeBSD msun's algorithm, as the console's libc.prx does #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. fix(libc): preserve errno across fmodf domain errors #9643 rewrites fmodf, the line above asinf; fix(libc): compute acosf with FreeBSD msun's algorithm, as the console's libc.prx does #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

AI-assisted (Claude Code).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

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

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.

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 5021 docs/dev/debt/libc.md
PR 6295 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 6713 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/src/Math.cpp
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7544 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 7648 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9772 docs/dev/debt/libc.md
PR 9863 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10324 docs/dev/debt/libc.md
PR 10749 docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11341 docs/dev/debt/libc.md

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

@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.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Back to draft: @MarcZX10 clarified that the #5239 intake guidance is still active (#9688 (comment)). While the open queue is above 700, a PR should name a title shown to need the change on current main. This one names none yet, so it waits here. I'll mark it ready once a title run shows the need, or once the queue drops below the threshold. The code and test evidence above are unchanged.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 499b1cf (head 9573ed1, no other changes). guest_math and the libc tests (8) pass in three runs out of three on Windows (UCRT GCC 15.2, Release), and check_conventions.py passes.

@JJenkx
JJenkx force-pushed the windows/libc-asinf-msun branch from 3cd4f5c to 9573ed1 Compare October 22, 2026 16:45

@MarcZX10 MarcZX10 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks. Same as on #9784: none of the commits since 499b1cf touch Math.cpp, GuestMath.cpp or the libc debt file, and the head still merges cleanly. I'll refresh and rerun guest_math when a maintainer is ready to merge, or as soon as a conflict appears. The order confirmations from @Noxy-HNR (#9643) and @rooklz (#9668) are still pending (asked in #9771 (comment)). The PR stays a draft until both have answered and a title needs it.

@MarcZX10

MarcZX10 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

I’ve noticed that asking you to hold this PR was unnecessary. It’s fine to keep submitting PRs; maintainers will handle them in due course.

@Noxy-HNR

Copy link
Copy Markdown
Contributor

Heads-up from #9643, which also edits core/libs/prx/libc/src/Math.cpp: its fmodf change is on the line just before your AsinfRational helper, so whichever of the two lands second gets one conflict hunk, resolved by keeping both. The tests and docs/dev/debt/libc.md merge cleanly, and the combined tree passes guest_math and guest_floating_point on Windows. I'm fine with either order and can rebase #9643 if yours lands first. Is either order fine for you?

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Yes, either order is fine for #9771. Thanks for checking the combined tree on Windows. If #9643 lands first, I will keep both your fmodf change and AsinfRational in the adjacent hunk, rebase #9771, and rerun the focused math tests.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

@Noxy-HNR thanks for checking the combined tree. Yes, either order is fine: whichever lands second keeps both the fmodf change and AsinfRational/asinf. That settles the #9643 side; only @rooklz (#9668, the adjacent acosf debt entry) is still to confirm.

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

I pushed 7d5caab: asinf now stores EDOM for |x| > 1, the infinities and NaN, instead of leaving errno unchanged.

Why: the libc.prx bundled with eleven titles stores EDOM there. I checked PPSA04158, PPSA05916, PPSA07923, PPSA09614, PPSA13934, PPSA20423, PPSA25636, PPSA26528, PPSA33655, PPSA34008 and PPSA35169, and all give byte-identical output on this probe. My earlier remark that PPSA13934's copy sets no errno here was wrong, and I have corrected it on #9668. This reverses #3693's errno == 0 expectation (cc @CouchRiv), as #10253 did for the double asin.

Tests, Windows, GCC 15.2 UCRT, Release:

  • CheckAsinfErrno now expects EDOM for ±2, ±inf and a NaN, and an untouched errno for ±1, 0.5 and 0.
  • guest_math and guest_dinkumware_internals pass in three runs out of three on 7d5caab.
  • Negative control: with the previous head's Math.cpp, guest_math aborts.
  • I loaded this branch's -O3 libc.prx DLL next to PPSA07923's copy: asinf bits and errno match on 400,012 sampled inputs.

AI-assisted: yes (Claude Code)

@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head 7d5caabc86 (7d5caabc860735191955107136527b805239628b), not merged onto anything else; its base on main is 938812df14.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON.

  • guest_math: passed in each of 3 runs, exit code 0 each time, with no failed, skipped or not-run tests.

This is the focused test only; no title, console or floating-point exception flags were checked.

AI-assisted: yes (Claude Code).

JJenkx and others added 2 commits October 25, 2026 22:18
…bc.prx does

asinf returned the host asin, so its last bit depended on the host: on
Windows it differs from the libc.prx that titles ship on 5,413,834 of the
floats in (-1, 1). That libc.prx is msun's e_asinf.c, which gives the same
bits as PPSA07923's and PPSA05916's copies for all 2^32 arguments. errno
is still left unchanged for |x| > 1 and NaN.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
asinf left errno unchanged for |x| > 1, the infinities and NaN, following
included, stores EDOM there and leaves it alone for |x| <= 1; asinf now
does the same, and the test expects it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/libc-asinf-msun branch from 7d5caab to 6d9fd5d Compare October 26, 2026 02:29
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@oneandonlydean
oneandonlydean merged commit 304bea4 into boykopovar:main Oct 27, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants