fix(libc): compute nextafter and nextafterf with FreeBSD msun's algorithm, as titles' libc.prx does #10354

Merged
JJenkx merged 2 commits from upstream-pr/10354 into main 2026-10-26 01:30:58 +00:00
JJenkx commented 2026-10-23 18:57:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-23T18:57:55Z.


What

nextafter and nextafterf returned the host's functions, which differ from the libc.prx that titles ship in two ways on Windows (UCRT):

  • errno: the titles' copies set ERANGE at msun's overflow and underflow returns only. UCRT also sets it in two places where they don't:
    • the first step from ±0 to the smallest subnormal, which returns early in msun. For example, nextafter(0, 1) is 0x0000000000000001 with errno untouched there and with ERANGE here, while nextafter(DBL_MIN, 0) sets ERANGE on both.
    • x == y with a subnormal x.
  • NaN arguments: msun returns x + y, which quiets a signalling NaN and keeps x's NaN when both are NaNs. UCRT returns a signalling NaN unchanged and keeps y's NaN for some pairs.

Against 4,000,784 sampled pairs per function, main differs from the titles' copies on 1,819 nextafter pairs (1,767 NaN results, 52 errno only) and 5,298 nextafterf pairs (5,246 NaN results, 52 errno only).

The titles' copies (NIDs h+J60RRlfnk and 3m2ro+Di+Ck) are FreeBSD msun's s_nextafter.c and s_nextafterf.c, with ERANGE at those two returns. Both functions now compute that, stepping the bit pattern towards y.

The NaN case computes x + y into a volatile for its exception flags and returns x's NaN, or else y's, quieted. Returning x + y directly is not enough: GCC at -O3, which Math.cpp is built with, commuted it to y + x and returned the other NaN for 31 double and 71 float pairs.

This leaves nextafterl and the nexttoward family to #6295, which adds them next to these lines and does not change these two.

Tested

Rebased after #10306 merged onto main 1e20650e8458f4e8916a85576ae231b5e144b581; exact tested head e7d3411eeab5079cfc87dc89b751c3e2dc147ee7.

Linux x86-64, GNU C++ 16.2.1, Release. Independent full libc/math target build; guest_math and guest_dinkumware_internals each passed 3/3 on this head.

  • Main’s expm1 test and debt entries are retained alongside this topic. Original production changes are identical; original topic test changes are also preserved.
  • Fresh CPU module comparisons reproduce the previous head exactly: 208/480 targeted and 4,080/16,384 expanded rows differ from the original FAR module in non-default floating-point environments. Follow-up #10468 addresses those differences; this refresh retains the algorithm and its original tests.
  • Diff and conventions checks pass. All six exact-head build/test logs, topic-preservation proof, patches and fresh CPU observations (SHA-256 489efaec7f020a092f28bec7884e12e3e8a14a541a4dc62866c579afce06baf6). No SDK binaries or code dumps are attached.
  • Original-module reference observations and existing probes were reused; the rebased port libraries were called afresh. No game entrypoint or GPU was executed.
  • Not tested for this refresh: Windows, macOS, physical console, other SDK release identities, unmasked exception traps or game/FPS behavior.

