feat(libc): implement the nextafter and nexttoward families #6295

Closed
Aryan-Solanki wants to merge 3 commits from upstream-pr/6295 into main
Aryan-Solanki commented 2026-10-15 09:46:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryan-Solanki on 2026-10-15T09:46:15Z.


What

Implements the rest of the C99 nextafter family in libc: nextafter, nextafterl,
nexttowardf, nexttoward and nexttowardl, beside the already exported nextafterf.
Each delegates to the host's std::nextafter / std::nexttoward, as the other libc math
exports do.

Tested

Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs x86_64-ucrt-posix-seh, release
15.2.0posix-14.0.0-ucrt-r7), CMake 3.31.8, Ninja 1.13.2.

Full build clean. guest_math passes in 0.03 s. All 72 libc-registered tests pass.
No title or homebrew: nothing available here imports these exports.

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
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Unblocks: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4871322705 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295). Originally posted by [Aryan-Solanki](https://github.com/Aryan-Solanki) on 2026-10-15T09:46:15Z. --- ### What Implements the rest of the C99 nextafter family in libc: `nextafter`, `nextafterl`, `nexttowardf`, `nexttoward` and `nexttowardl`, beside the already exported `nextafterf`. Each delegates to the host's `std::nextafter` / `std::nexttoward`, as the other libc math exports do. ### Tested Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs `x86_64-ucrt-posix-seh`, release `15.2.0posix-14.0.0-ucrt-r7`), CMake 3.31.8, Ninja 1.13.2. Full build clean. `guest_math` passes in 0.03 s. All 72 libc-registered tests pass. No title or homebrew: nothing available here imports these exports. ### Checklist - [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 - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's technical debt file - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Unblocks: none - [x] AI-assisted: yes
Aryan-Solanki commented 2026-10-15 12:27:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryan-Solanki on 2026-10-15T12:27:59Z.


#6044 relocates core/libs/tests/GuestMath.cpp as part of a test reorganisation; it does not
change the same exports or assertions, so there is no functional overlap and no ordering
dependency. Whichever lands second rebases. Depends on: none.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192037290 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#issuecomment-6192037290). Originally posted by [Aryan-Solanki](https://github.com/Aryan-Solanki) on 2026-10-15T12:27:59Z. --- #6044 relocates core/libs/tests/GuestMath.cpp as part of a test reorganisation; it does not change the same exports or assertions, so there is no functional overlap and no ordering dependency. Whichever lands second rebases. Depends on: none.
coltken95 commented 2026-10-15 21:02:43 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-15T21:02:43Z.


Tested on Linux x86-64 (Release build), with 40da63a8d merged into main 4eb91eacd:

  • With the PR: guest_math passes.
  • With main's libc/src/Math.cpp: guest_math_tests doesn't link (undefined reference to 'nextafter_nid_postfix', 'nextafterl_nid_postfix', 'nexttoward_nid_postfix', 'nexttowardf_nid_postfix', ...), since main doesn't export them.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6202213655 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#issuecomment-6202213655). Originally posted by [coltken95](https://github.com/coltken95) on 2026-10-15T21:02:43Z. --- Tested on Linux x86-64 (Release build), with 40da63a8d merged into main 4eb91eacd: - With the PR: `guest_math` passes. - With main's `libc/src/Math.cpp`: `guest_math_tests` doesn't link (`undefined reference to 'nextafter_nid_postfix'`, `'nextafterl_nid_postfix'`, `'nexttoward_nid_postfix'`, `'nexttowardf_nid_postfix'`, ...), since main doesn't export them. AI-assisted: yes (Claude Code)
Aryan-Solanki commented 2026-10-16 09:45:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Aryan-Solanki on 2026-10-16T09:45:55Z.


The overlap report now lists a real conflict: #6414 also adds nextafter_nid_postfix in
core/libs/prx/libc/src/Math.cpp. It adds the double form only, with no test changes, as part
of a change that also covers AGC, audio and videodec2. This PR opened earlier (09:46 vs
14:24) and adds the five remaining C99 functions of the family with assertions in
core/libs/tests/GuestMath.cpp.

Suggesting this one lands first and #6414 drops that line when it rebases; happy to do the
reverse if its author prefers. Depends on: none.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6212762654 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#issuecomment-6212762654). Originally posted by [Aryan-Solanki](https://github.com/Aryan-Solanki) on 2026-10-16T09:45:55Z. --- The overlap report now lists a real conflict: #6414 also adds nextafter_nid_postfix in core/libs/prx/libc/src/Math.cpp. It adds the double form only, with no test changes, as part of a change that also covers AGC, audio and videodec2. This PR opened earlier (09:46 vs 14:24) and adds the five remaining C99 functions of the family with assertions in core/libs/tests/GuestMath.cpp. Suggesting this one lands first and #6414 drops that line when it rebases; happy to do the reverse if its author prefers. Depends on: none.
cadenasynyster commented 2026-10-19 00:15:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by cadenasynyster on 2026-10-19T00:15:28Z.


Measurement of the host functions this PR delegates to, on Windows/MinGW: the long double ones get the sign of negative zero wrong.

Windows 11, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), -O0, inputs passed through volatile. A standalone program that calls std::nextafter and std::nexttoward directly; I did not build this PR. 21 checks per type and function, for float, double and long double, each compared bit for bit (signed zeros included) with a value worked out from numeric_limits: (1,2), (1,0), (-1,-2), (-1,0), (0,1), (0,-1), (-0,1), (denorm_min,0), (-denorm_min,0), (denorm_min,1), (max,inf), (-max,-inf), (inf,0), (-inf,0), (min,0), (min-denorm_min,1), (1,1), (0,-0), (-0,0), (nan,1), (1,nan).

