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

Merged
JJenkx merged 5 commits from upstream-pr/10234 into main 2026-11-03 08:07:08 +00:00
JJenkx commented 2026-10-23 15:08:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T15:08:49Z.


What

exp returned the host's exp, so its last bit and its errno depended on the host. On Windows (UCRT), it differs from the libc.prx that titles ship on 9,892,285 of 320 million random arguments. For example, exp(0x4033ddceb4331e0a) is 0x41b94d703f21e868 in the bundled libc.prx and 0x41b94d703f21e869 here, and exp(0xbfcc83b6d471607d) is 0x3fe99c0d1b777558 there and 0x3fe99c0d1b777559 here.

The titles' copy (NID NVadfnzQhHQ) is FreeBSD msun's e_exp.c. exp now computes it:

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

exp(1): the titles' copy departs from msun at exactly one argument. exp(1.0) returns the correctly rounded e, 0x4005bf0a8b145769, where msun's algorithm gives 0x4005bf0a8b14576a. The 4,000 floats on each side of 1 and −1, every integer from −745 to 709, and every i/j for |i| ≤ 64 and 1 ≤ j ≤ 16 all follow msun. This port returns that constant for x == 1 and is msun everywhere else. No FreeBSD release I checked (11.4, 12.0, main and 59bc2f0952) has that case, so it is Sony's.

errno: msun sets none, but the titles' copy sets ERANGE in exactly msun's overflow (x > o_threshold) and underflow (x < u_threshold) branches. A result that rounds to a subnormal on the general path leaves errno alone. NaN and the infinities never set it. This is the double-precision counterpart of the expf behaviour in #10113, and it does not touch that code.

Tested

Rebased without merge commits onto main 73f4f7e06e4f210a8c6570e335fd654dc0a86134. Tested exact head d6c2d09c9542061afd19afffbb7aea4818fd9b79 on Linux x86-64 (AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON, bundled dependencies).

Rebuilt libc, guest_math_tests, guest_math_exact_tests and guest_dinkumware_internals_tests. guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three consecutive times on both pinned main and this exact head: 12 passes per side, zero skips. Conventions and whitespace checks pass. GitHub current-main integration is now backed by these local tests; the latest fetched main also has a clean merge preview.

Original authored executable code and tests are preserved. The exp parent retains its FreeBSD notice; trailing whitespace was removed from three copyright-notice lines. No new Windows, macOS, bundled-SDK/console reference, GPU/title, performance or full-suite execution is claimed. Earlier reference comparisons retain their original source pins.

