fix(libc): compute sincos with FreeBSD msun's algorithm #16740

Merged
AlexWhitehouse merged 3 commits from upstream-pr/16740 into main 2026-11-11 06:02:19 +00:00
AlexWhitehouse commented 2026-11-07 14:16:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by AlexWhitehouse on 2026-11-07T14:16:45Z.


What

libc sincos (NID jMB7EFyu30Y) returned the host library's sin(x) and cos(x) (glibc on Linux, the UCRT on Windows). The console's libc uses FreeBSD msun, and this file's own sin, cos, tan, sinf, cosf, tanf and sincosf are already msun ports that match titles' libc.prx. On ordinary inputs the host result can be 1 ulp off msun. For example, for 0xbff38db77b728354 glibc's cosine is 0x3fd5ddfc9f502f3a and msun's is 0x3fd5ddfc9f502f39; for 0x3ff3cdd6b716682f the sine is …f80b against msun's …f80c.

This PR ports FreeBSD release/11.4.0 msun's s_sincos.c into core/libs/prx/libc/src/Math.cpp, with its notice copied byte for byte. It reuses the HighWord and RemPio2 argument reduction and the KernelSin and KernelCos kernels that sin and cos already use, which are the 11.4 revisions their debt entry cites. Upstream's k_sincos.h evaluates the same expressions as k_sin.c and k_cos.c (its y / 2 and z / 2 equal 0.5 * y and 0.5 * z exactly), so it is not copied. Upstream's tiny-argument cutoff and x - x NaN/Inf results are kept.

