fix(libc): compute sinf, cosf, tanf and sincosf with FreeBSD msun's algorithms, as titles' libc.prx does #9857

Merged
JJenkx merged 1 commit from upstream-pr/9857 into main 2026-10-24 09:12:12 +00:00
JJenkx commented 2026-10-22 19:09:25 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by JJenkx on 2026-10-22T19:09:25Z.


What

sinf, cosf, tanf and sincosf returned the host's sin, cos and tan, so their last bit depended on the host. On Windows (UCRT), counted over all 2^32 floats against the libc.prx that titles ship:

  • sinf differs on 3,349,410;
  • cosf on 3,382,210;
  • tanf on 303,874,018 (7 %).

For example, tanf(0x3fc00321) is 0x4161edb0 on the console.

The titles' versions are FreeBSD msun's s_sinf.c, s_cosf.c and s_tanf.c. Their sincosf (NID pztV4AF18iI) returns exactly sinf's and cosf's results. This PR ports:

  • the double-precision kernels k_sinf.c, k_cosf.c and k_tanf.c;
  • the n·π/2 octant tables of the three entry points;
  • e_rem_pio2f.c: its medium path (|x| < 2^28·π/2) uses three double operations, and larger arguments go through k_rem_pio2.c;
  • k_rem_pio2.c only for what a float uses: one input word, prec 0, and the first 66 ipio2 digits, of which a float reaches only a few.

All of this goes into Math.cpp as static helpers; sincosf calls the two ports.

errno: only an infinite argument sets EDOM on the console; NaN leaves it alone. The ports do the same.

sinf, cosf and tanf are each imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Because the kernels work in double precision, the FMA-contracted copy of PPSA13934 gives the same bits here, so one port matches every copy checked.

