feat(libSceLibcInternal): implement the msun trigonometric kernels #13778

Closed
croumegous wants to merge 1 commit from upstream-pr/13778 into main
croumegous commented 2026-10-31 05:48:09 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by croumegous on 2026-10-31T05:48:09Z.


What

libSceLibcInternal exports FreeBSD msun's four trigonometric kernels, and all four were parameterless placeholders that threw through NotImplemented_nid_no_patch:

Name NID Was
__kernel_cos -m7FIvSBbMM libSceLibcInternalInventory_263_nid_no_patch
__kernel_cosdf 7ruwcMCJVGI libSceLibcInternalInventory_264_nid_no_patch
__kernel_sindf 2Lvc7KWtErs libSceLibcInternalInventory_265_nid_no_patch
__kernel_sin zpy7LnTL5p0 sceLibcInternalUnknown6_109_zpy7LnTL5p0_nid_no_patch

They are now the FreeBSD 11.4 sources (k_cos.c, k_sin.c, k_cosf.c, k_sinf.c) with their real prototypes, in TrigKernels.cpp, exported as <name>_nid_postfix. The four placeholders, their header declarations and their inventory rows are removed, so each NID has one definition. The libc sinf/cosf keep their own private copies (TrigfKernelSin/TrigfKernelCos), unchanged.

These are what a FreeBSD-derived libm's sin, cos, tan, sinf and cosf call after range reduction; a title or bundled module importing them from libSceLibcInternal stopped on the first call.

The debt entry states what is unverified: the NIDs hash from these names, but the console module was not read, so that it runs this code with these signatures is an assumption (the module is FreeBSD-derived, and libc's sinf/cosf here already are msun's k_sinf.c/k_cosf.c).

