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

Merged
JJenkx merged 2118 commits from upstream-pr/10222 into main 2026-11-01 01:57:49 +00:00
JJenkx commented 2026-10-23 14:28:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T14:28:17Z.


What

expm1f returned the host's expm1, 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 155,520,115 of the 2^32 floats, for example 0x3eb17218, where the console gives 0x3ed413cd and UCRT 0x3ed413ce, and 0xbf400ddd, where the console gives 0xbf071989 and UCRT 0xbf071988.

The titles' expm1f (NID 3EgxfDRefdw) is FreeBSD msun's s_expm1f.c. #9867 already carries a full port of it as tanhf's private TanhfExpm1. This PR renames that function Expm1f and exports it as expm1f, so both functions share one copy of the algorithm.

errno: msun sets none. The titles' copy sets ERANGE exactly when a finite argument overflows to infinity: the 1,020,169,704 finite arguments from 0x42b17218 up. That includes 0x42b17218 itself, which takes msun's general path rather than its x > o_threshold branch. Large negative arguments, which take msun's tiny - one branch and return -1, the infinities and NaN leave errno alone. The exported expm1f sets ERANGE in that case only. tanhf calls Expm1f directly, and its arguments never reach that range.

Tested

Current Linux refresh (2026-10-25): head 057bca52441f84ec539e382428892d3dc3982d1c, a linear two-topic stack on main 366dd4e6b704b048e05eee65e0edead78352811a. It restacks the #9867 prerequisite's original topic commit before this PR's expm1f topic. The ready public #9867 branch was not changed by this refresh; its previously merged-main prefix is not copied into this linear stack. Depends on remains #9867.

The reviewed Expm1f, expm1f_nid_postfix, tanhf_nid_postfix, CheckExpm1fConsoleBits and CheckTanhfConsoleBits bodies are byte-identical to previous public #10222 head 3d8ff6b82ad249bba58a1b55a0d23a23ac228932; both regression calls occur once. The only rebase conflict was additive debt-file content, resolved by preserving main's child-process registry entry and both port entries.

Fresh Linux x86-64 Release libc/test build: guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three runs (12 passes). Replacing only production Math.cpp with the exact restacked #9867 parent makes guest_math abort as expected; the other three groups pass 9/9. Restoring the candidate gives 12 further passes and a byte-identical raw libc.prx. Conventions, whitespace and merge-tree checks pass.

Exact-head refresh logs, parent control and preservation proof (SHA-256 2c91e823f1738ab080043004562a4a26bb9acabcd105b11e7a7777eb4fae3a88). Earlier combined and Windows/exhaustive/module evidence below stays pinned to its original revisions; no new Windows, original-module, exhaustive, physical-console, exception-matrix, game/GPU or performance result is claimed.

Earlier combined Linux qualification (historical): That report used exact head 3d8ff6b82ad249bba58a1b55a0d23a23ac228932; that evidence update did not change the branch. Its earlier individual qualification stays pinned to that head. The new combined qualification below places this exact branch together with the refreshed expf/erff branches on main after the #10314 merge.

The combined source retains this PR's exact Expm1f, expm1f_nid_postfix, tanhf_nid_postfix bodies and regression calls, plus current main's newly merged log1p implementation. No new individual-head run is claimed for this unchanged branch.

Requested combined qualification: private merge 73e28f82193f608b3099be52d2eb5af6a31b7285 has exact refreshed #10226 head 6871263bf330ef858be42013737e02cb2b021add and unchanged #10222 head 3d8ff6b82ad249bba58a1b55a0d23a23ac228932 as parents. It includes refreshed #10113, #9867 and the attributed #10007 prerequisite. All five ports, helpers, regression functions/calls and debt entries are retained, together with main's log1p. All four focused groups pass3/3; conventions and whitespace pass. The merge commit is private test integration only.

Landing order: #10007 before #10113, with open #9771's previously declared earlier position retained; #9867 before #10222; both #10113 and #10222 before #10226. #9771 is not included in this combined run. #10226's insertion conflict is resolved by keeping both distinct ports and it will refresh after prerequisites land. The current combined qualification supersedes the earlier ce4b6685/18b148ba previews for these refreshed heads.

Fresh exact-head and combined logs, controls and preservation proofs (SHA-256 acf5aaa75e68171a861939c1fcdfada7a9a2bf6ca13b789e3a6b514597e2aeda).

