fix(libc): keep the sign of a failed strtof and match libc.prx strtof ERANGE #8865

Merged
hypermoist merged 7 commits from upstream-pr/8865 into main 2026-10-23 22:56:02 +00:00
hypermoist commented 2026-10-21 03:43:04 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-21T03:43:04Z.


What

A follow-up to #8379, and an answer to #9691 for strtof covering the cases where the float parsers still differ from the console. The evidence is the decrypted libSceLibcInternal.sprx (PS5 13.60): the exports were loaded and called directly, and their results compared with this build's. The addresses used are strtod 0x65860, strtof 0x731a0 (which is _Stof(s, end, 0)), strtold 0x749b0, wcstod 0x863c0, wcstof 0x88a10 and wcstold 0x89240.

  • Failed parse after a minus sign.

    • strtold and wcstold return -0.0: "-", "-x", "-.", "-e5", "- 1", "--1", " -.". If the character after the sign starts an inf/nan spelling (i, I, n, N), the result is +0.0: "-in", "-N", "-nax".
    • strtof and wcstof follow the titles' libc.prx (see below). A lone sign ("-", " -") gives +0.0. Every other failed parse after - gives -0.0, including "-in" and "-nax". libSceLibcInternal's _Stof does the reverse for those two groups.
    • strtod, wcstod and atof keep +0.0, as #3905 made them.

    #3905 (for #3090) made every failed parse +0.0 by following FreeBSD's gdtoa. The console's Dinkumware code differs from that for the float and long double forms, so CheckNoConversionZero (renamed from CheckNoConversionIsPositiveZero) now expects the console's signs. The end pointer stays at the start of the string in every case.

  • Hexadecimal overflow and underflow in strtold (and wcstold) leave errno alone. Examples: 0x1p+20000 → HUGE_VALL and 0x1p-16446 → 0, both without ERANGE. Of 40k random hexadecimal inputs, the console set ERANGE for none of them in strtold. Decimal text keeps ERANGE (strtold("1e-5000")).

  • strtof (and wcstof) follow the libc.prx titles ship. Titles import these NIDs from their own libc.prx; the firmware has no system libc module. Run side by side with this libc on a Linux host, the libc.prx of PPSA08427 (J03547016) and of PPSA03671 (J03848509) give the same results:

    • A decimal input whose result is a nonzero subnormal sets ERANGE only when it is inexact: 1e-45, 1e-40, 3e-45 and -1e-45 set it. The exact 110-digit expansion of 2^-149 and 1.17549435e-38 (FLT_MIN) do not. main cleared it in every case.
    • A hexadecimal input sets it for a nonzero subnormal only when the value lies below the smallest subnormal and rounds up to it (0x1.8p-150, 0x0.0000018p-126), as strtod does below. Values rounding between subnormals leave it alone (0x1.8p-149, 0x1.000001p-149, 0x1.4p-149). Underflow to zero and overflow set it (0x1p-150, 0x1p+200). @JJenkx found the round-up case.
    • -nan gives a negative quiet NaN (0xffc00000), as their strtod does. strtold keeps a positive one in both copies.
    • libSceLibcInternal.sprx's _Stof leaves errno alone for hexadecimal under- and overflow. An earlier revision of this PR followed it; it now follows libc.prx. #9691 reports the same strtof results for three other titles' libc.prx.
  • strtod (and wcstod) set ERANGE for hexadecimal text whose value lies below the smallest subnormal and rounds up to it. Examples: 0x1.8p-1075 → 0x1p-1074 with ERANGE. 0x1p-1074 and 0x1.8p-1074 are representable or round between subnormals, so they set nothing. Of 300k random subnormal hexadecimal results, exactly these cases (2483) set ERANGE, and no other rounding did. Decimal text does not do this ("3e-324" gives the smallest subnormal without ERANGE), which matches #8379.

Not changed. The console's _Stof does not round to nearest. About a third of decimal inputs within a relative 2^-51 of a halfway point between two floats, and some hexadecimal inputs with more than 24 significant bits, differ in the last bit; for example, 1e-40 is 0x1.16c3p-133 there. It is not (float)strtod either. Its rounding is not reproduced, and a debt entry records that.

Also not changed (recorded in a debt entry):

  • Both libc.prx builds take a NAN(...) payload in strtof (nan(0x123) → 0x7fc00123, -nan(5) → 0xffc00005). The payload depends on the digit count in a way not pinned down, so strtof returns the default NaN, with the sign kept.
  • Decimal strtod sets ERANGE for a nonzero subnormal in J03547016 but not in J03848509 or libSceLibcInternal. The current behaviour (no ERANGE) is kept.

