fix(libc): compute log1pl, acoshl, asinhl and atanhl as titles' libc.prx does #18694

Merged
JJenkx merged 2 commits from upstream-pr/18694 into main 2026-11-17 10:43:29 +00:00
JJenkx commented 2026-11-12 02:43:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-12T02:43:49Z.


What

libc's log1pl, acoshl, asinhl and atanhl returned the host C library's results. The libc.prx that titles bundle computes them with its own Dinkumware code on top of _LLog, sqrtl and _Feraise, all of which this libc already has. Run natively on Linux, that code and main disagree on a large share of arguments (table below).

This PR follows that code (PPSA08194's libc.prx, relinked: log1pl 0x40a80, acoshl 0x40990, asinhl 0x41620, atanhl 0x42110), operation by operation. All four run at the caller's x87 precision and never touch the control word, as the binary does.

  • log1pl(x):
    • ±0, NaN and +∞ are returned unchanged.
    • x < -1 raises FE_INVALID (_Feraise(1), EDOM) and returns a quiet NaN.
    • -1 raises FE_DIVBYZERO (_Feraise(4)) and returns −∞.
    • |x| < 2^-66 returns x.
    • Otherwise, with u = 1 + x, the result is _LLog(u, 0) + ((u - 1) - x) * (-1 / u).
  • acoshl(x):
    • NaN and +∞ are returned unchanged.
    • x < 1, −∞ included, raises FE_INVALID and returns NaN.
    • 1 returns +0.
    • x >= 2^67 returns _LLog(x, 0) + ln 2.
    • Otherwise, with t = x - 1, the result is log1pl(sqrtl(x * t + t) + t).
  • asinhl(x):
    • ±0, ±∞ and NaN are returned unchanged.
    • With a = |x|: below 2^-66 the result is a; from 2^67 it is _LLog(a, 0) + ln 2; otherwise it is log1pl(a / (sqrtl(a * a + 1) + 1) * a + a).
    • The sign of x is applied to the result.
  • atanhl(x):
    • ±0 and NaN are returned unchanged.
    • With a = |x|: a > 1, ±∞ included, raises FE_INVALID and returns NaN, and a == 1 raises FE_DIVBYZERO and returns ±∞.
    • Below 2^-33 the result is x; otherwise it is 0.5 * log1pl(2 * (a / (1 - a))), with the sign of x.

Comparisons follow the binary's branches for unordered operands as well: a pseudo-infinity such as ffff:0000000000000000 is not a NaN to _LDtest, compares unordered, and goes where the binary's jbe sends it (acoshl takes the logl branch and reports EDOM from _LLog).

The constants, read from the loaded module, are a quiet NaN, +∞, 2^-66, 2^-33, 2.0f (giving 2 / 2^-66 = 2^67), 0.5f, and the 80-bit ln 2 0xb.17217f7d1cf79acp-4. The binary's logl is _LLog(x, 0) (#18110 routes the export there), so this PR calls _LLog directly and does not depend on #18110.

Tested

Oracle. A small probe dlopens the title's relinked libc.prx (PPSA08194, libc.prx.guest.prx) next to this libc and calls both on the same arguments. There are 1,000,160 arguments per function: random exponents across the whole range and near 1, random mantissas, and fixed cases (zeros, infinities, NaNs, a pseudo-infinity, and both sides of every cutoff). It compares result bits, errno, and FE_INVALID/FE_DIVBYZERO/FE_OVERFLOW/FE_UNDERFLOW, with the x87 control word set to 64-, 53- or 24-bit precision for the call.

function main, 64 bits main, 53 bits main, 24 bits this PR, all three
log1pl 190,953 619,580 619,859 0
acoshl 64,097 (+1 errno) 166,500 (+1 errno) 183,906 (+1 errno) 0
asinhl 265,456 707,982 741,352 0
atanhl 136,769 136,785 136,431 0

main also differs in exception flags on 3 to 16 arguments per function and precision; this PR differs on none.

Tests. guest_dinkumware_internals_tests gains TestLongInverseHyperbolic: 51 exact-bit cases at 64 bits, including every cutoff on both sides, ±1, ±∞, a pseudo-infinity and the error results with their errno and flags. It adds 12 cases at 53 bits that also check the control word is unchanged. The expected values come from the binary through the probe.

The "this PR" column uses this branch's built libc.prx (head c0d823f4da). A control run of main's build through the same probe reproduces main's differences.

ctest -R "guest_dinkumware_internals|guest_math|guest_bessel": 5/5 pass (guest_dinkumware_internals, guest_math, guest_math_elementary, guest_math_exact, guest_bessel). Linux 7.2.14, Ryzen 9 7950X, GCC 16, Release. Not run on Windows or macOS.

python3 tools/check_conventions.py --base origin/main: no errors. The one warning is the host-computed long-double entry, which was already over 1,000 characters; this PR shortens it by removing these four functions.

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
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

Current branch: one commit d035a8ebd3cc on main 7b60f8ad8662. The four reviewed function bodies are unchanged. A debt fragment updates main's current host-results entry in place, removing these four names and retaining one separate implementation entry; erfl/erfcl are not re-added. Fresh Linux rebuilt guest_math and guest_dinkumware_internals both pass with frozen executable/dependency hashes; conventions and debt folding pass. Current validation and rendered debt. Prior numerical/module comparisons above are historical evidence, not rerun in this refresh.

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5270417614 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18694). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-12T02:43:49Z. --- ### What libc's `log1pl`, `acoshl`, `asinhl` and `atanhl` returned the host C library's results. The `libc.prx` that titles bundle computes them with its own Dinkumware code on top of `_LLog`, `sqrtl` and `_Feraise`, all of which this libc already has. Run natively on Linux, that code and `main` disagree on a large share of arguments (table below). This PR follows that code (PPSA08194's `libc.prx`, relinked: `log1pl` 0x40a80, `acoshl` 0x40990, `asinhl` 0x41620, `atanhl` 0x42110), operation by operation. All four run at the caller's x87 precision and never touch the control word, as the binary does. - **`log1pl(x)`:** - ±0, NaN and +∞ are returned unchanged. - `x < -1` raises `FE_INVALID` (`_Feraise(1)`, `EDOM`) and returns a quiet NaN. - `-1` raises `FE_DIVBYZERO` (`_Feraise(4)`) and returns −∞. - `|x| < 2^-66` returns `x`. - Otherwise, with `u = 1 + x`, the result is `_LLog(u, 0) + ((u - 1) - x) * (-1 / u)`. - **`acoshl(x)`:** - NaN and +∞ are returned unchanged. - `x < 1`, −∞ included, raises `FE_INVALID` and returns NaN. - `1` returns +0. - `x >= 2^67` returns `_LLog(x, 0) + ln 2`. - Otherwise, with `t = x - 1`, the result is `log1pl(sqrtl(x * t + t) + t)`. - **`asinhl(x)`:** - ±0, ±∞ and NaN are returned unchanged. - With `a = |x|`: below `2^-66` the result is `a`; from `2^67` it is `_LLog(a, 0) + ln 2`; otherwise it is `log1pl(a / (sqrtl(a * a + 1) + 1) * a + a)`. - The sign of `x` is applied to the result. - **`atanhl(x)`:** - ±0 and NaN are returned unchanged. - With `a = |x|`: `a > 1`, ±∞ included, raises `FE_INVALID` and returns NaN, and `a == 1` raises `FE_DIVBYZERO` and returns ±∞. - Below `2^-33` the result is `x`; otherwise it is `0.5 * log1pl(2 * (a / (1 - a)))`, with the sign of `x`. Comparisons follow the binary's branches for unordered operands as well: a pseudo-infinity such as `ffff:0000000000000000` is not a NaN to `_LDtest`, compares unordered, and goes where the binary's `jbe` sends it (`acoshl` takes the `logl` branch and reports `EDOM` from `_LLog`). The constants, read from the loaded module, are a quiet NaN, +∞, `2^-66`, `2^-33`, `2.0f` (giving `2 / 2^-66 = 2^67`), `0.5f`, and the 80-bit `ln 2` `0xb.17217f7d1cf79acp-4`. The binary's `logl` is `_LLog(x, 0)` (#18110 routes the export there), so this PR calls `_LLog` directly and does not depend on #18110. ### Tested **Oracle.** A small probe `dlopen`s the title's relinked `libc.prx` (PPSA08194, `libc.prx.guest.prx`) next to this libc and calls both on the same arguments. There are 1,000,160 arguments per function: random exponents across the whole range and near 1, random mantissas, and fixed cases (zeros, infinities, NaNs, a pseudo-infinity, and both sides of every cutoff). It compares result bits, `errno`, and `FE_INVALID`/`FE_DIVBYZERO`/`FE_OVERFLOW`/`FE_UNDERFLOW`, with the x87 control word set to 64-, 53- or 24-bit precision for the call. | function | `main`, 64 bits | `main`, 53 bits | `main`, 24 bits | this PR, all three | |---|---|---|---|---| | `log1pl` | 190,953 | 619,580 | 619,859 | 0 | | `acoshl` | 64,097 (+1 `errno`) | 166,500 (+1 `errno`) | 183,906 (+1 `errno`) | 0 | | `asinhl` | 265,456 | 707,982 | 741,352 | 0 | | `atanhl` | 136,769 | 136,785 | 136,431 | 0 | `main` also differs in exception flags on 3 to 16 arguments per function and precision; this PR differs on none. **Tests.** `guest_dinkumware_internals_tests` gains `TestLongInverseHyperbolic`: 51 exact-bit cases at 64 bits, including every cutoff on both sides, ±1, ±∞, a pseudo-infinity and the error results with their `errno` and flags. It adds 12 cases at 53 bits that also check the control word is unchanged. The expected values come from the binary through the probe. The "this PR" column uses this branch's built `libc.prx` (head `c0d823f4da`). A control run of `main`'s build through the same probe reproduces `main`'s differences. `ctest -R "guest_dinkumware_internals|guest_math|guest_bessel"`: 5/5 pass (`guest_dinkumware_internals`, `guest_math`, `guest_math_elementary`, `guest_math_exact`, `guest_bessel`). Linux 7.2.14, Ryzen 9 7950X, GCC 16, Release. Not run on Windows or macOS. `python3 tools/check_conventions.py --base origin/main`: no errors. The one warning is the host-computed long-double entry, which was already over 1,000 characters; this PR shortens it by removing these four functions. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 - [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 <!-- codex-pr18694-linear-debt-20261115 --> Current branch: one commit `d035a8ebd3cc` on main `7b60f8ad8662`. The four reviewed function bodies are unchanged. A debt fragment updates main's current host-results entry in place, removing these four names and retaining one separate implementation entry; erfl/erfcl are not re-added. Fresh Linux rebuilt `guest_math` and `guest_dinkumware_internals` both pass with frozen executable/dependency hashes; conventions and debt folding pass. [Current validation and rendered debt](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr18694-linear-debt-d035-linux-20261115.tar.gz). Prior numerical/module comparisons above are historical evidence, not rerun in this refresh.
PrivacyOnion commented 2026-11-12 06:12:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-12T06:12:01Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against c0d823f4da:

  • Conventions: tools/check_conventions.py passed; git diff --check clean.
  • Build: guest_dinkumware_internals_tests built cleanly.
  • CTest (guest_dinkumware_internals):
    1/1 Test #1114: guest_dinkumware_internals .......   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1114: guest_dinkumware_internals .......***Failed    0.01 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/Math.cpp:log1pl_nid_postfix: Verified handling of boundary conditions (±0, NaNs, +∞, x == -1 raising FE_DIVBYZERO (0x04) with -inf, and x < -1 raising FE_INVALID (0x01) with quiet NaN), tiny cutoff at |x| < 2^-66, and argument reduction via _LLog_nid_postfix.
    • core/libs/prx/libc/src/Math.cpp:acoshl_nid_postfix: Verified domain validation for x < 1.0 (raising FE_INVALID 0x01 and returning NaN), exact zero for x == 1.0, asymptotic branch for x >= 2^67 via _LLog_nid_postfix(x, 0) + Ln2, and the log1pl/sqrtl formulation for the intermediate range.
    • core/libs/prx/libc/src/Math.cpp:asinhl_nid_postfix: Verified classification via _LDtest_nid_postfix for zeros, infinities, and NaNs, sign restoration across all branches, tiny cutoff at |a| < 2^-66, large argument cutoff at |a| >= 2^67, and the numerically stable log1pl transformation.
    • core/libs/prx/libc/src/Math.cpp:atanhl_nid_postfix: Verified pole check at |a| == 1.0 raising FE_DIVBYZERO (0x04) returning signed infinity, domain validation for |a| > 1.0 raising FE_INVALID (0x01) returning quiet NaN, small argument bypass at |a| < 2^-33, and logarithmic transformation log1pl_nid_postfix(q + q) * 0.5L.
    • core/libs/tests/GuestDinkumwareInternals.cpp:CheckInverseHyperbolic: Verified test harness verifies bit exactness (SameBits), errno, floating-point exception flags (FE_INVALID, FE_DIVBYZERO), and control-word invariance at both 64-bit and 53-bit x87 precisions.
    • AGENTS.md and docs/dev/debt/libc.md: Verified zero comments in code, correct debt file updates reflecting the new implementations, no title-specific hardcoding, and clean repository conventions compliance.
  • Minor: please drop the Generated with footer from the description; the AI-assisted checkbox can stay.

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5869921949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18694#pullrequestreview-5869921949). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-12T06:12:01Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `c0d823f4da`: - **Conventions**: `tools/check_conventions.py` passed; `git diff --check` clean. - **Build**: `guest_dinkumware_internals_tests` built cleanly. - **CTest** (`guest_dinkumware_internals`): ``` 1/1 Test #1114: guest_dinkumware_internals ....... Passed 0.01 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1114: guest_dinkumware_internals .......***Failed 0.01 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/Math.cpp:log1pl_nid_postfix: Verified handling of boundary conditions (±0, NaNs, +∞, x == -1 raising FE_DIVBYZERO (0x04) with -inf, and x < -1 raising FE_INVALID (0x01) with quiet NaN), tiny cutoff at |x| < 2^-66, and argument reduction via _LLog_nid_postfix. - core/libs/prx/libc/src/Math.cpp:acoshl_nid_postfix: Verified domain validation for x < 1.0 (raising FE_INVALID 0x01 and returning NaN), exact zero for x == 1.0, asymptotic branch for x >= 2^67 via _LLog_nid_postfix(x, 0) + Ln2, and the log1pl/sqrtl formulation for the intermediate range. - core/libs/prx/libc/src/Math.cpp:asinhl_nid_postfix: Verified classification via _LDtest_nid_postfix for zeros, infinities, and NaNs, sign restoration across all branches, tiny cutoff at |a| < 2^-66, large argument cutoff at |a| >= 2^67, and the numerically stable log1pl transformation. - core/libs/prx/libc/src/Math.cpp:atanhl_nid_postfix: Verified pole check at |a| == 1.0 raising FE_DIVBYZERO (0x04) returning signed infinity, domain validation for |a| > 1.0 raising FE_INVALID (0x01) returning quiet NaN, small argument bypass at |a| < 2^-33, and logarithmic transformation log1pl_nid_postfix(q + q) * 0.5L. - core/libs/tests/GuestDinkumwareInternals.cpp:CheckInverseHyperbolic: Verified test harness verifies bit exactness (SameBits), errno, floating-point exception flags (FE_INVALID, FE_DIVBYZERO), and control-word invariance at both 64-bit and 53-bit x87 precisions. - AGENTS.md and docs/dev/debt/libc.md: Verified zero comments in code, correct debt file updates reflecting the new implementations, no title-specific hardcoding, and clean repository conventions compliance. - Minor: please drop the `Generated with` footer from the description; the AI-assisted checkbox can stay. Looks Good To Me
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!18694
No description provided.