feat(libSceLibcInternal): implement the Dinkumware extended-precision helpers #16237

Merged
Ducengarth merged 15 commits from upstream-pr/16237 into main 2026-11-14 20:17:39 +00:00
Ducengarth commented 2026-11-05 21:42:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ducengarth on 2026-11-05T21:42:12Z.


What

Implement 26 Dinkumware extended-precision helpers in libSceLibcInternal, replacing their parameterless throwing placeholders in Export.cpp:

  • _Dtento, _FDtento, _LDtento: *value * 10^power, where value points to a three-word extended-precision accumulator that is updated in place. On overflow or underflow they set the guest errno to ERANGE (34) and OR 1 into *error when it is not null.
  • _FXp_addh, _FXp_addx, _FXp_invx, _FXp_ldexpx, _FXp_movx, _FXp_mulh, _FXp_mulx, _FXp_setn, _FXp_setw, _FXp_sqrtx, _FXp_subx (float).
  • _LXp_addh, _LXp_addx, _LXp_getw, _LXp_invx, _LXp_ldexpx, _LXp_movx, _LXp_mulh, _LXp_mulx, _LXp_setn, _LXp_setw, _LXp_sqrtx, _LXp_subx (long double).

An extended-precision value is an array of words in decreasing magnitude, each holding at most half the significand (12 bits for float, 26 for double, 32 for long double), ended by a zero word. One template in the new ExtendedPrecision.cpp serves all three types.

Written from the specified behaviour. The helpers do not follow any implementation. Prototypes come from Dinkumware's xmath.h; long arguments are std::int64_t, as in the PS5 ABI.

  • Each operation reads its operands' words into an exact big integer (sign, 64-bit limbs and a binary exponent). Products are formed from 32-bit halves of the significands, so nothing rounds.
  • The result is stored as words cut from its top, WordBits bits at a time, truncated toward zero, without bits below the smallest subnormal. The words the old value occupied after the new terminating zero are cleared.
  • _Xp_invx is long division of a power of two by the value, and _Xp_sqrtx takes square-root digits two bits at a time, both to one word beyond the array. The scratch arrays of _Xp_mulx, _Xp_invx and _Xp_sqrtx are accepted and not used.
  • _Xp_setw splits a value into its leading half-significand word and the rest; the last word of a short array takes the whole rest. _Xp_setn follows the documented split by 10000 for float.
  • _Dtento multiplies by ten, or by one tenth cut to three base-2^WordBits digits, once for each unit of the power. It stops as soon as the value is zero or infinite, so a power of LONG_MIN underflows instead of looping.
  • _LXp_getw and the reads inside _Dtento follow the console's _FXp_getw from #14945, read from firmware 13.42: they add the fourth word when there are at least four.
  • Infinities, NaNs, zeros and the FE_INVALID cases give V6.00's results.

Differences from Dinkumware V6.00 that a caller can see, all recorded in docs/dev/debt/libSceLibcInternal.md:

  • V6.00 rounds into the last word of a full array in some cases and refines _Xp_invx and _Xp_sqrtx with Newton iterations. So the lowest word of a full array, and a one-word reciprocal or square root, can differ here. For example, _FXp_invx of {3} with one word gives the leading twelve bits of one third, not 1.0f / 3.
  • After a value that fits in one word, _Xp_setw writes one zero, where V6.00 writes two. Every helper stops reading at the first zero.
  • A one-word _Xp_mulx multiplier reads only that word, and a word count below 1 does nothing in _Xp_movx, _Xp_addh, _Xp_invx and _Xp_sqrtx. V6.00 reads past the array or calls memcpy with a negative size in these cases.

