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

Merged
JJenkx merged 1 commit from upstream-pr/10314 into main 2026-10-25 10:09:46 +00:00
JJenkx commented 2026-10-23 17:29:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T17:29:38Z.


What

log1p returned the host's log1p, so its last bit and its errno depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 24,477,433 of 320 million random arguments. For example, log1p(0x3f0f5ab6d3865cca) is 0x3f0f5a7962a0216c in the bundled libc.prx and 0x3f0f5a7962a0216d here.

The titles' copy (NID VfsML+n9cDM) is FreeBSD msun's s_log1p.c. log1p now computes it:

  • the small-argument shortcuts;
  • the k == 0 path for arguments in (-0.29, 0.41);
  • the 1 + x reduction with its correction term c, or c = 0 from 2^53 up;
  • the significand normalisation;
  • the Lp1–Lp7 polynomial.

errno: msun sets none. The titles' copy sets EDOM at exactly msun's NaN return, (x - x) / (x - x), taken by arguments below -1, by -inf and by negative NaNs. It leaves errno alone for log1p(-1), which returns -inf through msun's -two54 / vzero. Positive NaNs and +inf also leave it alone. This port sets EDOM at that return only.

log1p(-1) setting no ERANGE differs from the bundled library's log(0) (#10245) but matches its log10(0) (#10247). The rule here is what PPSA07923 and PPSA05916 give, not an assumption.

Tested

Fresh current-main qualification: head 185098a39d9179907e11d15fa9a58b27e6aac306, base main 6c7a3256f25d2ea56702d06a2283ecc27ff133b3, exact immediate parent 6c7a3256f25d2ea56702d06a2283ecc27ff133b3. One topic commit directly on current main.

Linux x86-64 Release: rebuilt libc and the three math test targets; guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass three repeats. Retaining the final tests and substituting only the immediate parent’s production Math.cpp makes guest_math abort; exact-math and Dinkumware controls pass. Restoring the candidate passes all four groups three times and produces a byte-identical libc. Source checks confirm the entire Math.cpp equals current main plus the original topic functions, and the relevant regression functions are unchanged. Conventions and whitespace checks pass. No reference-copy matrix, whole application, Windows or performance run was repeated for this refresh.

All three exact-head builds, regression/negative/restoration logs, source preservation, pins and topic patches, SHA-256 744790aa28706c7df7d8a54906ccc1d2b1a5d33ae7fcc85288d3778a0cf7d3c0.

Earlier qualification (historical source pins)

Refreshed on main 38d463a6cfd2bbddc147a34e8b14f5b6f661607a; exact head 1673ec6b712c70e1973fad68b614f457ebba16b2. Linux x86-64, GNU C++ 16.2.1, Release. Independent libc and all three focused math targets build successfully.

  • guest_math, guest_math_exact, guest_dinkumware_internals, public_headers and nid_database_cache each pass 3/3 on this head. The NID test still contains test_environment_variable_selects_the_database_and_help_names_it; it was not removed or disabled.
  • Final-tree negative control: retain the exact final fixture and all other production, replace only this topic's log1p function with its host forwarder. guest_math aborts and the Dinkumware group passes. Restore exact production and rebuild: libc is byte-identical to the original positive build; all three math groups pass another three invocations. Fixture hashes are unchanged.
  • The topic implementation body is byte-identical to the previous public head; source hashes and diffs are included. Current main's SDK-header and other math changes are retained. No original-library comparison matrix was rerun in this refresh.
  • Whitespace and conventions against local origin and the tested fresh main pass. No merge commits.
  • This is the actual parent of refreshed #10315: 1673ec6b712c70e1973fad68b614f457ebba16b2. The dependent branch shares this commit, rather than an independently copied log1p commit. Both integration orders produce the same child tree with one log1p test definition and one invocation.

Hosted Build run40144341933 on this updated PR is successful. Both macOS relinker architectures pass. The previously failing ARM macOS nid_database_cache test now passes (10.51 seconds), confirmed in job121022400866's raw log. This addresses the requested macOS rerun. Hosted relinker success does not establish Windows libc/math coverage; those full product jobs were skipped.

Remaining requested coverage: fresh exact-head Windows math. The local Windows machine remains explicitly user-paused. A separate hosted-math recipe was prepared, but GitHub rejects its dispatch because Actions is disabled for the fork; it has produced no test result.

Historical evidence: ycbuzi's independent Windows integration tested old head f4cb18645c with main fa515def85 as 4a5f6a67ba, Win11 Home 26200 / GCC15.2 UCRT, two passing focused repeats and a failing main-Math control. The original Windows bundled-module comparisons reported exact bits/errno for PPSA07923/PPSA05916 across 320 million random inputs and 68,216 structured inputs; PPSA13934's FMA build differed in 73,682 random results. Those reports are historical, not physical-console measurements.

The previous Linux 33,504-call matrix retained 10,076 differences against the supplied FAR module: 10,064 environment differences handled by #10512 and 12 upward-rounding bit differences retained in debt. This rebase does not resolve or hide those gaps.

