Skip to content

fix(libc): libm wrappers leave errno unchanged like FreeBSD msun - #3690

Closed
CouchRiv wants to merge 2 commits into
boykopovar:mainfrom
CouchRiv:fix-libc-acoshf-errno
Closed

CouchRiv wants to merge 2 commits into
boykopovar:mainfrom
CouchRiv:fix-libc-acoshf-errno

Conversation

@CouchRiv

@CouchRiv CouchRiv commented Oct 11, 2026 •

Copy link
Copy Markdown
Contributor

What

A guest that calls a libm function on a domain or range error and then reads errno sees a host errno that FreeBSD
never sets:

errno = 0; log(-1.0);    // main: errno 33 (EDOM)    FreeBSD: errno 0
errno = 0; exp(1024.0);  // main: errno 34 (ERANGE)  FreeBSD: errno 0

The libm exports in Math.cpp forward to the host std:: functions, and glibc sets errno for those errors. FreeBSD
msun never does: math_errhandling is MATH_ERREXCEPT (https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/msun/src/math.h#L67-L69).

PreserveErrno reads errno, runs the host call and writes errno back. Results and floating point exception flags do
not change. It wraps 39 exports, each one a one-line change:

Group Functions
double sin cos tan sinh cosh asinh acosh atanh exp expm1 log log2 log10 log1p sqrt pow atan2 hypot ldexp scalbn sincos
float sinf cosf tanf coshf expf exp2f logf log2f log10f log1pf sqrtf acoshf atan2f fmodf scalbnf ldexpf remainderf sincosf

The two empty asm statements in the helper keep the read, the call and the write in order. Without them GCC treats
sin, cos and tan as functions that never touch errno and removes a plain save and restore.

Not in this PR:

Tested

Linux (CachyOS), GCC 16 and clang 23.1.1; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4).

  • guest_math has one row per wrapped function: an error input and an in-range input, each pinning the result bits
    and that the caller's errno (set to EINTR before the call) is unchanged. sincos and sincosf check both
    outputs. guest_math passes on NVIDIA, RADV and lavapipe; tools/check_conventions.py --base origin/main and
    git diff --check are clean.
  • On main's Math.cpp the same test aborts at the first row (errno 33).
  • Compared with main on 239,332 inputs over 65 ops (special values, random bit patterns, int exponents), built with
    GCC and with clang: result bits, exception flags and out-arguments are identical (0 differences), errno stays 0 for
    every wrapped op. OpenLibm 0.8.8 (msun-derived), built offline, leaves errno at 0 on all of them and returns the
    same bits as main on every error input the tests use.
  • Windows, macOS and a console not tested.

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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source (none in this change)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes

AI-assisted: yes

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks. Build and guest_math pass here, and the new check fails without the change. Points before this can merge:

  • conventions is red: the checklist item "Unimplemented paths throw (NotImplemented_nid_no_patch)…" is unchecked. Tick it (nothing here is unimplemented).
  • Scope: FreeBSD msun never sets errno, so the same reasoning applies to acosh, asinh, atanh and the other std:: wrappers in Math.cpp, which still set errno on the host for domain/range errors. Either cover the family in one place (one PR, with a test row per function) or say why only acoshf differs.
  • GuestMath.cpp:499: CheckAcoshfErrno() is called between the abs_nid_postfix checks in main; move it next to the other Check* calls.

@github-actions github-actions Bot added the libc label Oct 11, 2026
@CouchRiv CouchRiv changed the title fix(libc): match FreeBSD acoshf errno fix(libc): libm wrappers leave errno unchanged like FreeBSD msun Oct 11, 2026
@CouchRiv

Copy link
Copy Markdown
Contributor Author
  1. Conventions red (checklist). The PR description has every box ticked, including the "Unimplemented paths throw"
    item (no unimplemented path is added). tools/check_conventions.py --base origin/main: "All conventions checks
    passed."
  2. Scope: acosh, asinh, atanh and the other wrappers. Agreed. One PR now covers the family: 39 wrappers behind one
    helper, one test row per function (CheckLibmErrnoPreserved). Evidence: on main, all 39 wrappers set errno (33 or 34) on at
    least one of the probe inputs (44,005 of 165,897 calls); OpenLibm 0.8.8 sets errno on none of the 65 ops probed. Left out on purpose and listed in the body: exp2 fix(libc): exp2 leaves errno unchanged #3599, fmod fix(libc): fmod leaves errno unchanged #3600, acosf fix(libc): keep the acosf result and leave errno unchanged #3688,
    asinf fix(libc): match FreeBSD asinf errno #3693 (open PRs), powf (fix(libc): powf(1, y) returns 1 for any y #3602 touches the same line), asin/acos (NaN sign also differs), nextafterf.
  3. CheckAcoshfErrno() placement. Removed. CheckLibmErrnoPreserved() is called next to the other Check* calls at
    the end of main, after CheckHypotSpecialValue(). The acoshf(0.5f) FE_INVALID check moved into it.