errno: upstream msun sets none, but titles' libc.prx sets EDOM for these functions. This file's sin and cos set it when the argument's high word is 0x7ff00000 or 0xfff00000 (the infinities and the NaNs sharing that word), and sincosf sets it for an infinite argument, both measured against titles' copies. sincos now follows sin and cos, so it keeps the EDOM that main (glibc) gave for infinities. Other NaNs leave errno alone, as with sin and cos. The libc.prx bundled with PPSA07923 and PPSA05916 gives sincos the same bits and errno as its own sin and cos on 300,000 inputs each, as a reviewer measured (https://github.com/boykopovar/AnyPS5/pull/16740#issuecomment-6718722071); the debt entry cites it.

Tested

  • guest_math_elementary (core/libs/tests/GuestMathElementary.cpp) gains 88 exact-bit sincos cases, each with its expected errno, plus 10,000 sampled finite inputs. Every existing assertion is kept.
    • The exact cases cover the inputs where glibc and msun differ, ±0 and the tiny-argument threshold, each reduction path (|x| < π/4, the medium reduction, and the large k_rem_pio2 reduction up to ±DBL_MAX), infinities and NaNs.
    • Expected values come from upstream msun compiled locally with -ffp-contract=off.
    • The sampled inputs check that both outputs equal this file's sin and cos bit for bit.
  • Linux x86-64 (Ubuntu 26.04, GCC 15, glibc 2.43, Release): with main's Math.cpp, guest_math_elementary fails with sincos(bff38db77b728354): got bfee12fe51d52ed4 3fd5ddfc9f502f3a, expected bfee12fe51d52ed4 3fd5ddfc9f502f39. With this PR (5c27ee6caa), guest_math, guest_math_elementary and guest_math_exact pass 3/3.
  • After review (3a43c7f59b, reusing KernelSin/KernelCos): on Linux, guest_math, guest_math_elementary and guest_math_exact pass with the same 88 exact cases and 10,000 samples. On Windows 11 (WinLibs GCC 15.2.0 UCRT, Release), a reviewer's run merged onto main 6b733c4ef2 passes the same three, 3 of 3 (https://github.com/boykopovar/AnyPS5/pull/16740#pullrequestreview-5815124572).
  • Windows 11 (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release, -ffp-contract=off): the same three pass at 5c27ee6caa, with no warnings. With main's Math.cpp, guest_math_elementary fails on the same input with the same message.
  • Mutation checks:
    • swapping the sine and cosine outputs in quadrant 1 fails the new test (sincos(3ff3cdd6b716682f): got 3fd4ec422e520ddc 3fee3dddabdaf80c, expected 3fee3dddabdaf80c 3fd4ec422e520ddc, at 3a43c7f59b);
    • removing the EDOM assignment fails its errno check.
  • python3 tools/check_conventions.py --base origin/main: all conventions checks passed. git diff --check flags only whitespace inside the copied upstream notices.
  • python3 tools/pr_overlap.py --branch fix/libc-sincos-msun: no open PR changes sincos. #13778 (msun kernels in libSceLibcInternal) does not touch this wrapper.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5202455358 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16740). Originally posted by [AlexWhitehouse](https://github.com/AlexWhitehouse) on 2026-11-07T14:16:45Z. --- ### What libc `sincos` (NID `jMB7EFyu30Y`) returned the host library's `sin(x)` and `cos(x)` (glibc on Linux, the UCRT on Windows). The console's libc uses FreeBSD msun, and this file's own `sin`, `cos`, `tan`, `sinf`, `cosf`, `tanf` and `sincosf` are already msun ports that match titles' `libc.prx`. On ordinary inputs the host result can be 1 ulp off msun. For example, for `0xbff38db77b728354` glibc's cosine is `0x3fd5ddfc9f502f3a` and msun's is `0x3fd5ddfc9f502f39`; for `0x3ff3cdd6b716682f` the sine is `…f80b` against msun's `…f80c`. This PR ports FreeBSD release/11.4.0 msun's [`s_sincos.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_sincos.c) into `core/libs/prx/libc/src/Math.cpp`, with its notice copied byte for byte. It reuses the `HighWord` and `RemPio2` argument reduction and the `KernelSin` and `KernelCos` kernels that `sin` and `cos` already use, which are the 11.4 revisions their debt entry cites. Upstream's [`k_sincos.h`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/k_sincos.h) evaluates the same expressions as `k_sin.c` and `k_cos.c` (its `y / 2` and `z / 2` equal `0.5 * y` and `0.5 * z` exactly), so it is not copied. Upstream's tiny-argument cutoff and `x - x` NaN/Inf results are kept. **`errno`:** upstream msun sets none, but titles' `libc.prx` sets `EDOM` for these functions. This file's `sin` and `cos` set it when the argument's high word is `0x7ff00000` or `0xfff00000` (the infinities and the NaNs sharing that word), and `sincosf` sets it for an infinite argument, both measured against titles' copies. `sincos` now follows `sin` and `cos`, so it keeps the `EDOM` that `main` (glibc) gave for infinities. Other NaNs leave `errno` alone, as with `sin` and `cos`. The `libc.prx` bundled with PPSA07923 and PPSA05916 gives `sincos` the same bits and `errno` as its own `sin` and `cos` on 300,000 inputs each, as a reviewer measured (https://github.com/boykopovar/AnyPS5/pull/16740#issuecomment-6718722071); the debt entry cites it. ### Tested - **`guest_math_elementary`** (`core/libs/tests/GuestMathElementary.cpp`) gains 88 exact-bit `sincos` cases, each with its expected `errno`, plus 10,000 sampled finite inputs. Every existing assertion is kept. - **The exact cases** cover the inputs where glibc and msun differ, ±0 and the tiny-argument threshold, each reduction path (`|x| < π/4`, the medium reduction, and the large `k_rem_pio2` reduction up to ±`DBL_MAX`), infinities and NaNs. - **Expected values** come from upstream msun compiled locally with `-ffp-contract=off`. - **The sampled inputs** check that both outputs equal this file's `sin` and `cos` bit for bit. - **Linux x86-64** (Ubuntu 26.04, GCC 15, glibc 2.43, Release): with `main`'s `Math.cpp`, `guest_math_elementary` fails with `sincos(bff38db77b728354): got bfee12fe51d52ed4 3fd5ddfc9f502f3a, expected bfee12fe51d52ed4 3fd5ddfc9f502f39`. With this PR (5c27ee6caa), `guest_math`, `guest_math_elementary` and `guest_math_exact` pass 3/3. - **After review (3a43c7f59b, reusing `KernelSin`/`KernelCos`):** on Linux, `guest_math`, `guest_math_elementary` and `guest_math_exact` pass with the same 88 exact cases and 10,000 samples. On Windows 11 (WinLibs GCC 15.2.0 UCRT, Release), a reviewer's run merged onto `main` 6b733c4ef2 passes the same three, 3 of 3 (https://github.com/boykopovar/AnyPS5/pull/16740#pullrequestreview-5815124572). - **Windows 11** (build 26300, WinLibs MinGW-w64 GCC 15.2.0 UCRT, Release, `-ffp-contract=off`): the same three pass at 5c27ee6caa, with no warnings. With `main`'s `Math.cpp`, `guest_math_elementary` fails on the same input with the same message. - **Mutation checks:** - swapping the sine and cosine outputs in quadrant 1 fails the new test (`sincos(3ff3cdd6b716682f): got 3fd4ec422e520ddc 3fee3dddabdaf80c, expected 3fee3dddabdaf80c 3fd4ec422e520ddc`, at 3a43c7f59b); - removing the `EDOM` assignment fails its `errno` check. - `python3 tools/check_conventions.py --base origin/main`: all conventions checks passed. `git diff --check` flags only whitespace inside the copied upstream notices. - **`python3 tools/pr_overlap.py --branch fix/libc-sincos-msun`:** no open PR changes `sincos`. #13778 (msun kernels in libSceLibcInternal) does not touch this wrapper. - Trial merges of 61 PRs touching `Math.cpp`, `GuestMathElementary.cpp` or `libc.md` add no conflict. That includes the open msun ports #10002, #10007, #10113, #10274, #10321, #10226, #11262, #11266, #11634, #16438, #8417, #7544, #12053, #10512, #12947 and #15802. - The one exception is #14212 (blank lines between debt entries), which gains a conflict at the new debt entry. ### 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] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes 🤖 Generated with [Claude Code](https://claude.com/claude-code)
gabichuuuuu commented 2026-11-07 16:06:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T16:06:19Z.

Review: COMMENTED


Read against FreeBSD release/11.4.0 s_sincos.c and k_sincos.h. sincos_nid_postfix matches statement by statement: the 0x3e400000 threshold, (int)x == 0, x - x for Inf/NaN, and the four quadrants with the swapped pointers.

As an independent check, I ported msun's sin/cos path to Python, without FMA, as -ffp-contract=off builds libc. All 66 small- and medium-range cases in the test match bit for bit. I did not port k_rem_pio2, so the 22 large-argument cases are not checked independently. The description's example also reproduces: the host gives cos(0xbff38db77b728354) = 0x3fd5ddfc9f502f3a, and msun gives …39.

Two small points, both optional:

  • KernelSincos (Math.cpp:2805) repeats KernelSin and KernelCos with the same coefficients. Its only differences are y / 2 and z / 2 in place of 0.5 * y and 0.5 * z, which are exact. *sinp = KernelSin(x, y, iy); *cosp = KernelCos(x, y); would keep the single argument reduction without the copy.
  • The two new license blocks (before lines 2795 and 2834) have no Adapted from FreeBSD release/11.4.0 …: <URL> line, which the file's other msun notices carry.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5813479618 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16740#pullrequestreview-5813479618). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T16:06:19Z. **Review:** COMMENTED --- Read against FreeBSD release/11.4.0 `s_sincos.c` and `k_sincos.h`. `sincos_nid_postfix` matches statement by statement: the `0x3e400000` threshold, `(int)x == 0`, `x - x` for Inf/NaN, and the four quadrants with the swapped pointers. As an independent check, I ported msun's `sin`/`cos` path to Python, without FMA, as `-ffp-contract=off` builds libc. All 66 small- and medium-range cases in the test match bit for bit. I did not port `k_rem_pio2`, so the 22 large-argument cases are not checked independently. The description's example also reproduces: the host gives `cos(0xbff38db77b728354) = 0x3fd5ddfc9f502f3a`, and msun gives `…39`. Two small points, both optional: - `KernelSincos` (`Math.cpp:2805`) repeats `KernelSin` and `KernelCos` with the same coefficients. Its only differences are `y / 2` and `z / 2` in place of `0.5 * y` and `0.5 * z`, which are exact. `*sinp = KernelSin(x, y, iy); *cosp = KernelCos(x, y);` would keep the single argument reduction without the copy. - The two new license blocks (before lines 2795 and 2834) have no `Adapted from FreeBSD release/11.4.0 …: <URL>` line, which the file's other msun notices carry. AI-assisted: yes.
gabichuuuuu commented 2026-11-07 21:39:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by gabichuuuuu on 2026-11-07T21:39:08Z.

Review: COMMENTED


The Windows rerun for 3a43c7f59b. Merged onto main 6b733c4ef2 (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release, ctest -R '^guest_math': guest_math, guest_math_elementary and guest_math_exact pass 3 of 3, plus a verbose guest_math_elementary run, also passing. main passes the same set.

I also checked the refactor against s_sincos.c quadrant by quadrant: case 1 gives sin = KernelCos, cos = -KernelSin, case 2 negates both, and case 3 gives sin = -KernelCos, cos = KernelSin, the same as the swapped-pointer calls with the negations applied after. The small-argument path keeps iy = 0. Nothing to add.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5815124572 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/16740#pullrequestreview-5815124572). Originally posted by [gabichuuuuu](https://github.com/gabichuuuuu) on 2026-11-07T21:39:08Z. **Review:** COMMENTED --- The Windows rerun for `3a43c7f59b`. Merged onto `main` `6b733c4ef2` (clean). Windows 11 x64, WinLibs GCC 15.2.0 r7 UCRT, Release, `ctest -R '^guest_math'`: `guest_math`, `guest_math_elementary` and `guest_math_exact` pass 3 of 3, plus a verbose `guest_math_elementary` run, also passing. `main` passes the same set. I also checked the refactor against `s_sincos.c` quadrant by quadrant: case 1 gives `sin = KernelCos`, `cos = -KernelSin`, case 2 negates both, and case 3 gives `sin = -KernelCos`, `cos = KernelSin`, the same as the swapped-pointer calls with the negations applied after. The small-argument path keeps `iy = 0`. Nothing to add. 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!16740
No description provided.