feat(libc): export the std::__sce_v2 binary scaling helpers #15099

Merged
Danigamer05 merged 1 commit from upstream-pr/15099 into main 2026-11-03 08:10:54 +00:00
Danigamer05 commented 2026-11-02 16:10:28 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-02T16:10:28Z.


What

Export six std::__sce_v2 Dinkumware binary scaling helpers from libc:

Export NID Original RVA
_ZNSt8__sce_v27_DscaleEPdl gRj52WoTf5Q 0xCB330
_ZNSt8__sce_v28_FDscaleEPfl wYwj23xDzPQ 0xCF0F0
_ZNSt8__sce_v28_LDscaleEPel +AqTGiV4vBA 0x10D2D0
_ZNSt8__sce_v29_DunscaleEPsPd 1Vlo0OcN+sk 0xCA210
_ZNSt8__sce_v210_FDunscaleEPsPf r8xEVOsN728 0xCA0C0
_ZNSt8__sce_v210_LDunscaleEPsPe iAKatJKy6QY 0xCA200

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

That module computes these helpers on the exponent and fraction bits:

  • NaN and infinity return their codes unchanged.
  • Subnormal inputs are normalized first.
  • _Dscale rounds a result that becomes subnormal to nearest even, returns an infinity on overflow and a signed zero on underflow.
  • _Dunscale returns the significand in [1/2, 1) and its exponent.
  • None of them sets errno or a floating-point exception flag.

The new ScaleFieldBits and UnscaleFieldBits in MathExact.cpp do the same with integer operations. The unversioned _Dscale and _Dunscale already here give the same values and codes for these inputs. They use host ldexp and frexp, which raise the denormal, underflow, overflow, inexact or invalid flags that the original never raises, so the versioned exports do not forward to them. As in the original, whose _LDscale and _LDunscale call the double forms, the __sce_v2 long double forms take an 8-byte double.

Tested

Windows x86-64, WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 5e85609d0b0152d14ae34d708d3237005ed311ca. Exact tested commit: 1c5093f845ed7ec88cd5533cb14c86fdc8cf182f.