Tested

  • New host-only ctest guest_trig_kernels (core/libs/tests/GuestTrigKernels.cpp, built with TrigKernels.cpp): 9 inputs from 2^-30 to π/4, both signs, with and without a tail y. All five results (__kernel_cos, __kernel_sin with iy 0 and 1, __kernel_cosdf, __kernel_sindf) are compared bit for bit with the unmodified FreeBSD 11.4 sources compiled by GCC for x86-64 (vectors generated from them; identical at -O0 and -O2). It also checks them against host cos/sin within 2^-52, and the signed zeros FreeBSD returns (__kernel_sin(-0, 0, 0) is +0; with iy = 1 it is -0).
  • Built and run as a host executable with GCC 12 at -O0, -O2 and -O3 -march=x86-64-v3: passes. Negative control: changing one __kernel_cos coefficient fails row 4.
  • compute_nid (tools/nid_names.py) gives exactly the four NIDs above for the four names.
  • g++ -fsyntax-only -Wall -Wextra on TrigKernels.cpp, InventoryExports.cpp, InventoryExports6.cpp: clean.
  • python3 tools/progress.py --module libSceLibcInternal: 73/652 → 77/652.
  • python3 tools/check_conventions.py --base origin/main: passed.
  • python3 tools/pr_overlap.py --branch feat/libcinternal-trig-kernels: no shared exports or new files with any of the 1,580 open PRs. It exits 2 because PRs 5021, 11750 and 11751 conflict with main and could not be merged: I compared their diffs by hand, and none of them contains these names, NIDs or TrigKernels. Its first run also flagged a textual conflict with #12924 and #13724 on the same debt line; the entry was moved, and both now merge cleanly with this branch
  • Not built: the full libSceLibcInternal shared library and its other tests (the full tree with submodules does not fit this machine), Windows, macOS. Not run on a PS5 or with a title.

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 (only the upstream licence notice, as in libc/src/Math.cpp)
  • 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 (n/a: four short msun functions ported with their notice, as the existing libc math is)
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes (Hermes agent on the submitter's behalf; test vectors generated from the FreeBSD sources, not by the model)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5094982679 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13778). Originally posted by [croumegous](https://github.com/croumegous) on 2026-10-31T05:48:09Z. --- ### What `libSceLibcInternal` exports FreeBSD msun's four trigonometric kernels, and all four were parameterless placeholders that threw through `NotImplemented_nid_no_patch`: | Name | NID | Was | | --- | --- | --- | | `__kernel_cos` | `-m7FIvSBbMM` | `libSceLibcInternalInventory_263_nid_no_patch` | | `__kernel_cosdf` | `7ruwcMCJVGI` | `libSceLibcInternalInventory_264_nid_no_patch` | | `__kernel_sindf` | `2Lvc7KWtErs` | `libSceLibcInternalInventory_265_nid_no_patch` | | `__kernel_sin` | `zpy7LnTL5p0` | `sceLibcInternalUnknown6_109_zpy7LnTL5p0_nid_no_patch` | They are now the FreeBSD 11.4 sources ([`k_cos.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/k_cos.c), [`k_sin.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/k_sin.c), [`k_cosf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/k_cosf.c), [`k_sinf.c`](https://github.com/freebsd/freebsd-src/blob/release/11.4.0/lib/msun/src/k_sinf.c)) with their real prototypes, in `TrigKernels.cpp`, exported as `<name>_nid_postfix`. The four placeholders, their header declarations and their inventory rows are removed, so each NID has one definition. The libc `sinf`/`cosf` keep their own private copies (`TrigfKernelSin`/`TrigfKernelCos`), unchanged. These are what a FreeBSD-derived libm's `sin`, `cos`, `tan`, `sinf` and `cosf` call after range reduction; a title or bundled module importing them from `libSceLibcInternal` stopped on the first call. The debt entry states what is unverified: the NIDs hash from these names, but the console module was not read, so that it runs this code with these signatures is an assumption (the module is FreeBSD-derived, and libc's `sinf`/`cosf` here already are msun's `k_sinf.c`/`k_cosf.c`). ### Tested - New host-only ctest `guest_trig_kernels` (`core/libs/tests/GuestTrigKernels.cpp`, built with `TrigKernels.cpp`): 9 inputs from `2^-30` to `π/4`, both signs, with and without a tail `y`. All five results (`__kernel_cos`, `__kernel_sin` with `iy` 0 and 1, `__kernel_cosdf`, `__kernel_sindf`) are compared **bit for bit** with the unmodified FreeBSD 11.4 sources compiled by GCC for x86-64 (vectors generated from them; identical at `-O0` and `-O2`). It also checks them against host `cos`/`sin` within `2^-52`, and the signed zeros FreeBSD returns (`__kernel_sin(-0, 0, 0)` is `+0`; with `iy = 1` it is `-0`). - Built and run as a host executable with GCC 12 at `-O0`, `-O2` and `-O3 -march=x86-64-v3`: passes. Negative control: changing one `__kernel_cos` coefficient fails row 4. - `compute_nid` (`tools/nid_names.py`) gives exactly the four NIDs above for the four names. - `g++ -fsyntax-only -Wall -Wextra` on `TrigKernels.cpp`, `InventoryExports.cpp`, `InventoryExports6.cpp`: clean. - `python3 tools/progress.py --module libSceLibcInternal`: 73/652 → 77/652. - `python3 tools/check_conventions.py --base origin/main`: passed. - `python3 tools/pr_overlap.py --branch feat/libcinternal-trig-kernels`: no shared exports or new files with any of the 1,580 open PRs. It exits 2 because PRs 5021, 11750 and 11751 conflict with `main` and could not be merged: I compared their diffs by hand, and none of them contains these names, NIDs or `TrigKernels`. Its first run also flagged a textual conflict with #12924 and #13724 on the same debt line; the entry was moved, and both now merge cleanly with this branch - Not built: the full `libSceLibcInternal` shared library and its other tests (the full tree with submodules does not fit this machine), Windows, macOS. Not run on a PS5 or with a title. ### 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) (only the upstream licence notice, as in `libc/src/Math.cpp`) - [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 (n/a: four short msun functions ported with their notice, as the existing libc math is) - [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 (Hermes agent on the submitter's behalf; test vectors generated from the FreeBSD sources, not by the model)
jotitadev commented 2026-10-31 09:36:44 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by jotitadev on 2026-10-31T09:36:44Z.

Review: COMMENTED


Linux run of head f66ce2078, merged (--no-commit) onto main 809c7fa2df, in the full tree (not a standalone executable). Ubuntu 26.04 (kernel 7.0), GCC 15.2.0, CMake 4.2.3, Ninja, Release, BUILD_TESTING=ON, i7-8700.

  • guest_trig_kernels builds through the libSceLibcInternal CMake block and passes 3/3.
  • Full build ok, libSceLibcInternal included. public_headers: 65/65 (the PR touches Api.hpp and InventoryExports.hpp). Related subset guest_math|guest_float|guest_floating|libc_internal|LibcInternal|guest_trig|guest_libc: 16/16.
  • Red, keeping the test: changing C2 in __kernel_cos from e-03 to e-02 makes it abort with failed: __kernel_cos (row 2) 3/3; a throw at the top of each of the five exports also aborts it 3/3.
  • main + #13778 + #13782 + #13835 (the three open libSceLibcInternal PRs; #13778/#13782 share CMakeLists.txt) merge cleanly; full build ok, the same subset 17/17 three times, public_headers 65/65.

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

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5722199043 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13778#pullrequestreview-5722199043). Originally posted by [jotitadev](https://github.com/jotitadev) on 2026-10-31T09:36:44Z. **Review:** COMMENTED --- Linux run of head `f66ce2078`, merged (`--no-commit`) onto `main` `809c7fa2df`, in the full tree (not a standalone executable). Ubuntu 26.04 (kernel 7.0), GCC 15.2.0, CMake 4.2.3, Ninja, Release, `BUILD_TESTING=ON`, i7-8700. - `guest_trig_kernels` builds through the `libSceLibcInternal` CMake block and passes 3/3. - Full build ok, `libSceLibcInternal` included. `public_headers`: 65/65 (the PR touches `Api.hpp` and `InventoryExports.hpp`). Related subset `guest_math|guest_float|guest_floating|libc_internal|LibcInternal|guest_trig|guest_libc`: 16/16. - Red, keeping the test: changing `C2` in `__kernel_cos` from `e-03` to `e-02` makes it abort with `failed: __kernel_cos (row 2)` 3/3; a `throw` at the top of each of the five exports also aborts it 3/3. - `main` + #13778 + #13782 + #13835 (the three open `libSceLibcInternal` PRs; #13778/#13782 share `CMakeLists.txt`) merge cleanly; full build ok, the same subset 17/17 three times, `public_headers` 65/65. Written with AI assistance (Claude Code). I ran the builds and tests above on my machine.
MarcZX10 commented 2026-11-20 06:43:33 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-20T06:43:33Z.

Review: COMMENTED


Reviewed — no blocking findings

Nothing blocks this change; the implementation follows the stated FreeBSD kernels and adds host tests, while console imports and signatures remain unverified.

What I ran

Reviewed head f66ce2078ec7044c30f57d7d925c5413f0bc6ad9 using the prepared diff and repo_read.py to inspect changed code at the pinned head. I did not run build or test commands.

Results

Check Result Note
Source review passed Changed code reviewed
Build and tests not run No commands run
Console behavior not run No console or title run

Findings

Blocking

None.

Non-blocking

None.

Not verified

Console module imports and the assumed signatures/NID-to-function mapping were not confirmed. No build or tests were run by this review; the reported host test results are author-reported. The reviewed files were CMakeLists.txt, InventoryExports.cpp, InventoryExports6.cpp, TrigKernels.cpp, include/Api.hpp, include/InventoryExports.hpp, GuestTrigKernels.cpp, and docs/dev/debt/libSceLibcInternal.md. files.json was not available in the reader, so excluded files and the complete reviewed set could not be confirmed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5988576141 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13778#pullrequestreview-5988576141). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-20T06:43:33Z. **Review:** COMMENTED --- # Reviewed — no blocking findings Nothing blocks this change; the implementation follows the stated FreeBSD kernels and adds host tests, while console imports and signatures remain unverified. ## What I ran Reviewed head `f66ce2078ec7044c30f57d7d925c5413f0bc6ad9` using the prepared diff and `repo_read.py` to inspect changed code at the pinned head. I did not run build or test commands. ## Results | Check | Result | Note | |---|---|---| | Source review | passed | Changed code reviewed | | Build and tests | not run | No commands run | | Console behavior | not run | No console or title run | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified Console module imports and the assumed signatures/NID-to-function mapping were not confirmed. No build or tests were run by this review; the reported host test results are author-reported. The reviewed files were `CMakeLists.txt`, `InventoryExports.cpp`, `InventoryExports6.cpp`, `TrigKernels.cpp`, `include/Api.hpp`, `include/InventoryExports.hpp`, `GuestTrigKernels.cpp`, and `docs/dev/debt/libSceLibcInternal.md`. `files.json` was not available in the reader, so excluded files and the complete `reviewed` set could not be confirmed. ## To get this to a good state Nothing is required.
ligmaSec commented 2026-12-03 13:16:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T13:16:48Z.

Review: COMMENTED


Checked the branch sources and built the focused tests against the existing Linux runtime. GuestTrigKernels passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6170531545 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/13778#pullrequestreview-6170531545). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T13:16:48Z. **Review:** COMMENTED --- Checked the branch sources and built the focused tests against the existing Linux runtime. `GuestTrigKernels` passed. AI-assisted: yes.

Pull request closed

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!13778
No description provided.