fix(libc): compute expm1 with FreeBSD msun's algorithm, as titles' libc.prx does #10306

Merged
JJenkx merged 1 commit from upstream-pr/10306 into main 2026-10-24 06:55:26 +00:00
JJenkx commented 2026-10-23 17:02:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T17:02:57Z.


What

expm1 returned the host's expm1, so its last bit and its errno depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 35,152,346 of 320 million random arguments. For example, expm1(0x3fc78b9e474afed8) (about 0.184) is 0x3fc9d99e60806b35 in the bundled libc.prx and 0x3fc9d99e60806b36 here.

The titles' copy (NID gqKfOiJaCOo) is FreeBSD msun's s_expm1.c. expm1 now computes it as a static Expm1:

  • the |x| >= 56 ln2 filter, with the large-negative tiny - 1 return;
  • the k = ±1 and general argument reductions with the c correction;
  • the Q1–Q5 rational;
  • every k case of the reconstruction, including k == 1024 and the subnormal scaling.

huge is read at run time, as msun's volatile read does. This is the double-precision counterpart of the expm1f in #10222.

errno: msun sets none. The titles' copy sets ERANGE exactly when a finite argument overflows to infinity, which is the same rule as its expm1f. Large negative arguments return -1 without errno, and so do NaN and the infinities. The exported expm1 sets ERANGE in that case only.

Tested

Current Linux refresh: 99abd2604d53b7588b6386de012527c60ad6dcc7 on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. Fresh isolated libc/math build passed; guest_math and guest_dinkumware_internals each passed three consecutive runs. Diff and conventions checks pass; Math.cpp and GuestMath.cpp topic changes remain unchanged after rebase. Linux logs and rebase checks.

The Windows module comparisons and negative control below are historical evidence from original head 92e60da1cc495f62a17a81000fb5778dec79df44; Windows was not rerun on the refreshed commit. Results described as console results mean the supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console.

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main 1e1c355a54.

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their expm1 with this PR's code, checking result bits (NaN payloads included) and errno, on:

    • 320 million random arguments per title: a third random bit patterns, two thirds random significands with exponents 2^-63 to 2^9;
    • 47,222 structured arguments:
      • every integer from −800 to 800 and every 1/64 step over that range;
      • the 4,000 floats on each side of ±56 ln2, ±ln2/2, ±1.5 ln2, the overflow threshold, ±2^-54, −0.25 and 1;
      • the special values.

    Results:

    • PPSA07923 and PPSA05916: identical bits and errno on all of them.
    • PPSA13934: identical errno. The bits differ on 43,433 random arguments, because that copy is an FMA-contracted build.
  • guest_math: the new CheckExpm1ConsoleBits has 30 cases with the bundled library's results and errno:

    • ±0 and the 2^-54 shortcut;
    • both sides of ln2/2 and 1.5 ln2;
    • −0.25, −0.5 and ±1;
    • ±56 ln2 and 50;
    • both sides of the overflow threshold and −1000;
    • ±DBL_MAX, the infinities and NaN payloads of both signs;
    • six arguments where UCRT differs.

    main's existing expm1 checks still pass.

    guest_math and guest_dinkumware_internals pass in three runs out of three on the exact head.

  • Negative control: with main's Math.cpp and these tests, guest_math aborts in a Require, while guest_dinkumware_internals still passes.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Overlap and order: Refreshed as one standalone expm1 topic on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. The prior #10283 dependency described series order, not a required implementation. Nearby math ports share text/test/debt anchors. #10316 still requires this PR's internal Expm1 helper.

  • Not tested: macOS, a physical console, floating-point exception flags, and expm1l. The original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above.

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: none; the msun function is rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986410401 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10306). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T17:02:57Z. --- ### What `expm1` returned the host's `expm1`, so its last bit and its `errno` depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 35,152,346 of 320 million random arguments. For example, `expm1(0x3fc78b9e474afed8)` (about 0.184) is `0x3fc9d99e60806b35` in the bundled `libc.prx` and `0x3fc9d99e60806b36` here. The titles' copy (NID `gqKfOiJaCOo`) is FreeBSD msun's [`s_expm1.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_expm1.c). `expm1` now computes it as a static `Expm1`: - the `|x| >= 56 ln2` filter, with the large-negative `tiny - 1` return; - the `k = ±1` and general argument reductions with the `c` correction; - the `Q1`–`Q5` rational; - every `k` case of the reconstruction, including `k == 1024` and the subnormal scaling. `huge` is read at run time, as msun's `volatile` read does. This is the double-precision counterpart of the `expm1f` in #10222. **`errno`:** msun sets none. The titles' copy sets `ERANGE` exactly when a finite argument overflows to infinity, which is the same rule as its `expm1f`. Large negative arguments return -1 without `errno`, and so do NaN and the infinities. The exported `expm1` sets `ERANGE` in that case only. ### Tested Current Linux refresh: `99abd2604d53b7588b6386de012527c60ad6dcc7` on main `6c52b649e7b1592f28db0cb8fe9ac604f0a242e7`. Fresh isolated libc/math build passed; `guest_math` and `guest_dinkumware_internals` each passed three consecutive runs. Diff and conventions checks pass; Math.cpp and GuestMath.cpp topic changes remain unchanged after rebase. [Linux logs and rebase checks](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10306-head99abd260-linux-validation.tar.gz). The Windows module comparisons and negative control below are historical evidence from original head `92e60da1cc495f62a17a81000fb5778dec79df44`; Windows was not rerun on the refreshed commit. Results described as console results mean the supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 1e1c355a54. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `expm1` with this PR's code, checking result bits (NaN payloads included) and `errno`, on: - 320 million random arguments per title: a third random bit patterns, two thirds random significands with exponents 2^-63 to 2^9; - 47,222 structured arguments: - every integer from −800 to 800 and every 1/64 step over that range; - the 4,000 floats on each side of ±56 ln2, ±ln2/2, ±1.5 ln2, the overflow threshold, ±2^-54, −0.25 and 1; - the special values. Results: - PPSA07923 and PPSA05916: identical bits and `errno` on all of them. - PPSA13934: identical `errno`. The bits differ on 43,433 random arguments, because that copy is an FMA-contracted build. - **`guest_math`:** the new `CheckExpm1ConsoleBits` has 30 cases with the bundled library's results and `errno`: - ±0 and the 2^-54 shortcut; - both sides of ln2/2 and 1.5 ln2; - −0.25, −0.5 and ±1; - ±56 ln2 and 50; - both sides of the overflow threshold and −1000; - `±DBL_MAX`, the infinities and NaN payloads of both signs; - six arguments where UCRT differs. `main`'s existing `expm1` checks still pass. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on the exact head. - **Negative control:** with `main`'s `Math.cpp` and these tests, `guest_math` aborts in a `Require`, while `guest_dinkumware_internals` still passes. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap and order:** Refreshed as one standalone expm1 topic on main `6c52b649e7b1592f28db0cb8fe9ac604f0a242e7`. The prior #10283 dependency described series order, not a required implementation. Nearby math ports share text/test/debt anchors. #10316 still requires this PR's internal Expm1 helper. - **Not tested:** macOS, a physical console, floating-point exception flags, and `expm1l`. The original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above. ### 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](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: none; the msun function is rewritten, as #9668, #5036 and the `sinhf` entry did - [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 AI-assisted (Claude Code).
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!10306
No description provided.