Fresh exact pins, source hashes, shared-parent/merge-order proof, builds, all focused logs/JUnit, production-only negatives and restoration (34,529 bytes; SHA-256 67d69a6f98f1ff9843b200c18bab0ac6271b5c35d54f81bc2e15a0017178fe33). No title data, saves, SDK binaries or disassembly included. No fresh title/FPS/macOS/Windows/console or unmasked-exception result is claimed here.

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

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4986741951 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10314). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T17:29:38Z. --- ### What `log1p` returned the host's `log1p`, so its last bit and its `errno` depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 24,477,433 of 320 million random arguments. For example, `log1p(0x3f0f5ab6d3865cca)` is `0x3f0f5a7962a0216c` in the bundled `libc.prx` and `0x3f0f5a7962a0216d` here. The titles' copy (NID `VfsML+n9cDM`) is FreeBSD msun's [`s_log1p.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_log1p.c). `log1p` now computes it: - the small-argument shortcuts; - the `k == 0` path for arguments in (-0.29, 0.41); - the `1 + x` reduction with its correction term `c`, or `c = 0` from 2^53 up; - the significand normalisation; - the `Lp1`–`Lp7` polynomial. **`errno`:** msun sets none. The titles' copy sets `EDOM` at exactly msun's NaN return, `(x - x) / (x - x)`, taken by arguments below -1, by `-inf` and by negative NaNs. It leaves `errno` alone for `log1p(-1)`, which returns `-inf` through msun's `-two54 / vzero`. Positive NaNs and `+inf` also leave it alone. This port sets `EDOM` at that return only. `log1p(-1)` setting no `ERANGE` differs from the bundled library's `log(0)` (#10245) but matches its `log10(0)` (#10247). The rule here is what PPSA07923 and PPSA05916 give, not an assumption. ### Tested Fresh current-main qualification: head `185098a39d9179907e11d15fa9a58b27e6aac306`, base main `6c7a3256f25d2ea56702d06a2283ecc27ff133b3`, exact immediate parent `6c7a3256f25d2ea56702d06a2283ecc27ff133b3`. One topic commit directly on current main. Linux x86-64 Release: rebuilt libc and the three math test targets; `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` and `public_headers` each pass three repeats. Retaining the final tests and substituting only the immediate parent’s production `Math.cpp` makes `guest_math` abort; exact-math and Dinkumware controls pass. Restoring the candidate passes all four groups three times and produces a byte-identical libc. Source checks confirm the entire Math.cpp equals current main plus the original topic functions, and the relevant regression functions are unchanged. Conventions and whitespace checks pass. No reference-copy matrix, whole application, Windows or performance run was repeated for this refresh. [All three exact-head builds, regression/negative/restoration logs, source preservation, pins and topic patches](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10314-head185098a3-pr10315-headcb1a8b2c-pr10565-head7ea2737c-main6c7a-linux-validation.tar.gz), SHA-256 `744790aa28706c7df7d8a54906ccc1d2b1a5d33ae7fcc85288d3778a0cf7d3c0`. #### Earlier qualification (historical source pins) <!-- OS, title or homebrew, what was checked. A performance change requires matched same-machine and scene summaries and traces. --> Refreshed on main `38d463a6cfd2bbddc147a34e8b14f5b6f661607a`; exact head `1673ec6b712c70e1973fad68b614f457ebba16b2`. Linux x86-64, GNU C++ 16.2.1, Release. Independent libc and all three focused math targets build successfully. - `guest_math`, `guest_math_exact`, `guest_dinkumware_internals`, `public_headers` and `nid_database_cache` each pass **3/3** on this head. The NID test still contains `test_environment_variable_selects_the_database_and_help_names_it`; it was not removed or disabled. - Final-tree negative control: retain the exact final fixture and all other production, replace only this topic's `log1p` function with its host forwarder. `guest_math` aborts and the Dinkumware group passes. Restore exact production and rebuild: libc is byte-identical to the original positive build; all three math groups pass another three invocations. Fixture hashes are unchanged. - The topic implementation body is byte-identical to the previous public head; source hashes and diffs are included. Current main's SDK-header and other math changes are retained. No original-library comparison matrix was rerun in this refresh. - Whitespace and conventions against local origin and the tested fresh main pass. No merge commits. - This is the actual parent of refreshed #10315: `1673ec6b712c70e1973fad68b614f457ebba16b2`. The dependent branch shares this commit, rather than an independently copied log1p commit. Both integration orders produce the same child tree with one log1p test definition and one invocation. Hosted [Build run40144341933](https://github.com/boykopovar/AnyPS5/actions/runs/40144341933) on this updated PR is **successful**. Both macOS relinker architectures pass. The previously failing ARM macOS `nid_database_cache` test now passes (10.51 seconds), confirmed in job121022400866's raw log. This addresses the requested macOS rerun. Hosted relinker success does not establish Windows libc/math coverage; those full product jobs were skipped. **Remaining requested coverage:** fresh exact-head Windows math. The local Windows machine remains explicitly user-paused. A separate hosted-math recipe was prepared, but GitHub rejects its dispatch because Actions is disabled for the fork; it has produced no test result. Historical evidence: [ycbuzi's independent Windows integration](https://github.com/boykopovar/AnyPS5/pull/10314#issuecomment-6386339276) tested old head `f4cb18645c` with main `fa515def85` as `4a5f6a67ba`, Win11 Home 26200 / GCC15.2 UCRT, two passing focused repeats and a failing main-Math control. The original Windows bundled-module comparisons reported exact bits/errno for PPSA07923/PPSA05916 across 320 million random inputs and 68,216 structured inputs; PPSA13934's FMA build differed in 73,682 random results. Those reports are historical, not physical-console measurements. The [previous Linux 33,504-call matrix](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-post-expm1-merge-refresh-1e20650e-linux-validation.tar.gz) retained 10,076 differences against the supplied FAR module: 10,064 environment differences handled by #10512 and 12 upward-rounding bit differences retained in debt. This rebase does not resolve or hide those gaps. [Fresh exact pins, source hashes, shared-parent/merge-order proof, builds, all focused logs/JUnit, production-only negatives and restoration](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10314-1673ec6b-pr10315-916ef2d7-main38d463a6-linux-validation-20261024.tar.gz) (34,529 bytes; SHA-256 `67d69a6f98f1ff9843b200c18bab0ac6271b5c35d54f81bc2e15a0017178fe33`). No title data, saves, SDK binaries or disassembly included. No fresh title/FPS/macOS/Windows/console or unmasked-exception result is claimed here. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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 AI-assisted (Claude Code).
harisibrar1 commented 2026-10-24 12:46:31 +00:00 (Migrated from github.com)

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