The change was first tested on main 52e9ae6d57f37200c6b27234efb226b45d754528 at ef367efd7b803f96dfa7e8be523480663de73e0b, including the negative control below, then rebased onto the main above without conflicts; the diff of the change is the same apart from context lines. All four selected CTests and all 35,144,604 side-by-side vectors were run again at the tested commit and pass.

  • Built libc, libkernel, their NID-patched production libraries and the guest_sce_v2_binary_scaling, guest_math_exact and guest_dinkumware_float_bits tests. All three pass, as does public_headers.

  • The new test checks 112 double and 96 float scaling cases and 42 unscaling cases, the long double forms included. Inputs are zeros, subnormals, extremes, NaN and infinity, and exponents include the int64 limits. Every expected code, exponent and output was taken from the original module, not from this implementation.

  • The original functions were loaded from the bundled module with its relocations applied. Its __error import was bound to this libc and __stack_chk_guard to a local value; every other import would have stopped the run, and none was called. MXCSR was reset to 0x1F80 and the x87 unit reinitialized before every call. The originals then ran side by side with the patched production library, called by NID, on 35,144,604 vectors:

    • special values, every binary exponent from -1100 to 1100 and random bit patterns;
    • exponents from the int64 minimum to its maximum;
    • 720,384 constructed cases at each rounding position, with results that tie, fall just below or above a tie, or carry into the smallest normal.

    Return code, output bits, exponent, errno, the full MXCSR and the x87 control and status words agree for all six exports.

  • Negative control, at the pre-rebase commit: two lines were temporarily altered. Rounding ties were taken upward, and the unscaled exponent was shifted by one. The new CTest failed, and the side-by-side run flagged all six exports and both tie sets. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and all four tests and all 35,144,604 vectors passed again.

  • Code conventions and git diff --check pass.

  • tools/pr_overlap.py read 1,748 open PRs at the tested commit and reports no confirmed overlap. An earlier placement of the CTest registration conflicted with #12887; it was moved before the rebase. It keeps exit 2 for 58 branches that already conflict with main (#5021, #5303, #6819, #7081, #7386, #7447, #7715, #8150, #8965, #9087, #9340, #9371, #9447, #9526, #9929, #10113, #10226, #10245, #10250, #10253, #10265, #10274, #10325, #10328, #10345, #10349, #10405, #10506, #10510, #10512, #10565, #10987, #11260, #11262, #11266, #11332, #11632, #11634, #11724, #11726, #11831, #11907, #12122, #12397, #12514, #12560, #12574, #12593, #12622, #12627, #12682, #12688, #12700, #12965, #13119, #13390, #13804, #14212). None of their added lines against their merge base contains the six names or NIDs. Merging each with this branch and with main using git merge-tree gives the same conflict blocks, and no conflict block contains this change.

Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, oracle 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:5123461360 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15099). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-02T16:10:28Z. --- ### What Export six `std::__sce_v2` Dinkumware binary scaling helpers from libc: | Export | NID | Original RVA | | --- | --- | --- | | `_ZNSt8__sce_v27_DscaleEPdl` | `gRj52WoTf5Q` | `0xCB330` | | `_ZNSt8__sce_v28_FDscaleEPfl` | `wYwj23xDzPQ` | `0xCF0F0` | | `_ZNSt8__sce_v28_LDscaleEPel` | `+AqTGiV4vBA` | `0x10D2D0` | | `_ZNSt8__sce_v29_DunscaleEPsPd` | `1Vlo0OcN+sk` | `0xCA210` | | `_ZNSt8__sce_v210_FDunscaleEPsPf` | `r8xEVOsN728` | `0xCA0C0` | | `_ZNSt8__sce_v210_LDunscaleEPsPe` | `iAKatJKy6QY` | `0xCA200` | The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). That module computes these helpers on the exponent and fraction bits: - NaN and infinity return their codes unchanged. - Subnormal inputs are normalized first. - `_Dscale` rounds a result that becomes subnormal to nearest even, returns an infinity on overflow and a signed zero on underflow. - `_Dunscale` returns the significand in [1/2, 1) and its exponent. - None of them sets `errno` or a floating-point exception flag. The new `ScaleFieldBits` and `UnscaleFieldBits` in `MathExact.cpp` do the same with integer operations. The unversioned `_Dscale` and `_Dunscale` already here give the same values and codes for these inputs. They use host `ldexp` and `frexp`, which raise the denormal, underflow, overflow, inexact or invalid flags that the original never raises, so the versioned exports do not forward to them. As in the original, whose `_LDscale` and `_LDunscale` call the `double` forms, the `__sce_v2` `long double` forms take an 8-byte `double`. ### 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 `5e85609d0b0152d14ae34d708d3237005ed311ca`. Exact tested commit: `1c5093f845ed7ec88cd5533cb14c86fdc8cf182f`. The change was first tested on main `52e9ae6d57f37200c6b27234efb226b45d754528` at `ef367efd7b803f96dfa7e8be523480663de73e0b`, including the negative control below, then rebased onto the main above without conflicts; the diff of the change is the same apart from context lines. All four selected CTests and all 35,144,604 side-by-side vectors were run again at the tested commit and pass. - Built libc, libkernel, their NID-patched production libraries and the `guest_sce_v2_binary_scaling`, `guest_math_exact` and `guest_dinkumware_float_bits` tests. All three pass, as does `public_headers`. - The new test checks 112 double and 96 float scaling cases and 42 unscaling cases, the `long double` forms included. Inputs are zeros, subnormals, extremes, NaN and infinity, and exponents include the int64 limits. Every expected code, exponent and output was taken from the original module, not from this implementation. - The original functions were loaded from the bundled module with its relocations applied. Its `__error` import was bound to this libc and `__stack_chk_guard` to a local value; every other import would have stopped the run, and none was called. MXCSR was reset to `0x1F80` and the x87 unit reinitialized before every call. The originals then ran side by side with the patched production library, called by NID, on 35,144,604 vectors: - special values, every binary exponent from -1100 to 1100 and random bit patterns; - exponents from the int64 minimum to its maximum; - 720,384 constructed cases at each rounding position, with results that tie, fall just below or above a tie, or carry into the smallest normal. Return code, output bits, exponent, `errno`, the full MXCSR and the x87 control and status words agree for all six exports. - Negative control, at the pre-rebase commit: two lines were temporarily altered. Rounding ties were taken upward, and the unscaled exponent was shifted by one. The new CTest failed, and the side-by-side run flagged all six exports and both tie sets. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and all four tests and all 35,144,604 vectors passed again. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,748 open PRs at the tested commit and reports no confirmed overlap. An earlier placement of the CTest registration conflicted with #12887; it was moved before the rebase. It keeps exit 2 for 58 branches that already conflict with main (#5021, #5303, #6819, #7081, #7386, #7447, #7715, #8150, #8965, #9087, #9340, #9371, #9447, #9526, #9929, #10113, #10226, #10245, #10250, #10253, #10265, #10274, #10325, #10328, #10345, #10349, #10405, #10506, #10510, #10512, #10565, #10987, #11260, #11262, #11266, #11332, #11632, #11634, #11724, #11726, #11831, #11907, #12122, #12397, #12514, #12560, #12574, #12593, #12622, #12627, #12682, #12688, #12700, #12965, #13119, #13390, #13804, #14212). None of their added lines against their merge base contains the six names or NIDs. Merging each with this branch and with main using `git merge-tree` gives the same conflict blocks, and no conflict block contains this change. Not tested: Linux, macOS, another SDK, a game, GPU or console. No game compatibility claim. No original module, extracted bytes, oracle 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)
BunnyFl commented 2026-11-02 16:43:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by BunnyFl on 2026-11-02T16:43:20Z.

