fix(libc): compute asinh with FreeBSD msun's algorithm #11737

Merged
xXPBSXx merged 4 commits from upstream-pr/11737 into main 2026-10-28 16:59:00 +00:00
xXPBSXx commented 2026-10-27 06:24:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-27T06:24:44Z.


What

asinh returned the host libm's result. It now follows the branches and thresholds of FreeBSD 11.4's s_asinh.c: below 2^-28 it returns x; up to |x| = 2 it uses this file's msun log1p; from there to 2^28 it uses log(2|x| + 1/(sqrt(x*x+1)+|x|)) and above 2^28 log(|x|) + ln2, with this file's log, which is still the host's. It sets no errno, as msun does not.

The host differs from msun in the last bit for many arguments: asinh(0x3e2fffffffffffff) is 0x3e2fffffffffffff in a local build of msun and 0x3e2ffffffffffffe with the Windows UCRT, and asinh(0x3fc858aceb437920) is 0x3fc833b16e1536cf in msun and 0x3fc833b16e1536ce with glibc 2.39. Of 2,000,000 random arguments in [0, 2] (both signs) the UCRT's asinh differs from msun on 319,633 and glibc's on 1,212, and this implementation on none (0 as well in [3.8e-9, 2] log-uniformly, 0 of 2,000,000 below 2^-28 on both hosts).

Bits are identical to msun up to |x| = 2. Above 2 they depend on the host log until it follows msun: of 2,000,000 log-uniform random arguments in (2, 2^28], 38,719 differ with the UCRT's log and 38,926 with glibc's (for the host's asinh the numbers are 131,924 and 38,926), and none differ above 2^28 on either host, which is how those two log implementations happen to round, not a property of this code. The entry in docs/dev/debt/libc.md says so.

The test guest_math gets CheckAsinhMsunBits (32 cases, both signs, with msun's bits as the expected values: signed zero, the smallest denormal, both sides of the 2^-28 cutoff, 0.5, 1, both sides of 2, 12 arguments below 2 where both the UCRT and glibc differ from msun, two just above 2 and three just above 2^28 where the neighbouring branch's formula would change the last bit; the latter three depend on the host log and match msun with the UCRT's and with glibc 2.39's) and CheckAsinhLargeArguments (14 arguments above 2 in both signs, accepted within 4 ulps of msun's bits because they depend on the host log, plus the infinities and a NaN). 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 b7105f7285 (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 asinh failed for 3e2fffffffffffff: got 3e2ffffffffffffe, expected 3e2fffffffffffff".
  • Eight deliberate defects in asinh_nid_postfix, each making guest_math fail on Windows with the failing argument in the message: the tiny-argument return removed, the 2^-28 cutoff moved down by one, the cutoff at 2 moved down by one, the cutoff at 2^28 moved down by one, the + ln2 dropped from the large branch, the factor 2 dropped from the middle branch, the 1 + dropped from the log1p branch, and the sign dropped.
  • 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), same base: the same arms give the same results, guest_math passes 3 of 3 runs, and guest_math_exact and guest_dinkumware_internals pass.
  • The run against main's Math.cpp and the eight deliberate defects were measured on an earlier rebase of these two commits (main 9e2e90dc00; git range-diff shows = for both commits, and Math.cpp and GuestMath.cpp are byte-identical to this head's); the three passing runs and the other two tests were repeated on this head, on both systems.
  • 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 random arguments with the asinh code taken from Math.cpp; that measures these hosts against msun. A differential run by others against the libc.prx of a title (300,000 arguments, comment, head 4902c38569) finds every argument up to 2 in magnitude bit-identical and 2,313 differences of one ulp above 2, which goes through this file's host log; its errno agrees on all 300,000 arguments. The system libSceLibcInternal is unmeasured.
  • Not run: macOS, bare-metal Linux, a sanitizer build, floating-point exceptions and errno (compared with nothing), builds with FMA contraction (-mfma, -march=native), other glibc versions.

Overlap