Historical measurements: previous Windows/module and integration reports below retain their measured revisions. No new Windows, title/module, exhaustive reference matrix, console, floating-point-exception or performance claim is added.

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

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their expm1f with this port for all 2^32 arguments, checking result bits and whether errno is set.

    • PPSA07923 and PPSA05916: identical bits and errno everywhere.
    • PPSA13934: identical errno everywhere. The bits differ by 1 ulp on 197,464 arguments, because that copy is an FMA-contracted build, as with expf (#10113) and tanhf (#9867).
    • The host std::expm1 that main uses differs from PPSA07923 on 155,520,115 arguments.
  • guest_math: the new CheckExpm1fConsoleBits has 34 cases with the console's results and errno:

    • values in each of msun's reduction ranges, and both sides of each range edge;
    • ±0, tiny and subnormal arguments, ±1;
    • the overflow edge on both sides, FLT_MAX and the infinities;
    • the tiny - one edge on both sides, -FLT_MAX;
    • NaN payloads of both signs.

    guest_math passes in three runs out of three on the exact head.

  • Negative control: with Math.cpp from #9867's head and this test, guest_math aborts in a Require.

  • Historical head: 33a697c559, this PR's one commit on top of #9867's refreshed head 82668d5961, both on current main 4997726a5f. guest_math passes in three runs out of three on this exact head. python tools/check_conventions.py --base 82668d5961 and git diff --check pass.

  • Landing order with #10226: this PR lands first. The two PRs each replace one of two adjacent host wrappers in Math.cpp (expm1f here, erff there), so their merge has a one-hunk textual conflict whose resolution keeps both ports. A preview merge of both refreshed heads with that resolution (18b148ba55) passes guest_math three times out of three. #10226 records Depends on: #10222.

  • Not tested: Linux and macOS (the result no longer depends on the host's expm1), a console, the floating-point exception flags.

Current Linux qualification (2026-10-28)

Rebased onto main 2227f1366188cda5699dd20e2e756a18006a5d4b. Linux CPU validation on 2026-10-28 at de5fd246210fbef0ecfdae38cb776d7708f0bf95 (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON).

libc, guest_math_tests and guest_math_exact_tests rebuilt from this exact source. guest_math and guest_math_exact each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass.

The earlier review cleanup is preserved: TanhfExpm1 is absent, tanhf shares the single Expm1f implementation, and the debt entry describes the exported port.

Exact source pins, preservation proofs, dependency checks, build logs and JUnit, SHA-256 1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed.

Qualification update — 2026-10-30, repeat 2

Integrated pinned main e471817c68d4e1b1e44b0ce369ed88bafe4ae8ed at 74fceb9201aa7e636bd0a59dcb0e7e1ab023ffbb, retaining original topic commits as ancestors. Actual parent ancestry: #9867 a06ed844e352d33f89f6ed015da3203745330acd. The full current-main logb/logbf debt entry is retained; inherited unused TanhfExpm1 is absent where shared Expm1f supersedes it.

Actual Linux libc/focused binaries rebuilt from this final head; guest_math, guest_math_exact, guest_dinkumware_internals each pass three consecutive runs (9 runtime passes), plus public_headers. Conventions against current origin/main and whitespace pass. Pinned main passed all seven runtime groups plus the header audit three times (24 runs). Full source/function/test/debt/header and 31 recursive dependency / 17,742 tracked-blob proofs are attached. Later main 1c6b316cf762 has 17 off-topic commits, no relevant libc/numeric/qsort/gitlink changes, and all 9 merge previews are clean; those preview trees were not executed.

Focused Linux CPU scope only: prior Windows/SDK/reference reports keep original pins. No fresh Windows, console, SDK-module, title/GPU or speed measurement is claimed by this report. AI-assisted: yes. Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math9-maine471-linux-20261030-repeat2.tar.gz (SHA256 5da4ce1732b45bb3fc566c31078c8c9ae8bf0c403654dfc60d15ee28490e20bf).

AI-assisted: yes (Codex).

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: #9867
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4984325077 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10222). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T14:28:17Z. --- ### What `expm1f` returned the host's `expm1`, 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 155,520,115 of the 2^32 floats, for example `0x3eb17218`, where the console gives `0x3ed413cd` and UCRT `0x3ed413ce`, and `0xbf400ddd`, where the console gives `0xbf071989` and UCRT `0xbf071988`. The titles' `expm1f` (NID `3EgxfDRefdw`) is FreeBSD msun's [`s_expm1f.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_expm1f.c). #9867 already carries a full port of it as `tanhf`'s private `TanhfExpm1`. This PR renames that function `Expm1f` and exports it as `expm1f`, so both functions share one copy of the algorithm. **`errno`:** msun sets none. The titles' copy sets `ERANGE` exactly when a finite argument overflows to infinity: the 1,020,169,704 finite arguments from `0x42b17218` up. That includes `0x42b17218` itself, which takes msun's general path rather than its `x > o_threshold` branch. Large negative arguments, which take msun's `tiny - one` branch and return -1, the infinities and NaN leave `errno` alone. The exported `expm1f` sets `ERANGE` in that case only. `tanhf` calls `Expm1f` directly, and its arguments never reach that range. ### Tested **Current Linux refresh (2026-10-25):** head `057bca52441f84ec539e382428892d3dc3982d1c`, a linear two-topic stack on main `366dd4e6b704b048e05eee65e0edead78352811a`. It restacks the #9867 prerequisite's original topic commit before this PR's `expm1f` topic. The ready public #9867 branch was not changed by this refresh; its previously merged-main prefix is not copied into this linear stack. Depends on remains #9867. The reviewed `Expm1f`, `expm1f_nid_postfix`, `tanhf_nid_postfix`, `CheckExpm1fConsoleBits` and `CheckTanhfConsoleBits` bodies are byte-identical to previous public #10222 head `3d8ff6b82ad249bba58a1b55a0d23a23ac228932`; both regression calls occur once. The only rebase conflict was additive debt-file content, resolved by preserving main's child-process registry entry and both port entries. Fresh Linux x86-64 Release libc/test build: `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three runs (12 passes). Replacing only production `Math.cpp` with the exact restacked #9867 parent makes `guest_math` abort as expected; the other three groups pass 9/9. Restoring the candidate gives 12 further passes and a byte-identical raw `libc.prx`. Conventions, whitespace and merge-tree checks pass. [Exact-head refresh logs, parent control and preservation proof](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10222-head057bca52-main366d-linux-refresh.tar.gz) (SHA-256 `2c91e823f1738ab080043004562a4a26bb9acabcd105b11e7a7777eb4fae3a88`). Earlier combined and Windows/exhaustive/module evidence below stays pinned to its original revisions; no new Windows, original-module, exhaustive, physical-console, exception-matrix, game/GPU or performance result is claimed. **Earlier combined Linux qualification (historical):** That report used exact head `3d8ff6b82ad249bba58a1b55a0d23a23ac228932`; that evidence update did not change the branch. Its earlier individual qualification stays pinned to that head. The new combined qualification below places this exact branch together with the refreshed expf/erff branches on main after the #10314 merge. The combined source retains this PR's exact `Expm1f`, `expm1f_nid_postfix`, `tanhf_nid_postfix` bodies and regression calls, plus current main's newly merged `log1p` implementation. No new individual-head run is claimed for this unchanged branch. **Requested combined qualification:** private merge `73e28f82193f608b3099be52d2eb5af6a31b7285` has exact refreshed #10226 head `6871263bf330ef858be42013737e02cb2b021add` and unchanged #10222 head `3d8ff6b82ad249bba58a1b55a0d23a23ac228932` as parents. It includes refreshed #10113, #9867 and the attributed #10007 prerequisite. All five ports, helpers, regression functions/calls and debt entries are retained, together with main's log1p. All four focused groups pass3/3; conventions and whitespace pass. The merge commit is private test integration only. **Landing order:** #10007 before #10113, with open #9771's previously declared earlier position retained; #9867 before #10222; both #10113 and #10222 before #10226. #9771 is not included in this combined run. #10226's insertion conflict is resolved by keeping both distinct ports and it will refresh after prerequisites land. The current combined qualification supersedes the earlier ce4b6685/18b148ba previews for these refreshed heads. [Fresh exact-head and combined logs, controls and preservation proofs](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10113-68c7bf01-pr10226-6871263b-combined-73e28f82-main63a8-linux-validation.tar.gz) (SHA-256 `acf5aaa75e68171a861939c1fcdfada7a9a2bf6ca13b789e3a6b514597e2aeda`). **Historical measurements:** previous Windows/module and integration reports below retain their measured revisions. No new Windows, title/module, exhaustive reference matrix, console, floating-point-exception or performance claim is added. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `expm1f` with this port for all 2^32 arguments, checking result bits and whether `errno` is set. - PPSA07923 and PPSA05916: identical bits and `errno` everywhere. - PPSA13934: identical `errno` everywhere. The bits differ by 1 ulp on 197,464 arguments, because that copy is an FMA-contracted build, as with `expf` (#10113) and `tanhf` (#9867). - The host `std::expm1` that `main` uses differs from PPSA07923 on 155,520,115 arguments. - **`guest_math`:** the new `CheckExpm1fConsoleBits` has 34 cases with the console's results and `errno`: - values in each of msun's reduction ranges, and both sides of each range edge; - ±0, tiny and subnormal arguments, ±1; - the overflow edge on both sides, `FLT_MAX` and the infinities; - the `tiny - one` edge on both sides, `-FLT_MAX`; - NaN payloads of both signs. `guest_math` passes in three runs out of three on the exact head. - **Negative control:** with `Math.cpp` from #9867's head and this test, `guest_math` aborts in a `Require`. - **Historical head:** `33a697c559`, this PR's one commit on top of #9867's refreshed head `82668d5961`, both on current `main` 4997726a5f. `guest_math` passes in three runs out of three on this exact head. `python tools/check_conventions.py --base 82668d5961` and `git diff --check` pass. - **Landing order with #10226:** this PR lands first. The two PRs each replace one of two adjacent host wrappers in `Math.cpp` (`expm1f` here, `erff` there), so their merge has a one-hunk textual conflict whose resolution keeps both ports. A preview merge of both refreshed heads with that resolution (`18b148ba55`) passes `guest_math` three times out of three. #10226 records `Depends on: #10222`. - **Not tested:** Linux and macOS (the result no longer depends on the host's `expm1`), a console, the floating-point exception flags. <!-- codex-math-refresh-20261028-repeat-pr10222 --> ### Current Linux qualification (2026-10-28) Rebased onto `main` `2227f1366188cda5699dd20e2e756a18006a5d4b`. Linux CPU validation on 2026-10-28 at `de5fd246210fbef0ecfdae38cb776d7708f0bf95` (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON). `libc`, `guest_math_tests` and `guest_math_exact_tests` rebuilt from this exact source. `guest_math` and `guest_math_exact` each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass. The earlier review cleanup is preserved: `TanhfExpm1` is absent, `tanhf` shares the single `Expm1f` implementation, and the debt entry describes the exported port. [Exact source pins, preservation proofs, dependency checks, build logs and JUnit](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math25-main2227-linux-20261028-repeat.tar.gz), SHA-256 `1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68`. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed. <!-- codex-maintenance-20261030-repeat2-10222 --> **Qualification update — 2026-10-30, repeat 2** Integrated pinned main `e471817c68d4e1b1e44b0ce369ed88bafe4ae8ed` at `74fceb9201aa7e636bd0a59dcb0e7e1ab023ffbb`, retaining original topic commits as ancestors. Actual parent ancestry: #9867 `a06ed844e352d33f89f6ed015da3203745330acd`. The full current-main logb/logbf debt entry is retained; inherited unused TanhfExpm1 is absent where shared Expm1f supersedes it. Actual Linux libc/focused binaries rebuilt from this final head; `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` each pass three consecutive runs (9 runtime passes), plus public_headers. Conventions against current origin/main and whitespace pass. Pinned main passed all seven runtime groups plus the header audit three times (24 runs). Full source/function/test/debt/header and 31 recursive dependency / 17,742 tracked-blob proofs are attached. Later main `1c6b316cf762` has 17 off-topic commits, no relevant libc/numeric/qsort/gitlink changes, and all 9 merge previews are clean; those preview trees were not executed. Focused Linux CPU scope only: prior Windows/SDK/reference reports keep original pins. No fresh Windows, console, SDK-module, title/GPU or speed measurement is claimed by this report. AI-assisted: yes. Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math9-maine471-linux-20261030-repeat2.tar.gz (SHA256 5da4ce1732b45bb3fc566c31078c8c9ae8bf0c403654dfc60d15ee28490e20bf). AI-assisted: yes (Codex). ### 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: #9867 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 14:45:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T14:45:14Z.

Review: CHANGES_REQUESTED


Re-review of exact head a2b86c6f364ffb1c0c5c0796198fd9fe5faef5d2. The #9867 dependency has an exact-head approval, and this PR reports exhaustive Windows comparison plus focused exact-head tests.

#10226 is now also open from your account and adds the separate erff/erfcf port. A pairwise merge preview of the exact #10222 and #10226 heads conflicts in Math.cpp. Please record a keep-both landing order between them (and Depends on on the later one), then rerun the focused math tests on the combined head. Current main also changed Math.cpp, GuestMath.cpp, and the libc debt file since this stack's merge base; refresh the stack before the final combined run.

This is an integration request only; the expm1f and erff/erfcf exports are distinct, and I am not asking for either PR to be closed.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5629925632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10222#pullrequestreview-5629925632). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T14:45:14Z. **Review:** CHANGES_REQUESTED --- Re-review of exact head `a2b86c6f364ffb1c0c5c0796198fd9fe5faef5d2`. The #9867 dependency has an exact-head approval, and this PR reports exhaustive Windows comparison plus focused exact-head tests. #10226 is now also open from your account and adds the separate `erff`/`erfcf` port. A pairwise merge preview of the exact #10222 and #10226 heads conflicts in `Math.cpp`. Please record a keep-both landing order between them (and `Depends on` on the later one), then rerun the focused math tests on the combined head. Current main also changed `Math.cpp`, `GuestMath.cpp`, and the libc debt file since this stack's merge base; refresh the stack before the final combined run. This is an integration request only; the `expm1f` and `erff`/`erfcf` exports are distinct, and I am not asking for either PR to be 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!10222
No description provided.