feat(libc): implement the Dinkumware _Sto*x conversions #8384
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8384"
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-20T11:53:06Z.
What
Implements nine absent Dinkumware conversion helpers in
DinkumwareInternals.cpp. The behaviour comes from running the decryptedlibSceLibcInternal.sprxexports on the host.Floating point:
_Stodx(QlcJbyd6jxM),_Stofx(7dlAxeH-htg),_Stoldx(BKidCxmLC5w),(text, end, pten, int* error).10^(exponent + pten)once, as_Stoddoes.*erroris always written: 1 on overflow or underflow to zero (errnois thenERANGE), otherwise 0.errnonor*error, for all three:_Stodx("1e-310", 0),_Stodx("1e-300", -10),_Stofx("1e-45", 0)and_Stoldx("1e-4940", 0)give error 0 and leaveerrnoalone._Stofxused to keep theERANGEthatstrtofsets for an inexact subnormal; it no longer does.ptenis a 64-bit value.errnosurvives a call without a range error. That includes text that underflows or overflows unscaled but is brought back into range bypten:_Stodx("1e-400", 300)gives1e-100,_Stodx("1e400", -100)gives1e300and_Stofx("1e39", -1)gives1e38, all with error 0._Stoldxuses the extended range:"1e400"is finite, error 0.ptenthrows, as in_Stod; thedocs/dev/debt/libc.mdentry for that throw now names all six float helpers._Stof(CpWcnrEZbLA) and_Stold(iNbtyJKM0iQ) arexor ecx, ecxplus a call to thexvariant, like_Stod._Stodnow goes through_Stodx.Integers:
_Stollx(mOnfZ5aNDQE),_Stoullx(pSpDCDyxkaY),_Stoulx(YDnLaav6W6Q),(text, end, base, int* error). All return 64-bit values (_Stoulxisstd::uint64_t, not the host'sunsigned long, which is 32 bits on Windows).strtoll/strtoullwith*errorset to theerrnothey produce (ERANGE) or 0.*error = EINVAL(22) and*end = text, and leaveserrnoalone._Stollnow goes through_Stollx, so it handles an invalid base the same way._Stolx(Ecwid6wJMhY) returnsstd::int64_tand has a quirk:*errorarestrtoll's;errnois set only when the text also overflowsstrtoull.So
"9223372036854775808"and"-18446744073709551615"give the clamped value with*error = ERANGEanderrnountouched, while"18446744073709551616"sets both.Tested
Linux (CachyOS, GCC, Release).
guest_dinkumware_internalsgainsTestFloatErrorsandTestIntegerErrorsand passes. Every assertion is a value, end pointer, error-argument value anderrnotaken from the console module's output. Removing the overflow-rescale condition makes the test fail on the rescaled vectors. The subnormal vectors were measured by running firmware 13.60'slibSceLibcInternal.sprx(and PPSA08427'slibc.prx, which agrees) in a Linux host process, not on a console; restoring the olderrnohandling fails_Stofx inexact subnormal. Also built for Windows (MinGW, Wine).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)DinkumwareInternals.cppand its test; this branch is stacked on it)Assisted With AI: Yes But all test done locally on actual hardware
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T20:40:25Z.
1.
_Stolxand_Stoulxreturn a 32-bit value on WindowsDinkumwareInternals.cpp#L135returns the host'slong, and#L131returns the host'sunsigned long. Both are 32 bits on Windows, but the guest'slongis 64 bits. I built the file with the Windows job's toolchain (WinLibs GCC 15.2.0,-O3)._Stolxreturns throughmov eax, ebx, so a guest calling_Stolx(text, &end, 10, &err)gets:"-5"→ 4294967291"4294967296"→ 0, witherr == 0"9223372036854775807"→ 4294967295, witherr == 0On Windows,
guest_dinkumware_internalsstops with_Stolx overflow inside the unsigned range(line 129). The PR checks don't show this because thewindowsjob runs only on push and workflow_dispatch (build.yml#L560).2243e17f34fixed the same problem for_Stoul:The test declares both functions with the same 32-bit types (lines 18-19), so it needs the change too. With only
_Stolxchanged, it still fails at line 134 (_Stoulx wraps a negative value). With both changes, the test passed in my WinLibs build. That build linked the two files against stand-ins that call the host'sstrto*, asStrings.cppdoes. Thelong ptenparameters are also guestlongs, so they should bestd::int64_ttoo.2. Digits that overflow before scaling are never rescaled
DinkumwareInternals.cpp#L52rescales only whenstd::isfinite(value)is true. That keeps"inf"and"nan"unscaled. But it also skips the inf thatstrtof/strtodreturn when the digits alone overflow, so a negative pten can't bring the value back._Stofx("1e39", &end, -1, &err)returns inf witherr == 1anderrno == ERANGE, on both Linux and Windows._Stodx("1e400", &end, -100, &err)does the same.By the rule in the PR description (digits × 10^(exponent + pten), rounded once), these should be 1e38f and 1e300 with no error. That is the mirror image of the
"1e-400"with 300 case the test already covers.Dinkumware's
_Stodxalso scales bypten + lo[0]in a single_Dtentoxcall (xxstod.h#L45). Only that call reports the range error (xxxdtent.h#L102-L107), and inf and nan are recognised from their text (FL_INF,FL_NAN).A negative pten is an ordinary input: Dinkumware's
num_getcounts leading fraction zeros into it (xlocnum#L752-L756). So reading"0.0001e41"as a float calls_Stofx("0.01e41", &ep, -2, &err), which here sets failbit instead of giving 1e37. A minimal fix:"inf"text does not setERANGE, so it still passes through unscaled. The existingguestErrno = 0before the second parse clears the leftoverERANGE. With this change,"1e39"with -1 gives 1e38f with error 0, and"1e400"with -100 gives 1e300. Running_Stofx("1e39", -1)through the console module would confirm the value to put inTestFloatErrors.3. The hexadecimal throw has no debt entry
DinkumwareInternals.cpp#L53throws for hexadecimal text with a non-zero pten, for example_Stod("0x3", &end, -1). This now covers_Stodx,_Stofxand_Stoldx, and through them_Stod,_Stofand_Stold. #8376 says the console's result there "does not follow a simple rule".AGENTS.md says "if the general behaviour is unknown, throw and record it in
docs/dev/debt/<library>.md", but neither PR changesdocs/dev/debt/libc.md. Alibc.mdentry linkingDinkumwareInternals.cppwould cover it. It would first say that these functions throw for hexadecimal text with a non-zero scale, then give the console data point from #8376 (0x3with -1 gives 0.29999999999999999).Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T22:48:54Z.
@hypermoist agreed with nikowolf123's three points. To keep it in one place:
_Stolx/_Stoulx(and everylong pten) should bestd::int64_t/std::uint64_t, in the test declarations too. On Windowslongis 32 bits, so the values and scales are cut to their low half."1e400"with -100 is 1e300 by the rule in the description)."inf"text sets no ERANGE, so it can be told apart.docs/dev/debt/libc.mdentry.The same three apply to #8376, which this branch builds on, so fixing them there first and rebasing this one keeps the two in step. After that, the bar is a full CI run with
guest_dinkumware_internalspassing on Linux and Windows.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T23:11:52Z.
Thanks @nikowolf123 and @oneandonlydean. All three points are fixed in
6a23fe146a(the merge of the updated #8376 into this branch):_Stolxnow returnsstd::int64_tand_Stoulxreturnsstd::uint64_t. Everypten(_Stodx/_Stofx/_Stoldx/_Stod/_Stof/_StoldandScaledFloat) isstd::int64_t, and the test declarations match. The 64-bit expectations in the test usedl/ulliterals, which are also 32 bits on Windows, so they are nowll/ull.ScaledFloatstill rescales when the first parse overflowed to infinity with ERANGE."inf"text sets no ERANGE, so it is left as it is. New vectors, both checked against the firmware:_Stofx("1e39", -1)gives 1e38 with*error == 0, and_Stodx("1e400", -100)gives 1e300 with*error == 0. With the overflow term removed, the test fails onan overflowing text scaled back into range.docs/dev/debt/libc.mdentry for the hexadecimal-with-scale throw now names_Stod,_Stodx,_Stofx,_Stoldx,_Stofand_Stold.guest_dinkumware_internals_testspasses on Linux andtools/check_conventions.pyis clean. I have no Windows toolchain locally, so the Windows CI job is the check for point 1.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T13:56:25Z.
Review: COMMENTED
Checked against main
522e32e3b6and Dinkumware's xxstod.h / xxxdtent.h (openqnx cc95df35):6a23fe1(64-bit returns andpten, rescaling an overflowed inf, the libc.md entry). The branch still merges cleanly into current main now that #8376 is in; the mergedDinkumwareInternals.cppis exactly this branch's file, and_Stodbehaves as on main.ScaledFloat(DinkumwareInternals.cpp:70) sets*error = 1only when the result is 0 or inf, but keeps whatever ERANGE the hoststrtod/strtofset. In Dinkumware's_Dtentox, errno and*perrare set in the same branch (xxxdtent.h L102-107), and the QNX copy also counts a denormal as underflow (L80-88). I couldn't run this; reading glibc's strtod suggests_Stodx("1e-310", &end, 0, &err)here gives errno = ERANGE with err = 0, which neither variant does. Since you note the console rounds once (so it may not be stock Dinkumware), could you run one subnormal case ("1e-310"with scale 0, and maybe"1e-300"with -10) through the console module and add it toTestFloatErrors? Whichever way it answers, the test would then pin the combination down.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.