Skip to content

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

Merged
oneandonlydean merged 5 commits into
boykopovar:mainfrom
JJenkx:windows/libc-log10f-log2f-msun
Nov 4, 2026
Merged

oneandonlydean merged 5 commits into
boykopovar:mainfrom
JJenkx:windows/libc-log10f-log2f-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

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 f6075c2.

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

AI-assisted (Claude Code).

@MarcZX10

Copy link
Copy Markdown
Contributor

The PR pins the 9.0.0 source, records the distinction from logf for zero errno behavior, and reports exhaustive comparisons against three bundled libraries plus focused Windows guest_math/libc runs. The hosted conventions, intake, registration and completed relinker checks pass; Linux/Windows product jobs are skipped and two relinker jobs are still in progress.

The Tested section names base f6075c2f4a; current upstream main is a0900b9d. GitHub compare reports the head is one commit ahead and two behind, with merge base f6075c2f4a. Please rebase onto current main and rerun the focused math checks/conventions on the final head. The overlap report identifies adjacent Math.cpp edits with #9841 (logf) and #9857 (tanf); your description says both changes can be retained. Please keep the agreed order clear in the descriptions/checklists so either landing order preserves all three implementations. No algorithm defect found in this pass; I have not independently run the Windows binaries here.

@JJenkx
JJenkx force-pushed the windows/libc-log10f-log2f-msun branch from 9793ee6 to 5441602 Compare October 22, 2026 20:24
@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 3bbd232; head is now 5441602, 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.

@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.

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.

@MarcZX10

Copy link
Copy Markdown
Contributor

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.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 7c900bb without conflicts; head is now 55659c8. 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.

@JJenkx
JJenkx force-pushed the windows/libc-log10f-log2f-msun branch from 5441602 to 55659c8 Compare October 22, 2026 20:41
@github-actions

github-actions Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

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 5021 docs/dev/debt/libc.md
PR 5303 docs/dev/debt/libc.md
PR 6295 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 6819 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
PR 7715 docs/dev/debt/libc.md
PR 8150 docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9340 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10987 docs/dev/debt/libc.md
PR 11260 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 11262 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 11266 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 11332 docs/dev/debt/libc.md
PR 11632 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 11634 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 11724 docs/dev/debt/libc.md
PR 11726 docs/dev/debt/libc.md
PR 11831 docs/dev/debt/libc.md
PR 12560 docs/dev/debt/libc.md
PR 12682 docs/dev/debt/libc.md
PR 12688 docs/dev/debt/libc.md
PR 12700 docs/dev/debt/libc.md
PR 12965 docs/dev/debt/libc.md
PR 13390 docs/dev/debt/libc.md
PR 13804 docs/dev/debt/libc.md
PR 14212 docs/dev/debt/libc.md
PR 14585 docs/dev/debt/libc.md
PR 14610 docs/dev/debt/libc.md

@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 55659c8 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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 23, 2026
@JJenkx
JJenkx force-pushed the windows/libc-log10f-log2f-msun branch from 55659c8 to 7dc1474 Compare October 23, 2026 21:36
@JJenkx

JJenkx commented Oct 23, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main d271a49580 as 7dc14749f4. The only conflict was the docs/dev/debt/libc.md entry list, and both entries are kept; Math.cpp and the tests applied unchanged. On this exact head, guest_math and guest_dinkumware_internals pass in three runs out of three on Windows (GCC 15.2.0 UCRT r7, Release). With main's Math.cpp and these tests, guest_math aborts. Conventions pass. The declared order after #9841 and #9857 is unchanged.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 23, 2026
@oneandonlydean oneandonlydean self-assigned this Oct 25, 2026
@JJenkx
JJenkx force-pushed the windows/libc-log10f-log2f-msun branch from 7dc1474 to 084659a Compare October 26, 2026 02:29
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 26, 2026
…s, as titles' libc.prx does

log10f and log2f returned the host log10 and log2, so their last bit and
errno depended on the host: on Windows log10f differs from the libc.prx
that titles ship on 13,120,001 of the 2^32 floats and log2f on
2,155,910,778. That libc.prx is msun 9.0's e_log10f.c and e_log2f.c over
that release's __kernel_logf, which give 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 some of them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/libc-log10f-log2f-msun branch from 084659a to fad8f0e Compare October 27, 2026 10:28
@JJenkx

