feat(libc): implement the Dinkumware _Stod, _Stoll, _Sin and _Log helpers #8376

Merged
hypermoist merged 4 commits from upstream-pr/8376 into main 2026-10-22 20:51:54 +00:00
hypermoist commented 2026-10-20 11:46:07 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T11:46:07Z.


What

Implements seven absent Dinkumware helpers from libSceLibcInternal. CUSA00223 (#8214) imports _Stod, _Stoll, _Log, _FLog and _Sin. The evidence is the decrypted libSceLibcInternal.sprx, plus a differential run that loads that module on the host and calls its exports directly.

Dispatchers (each body is a branch and a tail call to a public export):

Export Firmware body Here
_Sin (cCXjU72Z0Ow) test edi, edi: non-zero calls cos, zero calls sin cos_nid_postfix or sin_nid_postfix
_FSin (ZtjspkJQ+vw) the same with cosf / sinf
_LSin (m-Bu5Tzr82A) the same with cosl / sinl
_Log (zXCi78bYrEI) flag > 0 calls log10, 0 calls log, < 0 calls log2
_FLog (jjjRS7l1MPM) the same with log10f / logf / log2f
_Stoll (7pNKcscKrf8) _Stollx(s, end, base, 0) strtoll_nid_postfix; identical to the console's strtoll in value, end pointer and ERANGE for every probed input

Only real dispatchers are included: _LLog is a real implementation in the firmware, not a dispatcher, so it is left out.

_Stod (c41UEHVtiEA) is _Stodx(s, end, pten, 0). Run against the console module, it behaves as follows:

  • It returns the correctly rounded value of the digits times 10^(exponent + pten), so the scale is applied before rounding. For example, "-2.25e3" with 300 is -0x1.a3f7f806a8135p+1007, not strtod * 1e300.
  • "1e-400" with 300 is 1e-100 with no error.
  • The scale is a 64-bit value: "5" with 2^40 overflows to HUGE_VAL with ERANGE.
  • Overflow, and underflow to zero, set ERANGE. Text that overflows on its own but is brought back into range by the scale is rescaled: "1e400" with -100 is 1e300 with no error.
  • inf and nan pass through unscaled ("inf" text sets no ERANGE, so it is not mistaken for an overflow), the end pointer is strtod's, and text without a number gives 0.

The implementation reparses the consumed text with the exponent rewritten, through strtod_nid_postfix, and restores the guest errno between the two parses. The firmware's hexadecimal path with a non-zero scale does not follow a simple rule (0x3 with -1 gives 0.29999999999999999), so that one path throws NotImplemented_nid_no_patch; unscaled hexadecimal works. The throw is recorded in docs/dev/debt/libc.md.

The console's strtod and _Stod leave errno clear for a non-zero subnormal result; #8379 (merged) made upstream's strtod do the same, so _Stod follows it through strtod_nid_postfix.

Tested

Linux (CachyOS, GCC, Release). The new guest_dinkumware_internals test passes. Its _Stod expectations, including the bit patterns and errno values, are the console module's outputs from the differential run. It also checks the 64-bit scale, an overflow rescaled into range, _Stoll saturation and ERANGE, and every dispatch branch.

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: CUSA00223 #8214 imports _Stod, _Stoll, _Log, _FLog and _Sin; not run with the title
  • GPUs tested: n/a
  • AI-assisted: yes (Claude Code), reviewed by the contributor

🤖 Generated with Claude Code

https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4935246950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8376). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T11:46:07Z. --- ### What Implements seven absent Dinkumware helpers from `libSceLibcInternal`. CUSA00223 (#8214) imports `_Stod`, `_Stoll`, `_Log`, `_FLog` and `_Sin`. The evidence is the decrypted `libSceLibcInternal.sprx`, plus a differential run that loads that module on the host and calls its exports directly. **Dispatchers** (each body is a branch and a tail call to a public export): | Export | Firmware body | Here | |---|---|---| | `_Sin` (`cCXjU72Z0Ow`) | `test edi, edi`: non-zero calls `cos`, zero calls `sin` | `cos_nid_postfix` or `sin_nid_postfix` | | `_FSin` (`ZtjspkJQ+vw`) | the same with `cosf` / `sinf` | | | `_LSin` (`m-Bu5Tzr82A`) | the same with `cosl` / `sinl` | | | `_Log` (`zXCi78bYrEI`) | flag > 0 calls `log10`, 0 calls `log`, < 0 calls `log2` | | | `_FLog` (`jjjRS7l1MPM`) | the same with `log10f` / `logf` / `log2f` | | | `_Stoll` (`7pNKcscKrf8`) | `_Stollx(s, end, base, 0)` | `strtoll_nid_postfix`; identical to the console's `strtoll` in value, end pointer and `ERANGE` for every probed input | Only real dispatchers are included: `_LLog` is a real implementation in the firmware, not a dispatcher, so it is left out. **`_Stod` (`c41UEHVtiEA`)** is `_Stodx(s, end, pten, 0)`. Run against the console module, it behaves as follows: - It returns the correctly rounded value of the digits times `10^(exponent + pten)`, so the scale is applied before rounding. For example, `"-2.25e3"` with 300 is `-0x1.a3f7f806a8135p+1007`, not `strtod * 1e300`. - `"1e-400"` with 300 is `1e-100` with no error. - The scale is a 64-bit value: `"5"` with 2^40 overflows to `HUGE_VAL` with `ERANGE`. - Overflow, and underflow to zero, set `ERANGE`. Text that overflows on its own but is brought back into range by the scale is rescaled: `"1e400"` with -100 is `1e300` with no error. - `inf` and `nan` pass through unscaled (`"inf"` text sets no `ERANGE`, so it is not mistaken for an overflow), the end pointer is `strtod`'s, and text without a number gives 0. The implementation reparses the consumed text with the exponent rewritten, through `strtod_nid_postfix`, and restores the guest `errno` between the two parses. The firmware's hexadecimal path with a non-zero scale does not follow a simple rule (`0x3` with -1 gives `0.29999999999999999`), so that one path throws `NotImplemented_nid_no_patch`; unscaled hexadecimal works. The throw is recorded in `docs/dev/debt/libc.md`. The console's `strtod` and `_Stod` leave `errno` clear for a non-zero subnormal result; #8379 (merged) made upstream's `strtod` do the same, so `_Stod` follows it through `strtod_nid_postfix`. ### Tested Linux (CachyOS, GCC, Release). The new `guest_dinkumware_internals` test passes. Its `_Stod` expectations, including the bit patterns and `errno` values, are the console module's outputs from the differential run. It also checks the 64-bit scale, an overflow rescaled into range, `_Stoll` saturation and `ERANGE`, and every dispatch branch. ### 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 - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: CUSA00223 #8214 imports `_Stod`, `_Stoll`, `_Log`, `_FLog` and `_Sin`; not run with the title - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code), reviewed by the contributor 🤖 Generated with [Claude Code](https://claude.com/claude-code) https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK
github-actions[bot] commented 2026-10-20 11:46:47 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-20T11:46:47Z.


