fix(libc): preserve tgammaf underflow sign #10467

Merged
MarcZX10 merged 2 commits from upstream-pr/10467 into main 2026-10-27 07:53:02 +00:00
MarcZX10 commented 2026-10-24 01:55:10 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T01:55:10Z.


What

The Windows tgammaf wrapper delegates to the host's double tgamma and casts the result, matching FreeBSD 11's s_tgammaf.c. For negative non-integer inputs whose result underflows to zero, the sign of Γ alternates with the parity of floor(-x). Nine title-bundled libc.prx builds return the corresponding signed zero, while Windows UCRT can return the opposite sign for those inputs. This preserves the host result except when the float result is zero and the input is a finite negative non-integer.

The measurements and broader platform survey are in #9707. This PR fixes only the tgammaf signed-zero discrepancy; it does not claim to resolve the survey's other math differences.

Tested

  • Linux x86_64, GCC 12.2, C++20: compiled the production Math.cpp and a focused executable with -Wall -Wextra -Werror; negative and positive underflow cases passed and produced -0 and +0 respectively.
  • GCC 12.2 syntax-checked core/libs/tests/GuestMath.cpp.
  • MinGW-w64 GCC 12 compiled the isolated helper with -Wall -Wextra -Werror; the full MinGW syntax check of Math.cpp was also attempted, but this toolchain does not declare the existing POSIX j0/j1/jn/y0/y1/yn symbols; no Windows runtime was available.
  • tools/check_conventions.py --base upstream/main and git diff --check pass.
  • Full CMake configuration was attempted but stops before generation: the SDL2 submodule is absent and the environment lacks the required XRandR support. Therefore guest_math and the full project build were not run locally. No title or console run was performed.

Checklist

  • Based on current main; no other open PR implements the same tgammaf behavior
  • One topic per PR; follow-up work goes 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
  • New third-party code is a submodule built from source
  • Generic behavior, not specific to one title
  • Depends on: #8417
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4991578874 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10467). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-24T01:55:10Z. --- ### What The Windows `tgammaf` wrapper delegates to the host's double `tgamma` and casts the result, matching FreeBSD 11's `s_tgammaf.c`. For negative non-integer inputs whose result underflows to zero, the sign of Γ alternates with the parity of `floor(-x)`. Nine title-bundled `libc.prx` builds return the corresponding signed zero, while Windows UCRT can return the opposite sign for those inputs. This preserves the host result except when the float result is zero and the input is a finite negative non-integer. The measurements and broader platform survey are in [#9707](https://github.com/boykopovar/AnyPS5/issues/9707). This PR fixes only the `tgammaf` signed-zero discrepancy; it does not claim to resolve the survey's other math differences. ### Tested - Linux x86_64, GCC 12.2, C++20: compiled the production `Math.cpp` and a focused executable with `-Wall -Wextra -Werror`; negative and positive underflow cases passed and produced `-0` and `+0` respectively. - GCC 12.2 syntax-checked `core/libs/tests/GuestMath.cpp`. - MinGW-w64 GCC 12 compiled the isolated helper with `-Wall -Wextra -Werror`; the full MinGW syntax check of `Math.cpp` was also attempted, but this toolchain does not declare the existing POSIX `j0`/`j1`/`jn`/`y0`/`y1`/`yn` symbols; no Windows runtime was available. - `tools/check_conventions.py --base upstream/main` and `git diff --check` pass. - Full CMake configuration was attempted but stops before generation: the SDL2 submodule is absent and the environment lacks the required XRandR support. Therefore `guest_math` and the full project build were not run locally. No title or console run was performed. ### 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 `tgammaf` behavior - [x] One topic per PR; follow-up work goes 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 - [x] New third-party code is a submodule built from source - [x] Generic behavior, not specific to one title - [x] Depends on: #8417 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-24 04:57:49 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-24T04:57:49Z.

Review: APPROVED


Checked against main 6c52b649e7 and FreeBSD release/11.4.0 lib/msun/src/s_tgammaf.c and lib/msun/bsdsrc/b_tgamma.c:

  • Confirmed that 11.4's tgammaf is just return (tgamma(x));, so keeping the double delegation matches it.
  • msun's neg_gam makes the result negative when ceil(x) is even and returns sgn*tiny*tiny below -190, so your "floor(-x) even ⇒ negative" rule is the same condition. A Python check over 2M random negative non-integer floats gave 0 mismatches, and both test inputs give the right sign.
  • The override only touches zero results for finite negative non-integers, so poles, -0 and NaN are unchanged from main (Math.cpp:409). It still merges cleanly onto current main.
  • Small question: the diff doesn't seem to need #8417. Is "Depends on: #8417" meant as a note that the double tgamma added there would need the same sign handling?

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5636913403 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10467#pullrequestreview-5636913403). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-24T04:57:49Z. **Review:** APPROVED --- Checked against main 6c52b649e7 and FreeBSD release/11.4.0 `lib/msun/src/s_tgammaf.c` and `lib/msun/bsdsrc/b_tgamma.c`: - Confirmed that 11.4's `tgammaf` is just `return (tgamma(x));`, so keeping the double delegation matches it. - msun's `neg_gam` makes the result negative when `ceil(x)` is even and returns `sgn*tiny*tiny` below -190, so your "`floor(-x)` even ⇒ negative" rule is the same condition. A Python check over 2M random negative non-integer floats gave 0 mismatches, and both test inputs give the right sign. - The override only touches zero results for finite negative non-integers, so poles, -0 and NaN are unchanged from main (Math.cpp:409). It still merges cleanly onto current main. - Small question: the diff doesn't seem to need #8417. Is "Depends on: #8417" meant as a note that the double `tgamma` added there would need the same sign handling? Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
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!10467
No description provided.