fix(libc): compute log10f and log2f with FreeBSD msun 9.0's algorithms, as titles' libc.prx does #9873

Merged
JJenkx merged 5 commits from upstream-pr/9873 into main 2026-11-04 03:58:47 +00:00
JJenkx commented 2026-10-22 20:13:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T20:13:09Z.


What

log10f and log2f returned the host's log10 and log2, so their last bit and their errno depended on the host. On Windows (UCRT), against the libc.prx that titles ship, log10f differs on 13,120,001 of the 2^32 floats and log2f on 2,155,910,778 of them (50 %). Examples, console then main: log2f(0x40400990) → 0x3fcae940, and log10f(0x3f7f1a73) → 0xbac7bc3b.

The titles' versions are FreeBSD msun's e_log10f.c and e_log2f.c as of release/9.0.0, over that release's __kernel_logf in k_logf.h. That kernel has a short path for |f| < 2^-9, and the final combination differs from 11.4's. I found it by disassembling the log10f export of PPSA07923's copy. 11.4's code, either its float or its float_t path, differs from the titles by 1 ulp on about 2.4M arguments.

This PR adds:

  • Log9Kernel: that kernel;
  • Log9Reduce: the shared subnormal scaling, special cases and normalisation;
  • the two functions' final combinations.

errno: as in the titles' copy, every argument with the sign bit set except -0 sets EDOM, negative NaNs and -inf included. ±0 return -inf without errno, unlike logf (#9841), which sets ERANGE there. Positive NaNs and +inf leave errno alone.

log2f is imported by 21 and log10f by 20 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA04158 and PPSA09614.

Tested

Main conflict refresh, 2026-10-27 10:18 UTC pass: exact head fad8f0e3acd059f4a86db1d5010d18c84fd2b1c3 on tested main 3fea2a937ba9fbab2f06c51691fe0c5763659aa3. The topic implementations and fixtures are retained alongside current main's newer math functions and coverage.

Linux Release focused rebuild: guest_math and guest_math_exact each pass three repetitions: 6 passes, zero skips. Conventions, whitespace and merge checks pass. Every refreshed head's submodule pointers match the pinned main; the private dependency checkouts used for the gates are aligned. No new negative-control, Windows, console, title, GPU, full-suite or performance run is claimed. Earlier measurements below retain their original exact pins. Fulfilled merge-order dependencies have been cleared from the checklist; open prerequisites remain.

Exact source/dependency pins, patch-preservation and gitlink proofs, builds, verbose tests, JUnit and lease-push receipts. SHA-256: 702c4110ee9dde421c2dd5e057ed53a2df05e0cf7b01f67e8fb9cb779e332f7c; downloaded copy verified.

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

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared each copy's log10f and log2f with this build's for all 2^32 arguments, bits and errno.

    • PPSA07923 and PPSA05916: identical bits and errno everywhere, for both.
    • PPSA13934: identical errno everywhere. Its FMA-contracted build differs by 1 ulp on 930,947 (log10f) and 61,530 (log2f) arguments.
  • guest_math: the new CheckLog10fLog2fConsoleBits has 21 inputs, each checked through both functions with the console's results and errno: values where UCRT differs, exact powers (1, 2, 8, 100), the smallest subnormal and normal, FLT_MAX, +inf, ±0, a negative subnormal, -1, -inf, NaN payloads of both signs and the neighbours of 1. guest_math (including the existing log10f(100) == 2 check) 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.

  • Overlap: two of the conflicts are with my own PRs and come from neighbouring lines in Math.cpp. Keeping both changes resolves each.

    • log2f is defined on the line after logf, which #9841 replaces.
    • log10f is the line after tanf, which #9857 moves.

    tools/pr_overlap.py also finds one docs/dev/debt/libc.md adjacency with #8284. The branch merges cleanly with #9771, #9777, #9784, #9839, #9844, #9850, #9862 and #9867.

  • Independent Linux review: Dom3005's report tested head 55659c815 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.

