Skip to content

fix(libc): preserve nextafter exception flags and denormal modes - #10468

Merged
adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:fix/libc-nextafter-fenv
Oct 26, 2026
Merged

adriadam10 merged 2 commits into
boykopovar:mainfrom
JJenkx:fix/libc-nextafter-fenv

Conversation

@JJenkx

@JJenkx JJenkx commented Oct 24, 2026 •

Copy link
Copy Markdown
Contributor

What

Follow-up to #10354: preserve the floating-point environment behavior of nextafter and nextafterf. The branch contains a rebased copy of that prerequisite, the environment fix, and its portable test correction: three linear commits on main f26434afa36e58778a9ff4b948aea52dff19eb0c. The prerequisite PR itself remains separate and open.

With gradual underflow, a step from zero must raise underflow/inexact while preserving errno. Under DAZ, floating-point comparisons in the current step-direction calculation can choose the wrong adjacent value; a step from zero can also return zero after the library's actual arithmetic. Use representation ordering for the direction, retain the underflow-producing operations, and use the existing _Feraise helper for conditional underflow and overflow reporting. Keep #10354's NaN payload behavior.

The contract comes from FAR: Changing Tides PPSA03766's bundled sce_module/libc.prx: nextafter VA 0x51b30, nextafterf VA 0x51c80, _Feraise VA 0x11da0. The CPU probe executes the converted module; the compared mathematical function bytes are identical to the input module, with hashes in the evidence. This is a Linux execution of original library code, not a physical-console measurement. FreeBSD's double and float sources corroborate the integer direction and underflow arithmetic; the bundled library supplies the errno/helper behavior. No third-party implementation was copied into this follow-up.

FAR already loads its own bundled math implementation. This corrects the host fallback; it does not claim to unblock FAR or improve title FPS. Unmasked exceptions and physical-console behavior remain unverified in the debt entry.

Overlap: #10354 is the explicit prerequisite. #6295 changes the long-double/nexttoward family, not these two functions. The overlap tool also reports additive test/debt conflicts and lists older branches it cannot compare against main; those are retained in the attached report. Live nextafter PR search found no separate implementation of this environment fix.

Tested

Exact published-head Windows validation: ef0afbb7319bbbfeb5fd8ced320cde189fa2fd50, Windows 11 Enterprise 10.0.22621, WinLibs GCC 15.2.0 UCRT r7, Release. Native in-tree libc plus guest_math, guest_math_exact and guest_dinkumware_internals built successfully. All three suites plus the public_headers manifest audit pass three executions each before and after the valid production-only negative (12 passes per phase, no skips). Restoring only pinned pre-change main Math.cpp while retaining the exact PR fixtures forces a real recompilation and different DLL hash; guest_math aborts (0xc0000409), while exact-math, Dinkumware and public-header controls pass. Original source is restored and matches the published head. Actual private libc and compiler-runtime DLL paths/hashes are verified; positive GDB inferiors exit normally. Restored PE binaries are not byte-identical to initial binaries, and no cause or binary-identity claim is inferred. Full logs, JUnit, exact pins, source/runtime hashes and rejected harness attempts, SHA-256 81b1db8d50d2885246612fca02f837d3df42a4e740845f62c0c4c941d0cec19d. These are Windows CPU checks, without a new bundled-library differential matrix, physical-console or title run. Earlier results below retain their original source pins.

Current Linux refresh: 946149edde2932f7c23797ebd8424874bb6cd316 is refreshed onto main a5a780eaaa7f187fe8f5c7f2ef244f6a6dab2762, with prerequisite authorship retained and no merge commits. The original production and regression function bodies are preserved, along with main's log1p implementation, regression calls and current public declarations.

Fresh Linux x86-64 Release: guest_math, guest_math_exact, guest_dinkumware_internals and public_headers each pass3/3 before and after the production-only main-Math negative control. The negative aborts as expected while exact-math/Dinkumware controls pass. Restored libc is byte-identical and sources clean. Conventions, whitespace and merge-tree checks pass.

Exact-head logs, negative/restoration results and preservation proof (SHA-256 f107a42be1dabc4ef72ffddcc6274d35e65ebd40146c811214558b28d4952a90). No fresh original-module matrix, Windows/macOS/title/console or performance measurement is claimed by this refresh. Earlier reports below retain their measured source pins.

Previous qualification (historical source pins)

