feat(libSceLibcInternal): implement std::_MP_Add, _MP_Get, _MP_Mul and _MP_Rem #14936

Merged
PanagiotisKaraliolios merged 3 commits from upstream-pr/14936 into main 2026-11-05 23:45:18 +00:00
PanagiotisKaraliolios commented 2026-11-02 01:53:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PanagiotisKaraliolios on 2026-11-02T01:53:32Z.


What

Implements four pending libSceLibcInternal exports, the Dinkumware multiprecision helpers that std::linear_congruential_engine uses when a * x + c does not fit in 64 bits:

Export Mangled name NID
std::_MP_Get(unsigned long long*) _ZSt7_MP_GetPy lrQSsTRMMr4
std::_MP_Add(unsigned long long*, unsigned long long) _ZSt7_MP_AddPyy QfPuSqhub7o
std::_MP_Mul(unsigned long long*, unsigned long long, unsigned long long) _ZSt7_MP_MulPyyy Gav1Xt1Ce+c
std::_MP_Rem(unsigned long long*, unsigned long long) _ZSt7_MP_RemPyy Ozk+Z6QnlTY
  • Parameters come from the mangled names. Return types and behaviour are derived from stl/src/multprec.cpp of the first microsoft/STL release (Apache-2.0 WITH LLVM-exception). Its notice is kept at the top of MultiPrecision.cpp. That file is Dinkumware's code, with the same algorithm as V6.00.
  • A value is five 64-bit words of base 2^32 digits. _MP_Mul is Knuth's Algorithm M and _MP_Rem is Algorithm D. They give that code's results for words wider than a digit, a carry into the fifth word and a borrow propagated past the top digit.
  • The 2025 MSVC rewrite differs in those cases. It postdates every firmware, so the 2019 code is followed.
  • The exports throw std::invalid_argument only where that code reads outside its arrays:
    • a null array, for every export;
    • for _MP_Rem, a zero divisor or value, a nonzero fifth word, or a nonzero quotient digit with a divisor below 2^32 (the code reads v[-1] there).
  • Small-divisor inputs that the code defines, such as a digit below the divisor, give its result.
  • Firmware 13.42's libSceLibcInternal.sprx exports all four NIDs (review).
  • The exports are literal NID aliases of _nid_no_patch functions, as in AlignedNew.cpp. The declarations are in a new include/MultiPrecision.hpp, included from libSceLibcInternal.hpp like MathInternals.hpp.
  • Inventory stubs 247–250 are removed together with their declarations and table rows. The inventory entry now counts 288 entries, 282 of them in the 3.20 catalogue. Main says 295/289, but InventoryExports.cpp and the table hold 292 there, because #14659 removed three 3.20 entries without changing the count.
  • tools/progress.py --module libSceLibcInternal: 95/1140 on main 9ab6a281ea, 99/1140 with this PR.
  • Not verified on the console: the module's code was not run and no title was seen calling these functions. This is recorded under Functional in docs/dev/debt/libSceLibcInternal.md.

Tested

Debian 13 on WSL2, GCC 14.2, full Release build with BUILD_TESTING=ON.

New test. ctest --test-dir build -R guest_libc_internal_multi_precision -V passed in 0.01 s. It checks:

  • _MP_Mul against a schoolbook product, for 49 operand pairs;
  • _MP_Add carries, including into and out of the fifth word, and _MP_Get;
  • 30,000 linear congruential steps (Mul, Add, Rem, Get) for three moduli, against a shift-and-add mulmod;
  • fixed remainders, including two corrections of the trial quotient and one-digit divisors;
  • V6.00's results for words wider than a digit;
  • the rejected inputs, which must leave the value unchanged.

Related tests. The 79 tests matching libc_internal|public_headers|...|progress_report|test_registration passed.

Full suite. ctest -j 16 passed 3146 of 3154 tests. The 8 failures are AGC driver and shader tests, and none comes from this change:

  • agc_driver_division, agc_driver_f32_constant_factor_tininess, agc_driver_f32_denormal_flush, agc_driver_mullit_f32_denormal_modes and vector_constant_factor_tininess fail the same way on main 93dbd0f82 in the same build tree.
  • agc_driver_pipeline_journal, agc_driver_runtime_formatted_store and agc_driver_runtime_formatted_store_x2 timed out under -j 16. They pass when rerun serially, on main and on this branch.

