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

Merged
JJenkx merged 1 commit from upstream-pr/9841 into main 2026-10-24 01:19:41 +00:00
JJenkx commented 2026-10-22 17:20:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T17:20:55Z.


What

logf returned the host's log, 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 13,225,175 of the 2^32 floats, for example 0x3f7f0a10 → 0xbb76666f and 0x40400de0 → 0x3f8ca894.

The titles' logf (NID RQXLbdT2lc4) is FreeBSD msun's e_logf.c. logf now computes it the same way:

  • subnormal scaling by 2^25;
  • normalisation to [sqrt(2)/2, sqrt(2));
  • the short |f| < 2^-9 path;
  • the Lg1–Lg4 polynomial with its two branches;
  • vzero read at run time, as msun's volatile read does.

errno: msun sets none, but the titles' copy sets ERANGE for ±0 (result -inf) and EDOM for every argument with the sign bit set, negative NaNs and -inf included. Positive NaNs and +inf leave errno unchanged. These map onto msun's own branches, so the port sets ERANGE in the zero branch and EDOM in the ix < 0 branch.

logf is imported by 20 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA04158. This is the logf counterpart of #9839 (expf).

Tested

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

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their logf with this build's logf_nid_postfix for all 2^32 arguments, bits and errno.
    • PPSA07923 and PPSA05916: identical bits and errno everywhere.
    • PPSA13934: identical errno everywhere. The bits differ by 1 ulp on 961,881 arguments, because that copy is an FMA-contracted build, as with expf (#9839) and asinf (#9771).
  • guest_math: the new CheckLogfConsoleBits has 21 cases with the console's results and errno: values where UCRT differs (a subnormal, both sides of 1, and larger values), 1, 2, the smallest subnormal and normal, FLT_MAX, +inf, the neighbours of 1, a NaN payload, ±0, a negative subnormal, -1, -inf and a negative NaN. guest_math and the libc tests (8) pass in three runs out of three. With main's Math.cpp and this test, guest_math aborts in a Require.
  • python tools/check_conventions.py --base origin/main and git diff --check pass. The branch merges without conflicts with #8865, #9771, #9777, #9784 and #9839.
  • Independent Linux review: Dom3005's report tested head a22e959cd merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports five passing guest_math runs (--repeat until-fail:5). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.
  • Not tested: macOS, a console, the floating-point exception flags.

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:4971350982 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9841). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T17:20:55Z. --- ### What `logf` returned the host's `log`, 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 13,225,175 of the 2^32 floats, for example `0x3f7f0a10` → `0xbb76666f` and `0x40400de0` → `0x3f8ca894`. The titles' `logf` (NID `RQXLbdT2lc4`) is FreeBSD msun's [`e_logf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_logf.c). `logf` now computes it the same way: - subnormal scaling by `2^25`; - normalisation to `[sqrt(2)/2, sqrt(2))`; - the short `|f| < 2^-9` path; - the `Lg1`–`Lg4` polynomial with its two branches; - `vzero` read at run time, as msun's `volatile` read does. **`errno`:** msun sets none, but the titles' copy sets `ERANGE` for ±0 (result `-inf`) and `EDOM` for every argument with the sign bit set, negative NaNs and `-inf` included. Positive NaNs and `+inf` leave `errno` unchanged. These map onto msun's own branches, so the port sets `ERANGE` in the zero branch and `EDOM` in the `ix < 0` branch. `logf` is imported by 20 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA04158. This is the `logf` counterpart of #9839 (`expf`). ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 11f976b27c. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `logf` with this build's `logf_nid_postfix` for all 2^32 arguments, bits and `errno`. - PPSA07923 and PPSA05916: identical bits and `errno` everywhere. - PPSA13934: identical `errno` everywhere. The bits differ by 1 ulp on 961,881 arguments, because that copy is an FMA-contracted build, as with `expf` (#9839) and `asinf` (#9771). - **`guest_math`:** the new `CheckLogfConsoleBits` has 21 cases with the console's results and `errno`: values where UCRT differs (a subnormal, both sides of 1, and larger values), 1, 2, the smallest subnormal and normal, `FLT_MAX`, `+inf`, the neighbours of 1, a NaN payload, ±0, a negative subnormal, -1, `-inf` and a negative NaN. `guest_math` and the libc tests (8) pass in three runs out of three. With `main`'s `Math.cpp` and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. The branch merges without conflicts with #8865, #9771, #9777, #9784 and #9839. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9841#issuecomment-6353485943) tested head `a22e959cd` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports five passing `guest_math` runs (`--repeat until-fail:5`). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim. - Not tested: macOS, a console, the floating-point exception flags. ### 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 AI-assisted (Claude Code).
MarcZX10 commented 2026-10-22 17:25:17 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T17:25:17Z.

Review: APPROVED


Reviewed the 56-line port against FreeBSD 11.4 e_logf.c, including subnormal scaling, normalization, polynomial branches, and special-value handling. I also compiled the exact PR Math.cpp and ran a focused local harness covering all 21 added bit/errno cases; it passed. The PR reports exhaustive 2^32-input comparisons against three bundled title libraries, with the documented 1-ulp FMA variant. The hosted Linux/Windows product jobs are skipped by policy, and the Windows relinker and macOS x86-64 relinker checks are still pending at review time.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618217191 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9841#pullrequestreview-5618217191). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T17:25:17Z. **Review:** APPROVED --- Reviewed the 56-line port against FreeBSD 11.4 `e_logf.c`, including subnormal scaling, normalization, polynomial branches, and special-value handling. I also compiled the exact PR `Math.cpp` and ran a focused local harness covering all 21 added bit/errno cases; it passed. The PR reports exhaustive 2^32-input comparisons against three bundled title libraries, with the documented 1-ulp FMA variant. The hosted Linux/Windows product jobs are skipped by policy, and the Windows relinker and macOS x86-64 relinker checks are still pending at review time.
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!9841
No description provided.