fix(libc): raise only divide-by-zero for lgammal poles #24272

Merged
JJenkx merged 4 commits from upstream-pr/24272 into main 2026-11-21 10:12:48 +00:00
JJenkx commented 2026-11-20 12:51:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-11-20T12:51:53Z.


What

Handle every nonpositive integer pole of lgammal before entering the host C library. The current host wrapper can add FE_INEXACT at negative integers (for example −2); the bundled reference raises only divide-by-zero. Classify with the existing _LDint, raise through _Feraise, and return positive infinity. Signed zeros and integers beyond64-bit integer range use the same path; existing flags, control modes and signgam are preserved.

This is a pole/FPU correctness correction, not a replacement of the finite-input approximation. Non-pole inputs still delegate to host math and retain known numerical/flag differences. No new export, game unblock or performance claim.

Tested

Linux x86-64 CPU-only, Ryzen9 7950X, same machine and identical new fixture on both revisions:

  • Pinned upstream main fbb3ef67cd03fba0dd56c5f622841810be129c79 plus fixture 27437cd27937f7a403ff7c2240ccedb2e9d4d869: full SDK/relinker build passes; the new regression fails at the exception-flag assertion. Existing guest_math, guest_math_exact and guest_floating_point pass.
  • Change bf71536f12d073315aae233acd9ab440045c303a: full SDK/relinker build and all four groups pass. New fixture checks240 pole cases: ten inputs (±0, several negative integers, −2^63, −2^100 and −LDBL_MAX) × three x87 precision modes × four rounding modes × clean/preexisting-invalid states, plus non-pole controls.
  • Guarded native CPU reference from title-bundled PPSA21564 libc.prx:384 observations including240 poles, all240 confirm +infinity, divide-by-zero without new inexact, errno34, preserved incoming invalid and unchanged control word. dladdr plus device/inode checks establish the actual #guest export; source, original module and SDK hashes stay unchanged. This is not console execution. No original module, asset or disassembly is distributed.
  • Conventions against updated origin/main pass; read-only merge check is clean. Official function overlap found no competing lgammal PR. The branch scan checked2051 PRs and failed closed on15 unrelated shared-file comparisons; those15 pinned diffs were manually reviewed (including #7544's double lgamma, which is a separate export). No duplicate function, test name or functional dependency was found; reports and manual comparison receipts are included.

Downloaded and hash-verified68-payload evidence bundle; SHA-256 d2a33aac2a0d73dc71e5f3842780775a0d818b48d6ef78c2b5861c019da71aac. Includes source patch, fullbuild/test logs, reference numeric observations/provenance hashes and overlap/conventions receipts.

No Windows machine, game, GPU or FPS run was performed for this CPU math change.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5413632486 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24272). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-11-20T12:51:53Z. --- ### What Handle every nonpositive integer pole of `lgammal` before entering the host C library. The current host wrapper can add `FE_INEXACT` at negative integers (for example −2); the bundled reference raises only divide-by-zero. Classify with the existing `_LDint`, raise through `_Feraise`, and return positive infinity. Signed zeros and integers beyond64-bit integer range use the same path; existing flags, control modes and `signgam` are preserved. This is a pole/FPU correctness correction, not a replacement of the finite-input approximation. Non-pole inputs still delegate to host math and retain known numerical/flag differences. No new export, game unblock or performance claim. ### Tested Linux x86-64 CPU-only, Ryzen9 7950X, same machine and identical new fixture on both revisions: - Pinned upstream main `fbb3ef67cd03fba0dd56c5f622841810be129c79` plus fixture `27437cd27937f7a403ff7c2240ccedb2e9d4d869`: full SDK/relinker build passes; the new regression fails at the exception-flag assertion. Existing `guest_math`, `guest_math_exact` and `guest_floating_point` pass. - Change `bf71536f12d073315aae233acd9ab440045c303a`: full SDK/relinker build and all four groups pass. New fixture checks240 pole cases: ten inputs (±0, several negative integers, −2^63, −2^100 and −LDBL_MAX) × three x87 precision modes × four rounding modes × clean/preexisting-invalid states, plus non-pole controls. - Guarded native CPU reference from title-bundled PPSA21564 `libc.prx`:384 observations including240 poles, all240 confirm +infinity, divide-by-zero without new inexact, errno34, preserved incoming invalid and unchanged control word. `dladdr` plus device/inode checks establish the actual `#guest` export; source, original module and SDK hashes stay unchanged. **This is not console execution.** No original module, asset or disassembly is distributed. - Conventions against updated `origin/main` pass; read-only merge check is clean. Official function overlap found no competing `lgammal` PR. The branch scan checked2051 PRs and failed closed on15 unrelated shared-file comparisons; those15 pinned diffs were manually reviewed (including #7544's double `lgamma`, which is a separate export). No duplicate function, test name or functional dependency was found; reports and manual comparison receipts are included. [Downloaded and hash-verified68-payload evidence bundle](https://github.com/JJenkx/AnyPS5/releases/download/linux-validation-20261023/lgammal-pole-flags-bf71536f-linux-20261120.tar.gz); SHA-256 `d2a33aac2a0d73dc71e5f3842780775a0d818b48d6ef78c2b5861c019da71aac`. Includes source patch, fullbuild/test logs, reference numeric observations/provenance hashes and overlap/conventions receipts. No Windows machine, game, GPU or FPS run was performed for this CPU math change. <!-- 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. --> ### 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) and the licence notice of code adapted from upstream (see AGENTS.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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no -->
PrivacyOnion commented 2026-11-20 12:55:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-20T12:55:52Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against bf71536f12:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_lgamma_poles_tests built cleanly.
  • CTest (guest_lgamma_poles):
    1/1 Test #1216: guest_lgamma_poles ...............   Passed    0.02 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1216: guest_lgamma_poles ...............Subprocess aborted***Exception:   0.38 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • core/libs/prx/libc/src/Math.cpp: verified lgammal_nid_postfix correctly tests nonpositive integers (poles) using _LDint_nid_postfix(&integral, 0) == 0 && x <= 0.0L, raises FE_DIVBYZERO (0x04) via _Feraise_nid_postfix, and returns +inf without FE_INEXACT
    • core/libs/prx/libc/src/Math.cpp: verified handling of signed zero (+0.0L and -0.0L) as poles where _LDint returns 0 and x <= 0.0L evaluates to true, correctly avoiding host std::lgamma which can set inexact or differ on errno
    • core/libs/prx/libc/CMakeLists.txt: verified test guest_lgamma_poles_tests is properly added under BUILD_TESTING with WIN32 environment modification and timeout configuration matching repository standards
    • core/libs/tests/GuestLgammaPoles.cpp: verified comprehensive testing across x87 precision modes (0, 0x200, 0x300), 4 rounding modes, clean vs pending FE_INVALID, signgam isolation, errno preservation, and non-pole control inputs
    • docs/dev/debt/changes/lgammal-pole-flags.md: verified debt change fragment formatting and AGENTS.md compliance for functional gaps and console characterization references

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5993021870 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24272#pullrequestreview-5993021870). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-20T12:55:52Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `bf71536f12`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_lgamma_poles_tests` built cleanly. - **CTest** (`guest_lgamma_poles`): ``` 1/1 Test #1216: guest_lgamma_poles ............... Passed 0.02 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/Math.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1216: guest_lgamma_poles ...............Subprocess aborted***Exception: 0.38 sec 0% tests passed, 1 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - core/libs/prx/libc/src/Math.cpp: verified lgammal_nid_postfix correctly tests nonpositive integers (poles) using _LDint_nid_postfix(&integral, 0) == 0 && x <= 0.0L, raises FE_DIVBYZERO (0x04) via _Feraise_nid_postfix, and returns +inf without FE_INEXACT - core/libs/prx/libc/src/Math.cpp: verified handling of signed zero (+0.0L and -0.0L) as poles where _LDint returns 0 and x <= 0.0L evaluates to true, correctly avoiding host std::lgamma which can set inexact or differ on errno - core/libs/prx/libc/CMakeLists.txt: verified test guest_lgamma_poles_tests is properly added under BUILD_TESTING with WIN32 environment modification and timeout configuration matching repository standards - core/libs/tests/GuestLgammaPoles.cpp: verified comprehensive testing across x87 precision modes (0, 0x200, 0x300), 4 rounding modes, clean vs pending FE_INVALID, signgam isolation, errno preservation, and non-pole control inputs - docs/dev/debt/changes/lgammal-pole-flags.md: verified debt change fragment formatting and AGENTS.md compliance for functional gaps and console characterization references Looks Good To Me
MarcZX10 commented 2026-11-20 12:55:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T12:55:57Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected code supports the stated pole handling, and the author reports passing build and regression checks.