Tested

Linux (CachyOS, GCC, Release), merged with current main: guest_math and guest_wide_strings pass, and each case they gain was checked against the firmware or the libc.prx builds above. Under WinLibs GCC 15.2 + Wine, guest_wide_strings passes and the patched libc.prx builds. All new and changed guest_math checks pass there too, checked with a local non-aborting Require. Two existing checks in code this PR does not touch fail in that build: strtoul("-18446744073709551616!") in CheckIntegerConversions and the existing hypot3(0x1p1000, …) check.

Differential of this revision against a title's bundled libc.prx: 300k random hexadecimal strings with exponents -160 to -121, plus each sign prefix ("", "-", "+", " -", " ") combined with failed and inf/nan spellings. The only differences are the 5 nan(5) payload cases.

Earlier differentials, unchanged by this revision:

  • libSceLibcInternal.sprx: 600k random short strings through strtof, strtod and strtold, with no mismatch in end pointer, sign or (except strtof hex) errno.
  • 120k random hexadecimal inputs.

tools/check_conventions.py is clean.

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 (answers #9691 for strtof)
  • Unblocks: none
  • GPUs tested: n/a (libc only)
  • AI-assisted: yes (Claude Code), reviewed by the contributor

🤖 Generated with Claude Code

https://claude.ai/code/session_01RbLgECLQ2bpDM7oe2urGZK

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4946053341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8865). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-21T03:43:04Z. --- ### What A follow-up to #8379, and an answer to #9691 for `strtof` covering the cases where the float parsers still differ from the console. The evidence is the decrypted `libSceLibcInternal.sprx` (PS5 13.60): the exports were loaded and called directly, and their results compared with this build's. The addresses used are `strtod` 0x65860, `strtof` 0x731a0 (which is `_Stof(s, end, 0)`), `strtold` 0x749b0, `wcstod` 0x863c0, `wcstof` 0x88a10 and `wcstold` 0x89240. - **Failed parse after a minus sign.** - `strtold` and `wcstold` return `-0.0`: `"-"`, `"-x"`, `"-."`, `"-e5"`, `"- 1"`, `"--1"`, `" -."`. If the character after the sign starts an `inf`/`nan` spelling (`i`, `I`, `n`, `N`), the result is `+0.0`: `"-in"`, `"-N"`, `"-nax"`. - `strtof` and `wcstof` follow the titles' `libc.prx` (see below). A lone sign (`"-"`, `" -"`) gives `+0.0`. Every other failed parse after `-` gives `-0.0`, including `"-in"` and `"-nax"`. `libSceLibcInternal`'s `_Stof` does the reverse for those two groups. - `strtod`, `wcstod` and `atof` keep `+0.0`, as #3905 made them. #3905 (for #3090) made every failed parse `+0.0` by following FreeBSD's gdtoa. The console's Dinkumware code differs from that for the float and long double forms, so `CheckNoConversionZero` (renamed from `CheckNoConversionIsPositiveZero`) now expects the console's signs. The end pointer stays at the start of the string in every case. - **Hexadecimal overflow and underflow in `strtold` (and `wcstold`) leave errno alone.** Examples: `0x1p+20000` → `HUGE_VALL` and `0x1p-16446` → 0, both without `ERANGE`. Of 40k random hexadecimal inputs, the console set `ERANGE` for none of them in `strtold`. Decimal text keeps `ERANGE` (`strtold("1e-5000")`). - **`strtof` (and `wcstof`) follow the `libc.prx` titles ship.** Titles import these NIDs from their own `libc.prx`; the firmware has no system `libc` module. Run side by side with this libc on a Linux host, the `libc.prx` of PPSA08427 (J03547016) and of PPSA03671 (J03848509) give the same results: - A decimal input whose result is a nonzero subnormal sets `ERANGE` only when it is inexact: `1e-45`, `1e-40`, `3e-45` and `-1e-45` set it. The exact 110-digit expansion of 2^-149 and `1.17549435e-38` (`FLT_MIN`) do not. `main` cleared it in every case. - A hexadecimal input sets it for a nonzero subnormal only when the value lies below the smallest subnormal and rounds up to it (`0x1.8p-150`, `0x0.0000018p-126`), as `strtod` does below. Values rounding between subnormals leave it alone (`0x1.8p-149`, `0x1.000001p-149`, `0x1.4p-149`). Underflow to zero and overflow set it (`0x1p-150`, `0x1p+200`). @JJenkx found the round-up case. - `-nan` gives a negative quiet NaN (`0xffc00000`), as their `strtod` does. `strtold` keeps a positive one in both copies. - `libSceLibcInternal.sprx`'s `_Stof` leaves `errno` alone for hexadecimal under- and overflow. An earlier revision of this PR followed it; it now follows `libc.prx`. #9691 reports the same `strtof` results for three other titles' `libc.prx`. - **`strtod` (and `wcstod`) set `ERANGE` for hexadecimal text whose value lies below the smallest subnormal and rounds up to it.** Examples: `0x1.8p-1075` → `0x1p-1074` with `ERANGE`. `0x1p-1074` and `0x1.8p-1074` are representable or round between subnormals, so they set nothing. Of 300k random subnormal hexadecimal results, exactly these cases (2483) set `ERANGE`, and no other rounding did. Decimal text does not do this (`"3e-324"` gives the smallest subnormal without `ERANGE`), which matches #8379. **Not changed.** The console's `_Stof` does not round to nearest. About a third of decimal inputs within a relative 2^-51 of a halfway point between two floats, and some hexadecimal inputs with more than 24 significant bits, differ in the last bit; for example, `1e-40` is `0x1.16c3p-133` there. It is not `(float)strtod` either. Its rounding is not reproduced, and a debt entry records that. Also not changed (recorded in a debt entry): - Both `libc.prx` builds take a `NAN(...)` payload in `strtof` (`nan(0x123)` → `0x7fc00123`, `-nan(5)` → `0xffc00005`). The payload depends on the digit count in a way not pinned down, so `strtof` returns the default NaN, with the sign kept. - Decimal `strtod` sets `ERANGE` for a nonzero subnormal in J03547016 but not in J03848509 or `libSceLibcInternal`. The current behaviour (no `ERANGE`) is kept. ### Tested Linux (CachyOS, GCC, Release), merged with current `main`: `guest_math` and `guest_wide_strings` pass, and each case they gain was checked against the firmware or the `libc.prx` builds above. Under WinLibs GCC 15.2 + Wine, `guest_wide_strings` passes and the patched `libc.prx` builds. All new and changed `guest_math` checks pass there too, checked with a local non-aborting `Require`. Two existing checks in code this PR does not touch fail in that build: `strtoul("-18446744073709551616!")` in `CheckIntegerConversions` and the existing `hypot3(0x1p1000, …)` check. Differential of this revision against a title's bundled `libc.prx`: 300k random hexadecimal strings with exponents -160 to -121, plus each sign prefix (`""`, `"-"`, `"+"`, `" -"`, `" "`) combined with failed and `inf`/`nan` spellings. The only differences are the 5 `nan(5)` payload cases. Earlier differentials, unchanged by this revision: - `libSceLibcInternal.sprx`: 600k random short strings through `strtof`, `strtod` and `strtold`, with no mismatch in end pointer, sign or (except `strtof` hex) errno. - 120k random hexadecimal inputs. `tools/check_conventions.py` is clean. ### 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 (answers #9691 for `strtof`) - [x] Unblocks: none - [x] GPUs tested: n/a (libc only) - [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
MarcZX10 commented 2026-10-21 04:13:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-21T04:13:54Z.

