feat(libc): implement the C99 math functions Minecraft Legends imports #1405

Closed
overlordxrz-source wants to merge 2 commits from upstream-pr/1405 into main
overlordxrz-source commented 2026-10-08 02:31:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-08T02:31:56Z.


What

Minecraft Legends (PPSA05510) imports 13 C99 math functions that libc does not export: acosh, asinh, atanh, cosh, sinh, expm1, log1p, ceil, floor, trunc, fabs, sqrt and nan. Their NIDs are in the PPSA05510 import list attached to #268, and tools/nid_names.py resolves them to these names. A title that imports any of them fails at startup with an unresolved import.

libc/src/Math.cpp now exports exactly these 13, each forwarding to the host <cmath> like the existing exports next to them. nan throws on a null tag. Guest errno is not set, since FreeBSD's libm reports errors only through the floating-point exceptions.

Per review, the float forms and the other C99 functions from the first version (erf, tgamma, fma, nextafter, rint, lrint and the rest) are dropped until a title is known to import them.

Related: #1196 adds hypot/hypotf, which PPSA05510 also imports. The two PRs are complementary, but both add to core/libs/tests/GuestMath.cpp, so whichever lands second needs a rebase.

Log

I don't own Minecraft Legends, so I can't paste the line where PPSA05510 stops on main. The evidence is the relinker import list in #268, where these 13 NIDs are among the unresolved libSceLibcInternal imports. If someone with the title can post the FAIL: unresolved ELF import ... line from main, I'll add it here.

Tested

aarch64 Debian 13 host. The full project cannot be built here because it needs x86-64.

  • CheckC99Math in core/libs/tests/GuestMath.cpp, linked against Math.cpp, passes natively against glibc. It checks exact results and edge cases: signed zeros, ceil near 2^52, floor just below 2, domain and pole results, and the quiet-NaN bits of nan(""). Transcendental results are compared with known values to 1e-15.
  • Math.cpp and GuestMath.cpp compile for x86_64-linux-gnu and x86_64-windows-gnu with -Wall -Wextra (Zig 0.17 clang). The only warning is the existing -Wreturn-type-c-linkage from General.hpp.
  • python3 tools/check_conventions.py --base origin/main passes.
  • Not run 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
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed in TechnicalDebt
  • 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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4780981978 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-08T02:31:56Z. --- ### What Minecraft Legends (PPSA05510) imports 13 C99 math functions that `libc` does not export: `acosh`, `asinh`, `atanh`, `cosh`, `sinh`, `expm1`, `log1p`, `ceil`, `floor`, `trunc`, `fabs`, `sqrt` and `nan`. Their NIDs are in the PPSA05510 import list attached to #268, and `tools/nid_names.py` resolves them to these names. A title that imports any of them fails at startup with an unresolved import. `libc/src/Math.cpp` now exports exactly these 13, each forwarding to the host `<cmath>` like the existing exports next to them. `nan` throws on a null tag. Guest `errno` is not set, since FreeBSD's libm reports errors only through the floating-point exceptions. Per review, the float forms and the other C99 functions from the first version (`erf`, `tgamma`, `fma`, `nextafter`, `rint`, `lrint` and the rest) are dropped until a title is known to import them. Related: #1196 adds `hypot`/`hypotf`, which PPSA05510 also imports. The two PRs are complementary, but both add to `core/libs/tests/GuestMath.cpp`, so whichever lands second needs a rebase. ### Log I don't own Minecraft Legends, so I can't paste the line where PPSA05510 stops on `main`. The evidence is the relinker import list in #268, where these 13 NIDs are among the unresolved `libSceLibcInternal` imports. If someone with the title can post the `FAIL: unresolved ELF import ...` line from `main`, I'll add it here. ### Tested aarch64 Debian 13 host. The full project cannot be built here because it needs x86-64. - `CheckC99Math` in `core/libs/tests/GuestMath.cpp`, linked against `Math.cpp`, passes natively against glibc. It checks exact results and edge cases: signed zeros, `ceil` near 2^52, `floor` just below 2, domain and pole results, and the quiet-NaN bits of `nan("")`. Transcendental results are compared with known values to 1e-15. - `Math.cpp` and `GuestMath.cpp` compile for x86_64-linux-gnu and x86_64-windows-gnu with `-Wall -Wextra` (Zig 0.17 clang). The only warning is the existing `-Wreturn-type-c-linkage` from `General.hpp`. - `python3 tools/check_conventions.py --base origin/main` passes. - Not run 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) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [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] AI-assisted: yes
Dextroid17 commented 2026-10-08 03:28:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T03:28:36Z.

Review: COMMENTED


Built it, ran it, and verified the NID mapping with your own tool rather than taking the body's word for it.

Linux (CachyOS, GCC, Release):

