fix(libc): keep the sign of a failed strtof and match libc.prx strtof ERANGE #8865
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8865"
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 hypermoist on 2026-10-21T03:43:04Z.
What
A follow-up to #8379, and an answer to #9691 for
strtofcovering the cases where the float parsers still differ from the console. The evidence is the decryptedlibSceLibcInternal.sprx(PS5 13.60): the exports were loaded and called directly, and their results compared with this build's. The addresses used arestrtod0x65860,strtof0x731a0 (which is_Stof(s, end, 0)),strtold0x749b0,wcstod0x863c0,wcstof0x88a10 andwcstold0x89240.Failed parse after a minus sign.
strtoldandwcstoldreturn-0.0:"-","-x","-.","-e5","- 1","--1"," -.". If the character after the sign starts aninf/nanspelling (i,I,n,N), the result is+0.0:"-in","-N","-nax".strtofandwcstoffollow the titles'libc.prx(see below). A lone sign ("-"," -") gives+0.0. Every other failed parse after-gives-0.0, including"-in"and"-nax".libSceLibcInternal's_Stofdoes the reverse for those two groups.strtod,wcstodandatofkeep+0.0, as #3905 made them.#3905 (for #3090) made every failed parse
+0.0by following FreeBSD's gdtoa. The console's Dinkumware code differs from that for the float and long double forms, soCheckNoConversionZero(renamed fromCheckNoConversionIsPositiveZero) now expects the console's signs. The end pointer stays at the start of the string in every case.Hexadecimal overflow and underflow in
strtold(andwcstold) leave errno alone. Examples:0x1p+20000→HUGE_VALLand0x1p-16446→ 0, both withoutERANGE. Of 40k random hexadecimal inputs, the console setERANGEfor none of them instrtold. Decimal text keepsERANGE(strtold("1e-5000")).strtof(andwcstof) follow thelibc.prxtitles ship. Titles import these NIDs from their ownlibc.prx; the firmware has no systemlibcmodule. Run side by side with this libc on a Linux host, thelibc.prxof PPSA08427 (J03547016) and of PPSA03671 (J03848509) give the same results:ERANGEonly when it is inexact:1e-45,1e-40,3e-45and-1e-45set it. The exact 110-digit expansion of 2^-149 and1.17549435e-38(FLT_MIN) do not.maincleared it in every case.0x1.8p-150,0x0.0000018p-126), asstrtoddoes below. Values rounding between subnormals leave it alone (0x1.8p-149,0x1.000001p-149,0x1.4p-149). Underflow to zero and overflow set it (0x1p-150,0x1p+200). @JJenkx found the round-up case.-nangives a negative quiet NaN (0xffc00000), as theirstrtoddoes.strtoldkeeps a positive one in both copies.libSceLibcInternal.sprx's_Stofleaveserrnoalone for hexadecimal under- and overflow. An earlier revision of this PR followed it; it now followslibc.prx. #9691 reports the samestrtofresults for three other titles'libc.prx.strtod(andwcstod) setERANGEfor hexadecimal text whose value lies below the smallest subnormal and rounds up to it. Examples:0x1.8p-1075→0x1p-1074withERANGE.0x1p-1074and0x1.8p-1074are representable or round between subnormals, so they set nothing. Of 300k random subnormal hexadecimal results, exactly these cases (2483) setERANGE, and no other rounding did. Decimal text does not do this ("3e-324"gives the smallest subnormal withoutERANGE), which matches #8379.Not changed. The console's
_Stofdoes not round to nearest. About a third of decimal inputs within a relative 2^-51 of a halfway point between two floats, and some hexadecimal inputs with more than 24 significant bits, differ in the last bit; for example,1e-40is0x1.16c3p-133there. It is not(float)strtodeither. Its rounding is not reproduced, and a debt entry records that.Also not changed (recorded in a debt entry):
libc.prxbuilds take aNAN(...)payload instrtof(nan(0x123)→0x7fc00123,-nan(5)→0xffc00005). The payload depends on the digit count in a way not pinned down, sostrtofreturns the default NaN, with the sign kept.strtodsetsERANGEfor a nonzero subnormal in J03547016 but not in J03848509 orlibSceLibcInternal. The current behaviour (noERANGE) is kept.Tested
Linux (CachyOS, GCC, Release), merged with current
main:guest_mathandguest_wide_stringspass, and each case they gain was checked against the firmware or thelibc.prxbuilds above. Under WinLibs GCC 15.2 + Wine,guest_wide_stringspasses and the patchedlibc.prxbuilds. All new and changedguest_mathchecks pass there too, checked with a local non-abortingRequire. Two existing checks in code this PR does not touch fail in that build:strtoul("-18446744073709551616!")inCheckIntegerConversionsand the existinghypot3(0x1p1000, …)check.Differential of this revision against a title's bundled
libc.prx: 300k random hexadecimal strings with exponents -160 to -121, plus each sign prefix ("","-","+"," -"," ") combined with failed andinf/nanspellings. The only differences are the 5nan(5)payload cases.Earlier differentials, unchanged by this revision:
libSceLibcInternal.sprx: 600k random short strings throughstrtof,strtodandstrtold, with no mismatch in end pointer, sign or (exceptstrtofhex) errno.tools/check_conventions.pyis clean.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)strtof)🤖 Generated with Claude Code
https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T04:13:54Z.
Review: APPROVED
Reviewed the parser changes and focused regression coverage. The sign handling only applies when conversion consumes no input, and the hexadecimal errno adjustment is restricted to the documented float/long-double or smallest-subnormal cases. The PR reports extensive firmware differential results, a full Linux build, all guest tests passing, and clean conventions. I found no blocking issue in the diff.
Imported from boykopovar/AnyPS5. Originally posted by tkcm01 on 2026-10-21T17:26:59Z.
Windows data point for
0b52caf718, merged ontomainec7eacc62(clean merge). GitHub Actionswindows-latestrunning this repo's Build workflow (full Windows build, WinLibs GCC, Release,BUILD_TESTING=ON) on my fork.guest_mathandguest_wide_stringsincluded (run).mainplus only this PR'sGuestMath.cppandGuestWideStrings.cpp:guest_mathandguest_wide_stringsabort (0xc0000409); the other 296 selected tests pass (run).Not tested: Linux, macOS, a title.
AI-assisted: yes
Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T09:10:16Z.
@tkcm01 thanks for the full Windows CI run (1787 tests passing) and the negative control showing
guest_mathandguest_wide_stringsabort without the implementation.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T13:35:23Z.
Revised after #9691. One part of this PR was wrong for the module these NIDs come from.
strtof/wcstofnow setERANGEthe way thelibc.prxtitles ship does. The earlier revision followedlibSceLibcInternal's_Stof, which leaveserrnoalone for hex under- and overflow. Titles importstrtoffrom their ownlibc.prx, and the firmware has no systemlibc.libc.prxagree:ERANGEonly when inexact.0x1p-150and0x1p+200set it.-0.0results for failed signed parses.strtoldhex withoutERANGE.strtodhex rounding-up case.main, rebuilt, andguest_math/guest_wide_stringspass on Linux. Under MinGW + Wine,guest_wide_stringspasses and the newstrtofchecks pass. The two existingguest_mathfailures there (strtoul,hypot3) are in code this PR doesn't touch.