fix(libc): compute sinh with FreeBSD msun's algorithm #11556

Merged
xXPBSXx merged 11 commits from upstream-pr/11556 into main 2026-10-28 00:03:48 +00:00
xXPBSXx commented 2026-10-26 19:21:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-26T19:21:50Z.


What

sinh returned the host libm's result. It now follows the thresholds and branches of FreeBSD 11.4's e_sinh.c: below 22 in magnitude through this file's Expm1 (the FreeBSD expm1 port already here), from 22 to ln(DBL_MAX) through exp, up to the overflow threshold through exp scaled as k_exp.c's __ldexp_exp does, and x * 1e307 beyond it. On overflow it sets errno to ERANGE, as the host's sinh did and sinhf does; msun itself sets none, and the console's sinh on overflow is not measured (the differential described under Tested may contain no overflowing argument).

The host differs from msun in the last bit for many arguments: sinh(0x3ff401fffa851ce0) is 0x3ff9a53cef355b40 in a local build of msun and 0x3ff9a53cef355b3f with the Windows UCRT; of 2,000,000 random arguments in [-20, 20] the UCRT's sinh differs from msun on 561,202 and glibc's on 1,795, and this implementation on none (0 of 2,000,000 as well in [-22, 22] and in [-0.001, 0.001]).

The thresholds and branches follow e_sinh.c; results are bit-identical to msun below 22 in magnitude. From 22 on they depend on this file's exp, which is still the host's, so they can differ from msun in the last bit until exp follows msun. In [22, 709.78], 190,199 of 2,000,000 random arguments differ with the UCRT's exp, against 189,717 for the host's sinh, so that range is marginally worse than before; between 709.78 and the overflow threshold (710.4758) it is much better, 189,439 against 1,140,352. The entry in docs/dev/debt/libc.md says that the results from 22 on can differ.

The test guest_math gets CheckSinhMsunBits: 13 structured inputs (signed zeros, the 2^-28 and 1.0 boundaries, 21.999..., the smallest denormal, the infinities) and 20 arguments where both the Windows UCRT and glibc differ from msun, with msun's bits as the expected values. Fix and test are separate commits; the test fails on main.

Tested

  • Windows 10 Pro 19045, MinGW-w64 GCC 15.2.0 (UCRT), Release, this head on main afc780e681 (ninja exit code 0): guest_math passes 3 of 3 runs; guest_math_exact and guest_dinkumware_internals pass.
  • With Math.cpp of main (the production change reverted) and the new test, guest_math fails (exit code 0xc0000409) with "Guest sinh failed for bfd6a0128184d680: got bfd719794d320781, expected bfd719794d320780".
  • Three deliberate defects in sinh_nid_postfix, each making guest_math fail on Windows: the threshold 22 changed to 21 (fails at 403527ff77fdabb1: got 41c6f9a671b8ec61, expected 41c6f9a671b8ec60), the sign of h dropped (fails at be30000000000000: got 3e30000000000000), and the |x| < 2^-28 branch removed (fails at 3e2fffffffffffff: got 3e30000000000000).
  • Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), GCC 13.3, Release (-O3; the CMake files set no -ffp-contract or -mfma): the same arms give the same results, and guest_math, guest_math_exact and guest_dinkumware_internals pass. This ran on an earlier head of these commits (cc83507285, on an older main) whose added and removed lines in Math.cpp and GuestMath.cpp are the same as this head's; only the surrounding lines from main and the place of the debt entry changed since.
  • The expected values come from a local build of FreeBSD 11.4's lib/msun (BSD licence; not in the repository) with all its symbols renamed, compared on the same random arguments with the sinh code taken from Math.cpp; that measures this host against msun. A differential run by others against the libc.prx of a title (300,000 arguments, comment, head 1f8adf12f5) finds every argument below 22 in magnitude bit-identical and 8,585 differences of one ulp in the exp range, which goes through this file's host exp; its errno agrees on all 300,000 arguments, a set that, as the commenter notes, may contain no overflowing one. The system libSceLibcInternal is unmeasured.
  • A follow-up commit sets errno to ERANGE in the overflow path, which the host's sinh did and the first version of this port did not; main is merged in (main 81267b2204). The new check puts a sentinel in errno and requires ERANGE with an infinity of the right sign for 0x408633cf00000000 and DBL_MAX in both signs, and the sentinel unchanged for a finite argument, the infinities, 1 and 0. Without the errno line guest_math fails with "Guest sinh overflow failed for 408633cf00000000: got 7ff0000000000000, errno 1234", and with errno set on every call it fails with "Guest sinh changed errno for 408633ce8fb9e87d: errno 34"; both on Windows (UCRT) and Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), where guest_math passes 3 of 3 runs on this head, as do guest_math_exact and guest_dinkumware_internals.
  • Not run: macOS, a sanitizer build, floating-point exceptions, and errno beyond the overflow case (compared with nothing), builds with FMA contraction (-mfma, -march=native), arguments of magnitude 22 or more against msun's exp.