cmake --build build --target guest_math_tests --parallel 16
ctest -R ^guest_math$    →  1/1 Passed
ctest -j 16 (full suite) →  410/410 Passed   (28.28 s)

The functions were genuinely missing, which I checked against the base rather than assuming: on pr-1405^ the file has 0 implementations of sinh, asinh, tgamma and log1p, and after this PR it has them (sinh 0 → 2, erf 1 → 3 for the float form). So this fills a real gap rather than re-exporting something the library already had.

The NID mapping round-trips through the project's own hash. I computed the NID for each name with nid_names.py's compute_nid, then asked the oracle to name them back:

sinhf  → 1t1-JoZ0sZQ  → already-implemented sinhf
acosh  → Fk7-KFKZi-8  → already-implemented acosh
expm1  → gqKfOiJaCOo  → already-implemented expm1
tgamma → b7J3q7-UABY  → already-implemented tgamma
erff   → RePA3bDBJqo  → already-implemented erff

Consistent in both directions for every function in the diff, so the names are the real symbol names for those NIDs and not a plausible-looking guess. Worth stating because it is the one part of a libc export that cannot be verified by a passing test.

Not a duplicate of #1196, and the tool shows why. Asking the same oracle about the two functions #1196 covers:

hypot  → YFoOw5GkkK0  → rename-ready hypot
hypotf → iz2shAGFIxc  → rename-ready hypotf

They are still rename-ready — unresolved names with no implementation — while everything this PR touches reports already-implemented. I expected to find overlap when I saw hypotf mentioned in the body, and the status column shows there is none: #1196 is complementary, and both should land. Since you cite #1196's ground in the body, a pointer to it would save the next reader that check.

One practical note for whoever merges second: this PR and #1196 both add to core/libs/tests/GuestMath.cpp, so expect a conflict between them. Whoever lands first, the other will need a rebase — worth saying in both bodies so it is not a surprise.

The forwarding itself is the house pattern used next to it — std::sinh for a float argument selects the float overload, so sinhf returns a float rather than a narrowed double, which is the thing that would be easy to get wrong here.

This is a comment, not an approval — I have no merge rights here.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5450998978 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405#pullrequestreview-5450998978). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T03:28:36Z. **Review:** COMMENTED --- Built it, ran it, and verified the NID mapping with your own tool rather than taking the body's word for it. **Linux (CachyOS, GCC, Release):** ``` cmake --build build --target guest_math_tests --parallel 16 ctest -R ^guest_math$ → 1/1 Passed ctest -j 16 (full suite) → 410/410 Passed (28.28 s) ``` **The functions were genuinely missing, which I checked against the base rather than assuming:** on `pr-1405^` the file has `0` implementations of `sinh`, `asinh`, `tgamma` and `log1p`, and after this PR it has them (`sinh` `0 → 2`, `erf` `1 → 3` for the float form). So this fills a real gap rather than re-exporting something the library already had. **The NID mapping round-trips through the project's own hash.** I computed the NID for each name with `nid_names.py`'s `compute_nid`, then asked the oracle to name them back: ``` sinhf → 1t1-JoZ0sZQ → already-implemented sinhf acosh → Fk7-KFKZi-8 → already-implemented acosh expm1 → gqKfOiJaCOo → already-implemented expm1 tgamma → b7J3q7-UABY → already-implemented tgamma erff → RePA3bDBJqo → already-implemented erff ``` Consistent in both directions for every function in the diff, so the names are the real symbol names for those NIDs and not a plausible-looking guess. Worth stating because it is the one part of a libc export that cannot be verified by a passing test. **Not a duplicate of #1196, and the tool shows why.** Asking the same oracle about the two functions #1196 covers: ``` hypot → YFoOw5GkkK0 → rename-ready hypot hypotf → iz2shAGFIxc → rename-ready hypotf ``` They are still `rename-ready` — unresolved names with no implementation — while everything this PR touches reports `already-implemented`. I expected to find overlap when I saw `hypotf` mentioned in the body, and the status column shows there is none: **#1196 is complementary, and both should land.** Since you cite #1196's ground in the body, a pointer to it would save the next reader that check. **One practical note for whoever merges second:** this PR and #1196 both add to `core/libs/tests/GuestMath.cpp`, so expect a conflict between them. Whoever lands first, the other will need a rebase — worth saying in both bodies so it is not a surprise. The forwarding itself is the house pattern used next to it — `std::sinh` for a `float` argument selects the float overload, so `sinhf` returns a float rather than a narrowed double, which is the thing that would be easy to get wrong here. This is a comment, not an approval — I have no merge rights here. Written with AI assistance.
oneandonlydean commented 2026-10-08 05:12:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T05:12:56Z.


