fix(libc): keep the acosf result and leave errno unchanged #3688

Merged
CouchRiv merged 2 commits from upstream-pr/3688 into main 2026-10-13 11:29:59 +00:00
CouchRiv commented 2026-10-11 15:22:43 +00:00 (Migrated from github.com)

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


What

acosf on an out-of-range input sets the host errno where FreeBSD does not:

errno = 0; acosf(2.0f);   // main: result 0xffc00000, errno 33 (EDOM)    FreeBSD: result 0xffc00000, errno 0

The export forwards to the host std::acos, and glibc sets errno for the domain error. FreeBSD msun never does:
math_errhandling is MATH_ERREXCEPT (github.com/freebsd/freebsd-src@8c639945a4/lib/msun/src/math.h (L67-L69)).
The result (0xffc00000, what e_acosf.c gives with (x-x)/(x-x) on x86) already matches.

acosf now calls std::acos and writes the caller's errno back, the same way as asinf in #3693. The result bits and
the floating point exception flags are the same as on main. The other libm functions are covered by #3690
(acoshf, sin, log, ... in one place), #3693 (asinf), #3599 (exp2) and #3600 (fmod).

Tested

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

  • guest_math checks acosf(2.0f) and acosf(-2.0f) return 0xffc00000 with errno unchanged, and acosf(0.5f)
    returns 0x3f860a92. scripts/verify-branch.sh with guest_math: build, ctest on the three drivers, conventions,
    diff check, publication gate, 8 of 8 pass.
  • On main's Math.cpp the same test aborts at the first errno check (errno 33).
  • Compared with main on 648 inputs (special values, random bit patterns): result bits and exception flags identical,
    0 differences; errno 0 on all (389 inputs set errno 33 on main). OpenLibm 0.8.8 gives the same bits and errno 0 on
    the inputs the test uses.
  • 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:4820120509 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3688). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T15:22:43Z. --- ### What `acosf` on an out-of-range input sets the host errno where FreeBSD does not: ``` errno = 0; acosf(2.0f); // main: result 0xffc00000, errno 33 (EDOM) FreeBSD: result 0xffc00000, errno 0 ``` The export forwards to the host `std::acos`, and glibc sets errno for the domain error. FreeBSD `msun` never does: `math_errhandling` is `MATH_ERREXCEPT` (https://github.com/freebsd/freebsd-src/blob/8c639945a44588f6cfdddfb057bb5aad88735da0/lib/msun/src/math.h#L67-L69). The result (`0xffc00000`, what `e_acosf.c` gives with `(x-x)/(x-x)` on x86) already matches. `acosf` now calls `std::acos` and writes the caller's errno back, the same way as `asinf` in #3693. The result bits and the floating point exception flags are the same as on `main`. The other libm functions are covered by #3690 (`acoshf`, `sin`, `log`, ... in one place), #3693 (`asinf`), #3599 (`exp2`) and #3600 (`fmod`). ### Tested Linux (CachyOS), GCC 16; RTX 4070 Ti (NVIDIA), Radeon 610M (RADV) and lavapipe (Mesa 26.2.4). - `guest_math` checks `acosf(2.0f)` and `acosf(-2.0f)` return `0xffc00000` with errno unchanged, and `acosf(0.5f)` returns `0x3f860a92`. `scripts/verify-branch.sh` with `guest_math`: build, ctest on the three drivers, conventions, diff check, publication gate, 8 of 8 pass. - On `main`'s `Math.cpp` the same test aborts at the first errno check (errno 33). - Compared with `main` on 648 inputs (special values, random bit patterns): result bits and exception flags identical, 0 differences; errno 0 on all (389 inputs set errno 33 on `main`). OpenLibm 0.8.8 gives the same bits and errno 0 on the inputs the test uses. - 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:26:40 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


Thanks for the fix. A few things to address:

  • core/libs/prx/libc/src/Math.cpp:44 returns std::numeric_limits<float>::quiet_NaN() = 0x7fc00000, but the description says the expected result is 0xffc00000 (what main returns and what FreeBSD e_acosf.c gives with (x-x)/(x-x) on x86; I measured 0xffc00000 for 0/0). The patch removes errno but also changes the value. Return 0xffc00000 explicitly, or compute (x - x) / (x - x), so only errno differs.
  • core/libs/tests/GuestMath.cpp:131 only checks errno. Pin the result bits for 2.0f and -2.0f too (std::bit_cast<std::uint32_t>(...) == 0xffc00000), and keep the in-range case (acosf(0.5f)).
  • The checklist item "Unimplemented paths throw..." is unchecked, so Conventions fails. Mark it [x] if it doesn't apply.
  • Same errno behaviour exists in neighbouring functions (asinf, ...); say in the description that they are left out on purpose, or open a follow-up.