Progress report (eea640a → e321a04)

📈 System libraries: 62.87% (+0.03%, +6 implemented, +1 declared functions)

✅ 6 implemented
Library Name
libc _FLog_nid_postfix
libc _FSin_nid_postfix
libc _LSin_nid_postfix
libc _Log_nid_postfix
libc _Sin_nid_postfix
libc _Stoll_nid_postfix
🆕 1 declared as stubs
Library Name
libc _Stod_nid_postfix
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6290126352 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8376#issuecomment-6290126352). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-20T11:46:47Z. --- <!-- progress-report --> ### Progress report (eea640a → e321a04) 📈 **System libraries**: 62.87% (+0.03%, +6 implemented, +1 declared functions) <details> <summary>✅ 6 implemented</summary> | Library | Name | | - | - | | libc | `_FLog_nid_postfix` | | libc | `_FSin_nid_postfix` | | libc | `_LSin_nid_postfix` | | libc | `_Log_nid_postfix` | | libc | `_Sin_nid_postfix` | | libc | `_Stoll_nid_postfix` | </details> <details> <summary>🆕 1 declared as stubs</summary> | Library | Name | | - | - | | libc | `_Stod_nid_postfix` | </details>
oneandonlydean commented 2026-10-20 22:48:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-20T22:48:52Z.


