fix(libc): compute log10 with fdlibm's algorithm, as titles' libc.prx does #10247

Merged
JJenkx merged 1593 commits from upstream-pr/10247 into main 2026-11-02 08:00:20 +00:00
JJenkx commented 2026-10-23 15:30:23 +00:00 (Migrated from github.com)

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


What

log10 returned the host's log10, so its last bit and its errno depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 3,066,397 of 320 million random arguments. For example, log10(0x4003a67d5567227b) is 0x3fd8fa5a3b281494 in the bundled libc.prx and 0x3fd8fa5a3b281493 here, and log10(0x4047f462f020acde) is 0x3ffae2ff9b87b792 there and 0x3ffae2ff9b87b793 here.

The titles' copy (NID WuMbPBKN1TU) is not the FreeBSD 11.4 e_log10.c, which works through k_log1p. That version differs from the bundled library on about 1% of arguments. It is the older fdlibm e_log10.c over its k_log.h __kernel_log, the same lineage as the erff in #10226. log10 now computes it:

  • the same subnormal scaling and normalisation as log;
  • __kernel_log as a static Log10Kernel;
  • the ivln10hi/ivln10lo and log10_2hi/log10_2lo recombination, with x - 1 split at its low word.

errno: the bundled library's handling differs from log's (#10245). log10 sets EDOM for every nonzero argument with the sign bit set: the negative numbers, negative subnormals, -inf and negative NaNs. It leaves errno alone for ±0, where it returns -inf, and for positive NaN and +inf. This port does the same, and keeps the NaN payloads of fdlibm's x + x and (x - x) / 0. _Log with a positive base calls log10, and its existing test still passes: log10(1000) is exactly 3 here too.

Tested

Current Linux refresh: 19999e0615e1f97ce018d3cea034071743014e32 on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. Fresh isolated libc/math build passed; guest_math and guest_dinkumware_internals each passed three consecutive runs. Diff and conventions checks pass; math and test topic changes remain unchanged after rebase. Linux logs and rebase checks.

The Windows module comparisons and negative control below are historical evidence from original head a24a71c90f2add35da580c8bed491078c791a2d8; Windows was not rerun on the refreshed commit. The original report's console results mean the supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console.

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

  • Comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error. It compared their log10 with this PR's code, checking result bits (NaN payloads included) and errno, on two sets:

    • 320 million random arguments per title, half random bit patterns and half random significands with exponents 2^-32 to 2^31;
    • 31,165 structured arguments:
      • the integers 1 to 1024 and their reciprocals;
      • the 4,000 floats on each side of 1, 10 and −1;
      • every power of two from 2^-1074 to 2^1023;
      • every power of ten from 10^-300 to 10^300, with two neighbours on each side;
      • the special values.
    • PPSA07923 and PPSA05916: identical bits and errno on all of them.
    • PPSA13934: identical errno. The bits differ on 229,944 random arguments, because that copy is an FMA-contracted build.
  • guest_math: the new CheckLog10ConsoleBits has 26 cases with the bundled library's results and errno:

    • ±0, ± the smallest subnormal and the smallest normal;
    • 1 and its neighbours, 10, 1000 and 0.1;
    • both sides of the |f| < 2^-20 edge;
    • DBL_MAX, ±inf, −1 and NaN payloads of both signs;
    • six arguments where UCRT differs.

    guest_math and guest_dinkumware_internals pass in three runs out of three on the exact head.

  • Negative control: with main's Math.cpp and these tests, guest_math aborts in a Require, while guest_dinkumware_internals still passes.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Overlap and order: Refreshed as one standalone log10 topic on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. The previous #10245 dependency described series order rather than a required implementation. Nearby math ports still share text/test/debt anchors. #10317 requires this PR's kernel and remains stacked on it.

  • Not tested: macOS, a physical console, floating-point exception flags, and log10l. Original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above.

Current Linux qualification (2026-10-28)

Rebased onto main 2227f1366188cda5699dd20e2e756a18006a5d4b. Linux CPU validation on 2026-10-28 at 6bceecd018be481d9a2b66a36f1704472eeeca5f (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON).

libc, guest_math_tests and guest_math_exact_tests rebuilt from this exact source. guest_math and guest_math_exact each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass.

Exact source pins, preservation proofs, dependency checks, build logs and JUnit, SHA-256 1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed.

Canonical declaration cleanup (2026-10-28)

At 2f72ecea0435, removed 93 duplicate local export declarations from GuestMath.cpp; its included Api.hpp supplies them. Production code and numeric test bodies are unchanged. Original fails the actual header audit; candidate passes. Per-branch affected-test syntax compilation and conventions/whitespace pass; full standalone-header compilation passed once for the byte-identical header tree shared by the four affected PRs. Source/proofs/logs, SHA256 44f560018e4f6b52e8dda4158383f64fc3059fcdb6444840fe5c04fb2c884f11. No new numerical-runtime/platform claim; earlier runs retain original pins. Normal fast-forward only, no rebase. AI-assisted: Codex.

Current qualification — 2026-10-30

Integrated current main 08a10d225306ad102b57408e2ef7566bcfb829b2 into this existing topic at 476bb9f96cb5f66b7bfba9de65143f5d2568d24c. Current-main functions, fixtures, unrelated debt rows and exact dependency gitlinks are preserved; original topic functions and authored commits are retained.

Rebuilt actual Linux libc and focused test binaries. guest_math, guest_math_exact, guest_dinkumware_internals each passed three consecutive runs (9 runtime passes), plus public_headers; conventions and whitespace checks pass. Pinned main passed all seven selected runtime suites plus the header audit three times (24 runs). This is focused Linux CPU qualification; prior Windows/reference evidence retains its original pins. No new Windows, console, SDK-module, title, GPU or performance claim. AI-assisted: yes.

Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math6-main08a10-linux-20261030.tar.gz (SHA-256 b984a6334dce65b5c9148e3791ba21605074a5e762ea0f07f96de2f38062ef6e).

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 fdlibm 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:4985187666 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10247). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T15:30:23Z. --- ### What `log10` returned the host's `log10`, so its last bit and its `errno` depended on the host. On Windows (UCRT) it differs from the `libc.prx` that titles ship on 3,066,397 of 320 million random arguments. For example, `log10(0x4003a67d5567227b)` is `0x3fd8fa5a3b281494` in the bundled `libc.prx` and `0x3fd8fa5a3b281493` here, and `log10(0x4047f462f020acde)` is `0x3ffae2ff9b87b792` there and `0x3ffae2ff9b87b793` here. The titles' copy (NID `WuMbPBKN1TU`) is not the FreeBSD 11.4 `e_log10.c`, which works through `k_log1p`. That version differs from the bundled library on about 1% of arguments. It is the older fdlibm [`e_log10.c`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/e_log10.c) over its [`k_log.h`](https://github.com/freebsd/freebsd-src/blob/59bc2f095289a10b8cfa55cbb2b45c3bd6c2d4ae/lib/msun/src/k_log.h) `__kernel_log`, the same lineage as the `erff` in #10226. `log10` now computes it: - the same subnormal scaling and normalisation as `log`; - `__kernel_log` as a static `Log10Kernel`; - the `ivln10hi`/`ivln10lo` and `log10_2hi`/`log10_2lo` recombination, with `x - 1` split at its low word. **`errno`:** the bundled library's handling differs from `log`'s (#10245). `log10` sets `EDOM` for every nonzero argument with the sign bit set: the negative numbers, negative subnormals, `-inf` and negative NaNs. It leaves `errno` alone for ±0, where it returns `-inf`, and for positive NaN and `+inf`. This port does the same, and keeps the NaN payloads of fdlibm's `x + x` and `(x - x) / 0`. `_Log` with a positive base calls `log10`, and its existing test still passes: `log10(1000)` is exactly 3 here too. ### Tested Current Linux refresh: `19999e0615e1f97ce018d3cea034071743014e32` on main `6c52b649e7b1592f28db0cb8fe9ac604f0a242e7`. Fresh isolated libc/math build passed; `guest_math` and `guest_dinkumware_internals` each passed three consecutive runs. Diff and conventions checks pass; math and test topic changes remain unchanged after rebase. [Linux logs and rebase checks](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10247-head19999e06-linux-validation.tar.gz). The Windows module comparisons and negative control below are historical evidence from original head `a24a71c90f2add35da580c8bed491078c791a2d8`; Windows was not rerun on the refreshed commit. The original report's console results mean the supplied titles' bundled libraries executed on the Windows host, not measurements on a physical console. Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` 4997726a5f. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. It compared their `log10` with this PR's code, checking result bits (NaN payloads included) and `errno`, on two sets: - 320 million random arguments per title, half random bit patterns and half random significands with exponents 2^-32 to 2^31; - 31,165 structured arguments: - the integers 1 to 1024 and their reciprocals; - the 4,000 floats on each side of 1, 10 and −1; - every power of two from 2^-1074 to 2^1023; - every power of ten from 10^-300 to 10^300, with two neighbours on each side; - the special values. - **PPSA07923 and PPSA05916:** identical bits and `errno` on all of them. - **PPSA13934:** identical `errno`. The bits differ on 229,944 random arguments, because that copy is an FMA-contracted build. - **`guest_math`:** the new `CheckLog10ConsoleBits` has 26 cases with the bundled library's results and `errno`: - ±0, ± the smallest subnormal and the smallest normal; - 1 and its neighbours, 10, 1000 and 0.1; - both sides of the `|f| < 2^-20` edge; - `DBL_MAX`, ±`inf`, −1 and NaN payloads of both signs; - six arguments where UCRT differs. `guest_math` and `guest_dinkumware_internals` pass in three runs out of three on the exact head. - **Negative control:** with `main`'s `Math.cpp` and these tests, `guest_math` aborts in a `Require`, while `guest_dinkumware_internals` still passes. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap and order:** Refreshed as one standalone log10 topic on main `6c52b649e7b1592f28db0cb8fe9ac604f0a242e7`. The previous #10245 dependency described series order rather than a required implementation. Nearby math ports still share text/test/debt anchors. #10317 requires this PR's kernel and remains stacked on it. - **Not tested:** macOS, a physical console, floating-point exception flags, and `log10l`. Original module comparison and negative control were Windows-only; the new Linux build and regression checks are scoped separately above. <!-- codex-math-refresh-20261028-repeat-pr10247 --> ### Current Linux qualification (2026-10-28) Rebased onto `main` `2227f1366188cda5699dd20e2e756a18006a5d4b`. Linux CPU validation on 2026-10-28 at `6bceecd018be481d9a2b66a36f1704472eeeca5f` (Arch Linux 7.2.9, AMD Ryzen 9 7950X, GCC 16.2.1, Ninja Release, BUILD_TESTING=ON). `libc`, `guest_math_tests` and `guest_math_exact_tests` rebuilt from this exact source. `guest_math` and `guest_math_exact` each passed three consecutive runs (6/6, no skips); the same two suites each passed three runs on the pinned main (6/6). Conventions and whitespace checks pass. [Exact source pins, preservation proofs, dependency checks, build logs and JUnit](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math25-main2227-linux-20261028-repeat.tar.gz), SHA-256 `1176269c9e6948b5252a5db9e660978d7f978fb2d9f4ed6c68bc050b10b5ae68`. This is focused Linux CPU qualification; earlier Windows/title/reference measurements retain their original pins. No new title or performance result is claimed. ### Canonical declaration cleanup (2026-10-28) At `2f72ecea0435`, removed 93 duplicate local export declarations from `GuestMath.cpp`; its included `Api.hpp` supplies them. Production code and numeric test bodies are unchanged. Original fails the actual header audit; candidate passes. Per-branch affected-test syntax compilation and conventions/whitespace pass; full standalone-header compilation passed once for the byte-identical header tree shared by the four affected PRs. [Source/proofs/logs](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-canonical-declarations-linux-20261028.tar.gz), SHA256 `44f560018e4f6b52e8dda4158383f64fc3059fcdb6444840fe5c04fb2c884f11`. No new numerical-runtime/platform claim; earlier runs retain original pins. Normal fast-forward only, no rebase. AI-assisted: Codex. <!-- codex-maintenance-20261030-repeat-10247 --> **Current qualification — 2026-10-30** Integrated current main `08a10d225306ad102b57408e2ef7566bcfb829b2` into this existing topic at `476bb9f96cb5f66b7bfba9de65143f5d2568d24c`. Current-main functions, fixtures, unrelated debt rows and exact dependency gitlinks are preserved; original topic functions and authored commits are retained. Rebuilt actual Linux libc and focused test binaries. `guest_math`, `guest_math_exact`, `guest_dinkumware_internals` each passed three consecutive runs (9 runtime passes), plus `public_headers`; conventions and whitespace checks pass. Pinned main passed all seven selected runtime suites plus the header audit three times (24 runs). This is focused Linux CPU qualification; prior Windows/reference evidence retains its original pins. No new Windows, console, SDK-module, title, GPU or performance claim. AI-assisted: yes. Evidence: https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/owned-math6-main08a10-linux-20261030.tar.gz (SHA-256 b984a6334dce65b5c9148e3791ba21605074a5e762ea0f07f96de2f38062ef6e). ### 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 fdlibm 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:47:38 +00:00 (Migrated from github.com)

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

