Skip to content

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

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libc-logf-msun
Oct 24, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
JJenkx:windows/libc-logf-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 11f976b.

Checklist

AI-assisted (Claude Code).

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@MarcZX10

MarcZX10 commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

I compared the arithmetic path with the primary FreeBSD 11.4 e_logf.c source: the constants, subnormal scaling, normalization, short path, polynomial branches, and vzero read match. The added errno behavior is separately supported by the reported title comparisons, and the 21 fixed-bit/errno cases cover the key edge classes. The exhaustive three-title report and Windows guest_math runs are author-reported; I did not run them locally. CI passes conventions, intake, eligibility, labels, register-context, relinker, ASAN, and write-watch; Linux/Windows product jobs are skipped.

The PR base is 6a6a35fd, while current main is 9062485e (25 commits ahead). CONTRIBUTING asks branches to stay current with main. Please rebase and rerun the focused guest_math/libc tests on the updated head before merge; then I can review the final combined state. I found no algorithmic issue in this diff.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks for checking it against the source. Same as on #9784 and #9844: I'll rebase and rerun guest_math and the libc tests on the final head when a maintainer is ready to merge, or as soon as a conflict with main appears. Its order with #9839 and #9844 (all mine) is settled: any order works.

@MarcZX10

Copy link
Copy Markdown
Contributor

Thanks for confirming the order among #9839 and #9844. The separate current-main item remains: CONTRIBUTING says to keep the branch up to date with main, and this base is currently 25 commits behind. I’m asking for that update so the review and checks cover today’s combined tree; this is not a request to merge or to change your stated order. Once rebased, please rerun the focused tests as you proposed and I’ll recheck the final head.

@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 10129 docs/dev/debt/libc.md L71-72
PR 10134 docs/dev/debt/libc.md L72

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 3965 docs/dev/debt/libc.md
PR 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6044 core/libs/tests/GuestMath.cpp
PR 7081 docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/src/Math.cpp
PR 7544 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 7648 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8306 docs/dev/debt/libc.md
PR 8417 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 8863 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9133 docs/dev/debt/libc.md
PR 9232 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 9364 docs/dev/debt/libc.md
PR 9408 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9527 docs/dev/debt/libc.md
PR 9552 docs/dev/debt/libc.md
PR 9669 docs/dev/debt/libc.md
PR 9833 core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10002 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 10003 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 10007 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main a6646f8 (head 551625b, no other changes). On Windows (UCRT GCC 15.2, Release), guest_math and the libc tests (8) pass in three runs out of three on this head, and check_conventions.py passes.

@MarcZX10

Copy link
Copy Markdown
Contributor

Thanks for the detailed final-head Windows run. I compared current upstream main (a0900b9d) with head 551625b1: GitHub's compare API reports the PR is 62 commits behind and one ahead, with merge base a6646f84. CONTRIBUTING asks branches to stay current with main; please rebase and rerun the focused math test/checks on the resulting head when practical. Your implementation and exhaustive comparison evidence remain useful; this is solely to ensure the final combined tree has been checked.

…c.prx does

logf returned the host log, so its last bit and errno depended on the host:
on Windows the bits differ from the libc.prx that titles ship on 13,225,175
of the 2^32 floats. That libc.prx is msun's e_logf.c, setting ERANGE for
+-0 and EDOM for every argument with the sign bit set. The port gives the
same bits and errno as PPSA07923's and PPSA05916's copies for every
argument; the FMA-contracted copy of PPSA13934 differs by 1 ulp on 961,881.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 3bbd232; head is now a22e959, with no conflicts. Windows (GCC 15.2.0 UCRT r7, Release) build of that exact head: guest_math and the libc tests (8) pass in three runs out of three, and tools/check_conventions.py --base origin/main passes. The other math PRs' implementations are untouched; this branch still only changes its own functions.

@JJenkx
JJenkx force-pushed the windows/libc-logf-msun branch from 551625b to a22e959 Compare October 22, 2026 20:22
@Dom3005

Dom3005 commented Oct 22, 2026

Copy link
Copy Markdown
Contributor

Linux run (the description lists Linux as not tested). Docker on Windows: Ubuntu 26.04, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. Head a22e959 merged onto main fcd4558 (clean).

  • guest_math pass 5 of 5 (--repeat until-fail:5).
  • Baseline: with this PR's test kept and main's versions of the changed sources, the test fails on Linux too, so it covers the change against glibc as well as UCRT.

No findings.

AI-assisted: yes

@oneandonlydean oneandonlydean self-assigned this Oct 23, 2026
@oneandonlydean
oneandonlydean merged commit 72641c9 into boykopovar:main Oct 24, 2026
19 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants