feat(libc): implement fabs, sqrt, trunc and fma #2167

Closed
reyani-dev wants to merge 1 commit from upstream-pr/2167 into main
reyani-dev commented 2026-10-09 10:33:35 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T10:33:35Z.


What

Exports the double versions of fabs, sqrt, trunc and fma. Each one forwards to the C++ standard library, like round and fmod next to them. All four are exact or correctly rounded in IEEE 754, so the host result matches the console's.

Titles: open-source homebrew from the #1578 catalog audit imports them from libSceLibcInternal (resolved through libc.prx):

  • fabs: PPSA74126 Stremio Plus, PPSA99003 ProsperoTV, PPSA99169 RetroArch
  • sqrt: PPSA99001 ProsperoRadio, PPSA99003 ProsperoTV, PPSA99169 RetroArch
  • trunc: PPSA99001 ProsperoRadio, PPSA99003 ProsperoTV
  • fma: PPSA99169 RetroArch, PPSA99764 Porpoise

I haven't run any of these titles.

Tested

Windows 11, WinLibs GCC 15.2.0 r7, Release, -DBUILD_TESTING=ON:

  • New CheckExactOperations in guest_math_tests:
    • trunc: signed zero (trunc(-0.5) is -0), 2^52+1, -1e300, infinities and NaN;
    • fabs: clears the sign of -0 and of a negative NaN;
    • sqrt: correctly rounded (sqrt(2) == 1.4142135623730951), sqrt(-0) == -0, sqrt(denorm_min), NaN for negatives;
    • fma: single rounding (fma(0.1, 10, -1) == 0x1p-54, where 0.1 * 10 - 1 gives 0), no intermediate overflow (fma(2^1023, 2, -2^1023) == 2^1023), and fma(inf, 0, 1) is NaN.
  • Mutation check: x * y + z for fma, pow(x, 0.5) for sqrt, a long long round trip for trunc and x < 0 ? -x : x for fabs each make guest_math fail.
  • cmake --build build --target libs: the patched libc.prx exports all 4 NIDs.
  • tools/check_conventions.py passes, and tools/pr_overlap.py --branch reports no overlap. This branch also merges cleanly with my #2158 and #2166.

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

🤖 Generated with Claude Code

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4799236348 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2167). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T10:33:35Z. --- ### What Exports the `double` versions of `fabs`, `sqrt`, `trunc` and `fma`. Each one forwards to the C++ standard library, like `round` and `fmod` next to them. All four are exact or correctly rounded in IEEE 754, so the host result matches the console's. Titles: open-source homebrew from the #1578 catalog audit imports them from `libSceLibcInternal` (resolved through `libc.prx`): - `fabs`: PPSA74126 Stremio Plus, PPSA99003 ProsperoTV, PPSA99169 RetroArch - `sqrt`: PPSA99001 ProsperoRadio, PPSA99003 ProsperoTV, PPSA99169 RetroArch - `trunc`: PPSA99001 ProsperoRadio, PPSA99003 ProsperoTV - `fma`: PPSA99169 RetroArch, PPSA99764 Porpoise I haven't run any of these titles. ### Tested Windows 11, WinLibs GCC 15.2.0 r7, Release, `-DBUILD_TESTING=ON`: - New `CheckExactOperations` in `guest_math_tests`: - `trunc`: signed zero (`trunc(-0.5)` is `-0`), 2^52+1, `-1e300`, infinities and NaN; - `fabs`: clears the sign of `-0` and of a negative NaN; - `sqrt`: correctly rounded (`sqrt(2) == 1.4142135623730951`), `sqrt(-0) == -0`, `sqrt(denorm_min)`, NaN for negatives; - `fma`: single rounding (`fma(0.1, 10, -1) == 0x1p-54`, where `0.1 * 10 - 1` gives 0), no intermediate overflow (`fma(2^1023, 2, -2^1023) == 2^1023`), and `fma(inf, 0, 1)` is NaN. - Mutation check: `x * y + z` for `fma`, `pow(x, 0.5)` for `sqrt`, a `long long` round trip for `trunc` and `x < 0 ? -x : x` for `fabs` each make `guest_math` fail. - `cmake --build build --target libs`: the patched `libc.prx` exports all 4 NIDs. - `tools/check_conventions.py` passes, and `tools/pr_overlap.py --branch` reports no overlap. This branch also merges cleanly with my #2158 and #2166. ### 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 🤖 Generated with [Claude Code](https://claude.com/claude-code)
github-actions[bot] commented 2026-10-09 10:33:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T10:33:56Z.