Checked: build + ctest of core/libs (74/74), and guest_math fails without the change.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5483841790 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3688#pullrequestreview-5483841790). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-11T15:26:40Z. **Review:** CHANGES_REQUESTED --- Thanks for the fix. A few things to address: - `core/libs/prx/libc/src/Math.cpp:44` returns `std::numeric_limits<float>::quiet_NaN()` = `0x7fc00000`, but the description says the expected result is `0xffc00000` (what `main` returns and what FreeBSD `e_acosf.c` gives with `(x-x)/(x-x)` on x86; I measured `0xffc00000` for `0/0`). The patch removes errno but also changes the value. Return `0xffc00000` explicitly, or compute `(x - x) / (x - x)`, so only errno differs. - `core/libs/tests/GuestMath.cpp:131` only checks `errno`. Pin the result bits for `2.0f` and `-2.0f` too (`std::bit_cast<std::uint32_t>(...) == 0xffc00000`), and keep the in-range case (`acosf(0.5f)`). - The checklist item "Unimplemented paths throw..." is unchecked, so Conventions fails. Mark it `[x]` if it doesn't apply. - Same errno behaviour exists in neighbouring functions (`asinf`, ...); say in the description that they are left out on purpose, or open a follow-up. Checked: build + ctest of core/libs (74/74), and `guest_math` fails without the change.
CouchRiv commented 2026-10-11 15:57:13 +00:00 (Migrated from github.com)

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


  1. Value changed (0x7fc00000 instead of 0xffc00000). Fixed. The explicit range check and quiet_NaN() are gone;
    acosf calls std::acos and restores errno, so only errno differs from main. Measured: acosf(2.0f) and
    acosf(-2.0f) give 0xffc00000 on main, on this branch and on OpenLibm 0.8.8.
  2. Test only checked errno. CheckAcosfErrno now pins std::bit_cast<std::uint32_t>(...) == 0xffc00000u for 2.0f
    and -2.0f, and keeps acosf(0.5f) (now also pinned: 0x3f860a92, equal on main and OpenLibm).
  3. Checklist. Every box ticked in the PR description; tools/check_conventions.py --base origin/main: "All conventions
    checks passed."
  4. Neighbouring functions. Not left out silently: asinf is #3693; the rest of the libm family (acosh, acoshf, sin,
    cos, tan, sinh, cosh, asinh, atanh, exp, expm1, log, log2, log10, log1p, sqrt, pow, atan2, hypot, ldexp, scalbn,
    sincos and their float versions) is #3690, widened in this round to cover them in one place; exp2 is #3599 and
    fmod is #3600.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6110859859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3688#issuecomment-6110859859). Originally posted by [CouchRiv](https://github.com/CouchRiv) on 2026-10-11T15:57:13Z. --- 1. **Value changed (0x7fc00000 instead of 0xffc00000).** Fixed. The explicit range check and `quiet_NaN()` are gone; `acosf` calls `std::acos` and restores errno, so only errno differs from `main`. Measured: `acosf(2.0f)` and `acosf(-2.0f)` give `0xffc00000` on `main`, on this branch and on OpenLibm 0.8.8. 2. **Test only checked errno.** `CheckAcosfErrno` now pins `std::bit_cast<std::uint32_t>(...) == 0xffc00000u` for `2.0f` and `-2.0f`, and keeps `acosf(0.5f)` (now also pinned: `0x3f860a92`, equal on `main` and OpenLibm). 3. **Checklist.** Every box ticked in the PR description; `tools/check_conventions.py --base origin/main`: "All conventions checks passed." 4. **Neighbouring functions.** Not left out silently: `asinf` is #3693; the rest of the libm family (acosh, acoshf, sin, cos, tan, sinh, cosh, asinh, atanh, exp, expm1, log, log2, log10, log1p, sqrt, pow, atan2, hypot, ldexp, scalbn, sincos and their float versions) is #3690, widened in this round to cover them in one place; `exp2` is #3599 and `fmod` is #3600.
adriadam10 commented 2026-10-13 09:56:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-13T09:56:01Z.

Review: APPROVED


Rebased onto current main (only a test-adjacency conflict in GuestMath.cpp; CheckPowfSpecialValue and CheckAcosfErrno both kept). Checked: guest_math passes with the change and fails with Math.cpp reverted; acosf(±2.0f) returns 0xffc00000 with errno unchanged. Merging once the full Build is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5501799109 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3688#pullrequestreview-5501799109). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-13T09:56:01Z. **Review:** APPROVED --- Rebased onto current `main` (only a test-adjacency conflict in `GuestMath.cpp`; `CheckPowfSpecialValue` and `CheckAcosfErrno` both kept). Checked: `guest_math` passes with the change and fails with `Math.cpp` reverted; `acosf(±2.0f)` returns `0xffc00000` with errno unchanged. Merging once the full Build is green.
github-actions[bot] commented 2026-10-13 10:27:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-13T10:27:34Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#4946 core/libs/tests/GuestMath.cpp L216, L840-841
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6143526695 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/3688#issuecomment-6143526695). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-13T10:27:34Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #4946 | `core/libs/tests/GuestMath.cpp` [L216](https://github.com/boykopovar/AnyPS5/blob/5d50886ee4045dc2a081d8d93bf8e667f6d82325/core/libs/tests/GuestMath.cpp#L216), [L840-841](https://github.com/boykopovar/AnyPS5/blob/5d50886ee4045dc2a081d8d93bf8e667f6d82325/core/libs/tests/GuestMath.cpp#L840-L841) | | |
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!3688
No description provided.