fix(libc): follow msun for log1pf #10007

Open
MarcZX10 wants to merge 6 commits from upstream-pr/10007 into main
MarcZX10 commented 2026-10-23 03:52:47 +00:00 (Migrated from github.com)

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


What

Implement FreeBSD msun's log1pf for finite inputs above -1 with its own port of s_log1pf.c, including its negative-input branches; invalid-domain and non-finite inputs retain the host fallback. Update the libc debt entry to record the implementation and remaining console-specific uncertainty.

Issue #9707 reports roughly 1.4 million result-bit differences against a Windows host implementation across 20 million samples. This is evidence of host-vs-title variation, not direct verification against libSceLibcInternal.

Tested

Linux, GCC 12.2: built and ran guest_math_tests successfully, including signed zero, tiny inputs, representative positive/negative values, values near -1, and domain/special values. python3 tools/check_conventions.py --base origin/main, git diff --check, and python3 tools/pr_overlap.py --branch codex/libc-log1pf-msun passed. No PS5 title or homebrew was used; exact console behavior and title-build FMA behavior remain unverified.

Current head 780fa4b76a: compared with the pinned s_log1pf.c (compiled in a harness with -ffp-contract=off) over every finite float from -1 (exclusive) to infinity, the port has no differing result bit; the previous mapping -log1p(-x/(1+x)) differed on about 82 million inputs in the same harness, the first at 0xb3000001. guest_math passes on this head, including a new table of bit-exact cases at the small-negative, range-reduction and large-input boundaries whose expected bits come from the pinned source. This host fails the startup CPU-feature gate, so guest_math was run in a throwaway build copy with that check disabled. Windows was not run.

Checklist

  • Based on current main; no other open PR implements the same function
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw or are documented
  • No notes, investigation files or images added
  • New third-party code is a submodule built from source (n/a)
  • 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:4977544828 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10007). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T03:52:47Z. --- ### What Implement FreeBSD msun's `log1pf` for finite inputs above -1 with its own port of [`s_log1pf.c`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/s_log1pf.c), including its negative-input branches; invalid-domain and non-finite inputs retain the host fallback. Update the libc debt entry to record the implementation and remaining console-specific uncertainty. Issue #9707 reports roughly 1.4 million result-bit differences against a Windows host implementation across 20 million samples. This is evidence of host-vs-title variation, not direct verification against `libSceLibcInternal`. ### Tested Linux, GCC 12.2: built and ran `guest_math_tests` successfully, including signed zero, tiny inputs, representative positive/negative values, values near -1, and domain/special values. `python3 tools/check_conventions.py --base origin/main`, `git diff --check`, and `python3 tools/pr_overlap.py --branch codex/libc-log1pf-msun` passed. No PS5 title or homebrew was used; exact console behavior and title-build FMA behavior remain unverified. Current head `780fa4b76a`: compared with the pinned `s_log1pf.c` (compiled in a harness with `-ffp-contract=off`) over every finite `float` from -1 (exclusive) to infinity, the port has no differing result bit; the previous mapping `-log1p(-x/(1+x))` differed on about 82 million inputs in the same harness, the first at `0xb3000001`. `guest_math` passes on this head, including a new table of bit-exact cases at the small-negative, range-reduction and large-input boundaries whose expected bits come from the pinned source. This host fails the startup CPU-feature gate, so `guest_math` was run in a throwaway build copy with that check disabled. Windows was not run. ### Checklist - [x] Based on current `main`; no other open PR implements the same function - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except technical debt - [x] Unimplemented paths throw or are documented - [x] No notes, investigation files or images added - [x] New third-party code is a submodule built from source (n/a) - [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
jotitadev commented 2026-10-23 08:00:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-23T08:00:54Z.

Review: COMMENTED


macOS build failure on head 89b9820fee: guest_math_tests does not compile with Apple clang / libc++.

core/libs/tests/GuestMath.cpp:1044:79: error: no member named 'max' in namespace 'std'; did you mean 'fmax'?
 1044 |         Require(std::abs(static_cast<double>(result) - test.expected) <= std::max(1e-45, std::abs(test.expected) * 2e-7));

GuestMath.cpp does not include <algorithm>; libstdc++ pulls it in transitively through the other headers, libc++ does not. This is the first std::max in the file, so main is not affected. Adding #include <algorithm> fixes it.

Setup: Apple M2 Max, macOS 27.2, Apple clang 21.0.0 (Xcode 27), x86_64 under Rosetta, Release. Base is #8658 @ 37a9a6b4be (main plus the macOS port; main alone does not build the guest libs on macOS), with this PR's diff applied.

With the include added:

  • guest_math passes 3/3.
  • With main's Math.cpp and the new test, guest_math also passes on this host. macOS's libm log1pf stays within the max(1e-45, |expected| * 2e-7) bound for these cases, so here the test does not tell the msun path from the host fallback. Comparing exact result bits, as #10001 does for coshf, would catch a host that differs from msun on these inputs.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5625503519 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10007#pullrequestreview-5625503519). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-23T08:00:54Z. **Review:** COMMENTED --- macOS build failure on head `89b9820fee`: `guest_math_tests` does not compile with Apple clang / libc++. ``` core/libs/tests/GuestMath.cpp:1044:79: error: no member named 'max' in namespace 'std'; did you mean 'fmax'? 1044 | Require(std::abs(static_cast<double>(result) - test.expected) <= std::max(1e-45, std::abs(test.expected) * 2e-7)); ``` `GuestMath.cpp` does not include `<algorithm>`; libstdc++ pulls it in transitively through the other headers, libc++ does not. This is the first `std::max` in the file, so `main` is not affected. Adding `#include <algorithm>` fixes it. Setup: Apple M2 Max, macOS 27.2, Apple clang 21.0.0 (Xcode 27), x86_64 under Rosetta, Release. Base is #8658 @ `37a9a6b4be` (main plus the macOS port; `main` alone does not build the guest libs on macOS), with this PR's diff applied. With the include added: - `guest_math` passes 3/3. - With `main`'s `Math.cpp` and the new test, `guest_math` also passes on this host. macOS's libm `log1pf` stays within the `max(1e-45, |expected| * 2e-7)` bound for these cases, so here the test does not tell the msun path from the host fallback. Comparing exact result bits, as #10001 does for `coshf`, would catch a host that differs from msun on these inputs. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-11-02 04:57:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-02T04:57:11Z.