Result: 124 of 126 checks pass. The two that fail are the same case, in the long double path, for both nextafter and nexttoward:

  • (-0, 1) returns -denorm_min (3.6452e-4951 with the sign bit set); the expected result is +denorm_min.

I checked it separately with the C function: nextafterl(-0.0L, 1) = -3.6452e-4951 and nextafterl(-0.0L, -1) = +3.6452e-4951, while nextafterl(+0.0L, 1) = +3.6452e-4951 is right. So for a negative-zero argument the sign comes out inverted. float and double are correct for all 21 cases.

Since the exports delegate to the host, nextafterl and nexttowardl would show this on Windows. A test row with (-0.0L, 1.0L) would show it in the Windows build; I did not run the PR's own tests, and did not check Linux/glibc or other MinGW versions.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6256943927 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#issuecomment-6256943927). Originally posted by [cadenasynyster](https://github.com/cadenasynyster) on 2026-10-19T00:15:28Z. --- Measurement of the host functions this PR delegates to, on Windows/MinGW: the long double ones get the sign of negative zero wrong. Windows 11, WinLibs GCC 15.2.0 r7 (x86_64-ucrt-posix-seh), -O0, inputs passed through volatile. A standalone program that calls std::nextafter and std::nexttoward directly; I did not build this PR. 21 checks per type and function, for float, double and long double, each compared bit for bit (signed zeros included) with a value worked out from numeric_limits: (1,2), (1,0), (-1,-2), (-1,0), (0,1), (0,-1), (-0,1), (denorm_min,0), (-denorm_min,0), (denorm_min,1), (max,inf), (-max,-inf), (inf,0), (-inf,0), (min,0), (min-denorm_min,1), (1,1), (0,-0), (-0,0), (nan,1), (1,nan). Result: 124 of 126 checks pass. The two that fail are the same case, in the long double path, for both nextafter and nexttoward: - (-0, 1) returns -denorm_min (3.6452e-4951 with the sign bit set); the expected result is +denorm_min. I checked it separately with the C function: nextafterl(-0.0L, 1) = -3.6452e-4951 and nextafterl(-0.0L, -1) = +3.6452e-4951, while nextafterl(+0.0L, 1) = +3.6452e-4951 is right. So for a negative-zero argument the sign comes out inverted. float and double are correct for all 21 cases. Since the exports delegate to the host, nextafterl and nexttowardl would show this on Windows. A test row with (-0.0L, 1.0L) would show it in the Windows build; I did not run the PR's own tests, and did not check Linux/glibc or other MinGW versions. AI-assisted: yes
oneandonlydean commented 2026-10-19 01:22:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-19T01:22:03Z.


Thanks, this is straightforward, and the tests are thorough. One fix is needed, for the case the Windows test report found.

On Windows, std::nextafter(long double) calls MinGW-w64's nextafterl, and nexttowardl is the same function under another name. Its zero branch keeps the sign of x:

u.ld = x;
if (x == 0.0L) { u.parts.mantissa = 1ull; return y > 0.0L ? u.ld : -u.ld; }

So with x = -0.0L, both directions come out wrong: (-0, 1) returns -denorm_min, and (-0, -1) returns +denorm_min. The double and float paths, and Linux/glibc, are correct. The current tests only start from +0, so Windows CI passes anyway.

Could you guard the two long double wrappers? For example:

