fix(libc): compute nextafter and nextafterf with FreeBSD msun's algorithm, as titles' libc.prx does #10354
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10354"
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 JJenkx on 2026-10-23T18:57:55Z.
What
nextafterandnextafterfreturned the host's functions, which differ from thelibc.prxthat titles ship in two ways on Windows (UCRT):errno: the titles' copies setERANGEat msun's overflow and underflow returns only. UCRT also sets it in two places where they don't:nextafter(0, 1)is0x0000000000000001witherrnountouched there and withERANGEhere, whilenextafter(DBL_MIN, 0)setsERANGEon both.x == ywith a subnormalx.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,
maindiffers from the titles' copies on 1,819nextafterpairs (1,767 NaN results, 52errnoonly) and 5,298nextafterfpairs (5,246 NaN results, 52errnoonly).The titles' copies (NIDs
h+J60RRlfnkand3m2ro+Di+Ck) are FreeBSD msun'ss_nextafter.cands_nextafterf.c, withERANGEat those two returns. Both functions now compute that, stepping the bit pattern towardsy.The NaN case computes
x + yinto avolatilefor its exception flags and returns x's NaN, or else y's, quieted. Returningx + ydirectly is not enough: GCC at-O3, whichMath.cppis built with, commuted it toy + xand returned the other NaN for 31 double and 71 float pairs.This leaves
nextafterland thenexttowardfamily 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 heade7d3411eeab5079cfc87dc89b751c3e2dc147ee7.Linux x86-64, GNU C++ 16.2.1, Release. Independent full libc/math target build;
guest_mathandguest_dinkumware_internalseach passed 3/3 on this head.489efaec7f020a092f28bec7884e12e3e8a14a541a4dc62866c579afce06baf6). No SDK binaries or code dumps are attached.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,main1e1ab77684.Comparison: a scratch loader runs the decrypted
libc.prxof eleven titles natively and supplies__error: PPSA04158, PPSA05916, PPSA07923, PPSA09614, PPSA13934, PPSA20423, PPSA25636, PPSA26528, PPSA33655, PPSA34008 and PPSA35169. It compared theirnextafterandnextafterfwith this PR's code, checking result bits (NaN payloads included) anderrno, on 4,000,784 pairs per function and title:DBL_MIN, 1, the largest finite values, infinity, signalling and quiet NaNs);xin the subnormal, smallest-normal or largest binades.All eleven give identical bits and
errnoon all of them, with this PR built at-O3, and at-O0and-O2for PPSA07923.ERANGEis set on 445,828 of the double pairs and 453,078 of the float ones.guest_math: the newCheckNextafterConsoleBitshas 16 double and 16 float cases with the console's results anderrno:errno);ERANGE);ERANGE) and infinity towards zero;y, and NaN pairs in both orders.main's existingnextafterchecks still pass.guest_mathandguest_dinkumware_internalspass in three runs out of three on the exact head.Negative control: with
main'sMath.cppand these tests,guest_mathaborts in aRequire, whileguest_dinkumware_internalsstill passes.A standalone
-O0and-O3build of the new code with the new vectors andmain's existing checks passes.python tools/check_conventions.py --base origin/mainandgit diff --checkpass.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 repository (attach them to this PR)sinhfentry didAI-assisted (Claude Code).
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.prxmeasurements 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.