What I ran

Reviewed head bf71536f12d073315aae233acd9ab440045c303a and the prepared diff. No build or tests were run during this review.

Results

Check Result Note
Patch and changed code passed All four changed files read
Build and regression tests not run Results in the PR description are author-reported
Console behavior not run Description explicitly says native CPU only

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author-reported SDK/relinker build, regression tests, and native reference observations were not independently run. Windows, console execution, and the full repository test suite were not verified. The review bundle did not provide files.json; the manifest lists four reviewed files, all of which were read.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5993023272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24272#pullrequestreview-5993023272). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T12:55:57Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected code supports the stated pole handling, and the author reports passing build and regression checks. ## What I ran Reviewed head `bf71536f12d073315aae233acd9ab440045c303a` and the prepared diff. No build or tests were run during this review. ## Results | Check | Result | Note | |---|---|---| | Patch and changed code | passed | All four changed files read | | Build and regression tests | not run | Results in the PR description are author-reported | | Console behavior | not run | Description explicitly says native CPU only | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author-reported SDK/relinker build, regression tests, and native reference observations were not independently run. Windows, console execution, and the full repository test suite were not verified. The review bundle did not provide `files.json`; the manifest lists four reviewed files, all of which were read. ## To get this to a good state Nothing is required.
MarcZX10 commented 2026-11-20 20:13:55 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:13:55Z.