Fresh Linux x86-64, GNU C++ 16.2.1, Release: exact head 5cc53a56c94d2b874a33019adcc26e11bb5a9343 on main f26434afa36e58778a9ff4b948aea52dff19eb0c. Independent libc and focused test-target builds pass. guest_math, guest_dinkumware_internals and public_headers each pass 3/3.

  • Addresses the mixed MXCSR/fenv representation review: map raw bits 0x01/0x04/0x08/0x10/0x20 explicitly to FE_INVALID/FE_DIVBYZERO/FE_OVERFLOW/FE_UNDERFLOW/FE_INEXACT, retaining the independent raw-MXCSR assertion. The shared template covers float and double. No assumption remains that host FE constants equal MXCSR bits.
  • Current main moves _Feraise into the public libc API header. Removed the now-duplicate .cpp declaration; the header audit passes. An intermediate rebased head passed math but failed this audit; its logs remain labeled intermediate in the archive.
  • Both nextafter function bodies are byte-identical to previously tested public head 23d3b36dd9f2c9ef429043b143bfd5beff49b382; source extraction hashes are included. Other main math/header changes are retained. SDK-reference calls and the production negative control were not rerun in this refresh.
  • Whitespace, conventions against local origin and fresh main, and merge-tree against the tested main pass. The branch retains fix(libc): compute nextafter and nextafterf with FreeBSD msun's algorithm, as titles' libc.prx does #10354 as its explicit prerequisite.
  • The Windows exact-head rerun requested for the portable FE-mask fix is complete on2026-10-26; see the Windows subsection above. The earlier Linux-only report retains its original platform scope. No macOS/console/title/FPS result is claimed.

Exact pins, patch/range diff, builds, all nine focused invocations/JUnit, header compatibility correction and production preservation (23,888 bytes; SHA-256 506174e69e07c92fe617332fd846d0b7d997371d33a7d864116c2184978d8561). No SDK binaries, game assets, saves or code dumps are attached.