Please keep the functions PPSA05510 imports (acosh, asinh, atanh, cosh, sinh, expm1, log1p, ceil, floor, trunc, fabs, sqrt, nan) and drop the rest until a title reaches them, and add the log line where PPSA05510 stops on main. erf, tgamma, fma, nextafter, rint, lrint and the others have no title named here.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6052802059 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405#issuecomment-6052802059). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T05:12:56Z. --- Please keep the functions PPSA05510 imports (`acosh`, `asinh`, `atanh`, `cosh`, `sinh`, `expm1`, `log1p`, `ceil`, `floor`, `trunc`, `fabs`, `sqrt`, `nan`) and drop the rest until a title reaches them, and add the log line where PPSA05510 stops on main. `erf`, `tgamma`, `fma`, `nextafter`, `rint`, `lrint` and the others have no title named here.
overlordxrz-source commented 2026-10-08 15:42:48 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by overlordxrz-source on 2026-10-08T15:42:48Z.


@oneandonlydean Done: the PR now exports only the 13 functions PPSA05510 imports, double forms only, and the rest are dropped. I don't own Minecraft Legends, so I can't post the line where it stops on main myself. The body points to the PPSA05510 import list in #268 for now, and I'll add the log line if someone with the title can share it.

@Dextroid17 Thanks for checking the NIDs both ways. The body now mentions #1196 and the GuestMath.cpp conflict.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6063572687 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405#issuecomment-6063572687). Originally posted by [overlordxrz-source](https://github.com/overlordxrz-source) on 2026-10-08T15:42:48Z. --- @oneandonlydean Done: the PR now exports only the 13 functions PPSA05510 imports, double forms only, and the rest are dropped. I don't own Minecraft Legends, so I can't post the line where it stops on main myself. The body points to the PPSA05510 import list in #268 for now, and I'll add the log line if someone with the title can share it. @Dextroid17 Thanks for checking the NIDs both ways. The body now mentions #1196 and the GuestMath.cpp conflict.
github-actions[bot] commented 2026-10-08 15:43:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T15:43:56Z.


Progress report (4b005b2 → 7278eea)

📈 System libraries: 84.82% (+0.06%, +13 implemented functions)

✅ 13 implemented
Library Name
libc acosh_nid_postfix
libc asinh_nid_postfix
libc atanh_nid_postfix
libc ceil_nid_postfix
libc cosh_nid_postfix
libc expm1_nid_postfix
libc fabs_nid_postfix
libc floor_nid_postfix
libc log1p_nid_postfix
libc nan_nid_postfix
libc sinh_nid_postfix
libc sqrt_nid_postfix
libc trunc_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6063593777 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405#issuecomment-6063593777). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T15:43:56Z. --- <!-- progress-report --> ### Progress report (4b005b2 → 7278eea) 📈 **System libraries**: 84.82% (+0.06%, +13 implemented functions) <details> <summary>✅ 13 implemented</summary> | Library | Name | | - | - | | libc | `acosh_nid_postfix` | | libc | `asinh_nid_postfix` | | libc | `atanh_nid_postfix` | | libc | `ceil_nid_postfix` | | libc | `cosh_nid_postfix` | | libc | `expm1_nid_postfix` | | libc | `fabs_nid_postfix` | | libc | `floor_nid_postfix` | | libc | `log1p_nid_postfix` | | libc | `nan_nid_postfix` | | libc | `sinh_nid_postfix` | | libc | `sqrt_nid_postfix` | | libc | `trunc_nid_postfix` | </details>
Dextroid17 commented 2026-10-08 16:45:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T16:45:18Z.


Checked the trimmed head (7278eea) against @oneandonlydean's list: Math.cpp declares exactly the 13 double forms and nothing else — sinh cosh asinh acosh atanh expm1 log1p sqrt fabs ceil floor trunc and nan, each a double …_nid_postfix — so the export half now matches what was asked.

The half still missing for the bar is the log line from a title, and I cannot supply it either: I don't have Minecraft Legends (PPSA05510) here. Naming #268's import list in the body is the honest interim state.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6064685777 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405#issuecomment-6064685777). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T16:45:18Z. --- Checked the trimmed head (`7278eea`) against @oneandonlydean's list: `Math.cpp` declares exactly the 13 double forms and nothing else — `sinh cosh asinh acosh atanh expm1 log1p sqrt fabs ceil floor trunc` and `nan`, each a `double …_nid_postfix` — so the export half now matches what was asked. The half still missing for the bar is the log line from a title, and I cannot supply it either: I don't have Minecraft Legends (PPSA05510) here. Naming #268's import list in the body is the honest interim state. Written with AI assistance.
oneandonlydean commented 2026-10-09 09:29:12 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T09:29:12Z.


Closing: the #268 import list shows that PPSA05510 imports these, not that it stops at them on main. Reopen when one is a title's next stop on main, with the log line.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6078223287 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1405#issuecomment-6078223287). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T09:29:12Z. --- Closing: the #268 import list shows that PPSA05510 imports these, not that it stops at them on main. Reopen when one is a title's next stop on main, with the log line.

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