Historical Windows report from the original submission, retained for its module/host comparison details; it was not rerun for this refresh. “Console” in that report refers to bundled-library code run on a host, not 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 1e1ab77684.

  • Comparison: a scratch loader runs the decrypted libc.prx of eleven titles natively and supplies __error: PPSA04158, PPSA05916, PPSA07923, PPSA09614, PPSA13934, PPSA20423, PPSA25636, PPSA26528, PPSA33655, PPSA34008 and PPSA35169. It compared their nextafter and nextafterf with this PR's code, checking result bits (NaN payloads included) and errno, on 4,000,784 pairs per function and title:

    • every signed pair of 14 edge values (zero, the subnormal edges, DBL_MIN, 1, the largest finite values, infinity, signalling and quiet NaNs);
    • 4 million random pairs, a third of them with x in the subnormal, smallest-normal or largest binades.

    All eleven give identical bits and errno on all of them, with this PR built at -O3, and at -O0 and -O2 for PPSA07923. ERANGE is set on 445,828 of the double pairs and 453,078 of the float ones.

  • guest_math: the new CheckNextafterConsoleBits has 16 double and 16 float cases with the console's results and errno:

    • steps away from ±0 (no errno);
    • steps to and from subnormals, and down to zero (ERANGE);
    • overflow to ±infinity (ERANGE) and infinity towards zero;
    • x equal to y, and ±0 towards 0;
    • a signalling NaN, a NaN y, and NaN pairs in both orders.

    main's existing nextafter 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.

  • A standalone -O0 and -O3 build of the new code with the new vectors and main's existing checks passes.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

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: none; the msun functions are rewritten, as #9668, #5036 and the sinhf entry did
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4987831949 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10354). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-23T18:57:55Z. --- ### What `nextafter` and `nextafterf` returned the host's functions, which differ from the `libc.prx` that titles ship in two ways on Windows (UCRT): - **`errno`:** the titles' copies set `ERANGE` at msun's overflow and underflow returns only. UCRT also sets it in two places where they don't: - the first step from ±0 to the smallest subnormal, which returns early in msun. For example, `nextafter(0, 1)` is `0x0000000000000001` with `errno` untouched there and with `ERANGE` here, while `nextafter(DBL_MIN, 0)` sets `ERANGE` on both. - `x == y` with a subnormal `x`. - **NaN arguments:** msun returns `x + y`, which quiets a signalling NaN and keeps x's NaN when both are NaNs. UCRT returns a signalling NaN unchanged and keeps y's NaN for some pairs. Against 4,000,784 sampled pairs per function, `main` differs from the titles' copies on 1,819 `nextafter` pairs (1,767 NaN results, 52 `errno` only) and 5,298 `nextafterf` pairs (5,246 NaN results, 52 `errno` only). The titles' copies (NIDs `h+J60RRlfnk` and `3m2ro+Di+Ck`) are FreeBSD msun's [`s_nextafter.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_nextafter.c) and [`s_nextafterf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_nextafterf.c), with `ERANGE` at those two returns. Both functions now compute that, stepping the bit pattern towards `y`. The NaN case computes `x + y` into a `volatile` for its exception flags and returns x's NaN, or else y's, quieted. Returning `x + y` directly is not enough: GCC at `-O3`, which `Math.cpp` is built with, commuted it to `y + x` and returned the other NaN for 31 double and 71 float pairs. This leaves `nextafterl` and the `nexttoward` family to #6295, which adds them next to these lines and does not change these two. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Rebased after #10306 merged onto main `1e20650e8458f4e8916a85576ae231b5e144b581`; exact tested head `e7d3411eeab5079cfc87dc89b751c3e2dc147ee7`. Linux x86-64, GNU C++ 16.2.1, Release. Independent full libc/math target build; `guest_math` and `guest_dinkumware_internals` each passed **3/3** on this head. - Main’s expm1 test and debt entries are retained alongside this topic. Original production changes are identical; original topic test changes are also preserved. - Fresh CPU module comparisons reproduce the previous head exactly: 208/480 targeted and 4,080/16,384 expanded rows differ from the original FAR module in non-default floating-point environments. Follow-up #10468 addresses those differences; this refresh retains the algorithm and its original tests. - Diff and conventions checks pass. [All six exact-head build/test logs, topic-preservation proof, patches and fresh CPU observations](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/libc-post-expm1-merge-refresh-1e20650e-linux-validation.tar.gz) (SHA-256 `489efaec7f020a092f28bec7884e12e3e8a14a541a4dc62866c579afce06baf6`). No SDK binaries or code dumps are attached. - Original-module reference observations and existing probes were reused; the rebased port libraries were called afresh. No game entrypoint or GPU was executed. - Not tested for this refresh: Windows, macOS, physical console, other SDK release identities, unmasked exception traps or game/FPS behavior. Historical Windows report from the original submission, retained for its module/host comparison details; it was not rerun for this refresh. “Console” in that report refers to bundled-library code run on a host, not 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` 1e1ab77684. - **Comparison:** a scratch loader runs the decrypted `libc.prx` of eleven titles natively and supplies `__error`: PPSA04158, PPSA05916, PPSA07923, PPSA09614, PPSA13934, PPSA20423, PPSA25636, PPSA26528, PPSA33655, PPSA34008 and PPSA35169. It compared their `nextafter` and `nextafterf` with this PR's code, checking result bits (NaN payloads included) and `errno`, on 4,000,784 pairs per function and title: - every signed pair of 14 edge values (zero, the subnormal edges, `DBL_MIN`, 1, the largest finite values, infinity, signalling and quiet NaNs); - 4 million random pairs, a third of them with `x` in the subnormal, smallest-normal or largest binades. All eleven give identical bits and `errno` on all of them, with this PR built at `-O3`, and at `-O0` and `-O2` for PPSA07923. `ERANGE` is set on 445,828 of the double pairs and 453,078 of the float ones. - **`guest_math`:** the new `CheckNextafterConsoleBits` has 16 double and 16 float cases with the console's results and `errno`: - steps away from ±0 (no `errno`); - steps to and from subnormals, and down to zero (`ERANGE`); - overflow to ±infinity (`ERANGE`) and infinity towards zero; - x equal to y, and ±0 towards 0; - a signalling NaN, a NaN `y`, and NaN pairs in both orders. `main`'s existing `nextafter` 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. - A standalone `-O0` and `-O3` build of the new code with the new vectors and `main`'s existing checks passes. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. ### 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: none; the msun functions are rewritten, as #9668, #5036 and the `sinhf` entry did - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-23 19:02:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T19:02:13Z.

Review: APPROVED


Reviewed the bit stepping against the reported title libc.prx measurements and the regression vectors. The zero special case avoids introducing the host UCRT underflow errno difference; finite stepping sets ERANGE only when the resulting exponent reaches zero or infinity; and the NaN path forces the addition for exception behavior while returning the first NaN operand quieted, matching the observed result selection. The reported 11-title comparison and negative control are strong focused evidence, and the added guest vectors cover both precisions. Eligibility, conventions, intake, labels, report, Ubuntu/macOS relinker and other applicable jobs pass; the Windows relinker job is still running at review time. Full product builds are skipped by policy, and exception flags/Linux/macOS/console were not tested as the PR states.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633180084 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10354#pullrequestreview-5633180084). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T19:02:13Z. **Review:** APPROVED --- Reviewed the bit stepping against the reported title `libc.prx` measurements and the regression vectors. The zero special case avoids introducing the host UCRT underflow errno difference; finite stepping sets ERANGE only when the resulting exponent reaches zero or infinity; and the NaN path forces the addition for exception behavior while returning the first NaN operand quieted, matching the observed result selection. The reported 11-title comparison and negative control are strong focused evidence, and the added guest vectors cover both precisions. Eligibility, conventions, intake, labels, report, Ubuntu/macOS relinker and other applicable jobs pass; the Windows relinker job is still running at review time. Full product builds are skipped by policy, and exception flags/Linux/macOS/console were not tested as the PR states.
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!10354
No description provided.