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

Merged
JJenkx merged 3 commits from upstream-pr/9844 into main 2026-10-25 03:36:35 +00:00
JJenkx commented 2026-10-22 17:41:41 +00:00 (Migrated from github.com)

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


What

exp2f returned the host's exp2, 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 213,306,266 of the 2^32 floats (5 %). Examples, console then main: 0x3f000303 → 0x3fb5066d, and 0xbf400909 → 0x3f183437.

The titles' exp2f (NID wuAQt-j+p4o) is FreeBSD msun's s_exp2f.c. exp2f now computes it the same way:

  • the 16-entry double table exp2ft;
  • the reduction x + redux, rounded to float as STRICT_ASSIGN does;
  • the degree-4 polynomial evaluated in double;
  • the 2^k scale built from its high word;
  • 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 msun's overflow branch (x >= 128) and its underflow branch (x <= -150). A subnormal result leaves errno alone, for example exp2f(-149) = 0x00000001. So do NaN and the infinities. This port sets ERANGE in those two branches only.

exp2f is imported by all 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Unlike expf (#9839) and logf (#9841), the FMA-contracted copy of PPSA13934 gives the same bits here, because the core is in double precision, so one port matches every copy checked.

Tested

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

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. Their exp2f and this build's exp2f_nid_postfix give the same bits and the same errno for all 2^32 arguments, in all three copies.
  • guest_math: the new CheckExp2fConsoleBits has 24 cases with the console's results and errno: values in each range where UCRT differs, ±0, the 2^-25 shortcut, 1, the edges below and at 128, FLT_MAX, the infinities, the smallest normal and subnormal results, -149 and its neighbour, -150 and below, -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.
  • Overlap: exp2f's one-line definition sits between the expf and logf lines of Math.cpp, so this conflicts textually with #9839 and #9841, which replace those neighbours. Keeping all three changes resolves it; the new exp2f body lives after logbf. There are no other conflicts with comparable PRs, and none with #9771, #9777 or #9784.
  • Independent Linux review: Dom3005's report tested head 45a5dde7d merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports five passing guest_math runs (--repeat until-fail:5). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.
  • Not tested: macOS, 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: #9841 (both edit the same spot in Math.cpp; this one rebases after it lands)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4971613805 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9844). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T17:41:41Z. --- ### What `exp2f` returned the host's `exp2`, 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 213,306,266 of the 2^32 floats (5 %). Examples, console then `main`: `0x3f000303` → `0x3fb5066d`, and `0xbf400909` → `0x3f183437`. The titles' `exp2f` (NID `wuAQt-j+p4o`) is FreeBSD msun's [`s_exp2f.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_exp2f.c). `exp2f` now computes it the same way: - the 16-entry double table `exp2ft`; - the reduction `x + redux`, rounded to float as `STRICT_ASSIGN` does; - the degree-4 polynomial evaluated in double; - the `2^k` scale built from its high word; - `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 msun's overflow branch (`x >= 128`) and its underflow branch (`x <= -150`). A subnormal result leaves `errno` alone, for example `exp2f(-149)` = `0x00000001`. So do NaN and the infinities. This port sets `ERANGE` in those two branches only. `exp2f` is imported by all 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Unlike `expf` (#9839) and `logf` (#9841), the FMA-contracted copy of PPSA13934 gives the same bits here, because the core is in double precision, so one port matches every copy checked. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 6a6a35fdf5. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. Their `exp2f` and this build's `exp2f_nid_postfix` give the same bits and the same `errno` for all 2^32 arguments, in all three copies. - **`guest_math`:** the new `CheckExp2fConsoleBits` has 24 cases with the console's results and `errno`: values in each range where UCRT differs, ±0, the `2^-25` shortcut, 1, the edges below and at 128, `FLT_MAX`, the infinities, the smallest normal and subnormal results, -149 and its neighbour, -150 and below, `-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. - **Overlap:** `exp2f`'s one-line definition sits between the `expf` and `logf` lines of `Math.cpp`, so this conflicts textually with #9839 and #9841, which replace those neighbours. Keeping all three changes resolves it; the new `exp2f` body lives after `logbf`. There are no other conflicts with comparable PRs, and none with #9771, #9777 or #9784. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9844#issuecomment-6353485654) tested head `45a5dde7d` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports five passing `guest_math` runs (`--repeat until-fail:5`). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim. - Not tested: macOS, 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: #9841 (both edit the same spot in `Math.cpp`; this one rebases after it lands) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 03:51:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T03:51:20Z.

Review: APPROVED


Reviewed exact head 45a5dde7df1031f0d7c563b6879b8cfd664b69fd. The port follows FreeBSD 11.4 msun's exp2f reduction, table, polynomial and scaling; errno handling and the 24 boundary/special-value cases are explicit. The production and test files are unchanged between the branch base and current upstream main; only the adjacent debt index changed.

I built the exact PR Math.cpp and GuestMath.cpp in the available older configured Release build and ran the isolated CheckExp2fConsoleBits; all 24 cases passed on Linux. This is a focused check, not a fresh full-tree build. The author reports exhaustive all-float comparisons on three Windows title libraries and repeated Windows tests; independent reviewer evidence reports five passing Linux guest_math runs. Hosted policy and relinker/register-context/write-watch checks pass; product jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623673709 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9844#pullrequestreview-5623673709). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T03:51:20Z. **Review:** APPROVED --- Reviewed exact head `45a5dde7df1031f0d7c563b6879b8cfd664b69fd`. The port follows FreeBSD 11.4 msun's `exp2f` reduction, table, polynomial and scaling; errno handling and the 24 boundary/special-value cases are explicit. The production and test files are unchanged between the branch base and current upstream `main`; only the adjacent debt index changed. I built the exact PR `Math.cpp` and `GuestMath.cpp` in the available older configured Release build and ran the isolated `CheckExp2fConsoleBits`; all 24 cases passed on Linux. This is a focused check, not a fresh full-tree build. The author reports exhaustive all-float comparisons on three Windows title libraries and repeated Windows tests; independent reviewer evidence reports five passing Linux `guest_math` runs. Hosted policy and relinker/register-context/write-watch checks pass; product jobs are skipped.
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!9844
No description provided.