Progress report (51f69aa → da7feb0)

📈 System libraries: 84.71% (+0.02%, +4 implemented functions)

✅ 4 implemented
Library Name
libc fabs_nid_postfix
libc fma_nid_postfix
libc sqrt_nid_postfix
libc trunc_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079159053 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2167#issuecomment-6079159053). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T10:33:56Z. --- <!-- progress-report --> ### Progress report (51f69aa → da7feb0) 📈 **System libraries**: 84.71% (+0.02%, +4 implemented functions) <details> <summary>✅ 4 implemented</summary> | Library | Name | | - | - | | libc | `fabs_nid_postfix` | | libc | `fma_nid_postfix` | | libc | `sqrt_nid_postfix` | | libc | `trunc_nid_postfix` | </details>
reyani-dev commented 2026-10-09 11:24:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-09T11:24:37Z.


Full Linux and Windows builds and tests for this head (da7feb06), using the project's Build workflow. I can't start the upstream manual run, so I ran the same build.yml in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current main (d70b8998):

Job Result
linux full build, ctest: 516/516 passed
windows full build, ctest: 507/507 passed
relinker (Linux, Windows, macOS) passed

Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37920409715

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6079902393 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2167#issuecomment-6079902393). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-09T11:24:37Z. --- Full Linux and Windows builds and tests for this head (`da7feb06`), using the project's Build workflow. I can't start the upstream manual run, so I ran the same `build.yml` in my fork through a mirror PR. Like the upstream run, the jobs check out the PR head merged into current `main` (`d70b8998`): | Job | Result | |---|---| | [linux](https://github.com/AchrafReyani/AnyPS5/actions/runs/37920409715/job/113786809048) | full build, `ctest`: 516/516 passed | | [windows](https://github.com/AchrafReyani/AnyPS5/actions/runs/37920409715/job/113786809025) | full build, `ctest`: 507/507 passed | | relinker (Linux, Windows, macOS) | passed | Run: https://github.com/AchrafReyani/AnyPS5/actions/runs/37920409715
oneandonlydean commented 2026-10-09 20:09:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T20:09:23Z.


Closing: these four come from the #1578 import list, and none of the titles named is shown stopping at them on main (they weren't run). Library functions go in when a title reaches them; please reopen with the title ID and the log line when one does.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6088433063 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2167#issuecomment-6088433063). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-09T20:09:23Z. --- Closing: these four come from the #1578 import list, and none of the titles named is shown stopping at them on main (they weren't run). Library functions go in when a title reaches them; please reopen with the title ID and the log line when one does.
babyhuey commented 2026-10-11 06:38:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by babyhuey on 2026-10-11T06:38:13Z.


@AchrafReyani fabs is now a title's next stop on main 52ce9b8a. With pthread_set_name_np (#3031) merged, PSBox Cloud Gaming (PPSA99810) reports this on Linux (catalog build, sce_module/libc.prx unwrapped as #2605 requires):

app.elf: symbol lookup error: app.elf: undefined symbol: 388LcMWHRCA

sqrt from this PR is already in through #3233, and the head now conflicts with main in GuestMath.cpp. With this PR's fabs, trunc and fma lines added to main, PSBox goes on to _umtx_op (#1482).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106314689 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2167#issuecomment-6106314689). Originally posted by [babyhuey](https://github.com/babyhuey) on 2026-10-11T06:38:13Z. --- @AchrafReyani `fabs` is now a title's next stop on `main` 52ce9b8a. With `pthread_set_name_np` (#3031) merged, PSBox Cloud Gaming (PPSA99810) reports this on Linux (catalog build, `sce_module/libc.prx` unwrapped as #2605 requires): `app.elf: symbol lookup error: app.elf: undefined symbol: 388LcMWHRCA` `sqrt` from this PR is already in through #3233, and the head now conflicts with `main` in `GuestMath.cpp`. With this PR's `fabs`, `trunc` and `fma` lines added to `main`, PSBox goes on to `_umtx_op` (#1482).
reyani-dev commented 2026-10-11 07:10:03 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by reyani-dev on 2026-10-11T07:10:03Z.


Thanks @babyhuey! Redone on current main as #3314: fabs, trunc and fma only, since sqrt is in through #3233.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6106540445 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2167#issuecomment-6106540445). Originally posted by [reyani-dev](https://github.com/reyani-dev) on 2026-10-11T07:10:03Z. --- Thanks @babyhuey! Redone on current `main` as #3314: `fabs`, `trunc` and `fma` only, since `sqrt` is in through #3233.

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