long double APS5_VABI nextafterl_nid_postfix(long double x, long double y) {
    if (x == 0.0L && y != 0.0L && !std::isnan(y)) return std::copysign(std::numeric_limits<long double>::denorm_min(), y);
    return std::nextafter(x, y);
}

and the same for nexttowardl_nid_postfix. Please also add -0 cases to CheckLongDouble, so Windows CI checks them: nextafterl(-0.0L, 1.0L) == denorm_min, nextafterl(-0.0L, -1.0L) == -denorm_min, and the same two for nexttowardl. == already distinguishes the two signs here. Adding the matching -0 cases for nextafter and nexttoward would be cheap too.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6257647199 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#issuecomment-6257647199). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-19T01:22:03Z. --- Thanks, this is straightforward, and the tests are thorough. One fix is needed, for the case the Windows test report found. On Windows, `std::nextafter(long double)` calls MinGW-w64's `nextafterl`, and `nexttowardl` is the same function under another name. Its zero branch keeps the sign of `x`: ```c u.ld = x; if (x == 0.0L) { u.parts.mantissa = 1ull; return y > 0.0L ? u.ld : -u.ld; } ``` So with `x = -0.0L`, both directions come out wrong: `(-0, 1)` returns `-denorm_min`, and `(-0, -1)` returns `+denorm_min`. The `double` and `float` paths, and Linux/glibc, are correct. The current tests only start from `+0`, so Windows CI passes anyway. Could you guard the two long double wrappers? For example: ```cpp long double APS5_VABI nextafterl_nid_postfix(long double x, long double y) { if (x == 0.0L && y != 0.0L && !std::isnan(y)) return std::copysign(std::numeric_limits<long double>::denorm_min(), y); return std::nextafter(x, y); } ``` and the same for `nexttowardl_nid_postfix`. Please also add `-0` cases to `CheckLongDouble`, so Windows CI checks them: `nextafterl(-0.0L, 1.0L) == denorm_min`, `nextafterl(-0.0L, -1.0L) == -denorm_min`, and the same two for `nexttowardl`. `==` already distinguishes the two signs here. Adding the matching `-0` cases for `nextafter` and `nexttoward` would be cheap too.
nikowolf123 commented 2026-10-20 22:04:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T22:04:23Z.