Overlap

No open PR changes sinh_nid_postfix; sinh has no other open or merged PR (#8023 changes the Dinkumware _FSinh scaling helper, not this function). 152 open PRs touch Math.cpp, GuestMath.cpp or docs/dev/debt/libc.md (or list 100 or more files); git merge-tree of this head with each (against main afc780e681): 108 merge without a conflict and 44 conflict, 43 of which already conflict with main on their own. #10250 (sin, cos and tan) edits the lines next to sinh_nid_postfix and conflicts with this change; it merges cleanly into main on its own, so whichever lands second rebases. From 22 on, sinh calls exp_nid_postfix; #10234 (exp) changes that function's body only, and results there should become bit-identical to msun once it lands (not measured).

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

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5023319397 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11556). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-26T19:21:50Z. --- ### What `sinh` returned the host `libm`'s result. It now follows the thresholds and branches of FreeBSD 11.4's [`e_sinh.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_sinh.c): below 22 in magnitude through this file's `Expm1` (the FreeBSD `expm1` port already here), from 22 to ln(DBL_MAX) through `exp`, up to the overflow threshold through `exp` scaled as `k_exp.c`'s `__ldexp_exp` does, and `x * 1e307` beyond it. On overflow it sets `errno` to `ERANGE`, as the host's `sinh` did and `sinhf` does; msun itself sets none, and the console's `sinh` on overflow is not measured (the differential described under Tested may contain no overflowing argument). The host differs from msun in the last bit for many arguments: `sinh(0x3ff401fffa851ce0)` is `0x3ff9a53cef355b40` in a local build of msun and `0x3ff9a53cef355b3f` with the Windows UCRT; of 2,000,000 random arguments in [-20, 20] the UCRT's `sinh` differs from msun on 561,202 and glibc's on 1,795, and this implementation on none (0 of 2,000,000 as well in [-22, 22] and in [-0.001, 0.001]). The thresholds and branches follow `e_sinh.c`; results are bit-identical to msun below 22 in magnitude. From 22 on they depend on this file's `exp`, which is still the host's, so they can differ from msun in the last bit until `exp` follows msun. In [22, 709.78], 190,199 of 2,000,000 random arguments differ with the UCRT's `exp`, against 189,717 for the host's `sinh`, so that range is marginally worse than before; between 709.78 and the overflow threshold (710.4758) it is much better, 189,439 against 1,140,352. The entry in `docs/dev/debt/libc.md` says that the results from 22 on can differ. The test `guest_math` gets `CheckSinhMsunBits`: 13 structured inputs (signed zeros, the 2^-28 and 1.0 boundaries, 21.999..., the smallest denormal, the infinities) and 20 arguments where both the Windows UCRT and glibc differ from msun, with msun's bits as the expected values. Fix and test are separate commits; the test fails on `main`. ### Tested - Windows 10 Pro 19045, MinGW-w64 GCC 15.2.0 (UCRT), Release, this head on `main` `afc780e681` (ninja exit code 0): `guest_math` passes 3 of 3 runs; `guest_math_exact` and `guest_dinkumware_internals` pass. - With `Math.cpp` of `main` (the production change reverted) and the new test, `guest_math` fails (exit code 0xc0000409) with "Guest sinh failed for bfd6a0128184d680: got bfd719794d320781, expected bfd719794d320780". - Three deliberate defects in `sinh_nid_postfix`, each making `guest_math` fail on Windows: the threshold 22 changed to 21 (fails at `403527ff77fdabb1`: got `41c6f9a671b8ec61`, expected `41c6f9a671b8ec60`), the sign of `h` dropped (fails at `be30000000000000`: got `3e30000000000000`), and the `|x| < 2^-28` branch removed (fails at `3e2fffffffffffff`: got `3e30000000000000`). - Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), GCC 13.3, Release (`-O3`; the CMake files set no `-ffp-contract` or `-mfma`): the same arms give the same results, and `guest_math`, `guest_math_exact` and `guest_dinkumware_internals` pass. This ran on an earlier head of these commits (`cc83507285`, on an older `main`) whose added and removed lines in `Math.cpp` and `GuestMath.cpp` are the same as this head's; only the surrounding lines from `main` and the place of the debt entry changed since. - The expected values come from a local build of FreeBSD 11.4's `lib/msun` (BSD licence; not in the repository) with all its symbols renamed, compared on the same random arguments with the `sinh` code taken from `Math.cpp`; that measures this host against msun. A differential run by others against the `libc.prx` of a title (300,000 arguments, [comment](https://github.com/boykopovar/AnyPS5/pull/11556#issuecomment-6446438870), head `1f8adf12f5`) finds every argument below 22 in magnitude bit-identical and 8,585 differences of one ulp in the `exp` range, which goes through this file's host `exp`; its `errno` agrees on all 300,000 arguments, a set that, as the commenter notes, may contain no overflowing one. The system `libSceLibcInternal` is unmeasured. - A follow-up commit sets `errno` to `ERANGE` in the overflow path, which the host's `sinh` did and the first version of this port did not; `main` is merged in (`main` `81267b2204`). The new check puts a sentinel in `errno` and requires `ERANGE` with an infinity of the right sign for `0x408633cf00000000` and `DBL_MAX` in both signs, and the sentinel unchanged for a finite argument, the infinities, 1 and 0. Without the `errno` line `guest_math` fails with "Guest sinh overflow failed for 408633cf00000000: got 7ff0000000000000, errno 1234", and with `errno` set on every call it fails with "Guest sinh changed errno for 408633ce8fb9e87d: errno 34"; both on Windows (UCRT) and Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), where `guest_math` passes 3 of 3 runs on this head, as do `guest_math_exact` and `guest_dinkumware_internals`. - Not run: macOS, a sanitizer build, floating-point exceptions, and `errno` beyond the overflow case (compared with nothing), builds with FMA contraction (`-mfma`, `-march=native`), arguments of magnitude 22 or more against msun's `exp`. ### Overlap No open PR changes `sinh_nid_postfix`; `sinh` has no other open or merged PR (#8023 changes the Dinkumware `_FSinh` scaling helper, not this function). 152 open PRs touch `Math.cpp`, `GuestMath.cpp` or `docs/dev/debt/libc.md` (or list 100 or more files); `git merge-tree` of this head with each (against `main` `afc780e681`): 108 merge without a conflict and 44 conflict, 43 of which already conflict with `main` on their own. #10250 (sin, cos and tan) edits the lines next to `sinh_nid_postfix` and conflicts with this change; it merges cleanly into `main` on its own, so whichever lands second rebases. From 22 on, `sinh` calls `exp_nid_postfix`; #10234 (exp) changes that function's body only, and results there should become bit-identical to msun once it lands (not measured). ### Checklist - [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 AI-assisted (Claude Code).
MarcZX10 commented 2026-10-26 19:25:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T19:25:13Z.

