feat(libc): export the std::__sce_v2 multiprecision helpers #15471

Merged
Danigamer05 merged 1 commit from upstream-pr/15471 into main 2026-11-07 01:15:10 +00:00
Danigamer05 commented 2026-11-03 14:08:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-03T14:08:01Z.


What

Export the four std::__sce_v2 multiprecision helpers from libc. They hold an unsigned value as five 32-bit digits, each stored in a 64-bit word.

Export NID Original RVA
_ZNSt8__sce_v27_MP_GetEPy PPRfp98KWh0 0x10CB80
_ZNSt8__sce_v27_MP_AddEPyy VXdN8rHxS4s 0x10CB90
_ZNSt8__sce_v27_MP_MulEPyyy dNaphWn7TxE 0x10CBF0
_ZNSt8__sce_v27_MP_RemEPyy aP5bpilz7aQ 0x10CC90

The behavior comes from the libc.prx bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6).

VersionedMultiprecision.cpp follows that module's operations:

  • _MP_Get combines the two low digits.
  • _MP_Add adds a 64-bit value and carries no further than the fifth digit.
  • _MP_Mul writes the four-digit product of two 64-bit values and clears the fifth digit.
  • _MP_Rem uses Knuth's algorithm D: it scales the dividend and divisor, reduces one digit at a time, and unscales. As in the module, it propagates a borrow through every upper digit and adds back into one digit only.

Three inputs cannot behave as in the module, and the debt entry records them:

  • Divisor below 2^32. The module's _MP_Rem reads the stack word past its one-digit divisor and the word before the dividend. Its result then changes with the contents of those words and with the dividend's address, and in about half of the side-by-side calls it is not the remainder. Here the exact remainder is returned.
  • Non-zero fifth dividend digit. The module also reads the word past the five-word array. Here that word is taken as zero.
  • Zero divisor. The module reads uninitialized stack. Here the dividend is left unchanged.

