fix(libc): set ERANGE for decimal strtod subnormals #9833

Merged
MarcZX10 merged 5 commits from upstream-pr/9833 into main 2026-10-27 05:21:38 +00:00
MarcZX10 commented 2026-10-22 16:49:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T16:49:58Z.


What

Follows the title-bundled libc.prx behavior for decimal strtod inputs that produce nonzero subnormal values: set ERANGE, including when the decimal exactly represents the result. This is based on the nine distinct libc.prx builds reported in #9691; two other builds and the system libSceLibcInternal.sprx leave errno unchanged. This updates the strtod behavior established by #8379 using the newer title-module evidence. Hexadecimal strtod handling is unchanged here because the title rule is not fully established. atof inherits the behavior through its existing strtod call.

Adds a focused registered regression test for inexact and exact decimal subnormals, a negative value, a normal result, hexadecimal subnormal errno preservation, underflow to zero, and end-pointer results. The existing guest_math assertions are updated to match.

This covers only the decimal strtod part of #9691; that issue also records unresolved strtof NaN payload behavior. Depends on #8865 because both changes touch the shared parser regression coverage; the strtof changes in that PR remain separate.

Tested

Linux x86-64, GCC 12.2:

  • The focused harness, built from the exact Strings.cpp and GuestStrtodSubnormal.cpp with -ffunction-sections -fdata-sections and --gc-sections, failed against current main at the inexact-subnormal errno assertion, then passed with this change.
  • GuestMath.cpp passes g++ -std=c++20 -Wall -Wextra -Werror -I core/libs -fsyntax-only.
  • python3 tools/check_conventions.py --base upstream/main --head HEAD passes.
  • git diff --check upstream/main...HEAD passes.

The full project CMake build was not available in this container: the bundled SDL2 submodule has no CMakeLists.txt, and system-dependency configuration cannot find SDL2. The normal SDL configuration also reports missing XRandR support.

Checklist

  • Based on current main; no open PR implements decimal strtod ERANGE for nonzero subnormal results (#8865 covers the separate strtof work and is declared as a dependency for shared parser test integration)
  • One topic per PR; follow-up work goes in a new pull request
  • No comments in code except technical debt
  • Unimplemented paths throw; silent stubs are documented
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: #8865
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4970967747 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9833). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T16:49:58Z. --- ### What Follows the title-bundled `libc.prx` behavior for decimal `strtod` inputs that produce nonzero subnormal values: set `ERANGE`, including when the decimal exactly represents the result. This is based on the nine distinct `libc.prx` builds reported in #9691; two other builds and the system `libSceLibcInternal.sprx` leave `errno` unchanged. This updates the `strtod` behavior established by #8379 using the newer title-module evidence. Hexadecimal `strtod` handling is unchanged here because the title rule is not fully established. `atof` inherits the behavior through its existing `strtod` call. Adds a focused registered regression test for inexact and exact decimal subnormals, a negative value, a normal result, hexadecimal subnormal errno preservation, underflow to zero, and end-pointer results. The existing `guest_math` assertions are updated to match. This covers only the decimal `strtod` part of #9691; that issue also records unresolved `strtof` NaN payload behavior. Depends on #8865 because both changes touch the shared parser regression coverage; the `strtof` changes in that PR remain separate. ### Tested Linux x86-64, GCC 12.2: - The focused harness, built from the exact `Strings.cpp` and `GuestStrtodSubnormal.cpp` with `-ffunction-sections -fdata-sections` and `--gc-sections`, failed against current `main` at the inexact-subnormal errno assertion, then passed with this change. - `GuestMath.cpp` passes `g++ -std=c++20 -Wall -Wextra -Werror -I core/libs -fsyntax-only`. - `python3 tools/check_conventions.py --base upstream/main --head HEAD` passes. - `git diff --check upstream/main...HEAD` passes. The full project CMake build was not available in this container: the bundled SDL2 submodule has no `CMakeLists.txt`, and system-dependency configuration cannot find SDL2. The normal SDL configuration also reports missing XRandR support. ### Checklist - [x] Based on current `main`; no open PR implements decimal `strtod` ERANGE for nonzero subnormal results (#8865 covers the separate `strtof` work and is declared as a dependency for shared parser test integration) - [x] One topic per PR; follow-up work goes in a new pull request - [x] No comments in code except technical debt - [x] Unimplemented paths throw; silent stubs are documented - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: #8865 - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes
harisibrar1 commented 2026-10-22 18:17:15 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-22T18:17:15Z.