Review: COMMENTED


Reviewed — no blocking findings

The pole-path change is supported by the inspected implementation; the test fixture does not restore the incoming x87 status flags.

What I ran

Reviewed head 9f7779b513b13558bc86ddd35aeb6e5d66097a72. I read the prepared description, commit list, manifest and diff, then the changed lines in all four changed files and the relevant _LDint, _Feraise and truncation code using repo_read.py tools. All changed files were included and read; no changed files were excluded. No commands or tests were run.

Results

Check Result Note
Changed-file review passed All four changed files read
Tests not run Author reports passing runs
Console behavior not run No console execution reported

Negative control

Not run. I did not alter the implementation or execute the regression test.

Findings

Blocking

None.

Non-blocking

  • core/libs/tests/GuestLgammaPoles.cpp:66 and core/libs/tests/GuestLgammaPoles.cpp:112 — The fixture clears floating-point exceptions but never saves and restores the original x87 status word. The process therefore finishes with different x87 exception flags than it started with. Save and restore the status flags along with the control word and MXCSR to keep the fixture self-contained.

Sources

Not verified

Tests and author-supplied build results were not independently verified. Windows, game behavior and console execution were not tested. All changed files listed in the manifest were read; no exclusions were reported by the review-folder listing.

To get this to a good state