Review: APPROVED


Reviewed the parser changes and focused regression coverage. The sign handling only applies when conversion consumes no input, and the hexadecimal errno adjustment is restricted to the documented float/long-double or smallest-subnormal cases. The PR reports extensive firmware differential results, a full Linux build, all guest tests passing, and clean conventions. I found no blocking issue in the diff.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5594883557 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8865#pullrequestreview-5594883557). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-21T04:13:54Z. **Review:** APPROVED --- Reviewed the parser changes and focused regression coverage. The sign handling only applies when conversion consumes no input, and the hexadecimal errno adjustment is restricted to the documented float/long-double or smallest-subnormal cases. The PR reports extensive firmware differential results, a full Linux build, all guest tests passing, and clean conventions. I found no blocking issue in the diff.
tkcm01 commented 2026-10-21 17:26:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by tkcm01 on 2026-10-21T17:26:59Z.


Windows data point for 0b52caf718, merged onto main ec7eacc62 (clean merge). GitHub Actions windows-latest running this repo's Build workflow (full Windows build, WinLibs GCC, Release, BUILD_TESTING=ON) on my fork.

  • With the PR: 1787 tests, all passed, guest_math and guest_wide_strings included (run).
  • Negative control, main plus only this PR's GuestMath.cpp and GuestWideStrings.cpp: guest_math and guest_wide_strings abort (0xc0000409); the other 296 selected tests pass (run).