Review: COMMENTED


Checked against main a6646f8464 and FreeBSD 11.4 contrib/gdtoa/strtod.c, gethex.c, gdtoaimp.h and lib/libc/locale/wcstod.c:

  • This merges cleanly onto main a6646f8464.
  • The decimal rule matches gdtoa. With Avoid_Underflow, scale is set whenever the decimal scale is at least 256, which every subnormal result needs (the leading ≤16 digits are below 10^16). The end of strtod_l then sets ERANGE whenever the exponent field is 0, with no inexact test. So exact subnormals get ERANGE too, and inputs that round up to DBL_MIN don't, exactly as magnitude < DBL_MIN does here.
  • wcstod still goes straight to ParseFloat (Strings.cpp:701-702), so wcstod(u"1e-310") keeps errno while strtod("1e-310") now sets ERANGE. FreeBSD 11.4's wcstod converts the string and calls strtod_l, so there the two agree. Could the check live in a shared helper both use, or could the debt entry mention wcstod? As far as I know, the titles' own wcstod hasn't been measured.
  • For the hex follow-up: in 11.4 gethex.c, ERANGE is only set when n = emin - e >= nbits, i.e. the exact value is below 2^-1074 and the result is 0 or the smallest denormal. That gives ERANGE for 0x1.8p-1075 (matching the console result reported above) and none for 0x1p-1074 (matching your new test). Inexact hex subnormals above 2^-1074 would keep errno. That could go in the debt line as the rule to verify.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5618843809 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9833#pullrequestreview-5618843809). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-22T18:17:15Z. **Review:** COMMENTED --- Checked against main a6646f8464 and FreeBSD 11.4 `contrib/gdtoa/strtod.c`, `gethex.c`, `gdtoaimp.h` and `lib/libc/locale/wcstod.c`: - This merges cleanly onto main a6646f8464. - The decimal rule matches gdtoa. With `Avoid_Underflow`, `scale` is set whenever the decimal scale is at least 256, which every subnormal result needs (the leading ≤16 digits are below 10^16). The end of `strtod_l` then sets ERANGE whenever the exponent field is 0, with no inexact test. So exact subnormals get ERANGE too, and inputs that round up to DBL_MIN don't, exactly as `magnitude < DBL_MIN` does here. - `wcstod` still goes straight to `ParseFloat` (Strings.cpp:701-702), so `wcstod(u"1e-310")` keeps errno while `strtod("1e-310")` now sets ERANGE. FreeBSD 11.4's `wcstod` converts the string and calls `strtod_l`, so there the two agree. Could the check live in a shared helper both use, or could the debt entry mention `wcstod`? As far as I know, the titles' own `wcstod` hasn't been measured. - For the hex follow-up: in 11.4 `gethex.c`, ERANGE is only set when `n = emin - e >= nbits`, i.e. the exact value is below 2^-1074 and the result is 0 or the smallest denormal. That gives ERANGE for `0x1.8p-1075` (matching the console result reported above) and none for `0x1p-1074` (matching your new test). Inexact hex subnormals above 2^-1074 would keep errno. That could go in the debt line as the rule to verify. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
MarcZX10 commented 2026-10-22 20:29:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-22T20:29:42Z.

Review: COMMENTED


I reviewed the current head 5084be7aa7deb887259aacaebfdfdf4a07b0f57b, including the errno reset added after the independent strtod calls. The reported focused GCC harnesses and Windows differential run cover the identified regression; current hosted checks pass conventions, eligibility, labels, register-context, relinker, ASan, and write-watch jobs, while Linux and Windows product jobs are skipped by policy. I did not run the code locally. No further actionable issue found in this pass. #8865 remains declared for the shared parser test integration.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5620445601 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/9833#pullrequestreview-5620445601). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-22T20:29:42Z. **Review:** COMMENTED --- I reviewed the current head `5084be7aa7deb887259aacaebfdfdf4a07b0f57b`, including the errno reset added after the independent `strtod` calls. The reported focused GCC harnesses and Windows differential run cover the identified regression; current hosted checks pass conventions, eligibility, labels, register-context, relinker, ASan, and write-watch jobs, while Linux and Windows product jobs are skipped by policy. I did not run the code locally. No further actionable issue found in this pass. #8865 remains declared for the shared parser test integration.
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!9833
No description provided.