No open PR changes asinh_nid_postfix, and asinh has no other open or merged PR (#1718 was closed; #3190 only exported it). 140 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 b7105f7285): 113 merge without a conflict and 27 conflict, 25 of which already conflict with main on their own. The other one is #10002 (asinhf), whose changed line is directly below asinh_nid_postfix; #11556 (sinh) has been merged, and this branch merged main after it. From 2 up, asinh calls this file's log_nid_postfix, which is still the host's log; #10245 (log) changes only that function's body, 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 (#11556 is merged)
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5029596811 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11737). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-27T06:24:44Z. --- ### What `asinh` returned the host `libm`'s result. It now follows the branches and thresholds of FreeBSD 11.4's [`s_asinh.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_asinh.c): below 2^-28 it returns `x`; up to `|x| = 2` it uses this file's msun `log1p`; from there to 2^28 it uses `log(2|x| + 1/(sqrt(x*x+1)+|x|))` and above 2^28 `log(|x|) + ln2`, with this file's `log`, which is still the host's. It sets no `errno`, as msun does not. The host differs from msun in the last bit for many arguments: `asinh(0x3e2fffffffffffff)` is `0x3e2fffffffffffff` in a local build of msun and `0x3e2ffffffffffffe` with the Windows UCRT, and `asinh(0x3fc858aceb437920)` is `0x3fc833b16e1536cf` in msun and `0x3fc833b16e1536ce` with glibc 2.39. Of 2,000,000 random arguments in [0, 2] (both signs) the UCRT's `asinh` differs from msun on 319,633 and glibc's on 1,212, and this implementation on none (0 as well in [3.8e-9, 2] log-uniformly, 0 of 2,000,000 below 2^-28 on both hosts). Bits are identical to msun up to `|x| = 2`. Above 2 they depend on the host `log` until it follows msun: of 2,000,000 log-uniform random arguments in (2, 2^28], 38,719 differ with the UCRT's `log` and 38,926 with glibc's (for the host's `asinh` the numbers are 131,924 and 38,926), and none differ above 2^28 on either host, which is how those two `log` implementations happen to round, not a property of this code. The entry in `docs/dev/debt/libc.md` says so. The test `guest_math` gets `CheckAsinhMsunBits` (32 cases, both signs, with msun's bits as the expected values: signed zero, the smallest denormal, both sides of the 2^-28 cutoff, 0.5, 1, both sides of 2, 12 arguments below 2 where both the UCRT and glibc differ from msun, two just above 2 and three just above 2^28 where the neighbouring branch's formula would change the last bit; the latter three depend on the host `log` and match msun with the UCRT's and with glibc 2.39's) and `CheckAsinhLargeArguments` (14 arguments above 2 in both signs, accepted within 4 ulps of msun's bits because they depend on the host `log`, plus the infinities and a NaN). 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` `b7105f7285` (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 asinh failed for 3e2fffffffffffff: got 3e2ffffffffffffe, expected 3e2fffffffffffff". - Eight deliberate defects in `asinh_nid_postfix`, each making `guest_math` fail on Windows with the failing argument in the message: the tiny-argument return removed, the 2^-28 cutoff moved down by one, the cutoff at 2 moved down by one, the cutoff at 2^28 moved down by one, the `+ ln2` dropped from the large branch, the factor 2 dropped from the middle branch, the `1 +` dropped from the `log1p` branch, and the sign dropped. - 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`), same base: the same arms give the same results, `guest_math` passes 3 of 3 runs, and `guest_math_exact` and `guest_dinkumware_internals` pass. - The run against `main`'s `Math.cpp` and the eight deliberate defects were measured on an earlier rebase of these two commits (`main` `9e2e90dc00`; `git range-diff` shows `=` for both commits, and `Math.cpp` and `GuestMath.cpp` are byte-identical to this head's); the three passing runs and the other two tests were repeated on this head, on both systems. - 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 random arguments with the `asinh` code taken from `Math.cpp`; that measures these hosts against msun. A differential run by others against the `libc.prx` of a title (300,000 arguments, [comment](https://github.com/boykopovar/AnyPS5/pull/11737#issuecomment-6446570900), head `4902c38569`) finds every argument up to 2 in magnitude bit-identical and 2,313 differences of one ulp above 2, which goes through this file's host `log`; its `errno` agrees on all 300,000 arguments. The system `libSceLibcInternal` is unmeasured. - Not run: macOS, bare-metal Linux, a sanitizer build, floating-point exceptions and `errno` (compared with nothing), builds with FMA contraction (`-mfma`, `-march=native`), other glibc versions. ### Overlap No open PR changes `asinh_nid_postfix`, and `asinh` has no other open or merged PR (#1718 was closed; #3190 only exported it). 140 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` `b7105f7285`): 113 merge without a conflict and 27 conflict, 25 of which already conflict with `main` on their own. The other one is #10002 (`asinhf`), whose changed line is directly below `asinh_nid_postfix`; #11556 (`sinh`) has been merged, and this branch merged `main` after it. From 2 up, `asinh` calls this file's `log_nid_postfix`, which is still the host's `log`; #10245 (`log`) changes only that function's body, 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 (#11556 is merged) - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes AI-assisted (Claude Code).
MarcZX10 commented 2026-10-27 06:29:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-27T06:29:30Z.

