feat(libc): export the std::__sce_v2 _Poly and _XLgamma helpers #15484

Merged
Danigamer05 merged 1 commit from upstream-pr/15484 into main 2026-11-08 02:06:31 +00:00
Danigamer05 commented 2026-11-03 14:47:15 +00:00 (Migrated from github.com)

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


What

Export the versioned std::__sce_v2 _Poly and the three _XLgamma overloads from libc.

Export NID Original RVA
_ZNSt8__sce_v25_PolyEdPKdi QKveql1SwSo 0x10BA60
_ZNSt8__sce_v28_XLgammaEf BEK4zTmR87w 0x10D2E0
_ZNSt8__sce_v28_XLgammaEd e7wxmyU9QtE 0x10D3D0
_ZNSt8__sce_v28_XLgammaEe HX-r2mZEP1A 0x10D4C0

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

The new code sits in MathExact.cpp, beside the other __sce_v2 math helpers:

  • _Poly evaluates the table with one fused multiply-add per coefficient, as the module's vfmadd213sd loop does. When an operand is a NaN, the module returns the quieted NaN of x first, then of the running value, then of the coefficient. The host fma picks another operand, so OperandOrderFma keeps the host result and only chooses which NaN to return. NaNs are tested on their bits, because a floating-point compare would raise the denormal flag the module does not raise.
  • _XLgamma is the module's six-term Lanczos series. It shifts by 5.5, sums the six coefficients over x + 1 to x + 6, scales by √(2π) / x and subtracts (x + 5.5) - (x + 0.5) log(x + 5.5). The constants, operation order and precision follow each overload:
    • float uses the float constants and starts the sum at 1;
    • double starts the sum at 1.000000000190015 and fuses the last multiply-subtract;
    • long double uses the double constants in x87 extended precision and does not fuse the last step, as the module does.

One known difference remains, and the debt entry records it. Each overload calls this libc's log, logf and logl, as the module calls its own. Those are host functions, not the module's. They return other last bits for some arguments and do not raise the module's x87 invalid flag on negative arguments, so _XLgamma inherits that difference. Matching them is a separate change to log itself.

Tested

Windows x86-64 (AMD Zen 2), WinLibs GCC 15.2.0, Release, BUILD_TESTING=ON, main 4f41f4c375f. Exact tested commit: 5609e53b0e6.

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

  • The new test checks:

    • each _XLgamma overload at 1, 2, 0.5, 3, 10, 0.25, 1.5, 100, 1e10, 1e300, 2^-20, ±0, -1, -2.5, 1e-300, the smallest subnormal, ±infinity, NaN, 7.25, 33 and 170.5, with the result bits and errno (EDOM for negative and -0 arguments, ERANGE for infinities);
    • _Poly with degrees -1 to 7 over a table holding a huge, a subnormal and ordinary coefficients;
    • _Poly NaN order, with quiet and signaling NaNs in x, in the first coefficient and in later coefficients, and with an infinite first coefficient times zero.

    Every expected value was taken from the original module, not from this implementation. Rows where this libc's log differs from the module's are left out of the _XLgamma tables: 7.25 for double, 1 and 1e10 for long double.

  • The original functions were loaded from the bundled module with its relocations applied. Its imports were bound by NID to this branch's libc and libkernel, with __stack_chk_guard pointing at a local value. The few imports with no provider would have stopped the run, and none was called.

  • Side by side with the patched production library, comparing the result bits, errno, MXCSR and the x87 control and status words:

    • _Poly: identical in all 2,000,000 calls over two seeds, with random and special x, degrees -2 to 14 and tables of random, scaled-down, special and zero coefficients.
    • _XLgamma: values and errno agree in 98.3% (double), 84% (long double) and 99.95% (float) of 1,000,000 calls each. The rest come from log: mostly the last bits, and for unnormal 80-bit arguments this libc's logl returns a NaN where the module's returns a number. The flags also differ on negative arguments, for the reason given above.
    • To check that the remaining differences come only from log, the same formula was run in the test program with the module's own log, logf and logl. It matched the module's _XLgamma in every one of 6,000,000 calls over two seeds, flags included. Over those arguments, this libc's log, logf and logl alone already differ from the module's in about 28%, 26% and 43% of calls once flags are counted.
  • Negative control: five lines were altered one at a time:

    • _Poly reused a coefficient;
    • _Poly preferred the running value's NaN over x's;
    • the Lanczos sum started at x + 2;
    • the last step was not fused;
    • the long double constants were written as long double literals.

    The new CTest failed on each, and the side-by-side run reported more differences than the unaltered code for each altered function. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run were back to the numbers above.

  • Code conventions and git diff --check pass.

  • tools/pr_overlap.py read 1,708 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 a line naming these exports or helpers. Merging each of the 36 with this branch and with main using git merge-tree gives the same conflict blocks. #10780 also edits the top of MathExact.cpp, so this change adds no include there. The branch also merges cleanly with #15336 and #15471.