One question on scope: exp2 (#3599), fmod (#3600), acosf (#3688) and asinf (#3693) are the same errno fix and conflict with this branch line by line. If you prefer them in this PR, I will fold them in and close those four; otherwise I keep them separate and rebase whichever lands second.

@adriadam10 adriadam10 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

main moved and Math.cpp conflicted with __powisf2 (adjacent lines). Rebased onto current main, keeping both sides; no logic change. Build and guest_math pass after the rebase, and it fails with Math.cpp reverted. Merging.

Note: #3599, #3600, #3688, #3693 and #3602 edit lines next to these ones in Math.cpp, so they will need a rebase after this lands.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 11, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Conflicts with main again in Math.cpp and GuestMath.cpp (#3693 and #3598 landed); please rebase. Keep #3599, #3600 and #3688 separate; they go on top of this one.

@CouchRiv
CouchRiv force-pushed the fix-libc-acoshf-errno branch from 3fb3e2e to 7956caa Compare October 11, 2026 23:22
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 11, 2026
@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@CouchRiv
CouchRiv force-pushed the fix-libc-acoshf-errno branch from 7956caa to 26d14e8 Compare October 12, 2026 05:36
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@github-actions github-actions Bot mentioned this pull request Oct 12, 2026
1 of 9 tasks
@oneandonlydean oneandonlydean self-assigned this Oct 12, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

guest_math aborts with this merged onto main, on Linux and on Windows: https://github.com/boykopovar/AnyPS5/actions/runs/38220387093. It passed before, so it is probably one of the libm changes that went in since your rebase. Please rebase again and run it; I'll rebuild after that.

CouchRiv and others added 2 commits October 12, 2026 05:09
acoshf returned 0xffc00000 (errno 33) for -1.0 (0xbf800000), where
FreeBSD lib/msun/src/math.h returns 0xffc00000 (errno 0).

It now returns the FreeBSD result for that case. A guest test covers it
and fails on main.

Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
The libm exports forwarded straight to the host, so a domain or range
error set the host errno (33 or 34) where FreeBSD lib/msun/src/math.h
(math_errhandling is MATH_ERREXCEPT) leaves the caller's errno alone.

PreserveErrno runs the host call between an errno read and an errno
write. The empty asm statements on the arguments and the result keep the
compiler from dropping or reordering them: GCC treats sin, cos and tan
as never setting errno and removed a plain save and restore.

It wraps the 39 libm exports whose results already match msun on the
error inputs. Results are unchanged. One guest test row per function
pins the result bits and the caller's errno for an error input and an
in-range input.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@CouchRiv
CouchRiv force-pushed the fix-libc-acoshf-errno branch from 26d14e8 to 7496683 Compare October 12, 2026 09:14
@CouchRiv

Copy link
Copy Markdown
Contributor Author

Rebased onto main at c36fd0a. Both commits are unchanged by the rebase (git range-diff shows them identical).

Tested locally, Release, gcc, the same configure options as the Linux job:

  • ctest -R guest_math: passed
  • ctest -R guest_: 308 of 308 passed

I could not reproduce the abort from run 38220387093 on the newer main. That run merged the previous head onto 8c4a295, which is older than c36fd0a. Please rebuild on this head; if it still aborts, I will investigate from that log.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 12, 2026
@oneandonlydean

Copy link
Copy Markdown
Collaborator

Three things before the next build:

  • It conflicts with main in core/libs/prx/libc/src/Math.cpp since fix(libc): exp2 leaves errno unchanged #3599 landed: main's exp2_nid_postfix is now a save and restore of its own, directly above the ldexp, scalbn and scalbnf lines this rewrites. Keep main's exp2 and your three lines; please rebase.
  • The guest_math abort was not a merged libm change; I was wrong there. The run built 26d14e8 on main 8c4a295, and between your old base and c36fd0a the only change to Math.cpp or GuestMath.cpp is the nearbyintf line from fix(libc): add missing Unity engine static imports #3648, so CI ran the same code that passes for you. The host differs: Ubuntu 24.04 with GCC 13.3 and glibc 2.39, and WinLibs GCC 15.2 on UCRT, and it aborts on both. CheckLibmErrnoPreserved compares result bits that the host libm produces (NaN sign bits, the in-range values), and PreserveErrno depends on how the compiler treats the empty asm. Require prints nothing, so the log does not show the row. Please have the table print the function before it aborts, run it with GCC 13 and with MinGW, and compare only what this PR sets: errno, and isnan instead of NaN bits.
  • feat(libc): implement sinhf and asinl (carried from #3519) #3725 reads sinhf and asinl out of a libc.prx and finds Sony's error helper setting ERANGE and EDOM where msun sets nothing. Which of the 39 were checked against a libc.prx and not only against FreeBSD source?

@CouchRiv

Copy link
Copy Markdown
Contributor Author

Closing this. To answer the last question: none of the 39 functions were checked against the console's own libc, only against FreeBSD msun source, and #3725 shows that is not enough for at least sinhf and asinl. I'd rather come back with a smaller change once there is evidence for the PS5 behaviour than keep this open. Thanks for the careful reviews.

@CouchRiv CouchRiv closed this Oct 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

libc needs rebase Conflicts with main, rebase needed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants