feat(libc): export the std::__sce_v2 _Exp and _FExp helpers #15501

Merged
Danigamer05 merged 1 commit from upstream-pr/15501 into main 2026-11-07 13:17:45 +00:00
Danigamer05 commented 2026-11-03 16:06:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Danigamer05 on 2026-11-03T16:06:55Z.


What

Export the versioned std::__sce_v2 _Exp and _FExp from libc.

Export NID Original RVA
_ZNSt8__sce_v24_ExpEPdds 1UlKEANeY44 0x10B5C0
_ZNSt8__sce_v25_FExpEPffs Hypw5kMsuRs 0x10B8A0

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

Both store y · e^x · 2^scale through the pointer and return the versioned _Dscale/_FDscale code. They are not the unversioned _Exp/_FExp. Run side by side, the unversioned exports in main differ from the module's versioned ones in about 60% of calls: they set errno on overflow and underflow, which the versioned module code never does, and they return other bits. So VersionedExp follows the module's versioned code:

  • a zero y is stored as is and returns 0;
  • arguments below -1842 (-229 for float) or above 1842 (229) return 0 or 1 with the module's underflow or overflow value, built as copysign(min, y) · min or copysign(max, y) · max;
  • otherwise the argument is reduced by n·ln 2, with n rounded half away from zero, truncated to 32 bits and then to 16 bits, and two fused steps of a split ln 2;
  • a reduced argument within 2^-54 (2^-25) of zero keeps y; any other uses the module's rational approximation of 2·e^x, with one fused step per coefficient where the module fuses, and n - 1;
  • the result is stored and scaled by the versioned _Dscale/_FDscale. Codes 0 and 1 are replaced with the underflow and overflow values above; other codes are returned.

The constants live in a per-type struct of constexpr values. MathExact.cpp is built with -frounding-math, so initializing a float from a double literal would happen at run time and raise the inexact flag, which the module does not do. When both y and the ratio are NaNs, the module's multiply returns y's NaN; ExpProduct keeps that order, since the compiler may swap the operands of a product.

Tested

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

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

  • The new test checks 25 rows per type, with the stored bits, the returned code and an unchanged errno: 0, ±1, 1/2, 10, a factor of 3 and -2, zero and negative zero factors, arguments too small for the reduction, positive and negative scales, a large argument, overflowing, underflowing and subnormal scaled results, arguments just past and at the limits, ±infinity, and NaN arguments and factors. Every expected value was taken from the original module, not from this implementation.

  • The original functions were loaded from the bundled module with its relocations applied. Their imports were bound by NID to this branch's libc and libkernel, with __stack_chk_guard pointing at a local value; the module's own copysign and versioned _Dscale/_FDscale ran from the module.

  • Side by side with the patched production library, the stored value, the returned code, errno, MXCSR and the x87 control and status words agree in all 10,000,000 calls over two seeds. The inputs mixed special values, random bit patterns, arguments around the limits and across the whole exponent range, factors of 1 and random factors, and scales of 0, small, large and random 16-bit values.

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

    • the second reduction step was removed;
    • the exponent was no longer decremented after the approximation;
    • n was rounded with +0.5 for negative arguments too;
    • one float coefficient was shortened.

    The new CTest failed on each, and the side-by-side run reported differences for each altered type. 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,724 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 36 branches that already conflict with main. None of them adds these exports; #10780 only has a test named CheckLongDoubleExpOverflowRounding. Merging each of the 36 with this branch and with main using git merge-tree gives the same conflict blocks. The branch also merges cleanly with #15336, #15471, #15484 and #15490.

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:5140499301 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15501). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-03T16:06:55Z. --- ### What Export the versioned `std::__sce_v2` `_Exp` and `_FExp` from libc. | Export | NID | Original RVA | | --- | --- | --- | | `_ZNSt8__sce_v24_ExpEPdds` | `1UlKEANeY44` | `0x10B5C0` | | `_ZNSt8__sce_v25_FExpEPffs` | `Hypw5kMsuRs` | `0x10B8A0` | The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). Both store `y · e^x · 2^scale` through the pointer and return the versioned `_Dscale`/`_FDscale` code. They are not the unversioned `_Exp`/`_FExp`. Run side by side, the unversioned exports in main differ from the module's versioned ones in about 60% of calls: they set `errno` on overflow and underflow, which the versioned module code never does, and they return other bits. So `VersionedExp` follows the module's versioned code: - a zero `y` is stored as is and returns 0; - arguments below -1842 (-229 for `float`) or above 1842 (229) return 0 or 1 with the module's underflow or overflow value, built as `copysign(min, y) · min` or `copysign(max, y) · max`; - otherwise the argument is reduced by n·ln 2, with n rounded half away from zero, truncated to 32 bits and then to 16 bits, and two fused steps of a split ln 2; - a reduced argument within 2^-54 (2^-25) of zero keeps `y`; any other uses the module's rational approximation of 2·e^x, with one fused step per coefficient where the module fuses, and n - 1; - the result is stored and scaled by the versioned `_Dscale`/`_FDscale`. Codes 0 and 1 are replaced with the underflow and overflow values above; other codes are returned. The constants live in a per-type struct of `constexpr` values. `MathExact.cpp` is built with `-frounding-math`, so initializing a `float` from a `double` literal would happen at run time and raise the inexact flag, which the module does not do. When both `y` and the ratio are NaNs, the module's multiply returns `y`'s NaN; `ExpProduct` keeps that order, since the compiler may swap the operands of a product. ### 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: `694cf319cf3`. - Built libc, libkernel, their NID-patched production libraries and the related tests. The new `guest_sce_v2_exp`, `public_headers`, `guest_math_exact` and `guest_dinkumware_float_bits` pass, along with the other `guest_sce_v2_*` CTests. - The new test checks 25 rows per type, with the stored bits, the returned code and an unchanged `errno`: 0, ±1, 1/2, 10, a factor of 3 and -2, zero and negative zero factors, arguments too small for the reduction, positive and negative scales, a large argument, overflowing, underflowing and subnormal scaled results, arguments just past and at the limits, ±infinity, and NaN arguments and factors. Every expected value was taken from the original module, not from this implementation. - The original functions were loaded from the bundled module with its relocations applied. Their imports were bound by NID to this branch's libc and libkernel, with `__stack_chk_guard` pointing at a local value; the module's own `copysign` and versioned `_Dscale`/`_FDscale` ran from the module. - Side by side with the patched production library, the stored value, the returned code, `errno`, MXCSR and the x87 control and status words agree in all 10,000,000 calls over two seeds. The inputs mixed special values, random bit patterns, arguments around the limits and across the whole exponent range, factors of 1 and random factors, and scales of 0, small, large and random 16-bit values. - Negative control: four lines were altered one at a time: - the second reduction step was removed; - the exponent was no longer decremented after the approximation; - n was rounded with +0.5 for negative arguments too; - one `float` coefficient was shortened. The new CTest failed on each, and the side-by-side run reported differences for each altered type. 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,724 open PRs at the tested commit and reports no overlap. It keeps exit 2 for 36 branches that already conflict with main. None of them adds these exports; #10780 only has a test named `CheckLongDoubleExpOverflowRounding`. Merging each of the 36 with this branch and with main using `git merge-tree` gives the same conflict blocks. The branch also merges cleanly with #15336, #15471, #15484 and #15490. 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!15501
No description provided.