Not tested: Linux, macOS, a title.
AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6322258112 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8865#issuecomment-6322258112). Originally posted by [tkcm01](https://github.com/tkcm01) on 2026-10-21T17:26:59Z. --- Windows data point for `0b52caf718`, merged onto `main` `ec7eacc62` (clean merge). GitHub Actions `windows-latest` running this repo's Build workflow (full Windows build, WinLibs GCC, Release, `BUILD_TESTING=ON`) on my fork. - With the PR: 1787 tests, all passed, `guest_math` and `guest_wide_strings` included ([run](https://github.com/tkcm01/AnyPS5/actions/runs/39554874188)). - Negative control, `main` plus only this PR's `GuestMath.cpp` and `GuestWideStrings.cpp`: `guest_math` and `guest_wide_strings` abort (0xc0000409); the other 296 selected tests pass ([run](https://github.com/tkcm01/AnyPS5/actions/runs/39557265238)). Not tested: Linux, macOS, a title. AI-assisted: yes
hypermoist commented 2026-10-22 09:10:16 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T09:10:16Z.


@tkcm01 thanks for the full Windows CI run (1787 tests passing) and the negative control showing guest_math and guest_wide_strings abort without the implementation.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6337085404 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8865#issuecomment-6337085404). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-22T09:10:16Z. --- @tkcm01 thanks for the full Windows CI run (1787 tests passing) and the negative control showing `guest_math` and `guest_wide_strings` abort without the implementation.
hypermoist commented 2026-10-22 13:35:23 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by hypermoist on 2026-10-22T13:35:23Z.


Revised after #9691. One part of this PR was wrong for the module these NIDs come from.

  • What changed: strtof/wcstof now set ERANGE the way the libc.prx titles ship does. The earlier revision followed libSceLibcInternal's _Stof, which leaves errno alone for hex under- and overflow. Titles import strtof from their own libc.prx, and the firmware has no system libc.
  • The rules: run side by side on a Linux host, PPSA08427's (J03547016) and PPSA03671's (J03848509) libc.prx agree:
    • A decimal input whose result is a nonzero subnormal sets ERANGE only when inexact.
    • A hexadecimal input never sets it for a nonzero subnormal.
    • 0x1p-150 and 0x1p+200 set it.
  • Unchanged and re-checked on both builds:
    • The -0.0 results for failed signed parses.
    • strtold hex without ERANGE.
    • The strtod hex rounding-up case.
  • Tests: merged current main, rebuilt, and guest_math/guest_wide_strings pass on Linux. Under MinGW + Wine, guest_wide_strings passes and the new strtof checks pass. The two existing guest_math failures there (strtoul, hypot3) are in code this PR doesn't touch.
  • Description: updated, plus a short debt entry for the NaN differences that are not reproduced.
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6342097338 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/8865#issuecomment-6342097338). Originally posted by [hypermoist](https://github.com/hypermoist) on 2026-10-22T13:35:23Z. --- Revised after #9691. One part of this PR was wrong for the module these NIDs come from. - **What changed:** `strtof`/`wcstof` now set `ERANGE` the way the `libc.prx` titles ship does. The earlier revision followed `libSceLibcInternal`'s `_Stof`, which leaves `errno` alone for hex under- and overflow. Titles import `strtof` from their own `libc.prx`, and the firmware has no system `libc`. - **The rules:** run side by side on a Linux host, PPSA08427's (J03547016) and PPSA03671's (J03848509) `libc.prx` agree: - A decimal input whose result is a nonzero subnormal sets `ERANGE` only when inexact. - A hexadecimal input never sets it for a nonzero subnormal. - `0x1p-150` and `0x1p+200` set it. - **Unchanged and re-checked on both builds:** - The `-0.0` results for failed signed parses. - `strtold` hex without `ERANGE`. - The `strtod` hex rounding-up case. - **Tests:** merged current `main`, rebuilt, and `guest_math`/`guest_wide_strings` pass on Linux. Under MinGW + Wine, `guest_wide_strings` passes and the new `strtof` checks pass. The two existing `guest_math` failures there (`strtoul`, `hypot3`) are in code this PR doesn't touch. - **Description:** updated, plus a short debt entry for the NaN differences that are not reproduced.
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!8865
No description provided.