Review: APPROVED


Checked against main 39a36bbec8 and fdlibm e_log10.c and k_log.h at freebsd-src 59bc2f0952:

  • It merges cleanly into current main. Main's log10 is still the host's (Math.cpp:1082), and _Log (DinkumwareInternals.cpp:87) is the only internal caller.
  • I compared log10_nid_postfix and Log10Kernel line by line with e_log10.c and __kernel_log. Everything matches: the subnormal scaling, the 0x95f64 normalisation, the (2+hx) shortcut and i|=j select, and the hi/lo recombination order. After scaling a subnormal, the low word is correctly taken from the scaled value.
  • All the constants (two54, ivln10hi/lo, log10_2hi/lo, Lg1-Lg7) match the source literals and their hex comments.
  • I ran a separate Python transcription of the cited fdlibm source on the 21 non-special cases in CheckLog10ConsoleBits. All of them give the expected bits, including the subnormals, DBL_MAX and the six UCRT-differing arguments.
  • Small note: this PR and #10253 both add their test function and call at the same place in GuestMath.cpp (just before CheckExpm1ConsoleBits), so they conflict textually and the second to land needs a trivial rebase.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5638288106 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10247#pullrequestreview-5638288106). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T12:47:38Z. **Review:** APPROVED --- Checked against main 39a36bbec8 and fdlibm e_log10.c and k_log.h at freebsd-src 59bc2f0952: - It merges cleanly into current main. Main's `log10` is still the host's (Math.cpp:1082), and `_Log` (DinkumwareInternals.cpp:87) is the only internal caller. - I compared `log10_nid_postfix` and `Log10Kernel` line by line with e_log10.c and `__kernel_log`. Everything matches: the subnormal scaling, the `0x95f64` normalisation, the `(2+hx)` shortcut and `i|=j` select, and the hi/lo recombination order. After scaling a subnormal, the low word is correctly taken from the scaled value. - All the constants (two54, ivln10hi/lo, log10_2hi/lo, Lg1-Lg7) match the source literals and their hex comments. - I ran a separate Python transcription of the cited fdlibm source on the 21 non-special cases in `CheckLog10ConsoleBits`. All of them give the expected bits, including the subnormals, DBL_MAX and the six UCRT-differing arguments. - Small note: this PR and #10253 both add their test function and call at the same place in GuestMath.cpp (just before `CheckExpm1ConsoleBits`), so they conflict textually and the second to land needs a trivial rebase. 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!10247
No description provided.