fix(libc): keep lgamma finite for subnormal arguments on Windows #5775

Merged
wachi35676 merged 1 commit from upstream-pr/5775 into main 2026-10-15 16:36:23 +00:00
wachi35676 commented 2026-10-14 10:04:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T10:04:12Z.


What

lgamma_nid_postfix (core/libs/prx/libc/src/Math.cpp) forwards to the host std::lgamma. The MinGW-w64 lgamma returns +inf and raises FE_OVERFLOW for nonzero |x| at or below 2^-1024 (e.g. lgamma(0x1p-1074)), where the value is finite: -log(2^-1074) ≈ 744.44.

FreeBSD's msun handles this range explicitly before its general path (lib/msun/src/e_lgamma_r.c, releng/11.4):

if(ix<0x3c700000) {	/* |x|<2**-56, return -log(|x|) */
    if((ix|lx)==0) return one/vzero;
    return -__ieee754_log(fabs(x));
}

lgamma_nid_postfix now takes the same branch for nonzero |x| < 2^-56 and otherwise still calls the host. Zero, infinities, NaN and every |x| ≥ 2^-56 are unchanged. Compared with an unmodified FreeBSD 11.4 msun build, lgamma(±0x1p-1074) now returns the same bits (0x40874385446d71c3); for 2^-60 … 2^-1000 the host already agreed with -log|x| to the last digit.

Tested

Windows 11 26200, WinLibs GCC 15.2.0 UCRT, full build with BUILD_TESTING=ON on main eecf90953.

  • New checks in core/libs/tests/GuestMath.cpp (guest_math) for ±denorm_min, ±2^-1025, ±2^-1022 and ±2^-57, and that no FE_OVERFLOW, FE_DIVBYZERO or FE_INVALID is raised. Fails on main, passes with this change.
  • The other libc formatting, math, wide and string tests (10) pass.
  • python tools/check_conventions.py --base origin/main passes.

The new check fails on main only on Windows; glibc's lgamma already returns the finite value, so on Linux it passes before and after. Not tested: Linux and macOS builds.

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
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4856212188 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5775). Originally posted by [wachi35676](https://github.com/wachi35676) on 2026-10-14T10:04:12Z. --- ### What `lgamma_nid_postfix` (`core/libs/prx/libc/src/Math.cpp`) forwards to the host `std::lgamma`. The MinGW-w64 `lgamma` returns +inf and raises FE_OVERFLOW for nonzero |x| at or below 2^-1024 (e.g. `lgamma(0x1p-1074)`), where the value is finite: -log(2^-1074) ≈ 744.44. FreeBSD's msun handles this range explicitly before its general path ([`lib/msun/src/e_lgamma_r.c`, releng/11.4](https://github.com/freebsd/freebsd-src/blob/releng/11.4/lib/msun/src/e_lgamma_r.c#L216-L221)): ```c if(ix<0x3c700000) { /* |x|<2**-56, return -log(|x|) */ if((ix|lx)==0) return one/vzero; return -__ieee754_log(fabs(x)); } ``` `lgamma_nid_postfix` now takes the same branch for nonzero |x| < 2^-56 and otherwise still calls the host. Zero, infinities, NaN and every |x| ≥ 2^-56 are unchanged. Compared with an unmodified FreeBSD 11.4 msun build, `lgamma(±0x1p-1074)` now returns the same bits (`0x40874385446d71c3`); for 2^-60 … 2^-1000 the host already agreed with `-log|x|` to the last digit. ### Tested Windows 11 26200, WinLibs GCC 15.2.0 UCRT, full build with `BUILD_TESTING=ON` on `main` `eecf90953`. - New checks in `core/libs/tests/GuestMath.cpp` (`guest_math`) for ±denorm_min, ±2^-1025, ±2^-1022 and ±2^-57, and that no FE_OVERFLOW, FE_DIVBYZERO or FE_INVALID is raised. Fails on `main`, passes with this change. - The other libc formatting, math, wide and string tests (10) pass. - `python tools/check_conventions.py --base origin/main` passes. The new check fails on `main` only on Windows; glibc's `lgamma` already returns the finite value, so on Linux it passes before and after. Not tested: Linux and macOS builds. ### 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] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-15 00:40:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-15T00:40:40Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
PR 6157 core/libs/prx/libc/src/Math.cpp L304-305
PR 6158 core/libs/prx/libc/src/Math.cpp L305
core/libs/tests/GuestMath.cpp L926
PR 6318 core/libs/prx/libc/src/Math.cpp L305
core/libs/tests/GuestMath.cpp L926
lgamma_nid_postfix

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6044 core/libs/tests/GuestMath.cpp
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6181685167 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5775#issuecomment-6181685167). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-15T00:40:40Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | [PR 6157](https://www.github.com/boykopovar/AnyPS5/pull/6157) | `core/libs/prx/libc/src/Math.cpp` [L304-305](https://github.com/boykopovar/AnyPS5/blob/0e6a5e7a5c0817fb0219d5d5c8707c25d915dbd5/core/libs/prx/libc/src/Math.cpp#L304-L305) | | | | [PR 6158](https://www.github.com/boykopovar/AnyPS5/pull/6158) | `core/libs/prx/libc/src/Math.cpp` [L305](https://github.com/boykopovar/AnyPS5/blob/0e6a5e7a5c0817fb0219d5d5c8707c25d915dbd5/core/libs/prx/libc/src/Math.cpp#L305)<br>`core/libs/tests/GuestMath.cpp` [L926](https://github.com/boykopovar/AnyPS5/blob/0e6a5e7a5c0817fb0219d5d5c8707c25d915dbd5/core/libs/tests/GuestMath.cpp#L926) | | | | [PR 6318](https://www.github.com/boykopovar/AnyPS5/pull/6318) | `core/libs/prx/libc/src/Math.cpp` [L305](https://github.com/boykopovar/AnyPS5/blob/0e6a5e7a5c0817fb0219d5d5c8707c25d915dbd5/core/libs/prx/libc/src/Math.cpp#L305)<br>`core/libs/tests/GuestMath.cpp` [L926](https://github.com/boykopovar/AnyPS5/blob/0e6a5e7a5c0817fb0219d5d5c8707c25d915dbd5/core/libs/tests/GuestMath.cpp#L926) | `lgamma_nid_postfix` | | Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap. | Pull request | Shared files not compared | |---|---| | [PR 6044](https://www.github.com/boykopovar/AnyPS5/pull/6044) | `core/libs/tests/GuestMath.cpp` |
adriadam10 commented 2026-10-15 12:50:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:50:38Z.


Checked the threshold (0x3c70000000000000 = 2^-56) and the -log|x| branch against FreeBSD e_lgamma_r.c; zero, inf, NaN and |x| ≥ 2^-56 still go to the host. Build and ctest (incl. guest_math) pass on Linux; the new rows only fail on MinGW, so the Windows job of the full Build is the evidence. Merging once it is green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6192472378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/5775#issuecomment-6192472378). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-15T12:50:38Z. --- Checked the threshold (0x3c70000000000000 = 2^-56) and the -log|x| branch against FreeBSD `e_lgamma_r.c`; zero, inf, NaN and |x| ≥ 2^-56 still go to the host. Build and ctest (incl. `guest_math`) pass on Linux; the new rows only fail on MinGW, so the Windows job of the full Build is the evidence. Merging once it is green.
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!5775
No description provided.