None of the three arises for a non-zero divisor of at least 2^32 and a dividend of at most four digits, such as a 64-bit product plus a 64-bit addend. On those inputs the module's _MP_Rem returned the exact remainder in every side-by-side call.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 4f41f4c375f. Exact tested commit: c4d4acb9804.

  • Built libc, libkernel, their NID-patched production libraries and the related tests. The new guest_sce_v2_multiprecision, public_headers, guest_math_exact and guest_dinkumware_float_bits pass, along with the other selected CTests.

  • The new test checks:

    • _MP_Get with digits wider than 32 bits;
    • _MP_Add carrying into the last digit, stopping early, the largest addend and wide digits;
    • _MP_Mul with zero, the largest operands and either half of the multiplier zero;
    • _MP_Rem with a zero dividend, a dividend shorter than or below the divisor, a top divisor digit of all ones (no scaling), normalized and small divisors, and three engine steps built with _MP_Mul and _MP_Add.

    Every expected word was taken from the original module, not from this implementation. One-digit divisors are checked against the exact remainder, which a separate 128-bit loop computes.

  • The original functions were loaded from the bundled module with its relocations applied. Its __stack_chk_guard was bound to a local value; every other import would have stopped the run, and none was called.

  • The originals then ran side by side with the patched production library on 15,875,432 calls over two seeds. The inputs were:

    • random and edge digits (0, 1, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff) at every length;
    • digits wider than 32 bits;
    • scalars with zero, all-ones, single-bit and half-zero patterns;
    • dividends built as a * x + c.

    The six words around and in the array agree for every call, except _MP_Rem with a divisor below 2^32 (600,729 calls). The module's own result was not the remainder in 311,656 of those, a count that changes between runs with the addresses involved. Changing only the word before the dividend, or only its address, changed the module's result. On two-digit divisors the remainders agree in all 3,274,703 calls. That includes the 36,912 calls, all with a non-zero fifth digit, where the module's result is not the remainder.

  • Negative control: four lines were altered one at a time:

    • _MP_Add's carry stopped one digit early;
    • _MP_Mul no longer cleared the fifth digit;
    • _MP_Rem skipped a digit whose top equals the divisor's top digit;
    • the final unscaling skipped the lowest digit.

    The new CTest failed on each, and the side-by-side run flagged the altered function each time. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run passed again.

  • Code conventions and git diff --check pass.

  • tools/pr_overlap.py read 1,700 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 36 branches that already conflict with main. Only #13486 adds a line naming these helpers, a table row for the unversioned _ZSt7_MP_* exports, which are other NIDs. Merging each of the 36 with this branch and with main using git merge-tree gives the same conflict blocks, and no block contains a line of this change. The branch also merges cleanly with #15098, #15099, #15210 and #15336.

Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, harness or log is committed or uploaded.

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:5138633013 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15471). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-03T14:08:01Z. --- ### What Export the four `std::__sce_v2` multiprecision helpers from libc. They hold an unsigned value as five 32-bit digits, each stored in a 64-bit word. | Export | NID | Original RVA | | --- | --- | --- | | `_ZNSt8__sce_v27_MP_GetEPy` | `PPRfp98KWh0` | `0x10CB80` | | `_ZNSt8__sce_v27_MP_AddEPyy` | `VXdN8rHxS4s` | `0x10CB90` | | `_ZNSt8__sce_v27_MP_MulEPyyy` | `dNaphWn7TxE` | `0x10CBF0` | | `_ZNSt8__sce_v27_MP_RemEPyy` | `aP5bpilz7aQ` | `0x10CC90` | The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). `VersionedMultiprecision.cpp` follows that module's operations: - `_MP_Get` combines the two low digits. - `_MP_Add` adds a 64-bit value and carries no further than the fifth digit. - `_MP_Mul` writes the four-digit product of two 64-bit values and clears the fifth digit. - `_MP_Rem` uses Knuth's algorithm D: it scales the dividend and divisor, reduces one digit at a time, and unscales. As in the module, it propagates a borrow through every upper digit and adds back into one digit only. Three inputs cannot behave as in the module, and the debt entry records them: - **Divisor below 2^32.** The module's `_MP_Rem` reads the stack word past its one-digit divisor and the word before the dividend. Its result then changes with the contents of those words and with the dividend's address, and in about half of the side-by-side calls it is not the remainder. Here the exact remainder is returned. - **Non-zero fifth dividend digit.** The module also reads the word past the five-word array. Here that word is taken as zero. - **Zero divisor.** The module reads uninitialized stack. Here the dividend is left unchanged. None of the three arises for a non-zero divisor of at least 2^32 and a dividend of at most four digits, such as a 64-bit product plus a 64-bit addend. On those inputs the module's `_MP_Rem` returned the exact remainder in every side-by-side call. ### 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. --> Windows x86-64, WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, main `4f41f4c375f`. Exact tested commit: `c4d4acb9804`. - Built libc, libkernel, their NID-patched production libraries and the related tests. The new `guest_sce_v2_multiprecision`, `public_headers`, `guest_math_exact` and `guest_dinkumware_float_bits` pass, along with the other selected CTests. - The new test checks: - `_MP_Get` with digits wider than 32 bits; - `_MP_Add` carrying into the last digit, stopping early, the largest addend and wide digits; - `_MP_Mul` with zero, the largest operands and either half of the multiplier zero; - `_MP_Rem` with a zero dividend, a dividend shorter than or below the divisor, a top divisor digit of all ones (no scaling), normalized and small divisors, and three engine steps built with `_MP_Mul` and `_MP_Add`. Every expected word was taken from the original module, not from this implementation. One-digit divisors are checked against the exact remainder, which a separate 128-bit loop computes. - The original functions were loaded from the bundled module with its relocations applied. Its `__stack_chk_guard` was bound to a local value; every other import would have stopped the run, and none was called. - The originals then ran side by side with the patched production library on 15,875,432 calls over two seeds. The inputs were: - random and edge digits (0, 1, 0x7fffffff, 0x80000000, 0x80000001, 0xfffffffe, 0xffffffff) at every length; - digits wider than 32 bits; - scalars with zero, all-ones, single-bit and half-zero patterns; - dividends built as `a * x + c`. The six words around and in the array agree for every call, except `_MP_Rem` with a divisor below 2^32 (600,729 calls). The module's own result was not the remainder in 311,656 of those, a count that changes between runs with the addresses involved. Changing only the word before the dividend, or only its address, changed the module's result. On two-digit divisors the remainders agree in all 3,274,703 calls. That includes the 36,912 calls, all with a non-zero fifth digit, where the module's result is not the remainder. - Negative control: four lines were altered one at a time: - `_MP_Add`'s carry stopped one digit early; - `_MP_Mul` no longer cleared the fifth digit; - `_MP_Rem` skipped a digit whose top equals the divisor's top digit; - the final unscaling skipped the lowest digit. The new CTest failed on each, and the side-by-side run flagged the altered function each time. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run passed again. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,700 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 36 branches that already conflict with main. Only #13486 adds a line naming these helpers, a table row for the unversioned `_ZSt7_MP_*` exports, which are other NIDs. Merging each of the 36 with this branch and with main using `git merge-tree` gives the same conflict blocks, and no block contains a line of this change. The branch also merges cleanly with #15098, #15099, #15210 and #15336. Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, harness or log is committed or uploaded. ### 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!15471
No description provided.