Refreshed focused validation (2026-10-23): head 7dc14749f4, rebased onto main d271a49580. The only conflict was the docs/dev/debt/libc.md list, and both entries are kept. guest_math and guest_dinkumware_internals pass in three runs out of three on this exact head (Windows, GCC 15.2.0 UCRT r7, Release). With main's Math.cpp and these tests, guest_math aborts. Conventions and git diff --check pass. This refresh does not repeat the exhaustive title-library comparisons above, which keep their original base. Earlier refresh: 55659c815c on main 7c900bbc21, 8/8 math/libc tests 3/3.

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 functions are rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none (#9841 merged at 72641c92f6f8; #9857 merged at e9a551d6f40e; present in audited main 3fea2a937ba)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4973492575 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9873). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T20:13:09Z. --- ### What `log10f` and `log2f` returned the host's `log10` and `log2`, so their last bit and their `errno` depended on the host. On Windows (UCRT), against the `libc.prx` that titles ship, `log10f` differs on 13,120,001 of the 2^32 floats and `log2f` on 2,155,910,778 of them (50 %). Examples, console then `main`: `log2f(0x40400990)` → `0x3fcae940`, and `log10f(0x3f7f1a73)` → `0xbac7bc3b`. The titles' versions are FreeBSD msun's [`e_log10f.c`](https://github.com/freebsd/freebsd-src/blob/release/9.0.0/lib/msun/src/e_log10f.c) and [`e_log2f.c`](https://github.com/freebsd/freebsd-src/blob/release/9.0.0/lib/msun/src/e_log2f.c) as of **release/9.0.0**, over that release's `__kernel_logf` in `k_logf.h`. That kernel has a short path for `|f| < 2^-9`, and the final combination differs from 11.4's. I found it by disassembling the `log10f` export of PPSA07923's copy. 11.4's code, either its float or its `float_t` path, differs from the titles by 1 ulp on about 2.4M arguments. This PR adds: - `Log9Kernel`: that kernel; - `Log9Reduce`: the shared subnormal scaling, special cases and normalisation; - the two functions' final combinations. **`errno`:** as in the titles' copy, every argument with the sign bit set except -0 sets `EDOM`, negative NaNs and `-inf` included. ±0 return `-inf` without `errno`, unlike `logf` (#9841), which sets `ERANGE` there. Positive NaNs and `+inf` leave `errno` alone. `log2f` is imported by 21 and `log10f` by 20 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA04158 and PPSA09614. ### Tested <!-- codex-main-refresh-20261027-1018-pr9873 --> **Main conflict refresh, 2026-10-27 10:18 UTC pass:** exact head `fad8f0e3acd059f4a86db1d5010d18c84fd2b1c3` on tested main `3fea2a937ba9fbab2f06c51691fe0c5763659aa3`. The topic implementations and fixtures are retained alongside current main's newer math functions and coverage. Linux Release focused rebuild: `guest_math` and `guest_math_exact` each pass three repetitions: 6 passes, zero skips. Conventions, whitespace and merge checks pass. Every refreshed head's submodule pointers match the pinned main; the private dependency checkouts used for the gates are aligned. No new negative-control, Windows, console, title, GPU, full-suite or performance run is claimed. Earlier measurements below retain their original exact pins. Fulfilled merge-order dependencies have been cleared from the checklist; open prerequisites remain. [Exact source/dependency pins, patch-preservation and gitlink proofs, builds, verbose tests, JUnit and lease-push receipts](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-cpu11-main3fea-linux-20261027-1018.tar.gz). SHA-256: `702c4110ee9dde421c2dd5e057ed53a2df05e0cf7b01f67e8fb9cb779e332f7c`; downloaded copy verified. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` f6075c2f4a. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared each copy's `log10f` and `log2f` with this build's for all 2^32 arguments, bits and `errno`. - PPSA07923 and PPSA05916: identical bits and `errno` everywhere, for both. - PPSA13934: identical `errno` everywhere. Its FMA-contracted build differs by 1 ulp on 930,947 (`log10f`) and 61,530 (`log2f`) arguments. - **`guest_math`:** the new `CheckLog10fLog2fConsoleBits` has 21 inputs, each checked through both functions with the console's results and `errno`: values where UCRT differs, exact powers (1, 2, 8, 100), the smallest subnormal and normal, `FLT_MAX`, `+inf`, ±0, a negative subnormal, -1, `-inf`, NaN payloads of both signs and the neighbours of 1. `guest_math` (including the existing `log10f(100) == 2` check) 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. - **Overlap:** two of the conflicts are with my own PRs and come from neighbouring lines in `Math.cpp`. Keeping both changes resolves each. - `log2f` is defined on the line after `logf`, which #9841 replaces. - `log10f` is the line after `tanf`, which #9857 moves. `tools/pr_overlap.py` also finds one `docs/dev/debt/libc.md` adjacency with #8284. The branch merges cleanly with #9771, #9777, #9784, #9839, #9844, #9850, #9862 and #9867. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9873#issuecomment-6353484189) tested head `55659c815` 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. **Refreshed focused validation (2026-10-23):** head `7dc14749f4`, rebased onto `main` `d271a49580`. The only conflict was the `docs/dev/debt/libc.md` list, and both entries are kept. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on this exact head (Windows, GCC 15.2.0 UCRT r7, Release). With `main`'s `Math.cpp` and these tests, `guest_math` aborts. Conventions and `git diff --check` pass. This refresh does not repeat the exhaustive title-library comparisons above, which keep their original base. Earlier refresh: `55659c815c` on `main` `7c900bbc21`, 8/8 math/libc tests 3/3. ### 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 functions are rewritten, as #9668, #5036 and the `sinhf` entry did - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#9841 merged at `72641c92f6f8`; #9857 merged at `e9a551d6f40e`; present in audited main `3fea2a937ba`) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-22 20:32:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T20:32:52Z.

Review: COMMENTED


I checked the implementation against the pinned FreeBSD 9.0.0 k_logf.h, e_log2f.c, and e_log10f.c: the polynomial constants, reduction/normalization, split high/low products, and final combinations match. The PR reports exhaustive bit/errno comparisons against three title libc.prx builds and 3/3 Windows focused runs. The current hosted run has conventions/eligibility/labels and several product-adjacent checks passing, with macOS registration/relinker and one write-watch job still pending; Linux/Windows product jobs are skipped. I did not run the math binaries locally. Current main is 1da03c6f, while this head is based on 3bbd2321 (12 commits behind); please rebase and rerun the focused checks once the pending jobs finish. The reported 1-ulp difference for the FMA-contracted third title build remains documented.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5620483026 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9873#pullrequestreview-5620483026). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T20:32:52Z. **Review:** COMMENTED --- I checked the implementation against the pinned FreeBSD 9.0.0 `k_logf.h`, `e_log2f.c`, and `e_log10f.c`: the polynomial constants, reduction/normalization, split high/low products, and final combinations match. The PR reports exhaustive bit/errno comparisons against three title `libc.prx` builds and 3/3 Windows focused runs. The current hosted run has conventions/eligibility/labels and several product-adjacent checks passing, with macOS registration/relinker and one write-watch job still pending; Linux/Windows product jobs are skipped. I did not run the math binaries locally. Current main is `1da03c6f`, while this head is based on `3bbd2321` (12 commits behind); please rebase and rerun the focused checks once the pending jobs finish. The reported 1-ulp difference for the FMA-contracted third title build remains documented.
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!9873
No description provided.