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

Closed
JJenkx wants to merge 1 commit from upstream-pr/9839 into main
JJenkx commented 2026-10-22 17:09:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T17:09:31Z.


What

expf returned the host's exp, so its last bit and its errno depended on the host. On Windows (UCRT) the bits differ from the libc.prx that titles ship on 19,194,808 of the 2^32 floats, for example 0x3f000306 → 0x3fd30bca. UCRT also sets ERANGE on a different set of arguments from the titles' copy.

The titles' expf (NID 8zsu04XNsZ4) is FreeBSD msun's e_expf.c. expf now computes it the same way:

  • the overflow/underflow filter;
  • the two argument reductions, including k = invln2*x + halF[xsb] truncated to int;
  • the P1/P2 rational and the 2^k scaling, with the k < -125 and k == 128 paths;
  • huge and twom100 read at run time, as msun's volatile reads do.

errno: msun sets none, but the titles' copy sets ERANGE in exactly two places: msun's overflow branch (x > o_threshold) and its underflow branch (x < u_threshold). A result that rounds to zero or a subnormal on the general path leaves errno alone. For example, expf(0xc2cff1b5) returns +0 with no errno and expf(0xc2cff1b6) returns +0 with ERANGE. NaN and the infinities never set it. This port sets ERANGE in those two branches only.

expf is imported by 20 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA04158.

Tested

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

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their expf with this build's expf_nid_postfix for all 2^32 arguments, bits and errno (an untouched sentinel on our side must match "no errno" there).
    • PPSA07923 and PPSA05916: identical bits and errno everywhere.
    • PPSA13934: identical errno everywhere. The bits differ by 1 ulp on 82,009 arguments, because that copy is an FMA-contracted build, as with asinf (#9771) and acosf (#9668). No single port matches every title's last bit.
  • guest_math: the new CheckExpfConsoleBits has 23 cases with the console's results and errno: values in each range where UCRT differs, ±0, tiny, 1, the overflow edge on both sides, FLT_MAX, the infinities, the subnormal-result range, the underflow edge on both sides, -FLT_MAX and NaN payloads of both signs. guest_math and the libc tests (8) pass in three runs out of three. With main's Math.cpp and this test, guest_math aborts in a Require.
  • python tools/check_conventions.py --base origin/main and git diff --check pass. tools/pr_overlap.py --branch finds no conflicting pull request among those it could compare. The branch also merges cleanly with #9771, #9777 and #9784.
  • Not tested: Linux and macOS (the result no longer depends on the host's expf), a console, the floating-point exception flags.

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:4971210373 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9839). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T17:09:31Z. --- ### What `expf` returned the host's `exp`, so its last bit and its `errno` depended on the host. On Windows (UCRT) the bits differ from the `libc.prx` that titles ship on 19,194,808 of the 2^32 floats, for example `0x3f000306` → `0x3fd30bca`. UCRT also sets `ERANGE` on a different set of arguments from the titles' copy. The titles' `expf` (NID `8zsu04XNsZ4`) is FreeBSD msun's [`e_expf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_expf.c). `expf` now computes it the same way: - the overflow/underflow filter; - the two argument reductions, including `k = invln2*x + halF[xsb]` truncated to `int`; - the `P1`/`P2` rational and the `2^k` scaling, with the `k < -125` and `k == 128` paths; - `huge` and `twom100` read at run time, as msun's `volatile` reads do. **`errno`:** msun sets none, but the titles' copy sets `ERANGE` in exactly two places: msun's overflow branch (`x > o_threshold`) and its underflow branch (`x < u_threshold`). A result that rounds to zero or a subnormal on the general path leaves `errno` alone. For example, `expf(0xc2cff1b5)` returns `+0` with no `errno` and `expf(0xc2cff1b6)` returns `+0` with `ERANGE`. NaN and the infinities never set it. This port sets `ERANGE` in those two branches only. `expf` is imported by 20 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA04158. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` b7d1d47a5f. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `expf` with this build's `expf_nid_postfix` for all 2^32 arguments, bits and `errno` (an untouched sentinel on our side must match "no `errno`" there). - PPSA07923 and PPSA05916: identical bits and `errno` everywhere. - PPSA13934: identical `errno` everywhere. The bits differ by 1 ulp on 82,009 arguments, because that copy is an FMA-contracted build, as with `asinf` (#9771) and `acosf` (#9668). No single port matches every title's last bit. - **`guest_math`:** the new `CheckExpfConsoleBits` has 23 cases with the console's results and `errno`: values in each range where UCRT differs, ±0, tiny, 1, the overflow edge on both sides, `FLT_MAX`, the infinities, the subnormal-result range, the underflow edge on both sides, `-FLT_MAX` and NaN payloads of both signs. `guest_math` and the libc tests (8) pass in three runs out of three. With `main`'s `Math.cpp` and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. `tools/pr_overlap.py --branch` finds no conflicting pull request among those it could compare. The branch also merges cleanly with #9771, #9777 and #9784. - Not tested: Linux and macOS (the result no longer depends on the host's `expf`), a console, the floating-point exception flags. ### 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).

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