Review: CHANGES_REQUESTED


Reviewed head 0567c826e7fb89b1b4de97bb1e07c21a8bd0133a and compared the implementation with the referenced FreeBSD 11.4 e_sinh.c and k_exp.c paths.

[P2] core/libs/tests/GuestMath.cpp, CheckSinhMsunBits: exercise the new large-input branches. Every finite added case has |x| < 22; the rest are signed zero, subnormal values, or infinities. Consequently none of these tests execute either new branch at |x| >= 22, including SinhLdexpExpHalf's exponent reconstruction or the final overflow path. Those branches cover values close to DBL_MAX and are a substantial part of this change. Please add positive and negative cases on both sides of the 22, 0x40862e42, and 0x408633ce cutoffs, including the largest finite result/first overflowing input as appropriate, with expectations or finite/overflow checks derived from the FreeBSD reference. The current lower-range bit vectors don't validate this code.

The reported Windows and WSL runs establish the lower-range behavior, but the PR body explicitly says inputs at or above 22 were not compared against msun's exp; no current test exercises those paths. I did not approve pending that focused coverage.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5657781417 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11556#pullrequestreview-5657781417). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T19:25:13Z. **Review:** CHANGES_REQUESTED --- Reviewed head `0567c826e7fb89b1b4de97bb1e07c21a8bd0133a` and compared the implementation with the referenced FreeBSD 11.4 `e_sinh.c` and `k_exp.c` paths. **[P2] `core/libs/tests/GuestMath.cpp`, `CheckSinhMsunBits`: exercise the new large-input branches.** Every finite added case has `|x| < 22`; the rest are signed zero, subnormal values, or infinities. Consequently none of these tests execute either new branch at `|x| >= 22`, including `SinhLdexpExpHalf`'s exponent reconstruction or the final overflow path. Those branches cover values close to `DBL_MAX` and are a substantial part of this change. Please add positive and negative cases on both sides of the 22, `0x40862e42`, and `0x408633ce` cutoffs, including the largest finite result/first overflowing input as appropriate, with expectations or finite/overflow checks derived from the FreeBSD reference. The current lower-range bit vectors don't validate this code. The reported Windows and WSL runs establish the lower-range behavior, but the PR body explicitly says inputs at or above 22 were not compared against msun's `exp`; no current test exercises those paths. I did not approve pending that focused coverage.
MarcZX10 commented 2026-10-27 00:21:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T00:21:42Z.