Tested

Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, BUILD_TESTING=ON, main a6fc8a467d.

  • Exhaustive comparison: a scratch loader runs the decrypted libc.prx of PPSA07923, PPSA05916 and PPSA13934 natively and supplies __error.

    • Their sinf, cosf and tanf and this build's give the same bits and errno for all 2^32 arguments: 9 runs, 0 differences.
    • Their sincosf equals the ported sinf/cosf pair for all 2^32 arguments.
  • guest_math: the new CheckTrigfConsoleBits has 21 inputs, each checked through all four functions with the console's results and errno:

    • each octant range and a negative one;
    • medium reduction, the 2^28·π/2 boundary and the k_rem_pio2 range up to ±FLT_MAX;
    • π/4 and π, tiny values, ±0, the infinities (EDOM) and a NaN payload.

    guest_math and the libc tests (8) pass in three runs out of three. With main's Math.cpp and this test, guest_math aborts in a Require.

  • python tools/check_conventions.py --base origin/main and git diff --check pass.

  • Overlap: the branch merges without conflicts with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844, #9850). tools/pr_overlap.py finds one adjacency in Math.cpp with #8417, whose new gamma functions end just above sinf; keeping both resolves it.

  • Independent Linux review: Dom3005's report tested head e601dbd7a merged cleanly onto main fcd455870: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, BUILD_TESTING=ON. The reviewer reports five passing guest_math runs (--repeat until-fail:5). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim.

  • Not tested: macOS, a console, the floating-point exception flags.

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: none; msun's functions are rewritten as static helpers, as #9668, #5036 and the sinhf entry did (this one is larger because of the argument reduction)
  • 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:4972722272 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9857). Originally posted by [JJenkx](https://github.com/JJenkx) on 2026-10-22T19:09:25Z. --- ### What `sinf`, `cosf`, `tanf` and `sincosf` returned the host's `sin`, `cos` and `tan`, so their last bit depended on the host. On Windows (UCRT), counted over all 2^32 floats against the `libc.prx` that titles ship: - `sinf` differs on 3,349,410; - `cosf` on 3,382,210; - `tanf` on 303,874,018 (7 %). For example, `tanf(0x3fc00321)` is `0x4161edb0` on the console. The titles' versions are FreeBSD msun's [`s_sinf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_sinf.c), [`s_cosf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_cosf.c) and [`s_tanf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/s_tanf.c). Their `sincosf` (NID `pztV4AF18iI`) returns exactly `sinf`'s and `cosf`'s results. This PR ports: - the double-precision kernels `k_sinf.c`, `k_cosf.c` and `k_tanf.c`; - the `n·π/2` octant tables of the three entry points; - [`e_rem_pio2f.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/e_rem_pio2f.c): its medium path (`|x| < 2^28·π/2`) uses three double operations, and larger arguments go through [`k_rem_pio2.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/k_rem_pio2.c); - `k_rem_pio2.c` only for what a float uses: one input word, `prec` 0, and the first 66 `ipio2` digits, of which a float reaches only a few. All of this goes into `Math.cpp` as static helpers; `sincosf` calls the two ports. **`errno`:** only an infinite argument sets `EDOM` on the console; NaN leaves it alone. The ports do the same. `sinf`, `cosf` and `tanf` are each imported by 21 of the 22 titles whose relinker registries I audited, for example PPSA01528, PPSA02630 and PPSA09614. Because the kernels work in double precision, the FMA-contracted copy of PPSA13934 gives the same bits here, so one port matches every copy checked. ### Tested Windows 11 Enterprise 10.0.22621, Ryzen 9 5950X, WinLibs GCC 15.2.0 UCRT r7, Ninja, Release, `BUILD_TESTING=ON`, `main` a6fc8a467d. - **Exhaustive comparison:** a scratch loader runs the decrypted `libc.prx` of PPSA07923, PPSA05916 and PPSA13934 natively and supplies `__error`. - Their `sinf`, `cosf` and `tanf` and this build's give the same bits and `errno` for all 2^32 arguments: 9 runs, 0 differences. - Their `sincosf` equals the ported `sinf`/`cosf` pair for all 2^32 arguments. - **`guest_math`:** the new `CheckTrigfConsoleBits` has 21 inputs, each checked through all four functions with the console's results and `errno`: - each octant range and a negative one; - medium reduction, the `2^28·π/2` boundary and the `k_rem_pio2` range up to `±FLT_MAX`; - π/4 and π, tiny values, ±0, the infinities (`EDOM`) and a NaN payload. `guest_math` and the libc tests (8) pass in three runs out of three. With `main`'s `Math.cpp` and this test, `guest_math` aborts in a `Require`. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - **Overlap:** the branch merges without conflicts with my other math PRs (#9771, #9777, #9784, #9839, #9841, #9844, #9850). `tools/pr_overlap.py` finds one adjacency in `Math.cpp` with #8417, whose new gamma functions end just above `sinf`; keeping both resolves it. - **Independent Linux review:** [Dom3005's report](https://github.com/boykopovar/AnyPS5/pull/9857#issuecomment-6353485100) tested head `e601dbd7a` merged cleanly onto `main` `fcd455870`: Ubuntu 26.04 in Docker on Windows, GCC 15, glibc 2.43, WSL2 kernel 6.6, Release, `BUILD_TESTING=ON`. The reviewer reports five passing `guest_math` runs (`--repeat until-fail:5`). Keeping this PR's test while restoring main's changed production sources makes the test fail on Linux as well. This supplies a Linux regression comparison in addition to the Windows evidence above; it is reviewer-supplied evidence for those revisions, not a local rerun or a latest-main claim. - Not tested: macOS, a console, the floating-point exception flags. ### 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: none; msun's functions are rewritten as static helpers, as #9668, #5036 and the `sinhf` entry did (this one is larger because of the argument reduction) - [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-23 03:46:01 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T03:46:01Z.

Review: APPROVED


Reviewed exact head e601dbd7a41f92133e62c300214d63b187c98e3b. The float entry points and reduction/kernel code follow the FreeBSD 11.4 msun routines named in the change; the PR also records exact console-library comparisons for all 2^32 float inputs and errno behavior. The production and test files are unchanged between their tested base and current upstream main; only the debt index changed in that interval.

I locally built the exact PR Math.cpp and test source and ran its isolated CheckTrigfConsoleBits check on Linux; all 21 inputs across sinf, cosf, tanf, and sincosf passed. This used an existing older configured build, not a fresh current-main build. Independent reviewer evidence on this head reports five passing Linux guest_math runs; the author reports repeated Windows tests and exhaustive comparisons. Required hosted checks pass; product jobs are skipped.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5623642908 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9857#pullrequestreview-5623642908). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T03:46:01Z. **Review:** APPROVED --- Reviewed exact head `e601dbd7a41f92133e62c300214d63b187c98e3b`. The float entry points and reduction/kernel code follow the FreeBSD 11.4 msun routines named in the change; the PR also records exact console-library comparisons for all 2^32 float inputs and errno behavior. The production and test files are unchanged between their tested base and current upstream `main`; only the debt index changed in that interval. I locally built the exact PR `Math.cpp` and test source and ran its isolated `CheckTrigfConsoleBits` check on Linux; all 21 inputs across `sinf`, `cosf`, `tanf`, and `sincosf` passed. This used an existing older configured build, not a fresh current-main build. Independent reviewer evidence on this head reports five passing Linux `guest_math` runs; the author reports repeated Windows tests and exhaustive comparisons. Required hosted checks pass; product jobs are skipped.
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!9857
No description provided.