Scope: _FXp_getw is already in main (#14945), and so are the double _Xp_* (ExtendedPrecisionDouble.cpp, c5e1e8a203). This PR does not touch them; _Dtento uses this file's own helpers. _FAtan, _FSincos, _FTan, _FTgamma, _FLogpoly, _FQuad, _FQuadph and their _L and double versions still throw: no source I found gives their signatures or behaviour.

No title's trace or disassembly was used, and no export was measured on a PS5.

Overlap: pr_overlap --branch on the branch merged with main reports no other pull request with the same exports or new files. It could not compare 7 pull requests that conflict with main (#13778, #13782, #14834, #15822, #17300, #18135, #18298). I scanned the lines each of them adds and removes: none touches these 26 names or ExtendedPrecision.cpp. #17300 renames the double _Xp_subx placeholder, which is already implemented in main.

Tested

Head b543659248, with main d4556df8a8 merged in. Release builds on Windows 11 with MinGW-w64 GCC 15.2 (WinLibs) and on Ubuntu 24.04 under WSL2 with GCC 13.3. No title or homebrew.

  • Builds and test: on both systems the target builds with no warnings from these files, and guest_libc_internal_extended_precision passes 3 of 3 runs. The test calls the 26 exports and _FXp_getw from main, and checks:
    • setw splits 20000 random floats into at most two exact half-width words and one zero; it also covers zero, one word, infinity, no room and subnormals.
    • setn holds 32-bit integers exactly, and rounds the quotient by 10000 to float first for wider ones.
    • addh keeps exact sums of half-precision addends in decreasing words. It also covers cancellation, infinity, Inf - Inf (FE_INVALID), NaN, a full array, and a borrow across four long double words that keeps every word the same sign.
    • mulh and mulx keep exact or six-word products. They also cover the specials, a subnormal product kept exactly, and bits below the smallest subnormal being dropped.
    • movx, ldexpx, addx and subx behave as specified.
    • invx and sqrtx land within 2^-40 and 2^-36 of the exact result and read back within one ulp. A one-word invx keeps the leading twelve bits of one third. sqrtx also covers negative values, -0 and ±infinity.
    • _Dtento gives correctly rounded powers of ten from 10^-22 to 10^22, the one-tenth words for each type, overflow, underflow, a null error pointer and a power of INT64_MIN.
    • _LXp_getw adds a fourth word before rounding.
  • Changed checks: compared with the previous head, the test changes two checks and adds three.
    • Changed: setw of a value with a single word no longer has to write a second zero, and the one-word invx check gains one third.
    • Added: the borrow across words, and the two subnormal products.
  • Mutations: 15 deliberate mutations each make the test fail. They cover:
    • dropping the carry, the multi-limb borrow, a cross term of the 32-bit halves, or the clearing of the old words;
    • a 13-bit window, or letting bits below the subnormal limit through;
    • halving the reciprocal precision, or skipping the square-root exponent parity;
    • rounding the tenth digits, ignoring getw's fourth word, or splitting the last setw word;
    • forgetting the sign in subx, and KeepBelow without its mask.
  • Exports: objdump -p on the Windows libSceLibcInternal.prx lists all 26 names, and _FXp_getw.
  • Repository checks: python3 tools/check_conventions.py --base upstream/main passes.

Not covered: macOS, the rest of the libSceLibcInternal suite after this merge of main, and a run inside a title.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5179155369 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16237). Originally posted by [Ducengarth](https://github.com/Ducengarth) on 2026-11-05T21:42:12Z. --- ### What Implement 26 Dinkumware extended-precision helpers in `libSceLibcInternal`, replacing their parameterless throwing placeholders in `Export.cpp`: - `_Dtento`, `_FDtento`, `_LDtento`: `*value * 10^power`, where `value` points to a three-word extended-precision accumulator that is updated in place. On overflow or underflow they set the guest `errno` to `ERANGE` (34) and OR 1 into `*error` when it is not null. - `_FXp_addh`, `_FXp_addx`, `_FXp_invx`, `_FXp_ldexpx`, `_FXp_movx`, `_FXp_mulh`, `_FXp_mulx`, `_FXp_setn`, `_FXp_setw`, `_FXp_sqrtx`, `_FXp_subx` (`float`). - `_LXp_addh`, `_LXp_addx`, `_LXp_getw`, `_LXp_invx`, `_LXp_ldexpx`, `_LXp_movx`, `_LXp_mulh`, `_LXp_mulx`, `_LXp_setn`, `_LXp_setw`, `_LXp_sqrtx`, `_LXp_subx` (`long double`). An extended-precision value is an array of words in decreasing magnitude, each holding at most half the significand (12 bits for `float`, 26 for `double`, 32 for `long double`), ended by a zero word. One template in the new `ExtendedPrecision.cpp` serves all three types. **Written from the specified behaviour.** The helpers do not follow any implementation. Prototypes come from Dinkumware's [`xmath.h`](https://github.com/ganboing/crt.vc12/blob/f1d47126bf13d1c9dccaa6c53b72217fd0474968/src/xmath.h); `long` arguments are `std::int64_t`, as in the PS5 ABI. - Each operation reads its operands' words into an exact big integer (sign, 64-bit limbs and a binary exponent). Products are formed from 32-bit halves of the significands, so nothing rounds. - The result is stored as words cut from its top, `WordBits` bits at a time, truncated toward zero, without bits below the smallest subnormal. The words the old value occupied after the new terminating zero are cleared. - `_Xp_invx` is long division of a power of two by the value, and `_Xp_sqrtx` takes square-root digits two bits at a time, both to one word beyond the array. The scratch arrays of `_Xp_mulx`, `_Xp_invx` and `_Xp_sqrtx` are accepted and not used. - `_Xp_setw` splits a value into its leading half-significand word and the rest; the last word of a short array takes the whole rest. `_Xp_setn` follows the documented split by 10000 for `float`. - `_Dtento` multiplies by ten, or by one tenth cut to three base-2^`WordBits` digits, once for each unit of the power. It stops as soon as the value is zero or infinite, so a power of `LONG_MIN` underflows instead of looping. - `_LXp_getw` and the reads inside `_Dtento` follow the console's `_FXp_getw` from #14945, read from firmware 13.42: they add the fourth word when there are at least four. - Infinities, NaNs, zeros and the `FE_INVALID` cases give V6.00's results. Differences from Dinkumware V6.00 that a caller can see, all recorded in `docs/dev/debt/libSceLibcInternal.md`: - V6.00 rounds into the last word of a full array in some cases and refines `_Xp_invx` and `_Xp_sqrtx` with Newton iterations. So the lowest word of a full array, and a one-word reciprocal or square root, can differ here. For example, `_FXp_invx` of `{3}` with one word gives the leading twelve bits of one third, not `1.0f / 3`. - After a value that fits in one word, `_Xp_setw` writes one zero, where V6.00 writes two. Every helper stops reading at the first zero. - A one-word `_Xp_mulx` multiplier reads only that word, and a word count below 1 does nothing in `_Xp_movx`, `_Xp_addh`, `_Xp_invx` and `_Xp_sqrtx`. V6.00 reads past the array or calls `memcpy` with a negative size in these cases. Scope: `_FXp_getw` is already in `main` (#14945), and so are the `double` `_Xp_*` (`ExtendedPrecisionDouble.cpp`, c5e1e8a203). This PR does not touch them; `_Dtento` uses this file's own helpers. `_FAtan`, `_FSincos`, `_FTan`, `_FTgamma`, `_FLogpoly`, `_FQuad`, `_FQuadph` and their `_L` and `double` versions still throw: no source I found gives their signatures or behaviour. No title's trace or disassembly was used, and no export was measured on a PS5. **Overlap**: `pr_overlap --branch` on the branch merged with `main` reports no other pull request with the same exports or new files. It could not compare 7 pull requests that conflict with `main` (#13778, #13782, #14834, #15822, #17300, #18135, #18298). I scanned the lines each of them adds and removes: none touches these 26 names or `ExtendedPrecision.cpp`. #17300 renames the `double` `_Xp_subx` placeholder, which is already implemented in `main`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> Head `b543659248`, with `main` `d4556df8a8` merged in. Release builds on Windows 11 with MinGW-w64 GCC 15.2 (WinLibs) and on Ubuntu 24.04 under WSL2 with GCC 13.3. No title or homebrew. - **Builds and test**: on both systems the target builds with no warnings from these files, and `guest_libc_internal_extended_precision` passes 3 of 3 runs. The test calls the 26 exports and `_FXp_getw` from `main`, and checks: - `setw` splits 20000 random floats into at most two exact half-width words and one zero; it also covers zero, one word, infinity, no room and subnormals. - `setn` holds 32-bit integers exactly, and rounds the quotient by 10000 to `float` first for wider ones. - `addh` keeps exact sums of half-precision addends in decreasing words. It also covers cancellation, infinity, `Inf - Inf` (`FE_INVALID`), NaN, a full array, and a borrow across four `long double` words that keeps every word the same sign. - `mulh` and `mulx` keep exact or six-word products. They also cover the specials, a subnormal product kept exactly, and bits below the smallest subnormal being dropped. - `movx`, `ldexpx`, `addx` and `subx` behave as specified. - `invx` and `sqrtx` land within 2^-40 and 2^-36 of the exact result and read back within one ulp. A one-word `invx` keeps the leading twelve bits of one third. `sqrtx` also covers negative values, -0 and ±infinity. - `_Dtento` gives correctly rounded powers of ten from 10^-22 to 10^22, the one-tenth words for each type, overflow, underflow, a null `error` pointer and a power of `INT64_MIN`. - `_LXp_getw` adds a fourth word before rounding. - **Changed checks**: compared with the previous head, the test changes two checks and adds three. - Changed: `setw` of a value with a single word no longer has to write a second zero, and the one-word `invx` check gains one third. - Added: the borrow across words, and the two subnormal products. - **Mutations**: 15 deliberate mutations each make the test fail. They cover: - dropping the carry, the multi-limb borrow, a cross term of the 32-bit halves, or the clearing of the old words; - a 13-bit window, or letting bits below the subnormal limit through; - halving the reciprocal precision, or skipping the square-root exponent parity; - rounding the tenth digits, ignoring `getw`'s fourth word, or splitting the last `setw` word; - forgetting the sign in `subx`, and `KeepBelow` without its mask. - **Exports**: `objdump -p` on the Windows `libSceLibcInternal.prx` lists all 26 names, and `_FXp_getw`. - **Repository checks**: `python3 tools/check_conventions.py --base upstream/main` passes. Not covered: macOS, the rest of the `libSceLibcInternal` suite after this merge of `main`, and a run inside a title. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!16237
No description provided.