Historical evidence: previous exact-head module comparisons reported all 480 targeted and 16,384 expanded rows matching FAR's original library on Linux, while the prerequisite retained 208/480 and 4,080/16,384 differences. Earlier same-test negative control is likewise historical, not rerun on this head.

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
  • Generic behaviour, not specific to one title
  • Depends on: none (fix(libc): compute nextafter and nextafterf with FreeBSD msun's algorithm, as titles' libc.prx does #10354 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@github-actions

github-actions Bot commented Oct 24, 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 6713 docs/dev/debt/libc.md
PR 7081 docs/dev/debt/libc.md
PR 7201 core/libs/prx/libc/src/Math.cpp
PR 7386 docs/dev/debt/libc.md
PR 7447 docs/dev/debt/libc.md
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 9371 docs/dev/debt/libc.md
PR 9447 docs/dev/debt/libc.md
PR 9526 docs/dev/debt/libc.md
PR 9772 docs/dev/debt/libc.md
PR 9863 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 10007 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
docs/dev/debt/libc.md
PR 10405 docs/dev/debt/libc.md
PR 10467 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 11341 docs/dev/debt/libc.md

@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Linux run at this PR's exact head aad43247bc (aad43247bc9225ea83c68ccacefa10f0a193c563), not merged onto anything else; its base on main is db6af9e0fd.

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 or console was checked.

AI-assisted: yes (Claude Code).

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

Checked against main 6c52b64 and FreeBSD release/11.4.0 lib/msun/src/s_nextafter.c / s_nextafterf.c:

  • This still merges cleanly onto current main, and main still forwards to std::nextafter at Math.cpp:739-740.
  • The representation-ordered direction (signs differ || ux > uy) agrees with msun's signed-word branches: a Python model of both gave 0 mismatches over ~3M random and edge-case bit pairs.
  • The zero step, the x+x overflow and the x*x underflow probe use the same arithmetic and conditions as msun. The extra _Feraise calls add flags the arithmetic already raises, plus ERANGE, which matches what the description says the bundled module does.
  • Nothing forces MXCSR, so DAZ/FTZ and rounding stay the guest's. The DAZ/upward-rounding rows in the new test (no errno for 1ulp*1ulp == 1ulp) follow from that.

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

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@JJenkx
JJenkx force-pushed the fix/libc-nextafter-fenv branch from aad4324 to 23d3b36 Compare October 24, 2026 07:35
@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main 1e20650e84 as 23d3b36dd9f2c9ef429043b143bfd5beff49b382, preserving the merged expm1 entries and this topic. Both Linux math suites pass 3/3; fresh module comparisons reproduce the previous observations. Current-head evidence. Windows remains untested for this refresh.

@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 24, 2026
@ycbuzi

ycbuzi commented Oct 24, 2026

Copy link
Copy Markdown
Contributor

Windows run of 23d3b36 merged onto main fa515de (827a174ade, clean), since Windows was not run on this head. Windows 11 Home 10.0.26200, MinGW-w64 GCC 15.2.0 (ucrt-posix-seh r7), Release, BUILD_TESTING=ON.

Built libc, guest_math_tests and guest_dinkumware_internals_tests with no errors or warnings. guest_dinkumware_internals passes. guest_math aborts in CheckNextafterEnvironment<float, unsigned int>, at Require(exceptions == static_cast<int>(expectedFlags & FE_ALL_EXCEPT));.

The library's answer is right there: for nextafterf(0, 1.0f) the result is 1, the MXCSR flags are 0x32 and errno is unchanged, as the test expects. What differs is fetestexcept. With MinGW-w64's UCRT headers the FE_* values are not the MXCSR bit positions: FE_INEXACT is 0x01, FE_UNDERFLOW 0x02, FE_OVERFLOW 0x04, FE_DIVBYZERO 0x08 and FE_INVALID 0x10 (the _SW_* values in float.h). So fetestexcept(FE_ALL_EXCEPT) returns 0x3 for inexact and underflow, while 0x32 & FE_ALL_EXCEPT is 0x12.

With that line comparing against the FE_* values instead, guest_math passes on both repeats here, every float and double case in the four denormal modes:

Require(exceptions == (((expectedFlags & 0x01u) ? FE_INVALID : 0) | ((expectedFlags & 0x04u) ? FE_DIVBYZERO : 0) | ((expectedFlags & 0x08u) ? FE_OVERFLOW : 0) | ((expectedFlags & 0x10u) ? FE_UNDERFLOW : 0) | ((expectedFlags & 0x20u) ? FE_INEXACT : 0)));

On glibc x86 the FE_* values are the MXCSR bits, so this should give the same expectation there. Negative control: with src/Math.cpp taken from main and the rest of this head, guest_math aborts (exit code 0xc0000409) and guest_dinkumware_internals still passes. Not run: Linux with the change above, a title.

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

Blocking test portability failure: CheckNextafterEnvironment compares std::fetestexcept(FE_ALL_EXCEPT) directly with expectedFlags & FE_ALL_EXCEPT, but expectedFlags is expressed in the raw MXCSR status-bit layout (_mm_getcsr() & 0x3f). Those masks are not portable equivalents. The current Windows run on this exact head (MinGW-w64/UCRT) reports guest_math aborting at this assertion for nextafterf(0, 1): the operation and MXCSR flags match the expectation, but UCRT's FE_* constants use a different bit assignment, so fetestexcept() returns a different abstract mask. The reported mapping identifies the discrepancy, and the failure reproduces for both float and double environment cases.

Please convert the expected MXCSR status bits to the corresponding FE_INVALID, FE_DIVBYZERO, FE_OVERFLOW, FE_UNDERFLOW, and FE_INEXACT mask before comparing to fetestexcept, while retaining the separate raw-MXCSR assertion. Then rerun guest_math on Windows and Linux for the exact head. The focused tests reported on Linux do not cover this platform-specific mismatch.

The implementation change itself is not implicated by this finding: the independent Windows report says the result, raw MXCSR flags, and errno match, and the failure is at the test's mixed-representation assertion. I did not run the Windows test locally.

@JJenkx
JJenkx force-pushed the fix/libc-nextafter-fenv branch from 23d3b36 to 5cc53a5 Compare October 24, 2026 23:44
@JJenkx

JJenkx commented Oct 24, 2026

Copy link
Copy Markdown
Contributor Author

Corrected the test's MXCSR/fenv mask conversion on fresh main f26434afa36e58778a9ff4b948aea52dff19eb0c; exact new head 5cc53a56c94d2b874a33019adcc26e11bb5a9343. Raw status bits map to the five corresponding host FE_* constants, with the raw-MXCSR assertion retained for both float/double. Also removed the now-duplicate _Feraise cpp declaration after main's public-header split.

Linux libc/focused build passes; guest_math, guest_dinkumware_internals and public_headers each pass three repeats. Both nextafter implementation bodies are unchanged from the previous public head. Logs, JUnit, pins and source-preservation hashes; SHA-256 506174e69e07c92fe617332fd846d0b7d997371d33a7d864116c2184978d8561.

The requested exact-head Windows rerun is still needed. Our Windows machine is user-paused and I have not resumed it. The description distinguishes this remaining coverage from the Linux result.

@github-actions github-actions Bot added the needs rebase Conflicts with main, rebase needed label Oct 25, 2026
@JJenkx
JJenkx force-pushed the fix/libc-nextafter-fenv branch from 5cc53a5 to 946149e Compare October 25, 2026 12:12
@JJenkx

JJenkx commented Oct 25, 2026

Copy link
Copy Markdown
Contributor Author

Refreshed as 946149edde2932f7c23797ebd8424874bb6cd316 onto main a5a780eaaa7f, preserving the original function/regression bodies, prerequisite authors and main's log1p code/tests. No merge commits.

Fresh Linux: four focused groups pass3/3 before and after the expected main-Math negative failure; exact/Dinkumware controls pass and restored libc is byte-identical. Conventions/whitespace and merge-tree pass. No new Windows/title/original-module/performance claim.

Evidence (SHA-256 f107a42be1dabc4ef72ffddcc6274d35e65ebd40146c811214558b28d4952a90). AI-assisted: yes.

@github-actions github-actions Bot added needs rebase Conflicts with main, rebase needed and removed needs rebase Conflicts with main, rebase needed labels Oct 25, 2026
@JJenkx
JJenkx force-pushed the fix/libc-nextafter-fenv branch from 946149e to ebf162f 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
@JJenkx
JJenkx force-pushed the fix/libc-nextafter-fenv branch from ebf162f to ef0afbb Compare October 26, 2026 12:27
@github-actions github-actions Bot removed the needs rebase Conflicts with main, rebase needed label Oct 26, 2026
@JJenkx

JJenkx commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

@MarcZX10 The requested native Windows rerun for the portable raw-MXCSR-to-FE mask correction is complete on exact published head ef0afbb7319bbbfeb5fd8ced320cde189fa2fd50.

Windows 11 Enterprise22621, supported WinLibs GCC15.2.0 UCRT r7, Release: libc and all three math executables build; guest_math, guest_math_exact, guest_dinkumware_internals and the public_headers manifest audit each pass three runs before and after restoration (12 passes per phase, no skips). The production-only control restores pinned pre-change main Math.cpp with the PR fixtures retained and forces recompilation: guest_math aborts (0xc0000409) while the three common controls pass. Restored source matches the published head and the restored native suites pass.

Actual loaded private libc/runtime paths and hashes were verified, with positive GDB inferiors exiting normally. Restored PE hashes differ from initial hashes; binary identity is not claimed. The plain-abort fixture does not identify an exact assertion in this evidence, so this is scoped to the recorded production-only differential. A separate bounded negative-stack diagnostic is pending.

Windows logs, JUnit, exact pins, source/runtime hashes and accepted/rejected harness attempts, SHA-256 81b1db8d50d2885246612fca02f837d3df42a4e740845f62c0c4c941d0cec19d. The description now distinguishes this completed Windows run from earlier Linux-only reports and clears the already-merged prerequisite. Please re-review the current head.

AI-assisted: yes (GPT-6.1 Sol, medium effort); native Windows commands executed on the established Windows host via its separate FIFO.

@JJenkx

JJenkx commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

The bounded Windows GDB follow-up now attributes the production-only negative control to CheckNextafterEnvironment<float, unsigned int>, the first caller of ucrtbase!abort. The log confirms that the executable loaded its own private libs/unpatched/libc.prx. Restoring the exact public-head source and rebuilding restores all three math test groups to passing.

This identifies the failing fixture; the later unwind contains unknown frames, so it does not establish an assertion line or a complete stack. The GDB process-exit field was unavailable and is not used as success evidence. The original primary qualification archive remains unchanged.

Diagnostic logs, module paths, source/library hashes, script and terminal FIFO receipt, SHA-256 d32f3e2f7135ee496d61443af345acdfc2856ec85e873e46232df4872e6332b7; downloaded copy verified.

AI-assisted: yes.

@adriadam10 adriadam10 self-assigned this Oct 26, 2026
@adriadam10
adriadam10 dismissed MarcZX10’s stale review October 26, 2026 17:27

Dismissing a stale review: the requested changes were addressed after it (new head) and the merge queue blocks on it. Please request changes again if anything is still open.

@adriadam10
adriadam10 merged commit c9a6013 into boykopovar:main Oct 26, 2026
19 checks passed
@adriadam10

Copy link
Copy Markdown
Collaborator

Checked the step direction and the underflow/overflow arithmetic against msun s_nextafter.c/s_nextafterf.c, _Feraise usage and errno, and that the new test fails on main's version (flags and DAZ results); the mask fix requested by MarcZX10 is in. Debt entry updated; merged.

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