Nothing is required. Optional: preserve and restore the original x87 status flags in the fixture.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5998750539 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24272#pullrequestreview-5998750539). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T20:13:55Z. **Review:** COMMENTED --- # Reviewed — no blocking findings The pole-path change is supported by the inspected implementation; the test fixture does not restore the incoming x87 status flags. ## What I ran Reviewed head `9f7779b513b13558bc86ddd35aeb6e5d66097a72`. I read the prepared description, commit list, manifest and diff, then the changed lines in all four changed files and the relevant `_LDint`, `_Feraise` and truncation code using `repo_read.py` tools. All changed files were included and read; no changed files were excluded. No commands or tests were run. ## Results | Check | Result | Note | |---|---|---| | Changed-file review | passed | All four changed files read | | Tests | not run | Author reports passing runs | | Console behavior | not run | No console execution reported | ## Negative control Not run. I did not alter the implementation or execute the regression test. ## Findings ### Blocking None. ### Non-blocking - `core/libs/tests/GuestLgammaPoles.cpp:66` and `core/libs/tests/GuestLgammaPoles.cpp:112` — The fixture clears floating-point exceptions but never saves and restores the original x87 status word. The process therefore finishes with different x87 exception flags than it started with. Save and restore the status flags along with the control word and MXCSR to keep the fixture self-contained. ## Sources - Pole classification and explicit divide-by-zero raise: [`Math.cpp` at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/9f7779b513b13558bc86ddd35aeb6e5d66097a72/core/libs/prx/libc/src/Math.cpp#L1574-L1580). - Test setup and cleanup: [`GuestLgammaPoles.cpp` at the reviewed commit](https://github.com/boykopovar/AnyPS5/blob/9f7779b513b13558bc86ddd35aeb6e5d66097a72/core/libs/tests/GuestLgammaPoles.cpp#L19-L23) and [lines 66–70](https://github.com/boykopovar/AnyPS5/blob/9f7779b513b13558bc86ddd35aeb6e5d66097a72/core/libs/tests/GuestLgammaPoles.cpp#L66-L70). ## Not verified Tests and author-supplied build results were not independently verified. Windows, game behavior and console execution were not tested. All changed files listed in the manifest were read; no exclusions were reported by the review-folder listing. ## To get this to a good state Nothing is required. Optional: preserve and restore the original x87 status flags in the fixture.
MarcZX10 commented 2026-11-20 20:47:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T20:47:20Z.

Review: APPROVED


Reviewed — looks good

The change at dc82395995a36a01ec1ce135517a2ce95e7fd657 adds explicit handling for nonpositive integer lgammal poles and a focused regression test; the code inspected supports the stated pole behavior.

What I ran

No build or tests were run. The author reports Linux CPU-only build and fixture results in the PR description; those were not independently verified.

Results

Check Result Note
Diff and changed files passed Four changed files inspected
Build and tests not run Author reports successful results
Console behavior not run Description says no console execution

Findings

Blocking

None.

Non-blocking

None.

Not verified

No build, test fixture, Windows run, or console run was performed. The author’s reported test results and linked evidence bundle were not independently inspected. All files listed in the manifest were read; files.json was unavailable in the review reader, so its separate exclusion listing was not verified.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5999152788 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/24272#pullrequestreview-5999152788). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T20:47:20Z. **Review:** APPROVED --- # Reviewed — looks good The change at `dc82395995a36a01ec1ce135517a2ce95e7fd657` adds explicit handling for nonpositive integer `lgammal` poles and a focused regression test; the code inspected supports the stated pole behavior. ## What I ran No build or tests were run. The author reports Linux CPU-only build and fixture results in the PR description; those were not independently verified. ## Results | Check | Result | Note | |---|---|---| | Diff and changed files | passed | Four changed files inspected | | Build and tests | not run | Author reports successful results | | Console behavior | not run | Description says no console execution | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified No build, test fixture, Windows run, or console run was performed. The author’s reported test results and linked evidence bundle were not independently inspected. All files listed in the manifest were read; `files.json` was unavailable in the review reader, so its separate exclusion listing was not verified. ## To get this to a good state Nothing is required.
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!24272
No description provided.