fix(libc): compute _Exp and _FExp as titles do #12037

Merged
JJenkx merged 1 commit from upstream-pr/12037 into main 2026-10-28 09:40:20 +00:00
JJenkx commented 2026-10-27 17:19:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-27T17:19:11Z.


What

_Exp and _FExp computed finite results in a different form from the libc.prx that titles ship. As a result, about one result in five was one unit in the last place (ulp) away from the titles. The titles' disassembly (PPSA07923, _FExp at 0x8712d, _Exp at 0x86a60) shows three differences:

  • Rational step: the titles evaluate ((Q(z) + g·P(z)) + 1) / ((Q(z) − g·P(z)) + 1). main evaluated (w + g) / (w − g) · 2 and decremented the exponent.
  • Double constants: _Exp uses its own ln 2 split (0x1.62e42f8p-1, 0x1.be8e7bcd5e4f2p-27) and four coefficients. main used a different coefficient set. These are now taken bit for bit from the binary. _FExp's constants already matched.
  • Shortcut threshold: the titles return the multiplier unchanged only within half an epsilon of zero, not a full epsilon.

Special operands, cutoffs and flags are unchanged.

No audited title imports these two directly: none of the 22 relinker registries I checked lists them. They matter as building blocks. The titles' _FSinh and _FCosh go through _FExp, which is the gap the existing _FSinh/_FCosh debt entry describes.

Tested

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

Comparison: a scratch loader runs the decrypted libc.prx of 11 titles natively. It calls their _Exp/_FExp by NID and compares the full result bits, return kind and errno with this build's.

  • Inputs: 300,000 calls per function on each title. Arguments are half random bit patterns and half evenly spread values (±1000 for _Exp, ±100 for _FExp). Multipliers are 0.25 to 3 and either sign, and offsets are −3 to 3.
titles main _FExp / _Exp this PR _FExp / _Exp
PPSA04158, PPSA05916, PPSA07923, PPSA20423 60,665 / 55,281 0 / 0
PPSA09614, PPSA13934, PPSA25636, PPSA26528, PPSA33655, PPSA34008, PPSA35169 60,772 / 55,278 889 / 15

The second group's libc.prx contracts multiply-adds to FMA (PPSA13934's has 1,105 vfmadd-family instructions), which portable C++ doesn't reproduce. Its remaining differences are one ulp.

guest_dinkumware_internals: eight new checks pin titles' bits where main differed. That covers three ordinary arguments and one just above half an epsilon, for each function.

Results:

  • guest_dinkumware_internals and guest_math pass in each of 3 runs on f94e461665. That commit has the same code and tests as the head 79d65956a1, which only shortens the debt entry.
  • Negative control: with main's DinkumwareInternals.cpp and these tests, guest_dinkumware_internals fails at "_Exp rounds a negative exponent as titles do".
  • python tools/check_conventions.py --base origin/main and git diff --check pass.

Not tested: a console, Linux and macOS, signaling NaNs, and non-default rounding or flush-to-zero modes.

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
  • 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:5038187632 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12037). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-27T17:19:11Z. --- ### What `_Exp` and `_FExp` computed finite results in a different form from the `libc.prx` that titles ship. As a result, about one result in five was one unit in the last place (ulp) away from the titles. The titles' disassembly (PPSA07923, `_FExp` at 0x8712d, `_Exp` at 0x86a60) shows three differences: - **Rational step:** the titles evaluate `((Q(z) + g·P(z)) + 1) / ((Q(z) − g·P(z)) + 1)`. `main` evaluated `(w + g) / (w − g) · 2` and decremented the exponent. - **Double constants:** `_Exp` uses its own ln 2 split (`0x1.62e42f8p-1`, `0x1.be8e7bcd5e4f2p-27`) and four coefficients. `main` used a different coefficient set. These are now taken bit for bit from the binary. `_FExp`'s constants already matched. - **Shortcut threshold:** the titles return the multiplier unchanged only within half an epsilon of zero, not a full epsilon. Special operands, cutoffs and flags are unchanged. No audited title imports these two directly: none of the 22 relinker registries I checked lists them. They matter as building blocks. The titles' `_FSinh` and `_FCosh` go through `_FExp`, which is the gap the existing `_FSinh`/`_FCosh` debt entry describes. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, on `main` `3ae8209b64`. **Comparison:** a scratch loader runs the decrypted `libc.prx` of 11 titles natively. It calls their `_Exp`/`_FExp` by NID and compares the full result bits, return kind and `errno` with this build's. - **Inputs:** 300,000 calls per function on each title. Arguments are half random bit patterns and half evenly spread values (±1000 for `_Exp`, ±100 for `_FExp`). Multipliers are 0.25 to 3 and either sign, and offsets are −3 to 3. | titles | `main` `_FExp` / `_Exp` | this PR `_FExp` / `_Exp` | |---|---|---| | PPSA04158, PPSA05916, PPSA07923, PPSA20423 | 60,665 / 55,281 | **0 / 0** | | PPSA09614, PPSA13934, PPSA25636, PPSA26528, PPSA33655, PPSA34008, PPSA35169 | 60,772 / 55,278 | 889 / 15 | The second group's `libc.prx` contracts multiply-adds to FMA (PPSA13934's has 1,105 `vfmadd`-family instructions), which portable C++ doesn't reproduce. Its remaining differences are one ulp. **`guest_dinkumware_internals`:** eight new checks pin titles' bits where `main` differed. That covers three ordinary arguments and one just above half an epsilon, for each function. **Results:** - `guest_dinkumware_internals` and `guest_math` pass in each of 3 runs on `f94e461665`. That commit has the same code and tests as the head `79d65956a1`, which only shortens the debt entry. - **Negative control:** with `main`'s `DinkumwareInternals.cpp` and these tests, `guest_dinkumware_internals` fails at "_Exp rounds a negative exponent as titles do". - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. **Not tested:** a console, Linux and macOS, signaling NaNs, and non-default rounding or flush-to-zero modes. ### 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 - [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).
MarcZX10 commented 2026-10-27 19:44:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T19:44:23Z.

Review: APPROVED


Reviewed exact head 79d65956a182ed5f130e38d4eed9e51247f824a1. The implementation and pinned tests align with the cited bundled libc.prx operation order, constants and half-epsilon threshold. The report includes title-by-title oracle comparisons, a negative control showing the old implementation fails the new test, and records the remaining one-ulp differences for binaries that contract multiply-adds to FMA in the debt entry. The targeted guest_dinkumware_internals and guest_math tests are reported passing three times; hosted Windows and the current relinker, ASan, register-context and write-watch matrices pass. Conventions, intake, labels and report checks pass. Linux full-build checks were skipped, and I did not independently run the Windows title oracle or a console test.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5673268984 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/12037#pullrequestreview-5673268984). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T19:44:23Z. **Review:** APPROVED --- Reviewed exact head `79d65956a182ed5f130e38d4eed9e51247f824a1`. The implementation and pinned tests align with the cited bundled `libc.prx` operation order, constants and half-epsilon threshold. The report includes title-by-title oracle comparisons, a negative control showing the old implementation fails the new test, and records the remaining one-ulp differences for binaries that contract multiply-adds to FMA in the debt entry. The targeted `guest_dinkumware_internals` and `guest_math` tests are reported passing three times; hosted Windows and the current relinker, ASan, register-context and write-watch matrices pass. Conventions, intake, labels and report checks pass. Linux full-build checks were skipped, and I did not independently run the Windows title oracle or a console test.
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!12037
No description provided.