Review: DISMISSED


The numerical work is unusually well documented, and the branch structure and thresholds match FreeBSD 11.4 s_asinh.c (including its small-input return, 2 and 2^28 boundaries, formulas, and sign handling). I found two repository/process blockers before approval:

  1. The implementation is a close adaptation of s_asinh.c, whose Sun Microsystems permission explicitly requires retaining its notice: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_asinh.c . The changed code does not preserve that notice or identify how it is retained. Please add the required attribution/license text in a way compatible with this repository's conventions, or clarify that the code was independently written from the mathematical description and not adapted from that implementation.
  2. The PR's overlap report identifies open conflicts with #10002 (asinhf adjacent to this implementation) and #11556 (the adjacent GuestMath.cpp test insertion), but the checklist says Depends on: none and the report says only that whichever lands second will rebase. CONTRIBUTING.md requires the authors to agree which goes first and list it as a dependency. Please coordinate with both authors and update the checklist accordingly.

I compared the implementation against the cited FreeBSD source and reviewed the tests and debt entry. I did not run guest_math locally: this workspace's Linux configure stops because the installed system Vulkan-Headers lack the required VK_KHR_maintenance8; the product CI jobs are skipped on this PR. Current hosted conventions and Linux relinker checks pass; the Windows and macOS relinker jobs were still pending at the latest queue refresh.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5663078958 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11737#pullrequestreview-5663078958). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-27T06:29:30Z. **Review:** DISMISSED --- The numerical work is unusually well documented, and the branch structure and thresholds match FreeBSD 11.4 `s_asinh.c` (including its small-input return, `2` and `2^28` boundaries, formulas, and sign handling). I found two repository/process blockers before approval: 1. The implementation is a close adaptation of `s_asinh.c`, whose Sun Microsystems permission explicitly requires retaining its notice: https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_asinh.c . The changed code does not preserve that notice or identify how it is retained. Please add the required attribution/license text in a way compatible with this repository's conventions, or clarify that the code was independently written from the mathematical description and not adapted from that implementation. 2. The PR's overlap report identifies open conflicts with #10002 (`asinhf` adjacent to this implementation) and #11556 (the adjacent `GuestMath.cpp` test insertion), but the checklist says `Depends on: none` and the report says only that whichever lands second will rebase. `CONTRIBUTING.md` requires the authors to agree which goes first and list it as a dependency. Please coordinate with both authors and update the checklist accordingly. I compared the implementation against the cited FreeBSD source and reviewed the tests and debt entry. I did not run `guest_math` locally: this workspace's Linux configure stops because the installed system Vulkan-Headers lack the required `VK_KHR_maintenance8`; the product CI jobs are skipped on this PR. Current hosted conventions and Linux relinker checks pass; the Windows and macOS relinker jobs were still pending at the latest queue refresh.
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!11737
No description provided.