@hypermoist thanks. The dispatchers match the console module (_Sin sends a non-zero quadrant to cos, _Log picks log10/log/log2 by sign, and _Stod/_Stoll tail-call the x variants with 0), and the test passes on Linux. Three changes:

  • DinkumwareInternals.cpp:42: _Stod_nid_postfix(const char* text, char** end, long pten). The guest's long is 64 bits, but on Windows long is 32 bits, so the top half of the scale is dropped. With the Windows build, _Stod("1", &end, 4294967297) returns 10 with errno 0, while Linux returns inf with ERANGE. Please take std::int64_t pten as the other guest-long exports do (strtol, labs, ftell), change the test's declaration to match, and add a case with a scale above 2^32 (the current largest, 2147483647, fits in 32 bits).
  • DinkumwareInternals.cpp:47: the !std::isfinite(value) early return also skips an inf that came from digits overflowing before the scale. _Stod("1e-400", &end, 300) gives 1e-100, but _Stod("1e400", &end, -100) gives inf with ERANGE, where the description's rule (digits times 10^(exponent + pten), rounded once) gives 1e300. nikowolf123's suggestion on #8384 (rescale when the inf came with ERANGE; "inf" text sets no ERANGE) covers it. A test case for it, with the value checked against the console module, would pin it.
  • The hexadecimal-with-scale throw needs an entry in docs/dev/debt/libc.md (AGENTS.md: "throw and record it in docs/dev/debt/.md"), with the 0x3 / -1 data point from the description.

After that, the bar is a full CI run with guest_dinkumware_internals passing on Linux and Windows.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303516962 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8376#issuecomment-6303516962). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-20T22:48:52Z. --- @hypermoist thanks. The dispatchers match the console module (`_Sin` sends a non-zero quadrant to `cos`, `_Log` picks `log10`/`log`/`log2` by sign, and `_Stod`/`_Stoll` tail-call the `x` variants with 0), and the test passes on Linux. Three changes: - `DinkumwareInternals.cpp:42`: `_Stod_nid_postfix(const char* text, char** end, long pten)`. The guest's `long` is 64 bits, but on Windows `long` is 32 bits, so the top half of the scale is dropped. With the Windows build, `_Stod("1", &end, 4294967297)` returns 10 with errno 0, while Linux returns inf with ERANGE. Please take `std::int64_t pten` as the other guest-long exports do (`strtol`, `labs`, `ftell`), change the test's declaration to match, and add a case with a scale above 2^32 (the current largest, 2147483647, fits in 32 bits). - `DinkumwareInternals.cpp:47`: the `!std::isfinite(value)` early return also skips an inf that came from digits overflowing before the scale. `_Stod("1e-400", &end, 300)` gives 1e-100, but `_Stod("1e400", &end, -100)` gives inf with ERANGE, where the description's rule (digits times 10^(exponent + pten), rounded once) gives 1e300. nikowolf123's suggestion on #8384 (rescale when the inf came with ERANGE; `"inf"` text sets no ERANGE) covers it. A test case for it, with the value checked against the console module, would pin it. - The hexadecimal-with-scale throw needs an entry in `docs/dev/debt/libc.md` (AGENTS.md: "throw and record it in docs/dev/debt/<library>.md"), with the `0x3` / -1 data point from the description. After that, the bar is a full CI run with `guest_dinkumware_internals` passing on Linux and Windows.
hypermoist commented 2026-10-20 23:11:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-20T23:11:54Z.


@oneandonlydean the three points also apply here and are fixed on this branch:

  • _Stod takes std::int64_t pten, and its test declaration and helper match. New vector: "5" with a scale of 2^40 gives HUGE_VAL with ERANGE, where a 32-bit scale would have been cut to 0.
  • An overflow that comes from the digits is rescaled: _Stod("1e400", -100) is 1e300 (checked against the firmware). "inf" text sets no ERANGE and is not scaled. There is also a "0.01e41" with -2 → 1e37 vector.
  • The hexadecimal-with-scale throw has a docs/dev/debt/libc.md entry. It sits before "Long double math in MathExact.cpp" so it doesn't touch lines other open PRs edit.

#8384 has merged these changes and carries the same fixes for _Stodx/_Stofx/_Stoldx/_Stolx/_Stoulx.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6303861126 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8376#issuecomment-6303861126). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-20T23:11:54Z. --- @oneandonlydean the three points also apply here and are fixed on this branch: - `_Stod` takes `std::int64_t pten`, and its test declaration and helper match. New vector: `"5"` with a scale of 2^40 gives HUGE_VAL with ERANGE, where a 32-bit scale would have been cut to 0. - An overflow that comes from the digits is rescaled: `_Stod("1e400", -100)` is 1e300 (checked against the firmware). `"inf"` text sets no ERANGE and is not scaled. There is also a `"0.01e41"` with -2 → 1e37 vector. - The hexadecimal-with-scale throw has a `docs/dev/debt/libc.md` entry. It sits before "Long double math in MathExact.cpp" so it doesn't touch lines other open PRs edit. #8384 has merged these changes and carries the same fixes for `_Stodx`/`_Stofx`/`_Stoldx`/`_Stolx`/`_Stoulx`.
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!8376
No description provided.