Skip to content

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

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

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

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

What

expm1 returned the host's expm1, so its last bit and its errno depended on the host. On Windows (UCRT) it differs from the libc.prx that titles ship on 35,152,346 of 320 million random arguments. For example, expm1(0x3fc78b9e474afed8) (about 0.184) is 0x3fc9d99e60806b35 in the bundled libc.prx and 0x3fc9d99e60806b36 here.

The titles' copy (NID gqKfOiJaCOo) is FreeBSD msun's s_expm1.c. expm1 now computes it as a static Expm1:

  • the |x| >= 56 ln2 filter, with the large-negative tiny - 1 return;
  • the k = ±1 and general argument reductions with the c correction;
  • the Q1–Q5 rational;
  • every k case of the reconstruction, including k == 1024 and the subnormal scaling.

huge is read at run time, as msun's volatile read does. This is the double-precision counterpart of the expm1f in #10222.

errno: msun sets none. The titles' copy sets ERANGE exactly when a finite argument overflows to infinity, which is the same rule as its expm1f. Large negative arguments return -1 without errno, and so do NaN and the infinities. The exported expm1 sets ERANGE in that case only.

Tested

Current Linux refresh: 99abd2604d53b7588b6386de012527c60ad6dcc7 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.cpp and GuestMath.cpp 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 92e60da1cc495f62a17a81000fb5778dec79df44; Windows was not rerun on the refreshed commit. Results described as 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 1e1c355.

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

    • 320 million random arguments per title: a third random bit patterns, two thirds random significands with exponents 2^-63 to 2^9;
    • 47,222 structured arguments:
      • every integer from −800 to 800 and every 1/64 step over that range;
      • the 4,000 floats on each side of ±56 ln2, ±ln2/2, ±1.5 ln2, the overflow threshold, ±2^-54, −0.25 and 1;
      • the special values.

    Results:

    • PPSA07923 and PPSA05916: identical bits and errno on all of them.
    • PPSA13934: identical errno. The bits differ on 43,433 random arguments, because that copy is an FMA-contracted build.
  • guest_math: the new CheckExpm1ConsoleBits has 30 cases with the bundled library's results and errno:

    • ±0 and the 2^-54 shortcut;
    • both sides of ln2/2 and 1.5 ln2;
    • −0.25, −0.5 and ±1;
    • ±56 ln2 and 50;
    • both sides of the overflow threshold and −1000;
    • ±DBL_MAX, the infinities and NaN payloads of both signs;
    • six arguments where UCRT differs.

    main's existing expm1 checks still pass.

    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 expm1 topic on main 6c52b649e7b1592f28db0cb8fe9ac604f0a242e7. The prior fix(libc): compute cbrt with FreeBSD msun's algorithm, as titles' libc.prx does #10283 dependency described series order, not a required implementation. Nearby math ports share text/test/debt anchors. fix(libc): compute tanh with FreeBSD msun's algorithm, as titles' libc.prx does #10316 still requires this PR's internal Expm1 helper.

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

Checklist

AI-assisted (Claude Code).

@github-actions

github-actions Bot commented Oct 23, 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 6044 core/libs/tests/GuestMath.cpp
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 8863 core/libs/prx/libc/src/Math.cpp
docs/dev/debt/libc.md
PR 8965 docs/dev/debt/libc.md
PR 9232 core/libs/prx/libc/src/Math.cpp
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 9833 core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 9929 docs/dev/debt/libc.md
PR 9945 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 23, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head 92e60da1cc (92e60da1cc495f62a17a81000fb5778dec79df44), not merged onto anything else; its base on main is 1e1c355a54.

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
…bc.prx does

expm1 returned the host's expm1, whose last bit differs from the titles'
copy on Windows on 35,152,346 of 320 million random arguments. The titles'
copy is FreeBSD msun's s_expm1.c, and it sets ERANGE only when a finite
argument overflows to infinity. expm1 now computes that algorithm and sets
ERANGE there.

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

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Follow-up #10510 corrects finite positive overflow across rounding modes: exact positive infinity and ERANGE, with overflow/inexact in both x87 and SSE. The bundled PPSA03766 module and old-production/candidate comparison covers 41,856 calls per arm; all 9,216 overflow rows now match, with zero changes elsewhere. The 492 existing nonoverflow result-bit differences remain documented. Same-test negative control fails at the new overflow regression; the candidate passes both Linux math suites 3/3. #10510 depends on this PR; no Windows, physical-console or game-performance claim.

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.

2 participants