The four new exports behave differently on Windows and Linux when the result overflows or underflows: on Windows they raise no floating-point exception and leave errno unchanged (Math.cpp#L538, through L541).

I did not build this PR. I put the four wrapper bodies in a standalone program, built it with the CI toolchain (WinLibs GCC 15.2.0 r7, UCRT) and called each one after errno = 0; feclearexcept(FE_ALL_EXCEPT);:

  • nextafterl(LDBL_MAX, INFINITY) returns inf, raises no flag and leaves errno at 0.
  • nexttoward(DBL_MIN, 0.0L) and nexttowardf(FLT_MIN, 0.0L) return the largest subnormal, raise no flag and leave errno at 0.

On Linux (glibc 2.43), the same calls raise FE_OVERFLOW | FE_INEXACT or FE_UNDERFLOW | FE_INEXACT and set errno to ERANGE.

The cause is the same mingw-w64 code as the -0 sign. These calls link mingw-w64's own nextafterl, nexttoward and nexttowardf, which are static objects in WinLibs' libucrt.a; nexttowardl is an alias of nextafterl. They step the bits with integer arithmetic only. nextafter and nextafterf on main resolve to UCRT, and on Windows they already raise the flags and set ERANGE.

Neither behaviour is measured on a console here, but the two hosts should at least agree. The wrappers could report the result the way glibc does, which on Linux only repeats what glibc has already done:

template <typename T, typename Y>
T ReportNext(T x, Y y, T result) {
    if (std::isnan(x) || std::isnan(y) || x == y) return result;
    if (x == 0) {
        std::feraiseexcept(FE_UNDERFLOW | FE_INEXACT);
    } else if (std::isinf(result) || result == 0 || std::fpclassify(result) == FP_SUBNORMAL) {
        std::feraiseexcept((std::isinf(result) ? FE_OVERFLOW : FE_UNDERFLOW) | FE_INEXACT);
        errno = ERANGE;
    }
    return result;
}

long double APS5_VABI nextafterl_nid_postfix(long double x, long double y) { return ReportNext(x, y, std::nextafter(x, y)); }

The other three wrappers would call it the same way. A check in the style of CheckExp2Errno would cover this: errno == ERANGE and FE_OVERFLOW | FE_INEXACT after nextafterl_nid_postfix(LDBL_MAX, INFINITY). If the two hosts are meant to stay different, the Windows difference belongs in docs/dev/debt/libc.md.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6302778945 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#issuecomment-6302778945). Originally posted by [nikowolf123](https://github.com/nikowolf123) on 2026-10-20T22:04:23Z. --- The four new exports behave differently on Windows and Linux when the result overflows or underflows: on Windows they raise no floating-point exception and leave `errno` unchanged ([Math.cpp#L538](https://github.com/boykopovar/AnyPS5/blob/acbad2fa41cbb926542601e2f83e3a4cfd8caed2/core/libs/prx/libc/src/Math.cpp#L538), through L541). I did not build this PR. I put the four wrapper bodies in a standalone program, built it with the CI toolchain (WinLibs GCC 15.2.0 r7, UCRT) and called each one after `errno = 0; feclearexcept(FE_ALL_EXCEPT);`: - `nextafterl(LDBL_MAX, INFINITY)` returns inf, raises no flag and leaves `errno` at 0. - `nexttoward(DBL_MIN, 0.0L)` and `nexttowardf(FLT_MIN, 0.0L)` return the largest subnormal, raise no flag and leave `errno` at 0. On Linux (glibc 2.43), the same calls raise `FE_OVERFLOW | FE_INEXACT` or `FE_UNDERFLOW | FE_INEXACT` and set `errno` to `ERANGE`. The cause is the same mingw-w64 code as the -0 sign. These calls link mingw-w64's own `nextafterl`, `nexttoward` and `nexttowardf`, which are static objects in WinLibs' `libucrt.a`; `nexttowardl` is an alias of `nextafterl`. They step the bits with integer arithmetic only. `nextafter` and `nextafterf` on main resolve to UCRT, and on Windows they already raise the flags and set `ERANGE`. Neither behaviour is measured on a console here, but the two hosts should at least agree. The wrappers could report the result the way glibc does, which on Linux only repeats what glibc has already done: ```cpp template <typename T, typename Y> T ReportNext(T x, Y y, T result) { if (std::isnan(x) || std::isnan(y) || x == y) return result; if (x == 0) { std::feraiseexcept(FE_UNDERFLOW | FE_INEXACT); } else if (std::isinf(result) || result == 0 || std::fpclassify(result) == FP_SUBNORMAL) { std::feraiseexcept((std::isinf(result) ? FE_OVERFLOW : FE_UNDERFLOW) | FE_INEXACT); errno = ERANGE; } return result; } long double APS5_VABI nextafterl_nid_postfix(long double x, long double y) { return ReportNext(x, y, std::nextafter(x, y)); } ``` The other three wrappers would call it the same way. A check in the style of `CheckExp2Errno` would cover this: `errno == ERANGE` and `FE_OVERFLOW | FE_INEXACT` after `nextafterl_nid_postfix(LDBL_MAX, INFINITY)`. If the two hosts are meant to stay different, the Windows difference belongs in `docs/dev/debt/libc.md`.
MarcZX10 commented 2026-11-23 02:54:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-23T02:54:36Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the reviewed implementation and tests support merging.

What I ran

Reviewed head 32156f1bed22834f539381102c4865a7a2c63630. I read description.md, commits.md, manifest.json, diff.patch, Math.cpp (lines 520–560), and GuestMath.cpp (lines 390–430 and 1160–1200). No commands or tests were run. Both changed files listed in the manifest were included and read; no exclusions were reported in the manifest.

Results

Check Result Note
Code review passed Changed implementations and coverage inspected
Build and tests not run Only author-reported results are available

Findings

Blocking

None.

Non-blocking

None.

Not verified

The reported Windows build and test results were not independently run. Runtime behavior outside the inspected cases was not verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6016062440 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/6295#pullrequestreview-6016062440). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-23T02:54:36Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the reviewed implementation and tests support merging. ## What I ran Reviewed head `32156f1bed22834f539381102c4865a7a2c63630`. I read `description.md`, `commits.md`, `manifest.json`, `diff.patch`, `Math.cpp` (lines 520–560), and `GuestMath.cpp` (lines 390–430 and 1160–1200). No commands or tests were run. Both changed files listed in the manifest were included and read; no exclusions were reported in the manifest. ## Results | Check | Result | Note | |---|---|---| | Code review | passed | Changed implementations and coverage inspected | | Build and tests | not run | Only author-reported results are available | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The reported Windows build and test results were not independently run. Runtime behavior outside the inspected cases was not verified. ## To get this to a good state Nothing is required.

Pull request closed

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!6295
No description provided.