Review: APPROVED


Re-reviewed exact head 6e418d3342c1285bb92c27d3dcc391d0db17a36b. The follow-up adds positive and negative coverage across the |x| = 22, 0x40862e42, and 0x408633ce branch boundaries, includes the scaled-exponential case where direct exp overflows, and checks finite-near-overflow and signed overflow results. That addresses my earlier coverage request. The report says the new cases were checked against a local FreeBSD msun build; I did not run them locally in this workspace. Current hosted conventions and platform policy checks pass; product jobs are skipped by path policy. No remaining actionable finding from this review.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5660816392 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11556#pullrequestreview-5660816392). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T00:21:42Z. **Review:** APPROVED --- Re-reviewed exact head `6e418d3342c1285bb92c27d3dcc391d0db17a36b`. The follow-up adds positive and negative coverage across the `|x| = 22`, `0x40862e42`, and `0x408633ce` branch boundaries, includes the scaled-exponential case where direct `exp` overflows, and checks finite-near-overflow and signed overflow results. That addresses my earlier coverage request. The report says the new cases were checked against a local FreeBSD msun build; I did not run them locally in this workspace. Current hosted conventions and platform policy checks pass; product jobs are skipped by path policy. No remaining actionable finding from this review.
jotitadev commented 2026-10-27 01:16:46 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-27T01:16:46Z.

Review: COMMENTED


Run on bare-metal Linux and on current main, since the Linux run in the PR was WSL2 on an older head.

Exact head 6e418d3342c1285bb92c27d3dcc391d0db17a36b, merged on main cb45ae2dd (merges cleanly). Linux x86_64 (bare metal), i7-8700, Ubuntu 26.04, GCC 15.2, glibc 2.43, CMake 4.2.3, Ninja, Release, BUILD_TESTING=ON, full build.

  • guest_math passes, 3 of 3 runs. The 22 guest_math*|guest_dinkumware* tests pass, guest_math_exact and guest_dinkumware_internals included.
  • With only core/libs/prx/libc/src/Math.cpp reverted to main (new tests kept), guest_math aborts with Guest sinh failed for bfd6a0128184d680: got bfd719794d320781, expected bfd719794d320780, the same first mismatch as the UCRT run in the description. So glibc 2.43's sinh also misses the last bit there and the test fails on Linux too.

Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5661143614 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11556#pullrequestreview-5661143614). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-27T01:16:46Z. **Review:** COMMENTED --- Run on bare-metal Linux and on current `main`, since the Linux run in the PR was WSL2 on an older head. Exact head `6e418d3342c1285bb92c27d3dcc391d0db17a36b`, merged on `main` `cb45ae2dd` (merges cleanly). Linux x86_64 (bare metal), i7-8700, Ubuntu 26.04, GCC 15.2, glibc 2.43, CMake 4.2.3, Ninja, Release, `BUILD_TESTING=ON`, full build. - `guest_math` passes, 3 of 3 runs. The 22 `guest_math*|guest_dinkumware*` tests pass, `guest_math_exact` and `guest_dinkumware_internals` included. - With only `core/libs/prx/libc/src/Math.cpp` reverted to `main` (new tests kept), `guest_math` aborts with `Guest sinh failed for bfd6a0128184d680: got bfd719794d320781, expected bfd719794d320780`, the same first mismatch as the UCRT run in the description. So glibc 2.43's `sinh` also misses the last bit there and the test fails on Linux too. Written with AI assistance (Claude Code). I ran the builds and tests above 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!11556
No description provided.