feat(libc): implement the nextafter and nexttoward families #6295
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/6295"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
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,nexttowardandnexttowardl, beside the already exportednextafterf.Each delegates to the host's
std::nextafter/std::nexttoward, as the other libc mathexports do.
Tested
Windows 11, MinGW-w64 GCC 15.2.0 (WinLibs
x86_64-ucrt-posix-seh, release15.2.0posix-14.0.0-ucrt-r7), CMake 3.31.8, Ninja 1.13.2.Full build clean.
guest_mathpasses in 0.03 s. All 72 libc-registered tests pass.No title or homebrew: nothing available here imports these exports.
Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repositoryImported 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.
Imported from boykopovar/AnyPS5. Originally posted by coltken95 on 2026-10-15T21:02:43Z.
Tested on Linux x86-64 (Release build), with
40da63a8dmerged into main4eb91eacd:guest_mathpasses.libc/src/Math.cpp:guest_math_testsdoesn'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)
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.
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:
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
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'snextafterl, andnexttowardlis the same function under another name. Its zero branch keeps the sign ofx:So with
x = -0.0L, both directions come out wrong:(-0, 1)returns-denorm_min, and(-0, -1)returns+denorm_min. Thedoubleandfloatpaths, 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:
and the same for
nexttowardl_nid_postfix. Please also add-0cases toCheckLongDouble, so Windows CI checks them:nextafterl(-0.0L, 1.0L) == denorm_min,nextafterl(-0.0L, -1.0L) == -denorm_min, and the same two fornexttowardl.==already distinguishes the two signs here. Adding the matching-0cases fornextafterandnexttowardwould be cheap too.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
errnounchanged (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 leaveserrnoat 0.nexttoward(DBL_MIN, 0.0L)andnexttowardf(FLT_MIN, 0.0L)return the largest subnormal, raise no flag and leaveerrnoat 0.On Linux (glibc 2.43), the same calls raise
FE_OVERFLOW | FE_INEXACTorFE_UNDERFLOW | FE_INEXACTand seterrnotoERANGE.The cause is the same mingw-w64 code as the -0 sign. These calls link mingw-w64's own
nextafterl,nexttowardandnexttowardf, which are static objects in WinLibs'libucrt.a;nexttowardlis an alias ofnextafterl. They step the bits with integer arithmetic only.nextafterandnextafterfon main resolve to UCRT, and on Windows they already raise the flags and setERANGE.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:
The other three wrappers would call it the same way. A check in the style of
CheckExp2Errnowould cover this:errno == ERANGEandFE_OVERFLOW | FE_INEXACTafternextafterl_nid_postfix(LDBL_MAX, INFINITY). If the two hosts are meant to stay different, the Windows difference belongs indocs/dev/debt/libc.md.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 readdescription.md,commits.md,manifest.json,diff.patch,Math.cpp(lines 520–560), andGuestMath.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
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