Differential checks (outside the repository, all built with -fsanitize=address,undefined):

  • 4,000,000 random _MP_Rem inputs with divisors of at least 2^32, plus random _MP_Mul, _MP_Add and _MP_Get inputs, against V6.00 multprec.cpp compiled from the crt.vc12 source: no differences.
  • 3,000,000 _MP_Rem inputs, mostly with divisors below 2^32, classified by a bounds-checked copy of V6.00:
    • the 939,312 inputs V6.00 defines match V6.00, with no sanitizer report;
    • the 2,060,688 inputs where V6.00 reads outside its arrays throw and leave the value unchanged.
  • 300,000 vectors from a Python port of V6.00: no differences.

Repository tools.

  • python3 tools/check_conventions.py --base upstream/main and core/libs/tests/test_public_headers.py --audit-only passed.
  • python3 tools/pr_overlap.py --branch feat/libc-internal-multiprecision found no shared exports or new files.
    • It found only line-level overlaps: the end of CMakeLists.txt, the inventory count line and the Functional section of the debt file. They are with #10953, #13778, #13782, #14373, #14401, #14659, #14834 and #14864.
    • It could not compare #5021 and #14212. I checked them by hand, and neither touches these exports.

Not tested: Windows (MinGW) and macOS.

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:5113745261 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14936). Originally posted by [PanagiotisKaraliolios](https://github.com/PanagiotisKaraliolios) on 2026-11-02T01:53:32Z. --- ### What Implements four pending `libSceLibcInternal` exports, the Dinkumware multiprecision helpers that `std::linear_congruential_engine` uses when `a * x + c` does not fit in 64 bits: | Export | Mangled name | NID | | --- | --- | --- | | `std::_MP_Get(unsigned long long*)` | `_ZSt7_MP_GetPy` | `lrQSsTRMMr4` | | `std::_MP_Add(unsigned long long*, unsigned long long)` | `_ZSt7_MP_AddPyy` | `QfPuSqhub7o` | | `std::_MP_Mul(unsigned long long*, unsigned long long, unsigned long long)` | `_ZSt7_MP_MulPyyy` | `Gav1Xt1Ce+c` | | `std::_MP_Rem(unsigned long long*, unsigned long long)` | `_ZSt7_MP_RemPyy` | `Ozk+Z6QnlTY` | - Parameters come from the mangled names. Return types and behaviour are derived from [`stl/src/multprec.cpp`](https://github.com/microsoft/STL/blob/219514876ea86491de191ceaa88632616bbc0f19/stl/src/multprec.cpp) of the first microsoft/STL release (Apache-2.0 WITH LLVM-exception). Its notice is kept at the top of `MultiPrecision.cpp`. That file is Dinkumware's code, with the same algorithm as V6.00. - A value is five 64-bit words of base 2^32 digits. `_MP_Mul` is Knuth's Algorithm M and `_MP_Rem` is Algorithm D. They give that code's results for words wider than a digit, a carry into the fifth word and a borrow propagated past the top digit. - The [2025 MSVC rewrite](https://github.com/microsoft/STL/blob/68cbe34adf40b6db567c86f1b50c2e45e3c67944/stl/src/multprec.cpp) differs in those cases. It postdates every firmware, so the 2019 code is followed. - The exports throw `std::invalid_argument` only where that code reads outside its arrays: - a null array, for every export; - for `_MP_Rem`, a zero divisor or value, a nonzero fifth word, or a nonzero quotient digit with a divisor below 2^32 (the code reads `v[-1]` there). - Small-divisor inputs that the code defines, such as a digit below the divisor, give its result. - Firmware 13.42's `libSceLibcInternal.sprx` exports all four NIDs ([review](https://github.com/boykopovar/AnyPS5/pull/14936#pullrequestreview-5731406143)). - The exports are literal NID aliases of `_nid_no_patch` functions, as in `AlignedNew.cpp`. The declarations are in a new `include/MultiPrecision.hpp`, included from `libSceLibcInternal.hpp` like `MathInternals.hpp`. - Inventory stubs 247–250 are removed together with their declarations and table rows. The inventory entry now counts 288 entries, 282 of them in the 3.20 catalogue. Main says 295/289, but `InventoryExports.cpp` and the table hold 292 there, because #14659 removed three 3.20 entries without changing the count. - `tools/progress.py --module libSceLibcInternal`: 95/1140 on main 9ab6a281ea, 99/1140 with this PR. - Not verified on the console: the module's code was not run and no title was seen calling these functions. This is recorded under Functional in `docs/dev/debt/libSceLibcInternal.md`. ### 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. --> Debian 13 on WSL2, GCC 14.2, full Release build with `BUILD_TESTING=ON`. **New test.** `ctest --test-dir build -R guest_libc_internal_multi_precision -V` passed in 0.01 s. It checks: - `_MP_Mul` against a schoolbook product, for 49 operand pairs; - `_MP_Add` carries, including into and out of the fifth word, and `_MP_Get`; - 30,000 linear congruential steps (`Mul`, `Add`, `Rem`, `Get`) for three moduli, against a shift-and-add `mulmod`; - fixed remainders, including two corrections of the trial quotient and one-digit divisors; - V6.00's results for words wider than a digit; - the rejected inputs, which must leave the value unchanged. **Related tests.** The 79 tests matching `libc_internal|public_headers|...|progress_report|test_registration` passed. **Full suite.** `ctest -j 16` passed 3146 of 3154 tests. The 8 failures are AGC driver and shader tests, and none comes from this change: - `agc_driver_division`, `agc_driver_f32_constant_factor_tininess`, `agc_driver_f32_denormal_flush`, `agc_driver_mullit_f32_denormal_modes` and `vector_constant_factor_tininess` fail the same way on main 93dbd0f82 in the same build tree. - `agc_driver_pipeline_journal`, `agc_driver_runtime_formatted_store` and `agc_driver_runtime_formatted_store_x2` timed out under `-j 16`. They pass when rerun serially, on main and on this branch. **Differential checks** (outside the repository, all built with `-fsanitize=address,undefined`): - 4,000,000 random `_MP_Rem` inputs with divisors of at least 2^32, plus random `_MP_Mul`, `_MP_Add` and `_MP_Get` inputs, against V6.00 `multprec.cpp` compiled from the crt.vc12 source: no differences. - 3,000,000 `_MP_Rem` inputs, mostly with divisors below 2^32, classified by a bounds-checked copy of V6.00: - the 939,312 inputs V6.00 defines match V6.00, with no sanitizer report; - the 2,060,688 inputs where V6.00 reads outside its arrays throw and leave the value unchanged. - 300,000 vectors from a Python port of V6.00: no differences. **Repository tools.** - `python3 tools/check_conventions.py --base upstream/main` and `core/libs/tests/test_public_headers.py --audit-only` passed. - `python3 tools/pr_overlap.py --branch feat/libc-internal-multiprecision` found no shared exports or new files. - It found only line-level overlaps: the end of `CMakeLists.txt`, the inventory count line and the Functional section of the debt file. They are with #10953, #13778, #13782, #14373, #14401, #14659, #14834 and #14864. - It could not compare #5021 and #14212. I checked them by hand, and neither touches these exports. **Not tested:** Windows (MinGW) and macOS. ### 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)
BunnyFl commented 2026-11-02 02:06:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T02:06:15Z.

Review: APPROVED


Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 1dd458e616 (already on top of upstream/main 93dbd0f82a):

  • guest_libc_internal_multi_precision passes.
  • Mutation checks in MultiPrecision.cpp: making the carry propagation loop in the add path never run (carry != 0 && → false &&) fails the test, and so does dropping the borrow in the division step (borrow = TakeBorrow(...) → borrow = 0). So the carry-into-the-fifth-word and propagated-borrow cases you describe are covered.
  • All four mangled names hash to the NIDs in your table, and all four are defined entries in the dynamic symbol table of firmware 13.42's libSceLibcInternal.sprx (objdump -T; behaviour not run).

Following V6.00 rather than the 2025 MSVC rewrite, with the difference recorded, matches the firmware's age.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5731406143 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14936#pullrequestreview-5731406143). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T02:06:15Z. **Review:** APPROVED --- Windows 11, WinLibs GCC 15.2.0 r7, Release. Head 1dd458e616 (already on top of upstream/main 93dbd0f82a): - `guest_libc_internal_multi_precision` passes. - Mutation checks in `MultiPrecision.cpp`: making the carry propagation loop in the add path never run (`carry != 0 &&` → `false &&`) fails the test, and so does dropping the borrow in the division step (`borrow = TakeBorrow(...)` → `borrow = 0`). So the carry-into-the-fifth-word and propagated-borrow cases you describe are covered. - All four mangled names hash to the NIDs in your table, and all four are defined entries in the dynamic symbol table of firmware 13.42's `libSceLibcInternal.sprx` (`objdump -T`; behaviour not run). Following V6.00 rather than the 2025 MSVC rewrite, with the difference recorded, matches the firmware's age.
BunnyFl commented 2026-11-04 19:08:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-04T19:08:47Z.

Review: APPROVED


@PanagiotisKaraliolios approved again at 761dc38931, thanks for the rebase.

The rebase: git range-diff against 9f212960fe changes none of the implementation or test lines. The debt entry is unchanged too. It now follows main's new inet_netof entry, as you wrote. The provenance header and the microsoft/STL source of record are as I checked them above.

What I ran: head 761dc38931, which already contains main 9ab6a281ea. Windows 11, WinLibs GCC 15.2.0, Release:

  • guest_libc_internal_multi_precision and public_headers pass (2 of 2).
  • Mutation: with _MP_Get returning only value[0] (core/libs/prx/libSceLibcInternal/MultiPrecision.cpp:112), guest_libc_internal_multi_precision aborts (0xc0000409). So the tests read the high digit.
  • tools/check_conventions.py --base upstream/main --head on the head: all checks passed.
  • pr_overlap.py --branch on the head:
    • Line conflicts with #13881 (debt, line 348), #14945 and #15581 (CMakeLists.txt line 329), and #15584 and #15588 (the debt count on line 9). Whichever lands later needs a rebase.
    • It lists #13486 under the same exports. That branch has no MultiPrecision.cpp and none of this PR's commits; it only declares the same four NIDs as old inventory stubs (libSceLibcInternalInventory_247 to _250), the ones this PR removes.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5774188249 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14936#pullrequestreview-5774188249). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-04T19:08:47Z. **Review:** APPROVED --- @PanagiotisKaraliolios approved again at `761dc38931`, thanks for the rebase. **The rebase:** `git range-diff` against `9f212960fe` changes none of the implementation or test lines. The debt entry is unchanged too. It now follows `main`'s new `inet_netof` entry, as you wrote. The provenance header and the `microsoft/STL` source of record are as I checked them above. **What I ran:** head `761dc38931`, which already contains `main` `9ab6a281ea`. Windows 11, WinLibs GCC 15.2.0, Release: - `guest_libc_internal_multi_precision` and `public_headers` pass (2 of 2). - **Mutation:** with `_MP_Get` returning only `value[0]` (`core/libs/prx/libSceLibcInternal/MultiPrecision.cpp:112`), `guest_libc_internal_multi_precision` aborts (`0xc0000409`). So the tests read the high digit. - `tools/check_conventions.py --base upstream/main --head` on the head: all checks passed. - `pr_overlap.py --branch` on the head: - Line conflicts with #13881 (debt, line 348), #14945 and #15581 (`CMakeLists.txt` line 329), and #15584 and #15588 (the debt count on line 9). Whichever lands later needs a rebase. - It lists #13486 under the same exports. That branch has no `MultiPrecision.cpp` and none of this PR's commits; it only declares the same four NIDs as old inventory stubs (`libSceLibcInternalInventory_247` to `_250`), the ones this PR removes.
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!14936
No description provided.