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

Closed
CouchRiv wants to merge 2 commits from upstream-pr/3690 into main
CouchRiv commented 2026-10-11 15:22:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-11T15:22:47Z.


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 (github.com/freebsd/freebsd-src@8c639945a4/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:

  • exp2 (#3599), fmod (#3600), acosf (#3688), asinf (#3693): the same errno fix, already open.
  • powf: #3602 changes the same line.
  • asin, acos, and log10 with a negative input: the result is also a different NaN sign than msun, which is a
    value difference, not an errno one. log10 is wrapped and tested on the pole case log10(0.0) only.
  • nextafterf: left alone; its difference from msun is in the result for a signalling NaN, not in errno.
  • _FSinh, _FCosh, the long double functions: no msun counterpart, or covered by #3598.
  • tanh, tanhf, atan, atanf, cbrt, cbrtf, asinhf, hypotf, logbf, modf, modff, fma, round,
    roundf, trunc: the host did not set errno for any input tried, so they stay as they are.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4820120901 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T15:22:47Z. --- ### 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: - `exp2` (#3599), `fmod` (#3600), `acosf` (#3688), `asinf` (#3693): the same errno fix, already open. - `powf`: #3602 changes the same line. - `asin`, `acos`, and `log10` with a negative input: the result is also a different NaN sign than msun, which is a value difference, not an errno one. `log10` is wrapped and tested on the pole case `log10(0.0)` only. - `nextafterf`: left alone; its difference from msun is in the result for a signalling NaN, not in errno. - `_FSinh`, `_FCosh`, the long double functions: no msun counterpart, or covered by #3598. - `tanh`, `tanhf`, `atan`, `atanf`, `cbrt`, `cbrtf`, `asinhf`, `hypotf`, `logbf`, `modf`, `modff`, `fma`, `round`, `roundf`, `trunc`: the host did not set errno for any input tried, so they stay as they are. ### 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 - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in TechnicalDebt - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source (none in this change) - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes AI-assisted: yes
adriadam10 commented 2026-10-11 15:28:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T15:28:57Z.

Review: CHANGES_REQUESTED


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.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5483848782 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#pullrequestreview-5483848782). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T15:28:57Z. **Review:** CHANGES_REQUESTED --- 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.
CouchRiv commented 2026-10-11 15:57:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-11T15:57:11Z.


  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 #3599, fmod #3600, acosf #3688,
    asinf #3693 (open PRs), powf (#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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110859573 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#issuecomment-6110859573). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T15:57:11Z. --- 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` #3599, `fmod` #3600, `acosf` #3688, `asinf` #3693 (open PRs), `powf` (#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 commented 2026-10-11 22:01:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-11T22:01:31Z.

Review: APPROVED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5485173891 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#pullrequestreview-5485173891). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T22:01:31Z. **Review:** APPROVED --- `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.
oneandonlydean commented 2026-10-11 23:16:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-11T23:16:28Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6115000590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#issuecomment-6115000590). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-11T23:16:28Z. --- 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.
oneandonlydean commented 2026-10-12 09:04:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T09:04:48Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6121119725 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#issuecomment-6121119725). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T09:04:48Z. --- `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 commented 2026-10-12 09:14:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-12T09:14:12Z.


Rebased onto main at c36fd0a0e. 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 8c4a295ff, which is older than c36fd0a0e. Please rebuild on this head; if it still aborts, I will investigate from that log.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6121258662 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#issuecomment-6121258662). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-12T09:14:12Z. --- Rebased onto `main` at c36fd0a0e. 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 8c4a295ff, which is older than c36fd0a0e. Please rebuild on this head; if it still aborts, I will investigate from that log.
oneandonlydean commented 2026-10-12 20:34:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-12T20:34:41Z.


Three things before the next build:

  • It conflicts with main in core/libs/prx/libc/src/Math.cpp since #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 26d14e8b on main 8c4a295f, and between your old base and c36fd0a0 the only change to Math.cpp or GuestMath.cpp is the nearbyintf line from #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.
  • #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?
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6132154434 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#issuecomment-6132154434). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-12T20:34:41Z. --- Three things before the next build: - It conflicts with main in `core/libs/prx/libc/src/Math.cpp` since #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 26d14e8b on `main` 8c4a295f, and between your old base and c36fd0a0 the only change to `Math.cpp` or `GuestMath.cpp` is the `nearbyintf` line from #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. - #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 commented 2026-10-13 15:18:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by CouchRiv on 2026-10-13T15:18:07Z.


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6148871062 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3690#issuecomment-6148871062). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-13T15:18:07Z. --- 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.

Pull request closed

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!3690
No description provided.