feat(libc): add Dinkumware float bit helpers from firmware #8397
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/8397"
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-20T12:14:09Z.
What
Implements 18 absent Dinkumware floating-point helpers in
MathExact.cpp, next to_LDtest/_Dclass. The behaviour comes from running the decrypted firmware 13.60libSceLibcInternal.sprxexports in a Linux harness, not on a console.Return codes are the Dinkumware classes: -2 denormal, -1 finite, 0 zero, 1 infinite, 2 NaN.
_Dint(-vyIrREaQ0g),_FDint(HcpmBnY1RGE),_LDint(alNWe8glQH4),(value*, short scale).scale = -16411comes back unchanged with code 0._Dscale(FMU7jRhYCRU),_FDscale(SSvY6pTRAgk),_LDscale(s-Ml8NxBKf4),(value*, int64 exponent).long(std::int64_there, since the hostlongis 32 bits on Windows): 2^31 overflows to ±inf and -2^32 + 3 underflows to zero, as on the console._LDscaleit always stores +0.errnois untouched._Dunscale(4Wt5uzHO98o),_FDunscale(41SqJvOe8lA),_LDunscale(ArZF2KISb5k),(short* exponent, value*)._Dnorm(NApYynEzlco),_FDnorm(0NwCmZv7XcU),_LDnorm(ldbrWsQk+2A),(uint16_t* words)._LDnormshifts the 64-bit mantissa until bit 63 is set and returns minus the shift._LDnormloops forever. This build throwsstd::invalid_argumentinstead of hanging, anddocs/dev/debt/libc.mdrecords the difference._Fpcomp(vhPKxN6zs+A),_FFpcomp(EBkab3s8Jto),_LFpcomp(-BwLPxElT7U) return 1 when x < y, 2 when x == y and 4 when x > y, and 0 if either operand is NaN._LFpcompthe NaN test reads the raw bits (exponent 0x7fff and any fraction bit below bit 63)._Poly(aINUE2xbhZ4),_FPoly(5AN3vhTZ7f8),_LPoly(c6Qa0P3XKYQ),(x, table, degree).table[0]; a degree ≤ 0 returnstable[0].vfmadd, so the code usesstd::fma._LPolyis x87 multiply then add._Dtentoand the_Dsignfamily are left out (multi-part extended arithmetic, and #6085).Tested
Linux (CachyOS, GCC, Release).
libSceLibcInternal.sprxand compared each export against this build'slibc.prx, over 200 000 random inputs per function. The inputs were random bit patterns including denormals, unnormals, pseudo-zeros and out-of-range scales. Result: 0 mismatches. Unnormal_LFpcompoperands with unequal values were also run one by one through the firmware 13.60 module and PPSA08427's bundledlibc.prxin the same harness; both return 2, as this build does. Zero significands for_LDnormwere excluded because of the hang.guest_dinkumware_float_bitscovers every function, including the 64-bit scale vectors, with each expected value and return code taken from the console module's output. It passes, as doguest_mathandguest_math_exact.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)Assisted With AI: Yes. All tests were run locally on computer hardware (AMD Ryzen 5 5600G, 32 GB RAM, NVIDIA GeForce RTX 3060, CachyOS Linux 7.3, GCC 16.2), not on an actual console.
Imported from boykopovar/AnyPS5. Originally posted by nikowolf123 on 2026-10-20T20:40:29Z.
I built this with the Windows CI toolchain (WinLibs GCC 15.2.0 r7, UCRT). Two things break on Windows, and one debt entry is missing.
1.
_Dscale,_FDscaleand_LDscalelose the upper 32 bits of the exponent on Windows (MathExact.cpp#L387)The guest passes a 64-bit
long, and the console vector at GuestDinkumwareFloatBits.cpp#L114 depends on the full width:_Dscale(1.0, 1099511627776)returns 1 with +inf. Hostlongis 32 bits on Windows, soScaleBinary(L132) and the three exports read only ESI. I called_Dscale_nid_postfixthe way guest code does, through asysv_abipointer that takesstd::int64_t, with*value = 1.0:The test has the same problem. On Windows the literal
1099511627776Lnarrows to 0 (GCC warns with-Woverflow), so L114 aborts. Other exports take guestlongasstd::int64_t:scalbln_nid_postfixin this file, andldiv_nid_postfixandlabs_nid_postfixin Math.cpp.2243e17f34fixed the same mismatch in_Stoul. To fix this, takestd::int64_t exponentinScaleBinary(clamped withstd::clamp<std::int64_t>) and in the three exports. The test then needs the same type on L13-L15 and inCheckScale, andstd::int64_t{1} << 40on L114.2. The new test does not compile on Windows (GuestDinkumwareFloatBits.cpp#L54)
On Windows
APS5_VABIis__attribute__((sysv_abi))(VabiMacros.hpp#L5), and GCC will not convert those functions to the plain pointers theCheck*helpers take. All 70Check*calls fail to compile. The first error is:The checks on this PR skip the full
windowsjob, because build.yml runs it only on pushes to main and on workflow_dispatch. Fiber.cpp and GuestAjmTeardown.cpp put the macro in the pointer type, and that fixes it here too:With both changes, every vector passes on Windows and on Linux. I did not run the
_LDnormthrow case, because it needs the full build.3. The
_LDnormthrow has no debt entry (MathExact.cpp#L426)The PR description says the firmware loops forever on a zero significand, and this build throws instead. CONTRIBUTING.md asks for known gaps to be recorded in the debt files, with the gap stated first ("what differs from the console, what is unverified, what throws"). The
__modti3entry (ec2acea5bd) and thepthread_spin_lockentry record the same kind of divergence. For example, under Functional indocs/dev/debt/libc.md:Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T22:54:16Z.
@hypermoist Agreed with nikowolf123. I can confirm point 2 independently: with the Windows toolchain,
GuestDinkumwareFloatBits.cppstops at line 79 ("no matching function for call toCheckInt"), becauseAPS5_VABIfunctions aresysv_abithere and theCheck*helpers take plain function pointers. PuttingAPS5_VABIin the helpers' pointer types fixes it. So, to land this:APS5_VABIin theCheck*pointer parameters;std::int64_tfor thelong exponentof_Dscale,_FDscale,_LDscaleandScaleBinary(and in the test, including the 2^40 vector);docs/dev/debt/libc.mdentry for the_LDnormzero-significand throw.After that, the bar is a full CI run with
guest_dinkumware_float_bitspassing on Linux and Windows.Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T22:56:46Z.
Thanks for building it on Windows. All three are fixed in the latest push:
ScaleBinaryand_Dscale/_FDscale/_LDscaletakestd::int64_t(clamped withstd::clamp<std::int64_t>), and the test declarations,CheckScaleand the vectors usestd::int64_t(std::int64_t{1} << 40on L114). I added your two Windows-sensitive exponents as vectors, after running them through the console's_Dscalewith*value = 1.0: 2^31 gives 1 and +inf, and -2^32 + 3 gives 0 and +0.Check*helpers now takeshort (APS5_VABI* function)(...), as Fiber.cpp and GuestAjmTeardown.cpp do.libc.mdhas an_LDnormentry next to__modti3: a zero significand never returns on the console, and this throws instead.guest_dinkumware_float_bitspasses on Linux. I have no MinGW toolchain here, so I have not built it for Windows myself.Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T15:34:12Z.
Review: COMMENTED
Checked against main
b607bfdc8band the Dinkumware V5.00 sources in openqnx (trunk/lib/c/ansi xdscale.c, xdnorm.c, xdunscal.c, xldscale.c, inc/xmath.h):std::int64_texponent,APS5_VABIin theCheck*pointers, the_LDnormdebt entry) all look addressed ine9a3d03. None of these 18 functions exists on main yet, the branch merges without conflicts, and all 18 NIDs match their names._Dint/_FDint/_LDintdrop count and cut-offs match xdint's(_DBIAS + 48 + _DOFF + 1) - xchar - xexp(1075 / 150 / 0x3ffe + 64), and_Dnormmatches xdnorm.c, including the zero-fraction case returning 1 with only the sign kept. QNX's 80-bit_LDscalekeeps the sign on underflow, so the always-+0 behaviour here really is a newer-firmware difference, which your measurement covers._Dintreturns 0, not "the class", when no fraction bits are dropped. The code and the 3.0 vector already do this, matching Dinkumware._LFpcomp: the only unnormal vector compares two operands with the same value, so it can't distinguish that from the x87 unordered path (SDM vol. 1 §8.2.2 treats unnormals as invalid operands, which the pseudo-infinity → 2 vector also suggests). Reading the code, an unnormal 1.0 against 3.0L would give 2 here. I couldn't run it, so a console vector with unequal values would pin it down either way.Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-30T01:31:18Z.
Review: APPROVED
Reviewed — looks good
Reviewed exact head
1e89d2117d9448d9976510d88a0df1308f789911; the 18 declarations, implementations, CMake registration and regression cases align, and I found no blocking issue.The current upstream
mainis8abc96f0aefea85ee48601b936dd1401b24bf901; none of the five PR files changed since its basef26434afa36e58778a9ff4b948aea52dff19eb0c, and GitHub reports the PR mergeable. Hosted conventions, eligibility, report, relinker, registration and write-watch checks pass; hosted Linux and Windows product jobs are skipped. The author reports 200,000 randomized firmware-module comparisons per helper with no mismatches, passing Linux math/helper tests, and a 3/3 Windows helper-test run; I did not build or run tests locally._LDnorm's zero-significand throw instead of the module's non-returning loop is recorded as a remaining difference in the PR's debt entry.