Not tested: Linux, macOS, an Intel host, 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:5139263272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15484). Originally posted by [Danigamer05](https://github.com/Danigamer05) on 2026-11-03T14:47:15Z. --- ### What Export the versioned `std::__sce_v2` `_Poly` and the three `_XLgamma` overloads from libc. | Export | NID | Original RVA | | --- | --- | --- | | `_ZNSt8__sce_v25_PolyEdPKdi` | `QKveql1SwSo` | `0x10BA60` | | `_ZNSt8__sce_v28_XLgammaEf` | `BEK4zTmR87w` | `0x10D2E0` | | `_ZNSt8__sce_v28_XLgammaEd` | `e7wxmyU9QtE` | `0x10D3D0` | | `_ZNSt8__sce_v28_XLgammaEe` | `HX-r2mZEP1A` | `0x10D4C0` | The behavior comes from the `libc.prx` bundled with PPSA03671 v01.001.005 (J03848509, SHA-256 `483961e086b8a1fffa8138b3c0badf798987fb5f35ec908d828946408be2d8a6`). The new code sits in `MathExact.cpp`, beside the other `__sce_v2` math helpers: - `_Poly` evaluates the table with one fused multiply-add per coefficient, as the module's `vfmadd213sd` loop does. When an operand is a NaN, the module returns the quieted NaN of `x` first, then of the running value, then of the coefficient. The host `fma` picks another operand, so `OperandOrderFma` keeps the host result and only chooses which NaN to return. NaNs are tested on their bits, because a floating-point compare would raise the denormal flag the module does not raise. - `_XLgamma` is the module's six-term Lanczos series. It shifts by 5.5, sums the six coefficients over `x + 1` to `x + 6`, scales by √(2π) / x and subtracts `(x + 5.5) - (x + 0.5) log(x + 5.5)`. The constants, operation order and precision follow each overload: - `float` uses the `float` constants and starts the sum at 1; - `double` starts the sum at 1.000000000190015 and fuses the last multiply-subtract; - `long double` uses the `double` constants in x87 extended precision and does not fuse the last step, as the module does. One known difference remains, and the debt entry records it. Each overload calls this libc's `log`, `logf` and `logl`, as the module calls its own. Those are host functions, not the module's. They return other last bits for some arguments and do not raise the module's x87 invalid flag on negative arguments, so `_XLgamma` inherits that difference. Matching them is a separate change to `log` itself. ### 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 (AMD Zen 2), WinLibs GCC 15.2.0, Release, `BUILD_TESTING=ON`, main `4f41f4c375f`. Exact tested commit: `5609e53b0e6`. - Built libc, libkernel, their NID-patched production libraries and the related tests. The new `guest_sce_v2_log_gamma`, `public_headers`, `guest_math_exact` and `guest_dinkumware_float_bits` pass, along with the other `guest_sce_v2_*` CTests. - The new test checks: - each `_XLgamma` overload at 1, 2, 0.5, 3, 10, 0.25, 1.5, 100, 1e10, 1e300, 2^-20, ±0, -1, -2.5, 1e-300, the smallest subnormal, ±infinity, NaN, 7.25, 33 and 170.5, with the result bits and `errno` (EDOM for negative and -0 arguments, ERANGE for infinities); - `_Poly` with degrees -1 to 7 over a table holding a huge, a subnormal and ordinary coefficients; - `_Poly` NaN order, with quiet and signaling NaNs in `x`, in the first coefficient and in later coefficients, and with an infinite first coefficient times zero. Every expected value was taken from the original module, not from this implementation. Rows where this libc's `log` differs from the module's are left out of the `_XLgamma` tables: 7.25 for `double`, 1 and 1e10 for `long double`. - The original functions were loaded from the bundled module with its relocations applied. Its imports were bound by NID to this branch's libc and libkernel, with `__stack_chk_guard` pointing at a local value. The few imports with no provider would have stopped the run, and none was called. - Side by side with the patched production library, comparing the result bits, `errno`, MXCSR and the x87 control and status words: - `_Poly`: identical in all 2,000,000 calls over two seeds, with random and special `x`, degrees -2 to 14 and tables of random, scaled-down, special and zero coefficients. - `_XLgamma`: values and `errno` agree in 98.3% (`double`), 84% (`long double`) and 99.95% (`float`) of 1,000,000 calls each. The rest come from `log`: mostly the last bits, and for unnormal 80-bit arguments this libc's `logl` returns a NaN where the module's returns a number. The flags also differ on negative arguments, for the reason given above. - To check that the remaining differences come only from `log`, the same formula was run in the test program with the module's own `log`, `logf` and `logl`. It matched the module's `_XLgamma` in every one of 6,000,000 calls over two seeds, flags included. Over those arguments, this libc's `log`, `logf` and `logl` alone already differ from the module's in about 28%, 26% and 43% of calls once flags are counted. - Negative control: five lines were altered one at a time: - `_Poly` reused a coefficient; - `_Poly` preferred the running value's NaN over `x`'s; - the Lanczos sum started at `x + 2`; - the last step was not fused; - the `long double` constants were written as `long double` literals. The new CTest failed on each, and the side-by-side run reported more differences than the unaltered code for each altered function. The source was then restored byte for byte (SHA-256 checked) and rebuilt, and the test and side-by-side run were back to the numbers above. - Code conventions and `git diff --check` pass. - `tools/pr_overlap.py` read 1,708 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 a line naming these exports or helpers. Merging each of the 36 with this branch and with main using `git merge-tree` gives the same conflict blocks. #10780 also edits the top of `MathExact.cpp`, so this change adds no include there. The branch also merges cleanly with #15336 and #15471. Not tested: Linux, macOS, an Intel host, 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!15484
No description provided.