Review: APPROVED


Cross-checked against firmware 13.42's system libSceLibcInternal.sprx (sha256 a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285) as well as the title's bundled libc.prx. Read from disassembly only, not measured on a console:

  • All six NIDs are exported there.
  • _LDscale (0x11e100) is call 0xdc480, the versioned _Dscale.
  • _LDunscale (0xdb350) is call 0xdb360, the versioned _Dunscale.

So the 8-byte long double forwarding matches the system module too. I didn't re-run the flag and errno comparison against the system module.

Build and test on Windows (WinLibs GCC, BUILD_TESTING=ON), PR merged into current main: guest_sce_v2_binary_scaling and guest_math_exact pass. On main the exports don't exist, and the test doesn't link.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5739988348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15099#pullrequestreview-5739988348). Originally posted by [BunnyFl](https://github.com/BunnyFl) on 2026-11-02T16:43:20Z. **Review:** APPROVED --- Cross-checked against firmware 13.42's system `libSceLibcInternal.sprx` (sha256 `a01795ab49df639dd40abd6194956ada062244826824ce486c9acab227bf5285`) as well as the title's bundled `libc.prx`. Read from disassembly only, not measured on a console: - All six NIDs are exported there. - `_LDscale` (`0x11e100`) is `call 0xdc480`, the versioned `_Dscale`. - `_LDunscale` (`0xdb350`) is `call 0xdb360`, the versioned `_Dunscale`. So the 8-byte `long double` forwarding matches the system module too. I didn't re-run the flag and `errno` comparison against the system module. Build and test on Windows (WinLibs GCC, `BUILD_TESTING=ON`), PR merged into current `main`: `guest_sce_v2_binary_scaling` and `guest_math_exact` pass. On `main` the exports don't exist, and the test doesn't link.
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!15099
No description provided.