JJenkx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

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.

AI-assisted: yes.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 27, 2026
…below zero

Titles' libc.prx returns -inf for log10f(+-0) and log2f(+-0) without
raising FE_DIVBYZERO, and raises FE_INVALID with EDOM for every
negative input, a negative NaN included. The port computed -two25/zero,
which raised the division flag, and set errno for negatives without
raising FE_INVALID when (x - x) / zero had a NaN operand. Return the
infinity directly and raise through _Feraise; results and errno are
unchanged. The console-bits test now checks both flags.

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

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

New head 5d6c2ba14f adds one commit on top of fad8f0e3ac: the floating-point flags at the special inputs now match titles' libc.prx too. Values and errno were already exact.

The two flag differences fixed:

  • ±0: titles' log10f/log2f return −inf without raising FE_DIVBYZERO. The port computed -two25 / zero, which raised it; the infinity is now returned directly.
  • Negative inputs: titles raise FE_INVALID with EDOM for every negative input, −NaN included. With a NaN operand, (x - x) / zero raised nothing; the path now raises through _Feraise_nid_postfix(0x01).

Checks (Windows 11 Enterprise 10.0.22621, WinLibs GCC 15.2.0 UCRT r7, Release):

  • Edge inputs against PPSA07923's and PPSA13934's libc.prx (21 inputs: ±0, negatives, ±inf, ±NaN, subnormals, large values), comparing result bits, errno and FE_INVALID/FE_DIVBYZERO/FE_INEXACT:
    • fad8f0e3ac: 6 flag differences per function;
    • this head: 0.
  • Random-input comparison (PPSA07923, PPSA05916): still 0 value and 0 errno differences for both.
  • CheckLog10fLog2fConsoleBits now also requires FE_INVALID exactly for the EDOM rows and never FE_DIVBYZERO. guest_math passes in each of 3 runs, and with fad8f0e3ac's Math.cpp it fails.

AI-assisted: yes (Claude Code).

@JJenkx

JJenkx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head 5d6c2ba14f (5d6c2ba14fc596f6da5e0e217778f0f92a18c2aa), not merged onto anything else; its base on main is 3fea2a937b.

Arch Linux, kernel 7.2.9, Ryzen 9 7950X, g++ (GCC) 16.2.1 20260810, glibc 2.44, x86-64. Release, BUILD_TESTING=ON, Ninja -j4, private worktree.

  • ctest -R '^guest_math$': passes in each of 3 runs (3 passes).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Nov 2, 2026
# Conflicts:
#	core/libs/prx/libc/src/Math.cpp
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Nov 2, 2026
# Conflicts:
#	core/libs/prx/libc/src/Math.cpp
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Nov 2, 2026
@JJenkx

JJenkx commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Windows check of exact head 6ea7d0ee1c.

Setup: Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7 (the CI toolchain), Release, BUILD_TESTING=ON, private build. The build was clean, and the changed sources were rebuilt for this head.

  • ctest -R '^guest_math' --repeat until-fail:3: guest_math and guest_math_exact pass 3/3.

log10f and log2f carry over FreeBSD msun's e_log10f.c, k_logf.h and e_log2f.c at release/9.0.0. Name each source in the form the exp port uses, followed by that file's copyright and permission notice copied verbatim from that revision. e_log10f.c's sits above Log9Reduce, k_logf.h's above Log9Kernel and e_log2f.c's above log2f. No code changes.

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

JJenkx commented Nov 3, 2026

Copy link
Copy Markdown
Contributor Author

Added the source notes and licence notices this port was missing (57da2079b6, on top of 6ea7d0ee1c): the sources are msun's e_log10f.c, k_logf.h and e_log2f.c at release/9.0.0, each named in the exp port's form and followed by that file's notice copied verbatim from that revision. e_log10f.c's sits above Log9Reduce, k_logf.h's above Log9Kernel and e_log2f.c's above log2f. No code changes; check_conventions.py --base origin/main passes. The same correction oneandonlydean asked for on #10321.

AI-assisted: yes.

@oneandonlydean
oneandonlydean merged commit 19a34af into boykopovar:main Nov 4, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants