feat(libc): compute sinl, cosl and tanl with FreeBSD msun's ld80 algorithm #18513

Merged
AlfaRojo merged 10 commits from upstream-pr/18513 into main 2026-11-18 01:37:31 +00:00
AlfaRojo commented 2026-11-11 19:33:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlfaRojo on 2026-11-11T19:33:15Z.


What

sinl, cosl and tanl returned the host's std::sin, std::cos and std::tan. They now follow FreeBSD msun release/11.4.0: s_sinl.c, s_cosl.c, s_tanl.c with the ld80 k_sinl.c, k_cosl.c, k_tanl.c and e_rem_pio2l.h. The reduction reuses the libc KernelRemPio2 (__kernel_rem_pio2); its pi/2 table was cut to the 66 entries a double needs, so it is now the full 690 entries of msun's ipio2, needed for long double arguments up to the largest finite value. The table moves to core/libs/prx/libc/include/general/RemPio2Table.hpp, which libc and libSceLibcInternal's RemPio2.cpp both include instead of each keeping a copy. The debt entry in docs/dev/debt/libc.md moves the three functions out of the host-math line and records that the console result and errno are unverified.

Tested

Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, based on main 65351c2931.

  • guest_math 3/3 runs pass, as do guest_math_elementary and guest_math_exact. The new check pins ±0 and subnormal arguments, ±infinity (NaN, FE_INVALID, errno untouched), NaN, and 60 inputs (including 1e10, 1e300, 1e4000 and near multiples of pi/2 up to 2^5000) to the bit; every pinned result is within 0.53 ulp of an mpmath 24000-bit reference.
  • Bit for bit against the original msun C sources built with host GCC (k_rem_pio2.c, k_sinl.c, k_cosl.c, k_tanl.c, s_sinl.c, s_cosl.c, s_tanl.c, amd64 constants): 120 million results (40 million arguments, all three functions) over random arguments of every exponent, around pi/4 and the 2^25 pi/2 boundary, and near multiples of pi/2, 0 mismatches. Before the table was extended the harness found 78 mismatches in 3000, all for arguments above 2^1024.
  • Negative control: with Math.cpp from main the new check fails (GuestMath.cpp:3276); with the change it passes.
  • tools/check_conventions.py --base upstream/main passes; tools/check_test_registration.py passes.
  • Overlap (tools/pr_overlap.py): #11262 (atanl) and #11266 (exp2l) edit the same Math.cpp lines and #18110, #18481 the same debt line, so those will need a trivial rebase. #18122 lists sinl, cosl and tanl only as _nid_disambig1 forwards to the existing _nid_no_patch entry points, not an implementation. The comparisons the tool could not complete (PRs conflicting with main: 6295, 7544, 11109 touch Math.cpp; the others only docs/dev/debt/libc.md) were checked for sinl/cosl/tanl/rem_pio2l by search with no hit.
  • Head 0adfbb8749 (merged with upstream/main, which now has #18525 and #19328): guest_math, guest_math_elementary and guest_math_exact pass in 3/3 runs; check_conventions.py --base upstream/main passes.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5265633989 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18513). Originally posted by [AlfaRojo](https://github.com/AlfaRojo) on 2026-11-11T19:33:15Z. --- ### What `sinl`, `cosl` and `tanl` returned the host's `std::sin`, `std::cos` and `std::tan`. They now follow FreeBSD msun release/11.4.0: [`s_sinl.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_sinl.c), `s_cosl.c`, `s_tanl.c` with the ld80 `k_sinl.c`, `k_cosl.c`, `k_tanl.c` and `e_rem_pio2l.h`. The reduction reuses the libc `KernelRemPio2` (`__kernel_rem_pio2`); its pi/2 table was cut to the 66 entries a double needs, so it is now the full 690 entries of msun's `ipio2`, needed for long double arguments up to the largest finite value. The table moves to `core/libs/prx/libc/include/general/RemPio2Table.hpp`, which libc and libSceLibcInternal's `RemPio2.cpp` both include instead of each keeping a copy. The debt entry in `docs/dev/debt/libc.md` moves the three functions out of the host-math line and records that the console result and `errno` are unverified. ### Tested Windows 11 Pro 10.0.26200, WinLibs GCC 15.2.0 r7 UCRT, Ninja, Release, based on main 65351c2931. - `guest_math` 3/3 runs pass, as do `guest_math_elementary` and `guest_math_exact`. The new check pins ±0 and subnormal arguments, ±infinity (NaN, `FE_INVALID`, `errno` untouched), NaN, and 60 inputs (including 1e10, 1e300, 1e4000 and near multiples of pi/2 up to 2^5000) to the bit; every pinned result is within 0.53 ulp of an mpmath 24000-bit reference. - Bit for bit against the original msun C sources built with host GCC (`k_rem_pio2.c`, `k_sinl.c`, `k_cosl.c`, `k_tanl.c`, `s_sinl.c`, `s_cosl.c`, `s_tanl.c`, amd64 constants): 120 million results (40 million arguments, all three functions) over random arguments of every exponent, around pi/4 and the 2^25 pi/2 boundary, and near multiples of pi/2, 0 mismatches. Before the table was extended the harness found 78 mismatches in 3000, all for arguments above 2^1024. - Negative control: with `Math.cpp` from main the new check fails (`GuestMath.cpp:3276`); with the change it passes. - `tools/check_conventions.py --base upstream/main` passes; `tools/check_test_registration.py` passes. - Overlap (`tools/pr_overlap.py`): #11262 (atanl) and #11266 (exp2l) edit the same `Math.cpp` lines and #18110, #18481 the same debt line, so those will need a trivial rebase. #18122 lists `sinl`, `cosl` and `tanl` only as `_nid_disambig1` forwards to the existing `_nid_no_patch` entry points, not an implementation. The comparisons the tool could not complete (PRs conflicting with main: 6295, 7544, 11109 touch `Math.cpp`; the others only `docs/dev/debt/libc.md`) were checked for `sinl`/`cosl`/`tanl`/`rem_pio2l` by search with no hit. - Head `0adfbb8749` (merged with upstream/main, which now has #18525 and #19328): `guest_math`, `guest_math_elementary` and `guest_math_exact` pass in 3/3 runs; `check_conventions.py --base upstream/main` passes. ### 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
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!18513
No description provided.