Skip to content

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

Merged
oneandonlydean merged 3 commits into
boykopovar:mainfrom
JJenkx:windows/libc-exp2f-msun
Oct 25, 2026
Merged

oneandonlydean merged 3 commits into
boykopovar:mainfrom
JJenkx:windows/libc-exp2f-msun

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 22, 2026 •

Copy link
Copy Markdown
Contributor

What

exp2f returned the host's exp2, 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 213,306,266 of the 2^32 floats (5 %). Examples, console then main: 0x3f000303 → 0x3fb5066d, and 0xbf400909 → 0x3f183437.

The titles' exp2f (NID wuAQt-j+p4o) is FreeBSD msun's s_exp2f.c. exp2f now computes it the same way:

  • the 16-entry double table exp2ft;
  • the reduction x + redux, rounded to float as STRICT_ASSIGN does;
  • the degree-4 polynomial evaluated in double;
  • the 2^k scale built from its high word;
  • huge and twom100 read at run time, as msun's volatile reads do.

errno: msun sets none, but the titles' copy sets ERANGE in exactly msun's overflow branch (x >= 128) and its underflow branch (x <= -150). A subnormal result leaves errno alone, for example exp2f(-149) = 0x00000001. So do NaN and the infinities. This port sets ERANGE in those two branches only.

exp2f is imported by all 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Unlike expf (#9839) and logf (#9841), the FMA-contracted copy of PPSA13934 gives the same bits here, because the core is in double precision, so one port matches every copy checked.

Tested

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

Checklist

AI-assisted (Claude Code).

@MarcZX10

Copy link
Copy Markdown
Contributor

I compared the new function with the official FreeBSD 11.4 s_exp2f.c; its table, reduction, polynomial, scale construction and overflow/underflow branches correspond to the source, and the 24 test vectors cover the key boundaries and errno behavior. I did not rerun the math test locally; the exhaustive Windows/title comparison is author-reported. Current visible CI passes conventions, intake, labels, register-context and completed relinker jobs; Linux/Windows product jobs are skipped, while Windows and macOS x86-64 relinker jobs were still pending at this check.

Two integration items remain before merge: this head is based on c55d7c8e7b and is 25 commits behind current main (9062485ebe), and the body notes textual Math.cpp overlap with open #9839 and #9841 while the checklist still says Depends on: none. Please sync with those two PRs on merge order and declare any required dependencies, then rebase and rerun the focused math test on the updated head.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 thanks. #9839, #9841 and this PR are all mine, so their order is settled here: any order works. Whichever merges first, I'll rebase the other two keeping all three changes, and rerun guest_math on each, so none of them needs a Depends on entry. As on #9784, I'll refresh against main when a maintainer is ready to merge or a conflict with main appears, since main moves several commits an hour.

@MarcZX10

Copy link
Copy Markdown
Contributor

Thanks for clarifying the order among your three math PRs; no dependency entry is needed if any order works and you will preserve all changes. I still recommend updating this branch to current main before review approval: CONTRIBUTING requires branches to stay current, and the base is now 18 commits behind. This is to make the final diff/checks reviewable and is not a request for a maintainer to merge it. Please rebase and rerun the focused math test when convenient, then I’ll check the final head.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main a6646f8 (head 9f11a6c, 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.

@JJenkx
JJenkx force-pushed the windows/libc-exp2f-msun branch from cef6c79 to 9f11a6c Compare October 22, 2026 18:10
@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 7447 docs/dev/debt/libc.md L159-160
PR 10786 docs/dev/debt/libc.md L159

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 6295 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 6713 docs/dev/debt/libc.md
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 8417 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 8937 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 9133 docs/dev/debt/libc.md
PR 9269 docs/dev/debt/libc.md
PR 9371 docs/dev/debt/libc.md
PR 9408 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9499 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 9863 docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 10324 docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10460 docs/dev/debt/libc.md

@MarcZX10

Copy link
Copy Markdown
Contributor

Thanks for the detailed final-head Windows run. I compared current upstream main (a0900b9d) with head 9f11a6c1: 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 exhaustive comparison evidence and overlap explanation for preserving the adjacent expf/logf work are useful; this is solely to ensure the final combined tree has been checked.

@JJenkx

JJenkx commented Oct 22, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto current main 3bbd232; head is now 45a5dde, 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-exp2f-msun branch from 9f11a6c to 45a5dde 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 45a5dde 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

@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 exact head 45a5dde7df1031f0d7c563b6879b8cfd664b69fd. The port follows FreeBSD 11.4 msun's exp2f reduction, table, polynomial and scaling; errno handling and the 24 boundary/special-value cases are explicit. The production and test files are unchanged between the branch base and current upstream main; only the adjacent debt index changed.

I built the exact PR Math.cpp and GuestMath.cpp in the available older configured Release build and ran the isolated CheckExp2fConsoleBits; all 24 cases passed on Linux. This is a focused check, not a fresh full-tree build. The author reports exhaustive all-float comparisons on three Windows title libraries and repeated Windows tests; independent reviewer evidence reports five passing Linux guest_math runs. Hosted policy and relinker/register-context/write-watch checks pass; product jobs are skipped.

…bc.prx does

exp2f returned the host exp2, so its last bit and errno depended on the
host: on Windows the bits differ from the libc.prx that titles ship on
213,306,266 of the 2^32 floats. That libc.prx is msun's s_exp2f.c, setting
ERANGE only in its overflow and underflow branches. The port gives the same
bits and errno as PPSA07923's, PPSA05916's and PPSA13934's copies for every
argument.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@JJenkx
JJenkx force-pushed the windows/libc-exp2f-msun branch from 45a5dde to 8b83c68 Compare October 24, 2026 00:21
@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 24, 2026
Additive conflicts with main's newer libc math changes: the debt list keeps
both sides' entries (per entry), the GuestMath.cpp test functions and calls
from both sides are kept, and Math.cpp keeps both sides' independent changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head 7488308bb9 (7488308bb9d7fb4c39cd4090333fa64cd020763a), not merged onto anything else; its base on main is 1e20650e84.

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

  • guest_math: passed in each of 3 runs, exit code 0 each time, with no failed, skipped or not-run tests.

This is the focused test only; no title, console or floating-point exception flags were checked.

AI-assisted: yes (Claude Code).

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
@oneandonlydean
oneandonlydean merged commit e7e3e9c into boykopovar:main Oct 25, 2026
17 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.

5 participants