Review: APPROVED


Checked against main 39a36bbec8 and FreeBSD release/11.4.0 lib/msun/src/s_log1p.c:

  • The constants, thresholds and branch structure match s_log1p.c exactly, including the association of the final expressions. The only addition is the EDOM at msun's NaN return, as described.
  • I ported s_log1p.c to Python on my own (plain doubles, no FMA), and it reproduces all 30 CheckLog1pConsoleBits expectations, including NaN payloads and errno. So the vectors match the reference algorithm.
  • It still merges cleanly onto current main, and main's log1p is still the host forwarder.
  • Heads-up for merging alongside #10315: that branch carries its own copy of this commit (0c8ca62c7a, from before your rebase), so git won't treat it as already merged. Details are on #10315.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638285196 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10314#pullrequestreview-5638285196). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:46:31Z. **Review:** APPROVED --- Checked against main 39a36bbec8 and FreeBSD release/11.4.0 lib/msun/src/s_log1p.c: - The constants, thresholds and branch structure match s_log1p.c exactly, including the association of the final expressions. The only addition is the `EDOM` at msun's NaN return, as described. - I ported s_log1p.c to Python on my own (plain doubles, no FMA), and it reproduces all 30 `CheckLog1pConsoleBits` expectations, including NaN payloads and errno. So the vectors match the reference algorithm. - It still merges cleanly onto current main, and main's `log1p` is still the host forwarder. - Heads-up for merging alongside #10315: that branch carries its own copy of this commit (0c8ca62c7a, from before your rebase), so git won't treat it as already merged. Details are on #10315. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-24 21:13:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:13:44Z.

Review: CHANGES_REQUESTED


The source comparison and the reported Linux/Windows exact-head tests are strong. I also inspected the failing macOS relinker job: 231/232 tests passed; the sole failure is nid_database_cache, where test_environment_variable_selects_the_database_and_help_names_it timed out after 20 seconds while launching tools/nid_names.py --nid AqXeRqGAdKE --json. That test is outside this PR's changed files, so this looks like a transient test-timeout/infrastructure failure rather than a log1p regression; please confirm it passes on a rerun.

This branch is based on 1e20650, 6,822 commits behind current main. Please refresh it onto current main as required by CONTRIBUTING.md, then rerun the focused math tests and the failed macOS job on the updated head. The hosted Linux and Windows product jobs are skipped for this path.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640052074 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10314#pullrequestreview-5640052074). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:13:44Z. **Review:** CHANGES_REQUESTED --- The source comparison and the reported Linux/Windows exact-head tests are strong. I also inspected the failing macOS relinker job: 231/232 tests passed; the sole failure is `nid_database_cache`, where `test_environment_variable_selects_the_database_and_help_names_it` timed out after 20 seconds while launching `tools/nid_names.py --nid AqXeRqGAdKE --json`. That test is outside this PR's changed files, so this looks like a transient test-timeout/infrastructure failure rather than a `log1p` regression; please confirm it passes on a rerun. This branch is based on `1e20650`, 6,822 commits behind current `main`. Please refresh it onto current `main` as required by CONTRIBUTING.md, then rerun the focused math tests and the failed macOS job on the updated head. The hosted Linux and Windows product jobs are skipped for this path.
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!10314
No description provided.