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

Merged
JJenkx merged 2 commits from upstream-pr/10468 into main 2026-10-26 18:50:44 +00:00
JJenkx commented 2026-10-24 02:04:34 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-24T02:04:34Z.


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 #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 (#10354 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4991641235 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10468). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-24T02:04:34Z. --- ### 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](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_nextafter.c) and [float](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_nextafterf.c) 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 <!-- windows-validation-20261026-pr10468 --> 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](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/math-primary-windows-20261026.tar.gz), 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](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10510-230d2ddc-pr10506-9c79f0a1-pr10468-946149ed-maina5a7-linux-validation.tar.gz) (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](https://github.com/boykopovar/AnyPS5/pull/10468#pullrequestreview-5640180578): 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 #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](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/pr10468-head5cc53a56-mainf26434af-portable-fenv-linux-validation-20261024.tar.gz) (23,888 bytes; SHA-256 `506174e69e07c92fe617332fd846d0b7d997371d33a7d864116c2184978d8561`). No SDK binaries, game assets, saves or code dumps are attached. Historical evidence: [previous exact-head module comparisons](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-post-expm1-merge-refresh-1e20650e-linux-validation.tar.gz) 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](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-nextafter-fenv-headaad43247-linux-validation.tar.gz) is likewise historical, not rerun on this head. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none (#10354 is merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 04:57:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T04:57:51Z.

Review: APPROVED


Checked against main 6c52b649e7 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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636913543 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10468#pullrequestreview-5636913543). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T04:57:51Z. **Review:** APPROVED --- Checked against main 6c52b649e7 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.
MarcZX10 commented 2026-10-24 21:55:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:55:52Z.

Review: DISMISSED


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.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640180578 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10468#pullrequestreview-5640180578). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T21:55:52Z. **Review:** DISMISSED --- 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.
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!10468
No description provided.