Review: COMMENTED


Reviewed — docs contradiction confirmed, requested sentence matches the code

The atanhf entry still says the exported log1pf is the host's, which contradicts the log1pf entry and the code at head 780fa4b76a.

What I ran

Head 780fa4b76a6fb769a20ba2cf6ccc7859e00baef1 (live, OPEN). Read-only: gh pr view, gh api GET on comments and reviews, git show <head>:docs/dev/debt/libc.md and git show <head>:core/libs/prx/libc/src/Math.cpp, git grep at the head. Nothing was built or run.

Results

Check Result Note
Hosted checks 16 success, none failing mergeStateStatus CLEAN
libc.md contradiction Confirmed Lines 132 and 133
log1pf dispatch Confirmed Math.cpp:681-682

Findings

  • oneandonlydean (2026-11-01 19:36Z) asks for line 132 (the atanhf entry) to stop saying the exported log1pf is still the host's, since line 133 says it follows msun. Confirmed: line 132 contains "the exported log1pf is still the host's". The code at Math.cpp:681-682 sends non-finite inputs and x <= -1 to std::log1p, and everything else to Log1pfMsun. The requested sentence matches that.
  • BunnyFl (2026-10-27 03:28Z) reports Windows passes with a control abort. No question.
  • JJenkx (2026-10-28 00:48Z and 2026-10-29 00:19Z) reports native runs against the titles' libc.prx: value matches, with some errno and NaN-bit differences and an FMA-build difference. Those comments ask for no change. Their two comments give different errno counts (304 and 102). The msun branch-order claims were not checked against the pinned source here.

Sources

Not verified

  • The title-binary comparisons and probe results (external, not reproduced).
  • The msun source branch-order claims.
  • Whether the Windows and Linux test runs pass on the current main; only the hosted check state was read.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5732247842 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10007#pullrequestreview-5732247842). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-02T04:57:11Z. **Review:** COMMENTED --- # Reviewed — docs contradiction confirmed, requested sentence matches the code The `atanhf` entry still says the exported `log1pf` is the host's, which contradicts the `log1pf` entry and the code at head 780fa4b76a. ## What I ran Head `780fa4b76a6fb769a20ba2cf6ccc7859e00baef1` (live, OPEN). Read-only: `gh pr view`, `gh api` GET on comments and reviews, `git show <head>:docs/dev/debt/libc.md` and `git show <head>:core/libs/prx/libc/src/Math.cpp`, `git grep` at the head. Nothing was built or run. ## Results | Check | Result | Note | |---|---|---| | Hosted checks | 16 success, none failing | mergeStateStatus CLEAN | | `libc.md` contradiction | Confirmed | Lines 132 and 133 | | `log1pf` dispatch | Confirmed | `Math.cpp:681-682` | ## Findings - oneandonlydean (2026-11-01 19:36Z) asks for line 132 (the `atanhf` entry) to stop saying the exported `log1pf` is still the host's, since line 133 says it follows msun. Confirmed: line 132 contains "the exported `log1pf` is still the host's". The code at `Math.cpp:681-682` sends non-finite inputs and `x <= -1` to `std::log1p`, and everything else to `Log1pfMsun`. The requested sentence matches that. - BunnyFl (2026-10-27 03:28Z) reports Windows passes with a control abort. No question. - JJenkx (2026-10-28 00:48Z and 2026-10-29 00:19Z) reports native runs against the titles' `libc.prx`: value matches, with some `errno` and NaN-bit differences and an FMA-build difference. Those comments ask for no change. Their two comments give different `errno` counts (304 and 102). The msun branch-order claims were not checked against the pinned source here. ## Sources - https://github.com/boykopovar/AnyPS5/blob/780fa4b76a6fb769a20ba2cf6ccc7859e00baef1/docs/dev/debt/libc.md#L132-L133 - https://github.com/boykopovar/AnyPS5/blob/780fa4b76a6fb769a20ba2cf6ccc7859e00baef1/core/libs/prx/libc/src/Math.cpp#L680-L682 ## Not verified - The title-binary comparisons and probe results (external, not reproduced). - The msun source branch-order claims. - Whether the Windows and Linux test runs pass on the current main; only the hosted check state was read.
ligmaSec commented 2026-12-03 13:25:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:25:10Z.

Review: COMMENTED


Built the focused checks against the existing Linux runtime. GuestMath passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170653949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10007#pullrequestreview-6170653949). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:25:10Z. **Review:** COMMENTED --- Built the focused checks against the existing Linux runtime. `GuestMath` passed. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/10007:upstream-pr/10007'
git switch 'upstream-pr/10007'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/10007'
git switch 'upstream-pr/10007'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/10007'
git switch 'upstream-pr/10007'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/10007'
git switch 'main'
git merge --squash 'upstream-pr/10007'
git switch 'main'
git merge --ff-only 'upstream-pr/10007'
git switch 'main'
git merge 'upstream-pr/10007'
git push origin 'main'
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!10007
No description provided.