Source patches, source-preservation proof, exact pins, build logs, raw CTest logs, JUnit and binary hashes; SHA-256 46b6a1579d909a279510564f346f32b61073cc5d090decd6acc0684bfe6d706c. Uploaded copy downloaded and verified. 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: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4984895012 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10234). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T15:08:49Z. --- ### What `exp` returned the host's `exp`, so its last bit and its `errno` depended on the host. On Windows (UCRT), it differs from the `libc.prx` that titles ship on 9,892,285 of 320 million random arguments. For example, `exp(0x4033ddceb4331e0a)` is `0x41b94d703f21e868` in the bundled `libc.prx` and `0x41b94d703f21e869` here, and `exp(0xbfcc83b6d471607d)` is `0x3fe99c0d1b777558` there and `0x3fe99c0d1b777559` here. The titles' copy (NID `NVadfnzQhHQ`) is FreeBSD msun's [`e_exp.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_exp.c). `exp` now computes it: - the overflow and underflow filter; - the two argument reductions, including `k = invln2*x + halF[xsb]` truncated to `int`; - the `P1`–`P5` rational and the `2^k` scaling, with the `k == 1024` and `k < -1021` paths; - `huge` and `twom1000` read at run time, as msun's `volatile` reads do. **`exp(1)`:** the titles' copy departs from msun at exactly one argument. `exp(1.0)` returns the correctly rounded `e`, `0x4005bf0a8b145769`, where msun's algorithm gives `0x4005bf0a8b14576a`. The 4,000 floats on each side of 1 and −1, every integer from −745 to 709, and every `i/j` for `|i| ≤ 64` and `1 ≤ j ≤ 16` all follow msun. This port returns that constant for `x == 1` and is msun everywhere else. No FreeBSD release I checked (11.4, 12.0, `main` and `59bc2f0952`) has that case, so it is Sony's. **`errno`:** msun sets none, but the titles' copy sets `ERANGE` in exactly msun's overflow (`x > o_threshold`) and underflow (`x < u_threshold`) branches. A result that rounds to a subnormal on the general path leaves `errno` alone. NaN and the infinities never set it. This is the double-precision counterpart of the `expf` behaviour in #10113, and it does not touch that code. ### Tested <!-- codex-math-main73f-20261031-10234 --> Rebased without merge commits onto main `73f4f7e06e4f210a8c6570e335fd654dc0a86134`. Tested exact head `d6c2d09c9542061afd19afffbb7aea4818fd9b79` on Linux x86-64 (AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON, bundled dependencies). Rebuilt `libc`, `guest_math_tests`, `guest_math_exact_tests` and `guest_dinkumware_internals_tests`. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three consecutive times on both pinned main and this exact head: 12 passes per side, zero skips. Conventions and whitespace checks pass. GitHub current-main integration is now backed by these local tests; the latest fetched main also has a clean merge preview. Original authored executable code and tests are preserved. The exp parent retains its FreeBSD notice; trailing whitespace was removed from three copyright-notice lines. No new Windows, macOS, bundled-SDK/console reference, GPU/title, performance or full-suite execution is claimed. Earlier reference comparisons retain their original source pins. [Source patches, source-preservation proof, exact pins, build logs, raw CTest logs, JUnit and binary hashes](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-four-main73f-linux-20261031.tar.gz); SHA-256 `46b6a1579d909a279510564f346f32b61073cc5d090decd6acc0684bfe6d706c`. Uploaded copy downloaded and verified. 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: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 12:56:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T12:56:17Z.

Review: APPROVED


Checked against main 99feaf0eb8 and FreeBSD release/11.4.0 lib/msun/src/e_exp.c:

  • The port follows e_exp.c line for line. All 14 constants match msun's hex comments bit for bit. Apart from the exp(1) constant, the ERANGE lines and the dropped inexact trigger (all documented), I found no other change.
  • An independent Python version of msun's algorithm reproduces 28 of the 29 CheckExpConsoleBits expectations, bits and errno. The remaining one is the documented exp(1) case (msun gives 0x4005bf0a8b14576a), which I can't check against the bundled module.
  • It still merges cleanly into current main, and main's exp is still std::exp.
  • Small wording nit in the debt entry: for most nonzero tiny x, 1 + x is itself inexact and raises FE_INEXACT, so the flag is only missed where 1 + x is exact (e.g. x = 2^-30). Maybe "may not raise".
  • FYI: the exp commit 11209ebedd is also the first commit of #10321, identical there, so whichever lands second carries a duplicate. #10245 (log) also conflicts textually with this PR in GuestMath.cpp and libc.md (additive only).

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638318206 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10234#pullrequestreview-5638318206). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:56:17Z. **Review:** APPROVED --- Checked against main 99feaf0eb8 and FreeBSD release/11.4.0 lib/msun/src/e_exp.c: - The port follows `e_exp.c` line for line. All 14 constants match msun's hex comments bit for bit. Apart from the `exp(1)` constant, the `ERANGE` lines and the dropped inexact trigger (all documented), I found no other change. - An independent Python version of msun's algorithm reproduces 28 of the 29 `CheckExpConsoleBits` expectations, bits and errno. The remaining one is the documented `exp(1)` case (msun gives 0x4005bf0a8b14576a), which I can't check against the bundled module. - It still merges cleanly into current main, and main's `exp` is still `std::exp`. - Small wording nit in the debt entry: for most nonzero tiny x, `1 + x` is itself inexact and raises `FE_INEXACT`, so the flag is only missed where `1 + x` is exact (e.g. x = 2^-30). Maybe "may not raise". - FYI: the exp commit 11209ebedd is also the first commit of #10321, identical there, so whichever lands second carries a duplicate. #10245 (log) also conflicts textually with this PR in `GuestMath.cpp` and `libc.md` (additive only). Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!10234
No description provided.