fix(libc): honor strtod flush-to-zero with DAZ enabled #28284

Open
ibrahimmaq14 wants to merge 1 commit from upstream-pr/28284 into main
ibrahimmaq14 commented 2026-11-29 18:35:14 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ibrahimmaq14 on 2026-11-29T18:35:14Z.


What

Addresses the guest_strtod_subnormal failure in #27647. Apple Clang optimized the masked nonzero check into a floating-point comparison. With MXCSR denormals-are-zero enabled, that comparison sees a subnormal as zero and the flush-to-zero branch is skipped.

Compare the unsigned significand and exponent bits as a single range between zero and the smallest normal double. When FTZ is enabled and the magnitude is in that range, return signed zero with ERANGE. Add a negative decimal case for both strtod and wcstod.

Tested

  • macOS 27.2.1, Apple M1, x86-64 under Rosetta, Apple Clang 17. Built the existing GuestStrtodSubnormal.cpp test with Strings.cpp at -O3 -DNDEBUG. The test aborts on main at “FTZ flushes an inexact decimal subnormal” and passes with this change.
  • Linked the CMake Release objects for the same source and test into a standalone executable and ran it successfully. The standard CTest build stops in unchanged libc/src/exception/Runtime.cpp because the macOS C++ ABI headers lack __class_type_info::__contained_virtual_mask and related members.
  • tools/check_conventions.py and git diff --check passed. I compared both changed files against the open PRs and found no overlapping edits.

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 and the licence notice of code adapted from upstream (see AGENTS.md)
  • 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: none
  • GPUs tested: n/a
  • AI-assisted: yes

Summary by CodeRabbit

  • Bug Fixes
    • Corrected floating-point handling with flush-to-zero enabled: values below the normal range now return zero with the original sign preserved and report a range error.
    • Added coverage for negative subnormal values flushed to zero.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5553810813 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28284). Originally posted by [ibrahimmaq14](https://github.com/ibrahimmaq14) on 2026-11-29T18:35:14Z. --- ### What Addresses the `guest_strtod_subnormal` failure in #27647. Apple Clang optimized the masked nonzero check into a floating-point comparison. With MXCSR denormals-are-zero enabled, that comparison sees a subnormal as zero and the flush-to-zero branch is skipped. Compare the unsigned significand and exponent bits as a single range between zero and the smallest normal double. When FTZ is enabled and the magnitude is in that range, return signed zero with `ERANGE`. Add a negative decimal case for both `strtod` and `wcstod`. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - macOS 27.2.1, Apple M1, x86-64 under Rosetta, Apple Clang 17. Built the existing `GuestStrtodSubnormal.cpp` test with `Strings.cpp` at `-O3 -DNDEBUG`. The test aborts on `main` at “FTZ flushes an inexact decimal subnormal” and passes with this change. - Linked the CMake Release objects for the same source and test into a standalone executable and ran it successfully. The standard CTest build stops in unchanged `libc/src/exception/Runtime.cpp` because the macOS C++ ABI headers lack `__class_type_info::__contained_virtual_mask` and related members. - `tools/check_conventions.py` and `git diff --check` passed. I compared both changed files against the open PRs and found no overlapping edits. ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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) and the licence notice of code adapted from upstream (see AGENTS.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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes <!-- yes / no --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **Bug Fixes** * Corrected floating-point handling with flush-to-zero enabled: values below the normal range now return zero with the original sign preserved and report a range error. * Added coverage for negative subnormal values flushed to zero. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
PrivacyOnion commented 2026-11-29 18:38:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by PrivacyOnion on 2026-11-29T18:38:20Z.

Review: APPROVED


Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against 8a70bddf1e:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_strtod_subnormal_tests built cleanly.
  • CTest (guest_strtod_subnormal):
    1/1 Test #1333: guest_strtod_subnormal ...........   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Strings.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1333: guest_strtod_subnormal ...........   Passed    0.09 sec
    100% tests passed, 0 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified that std::bit_cast(bits & signMask) correctly preserves the sign bit and produces +0.0 or -0.0 without invoking any floating-point arithmetic or triggering DAZ flush-to-zero in Strings.cpp:585.
    • Verified bitwise check 'magnitude - 1 < minimumNormalBits - 1' correctly identifies non-zero subnormal double values in magnitude range [1, 0x000fffffffffffff] when minimumNormalBits is 0x0010000000000000. Zero magnitude wraps to UINT64_MAX, so 0.0 is correctly excluded.
    • Verified errno is properly set to ERANGE upon flushing to zero in ParseFloat, conforming to C/POSIX and PS5 libc strtod/wcstod specifications.
    • Verified no floating-point comparisons are performed that could be affected by Apple Clang or x86/ARM MXCSR DAZ/FTZ optimization passes.
    • Verified GuestStrtodSubnormal.cpp adds comprehensive verification for signed negative subnormal flushing across both strtod and wcstod via CheckBoth.
    • Verified strict compliance with AGENTS.md conventions (no disallowed comments, no made-up error codes, no special-casing of title IDs).

Looks Good To Me

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6104230741 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28284#pullrequestreview-6104230741). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-29T18:38:20Z. **Review:** APPROVED --- Verified on Linux (Ubuntu 24.04, GCC 13.3, Ninja, Release) against `8a70bddf1e`: - **Conventions**: `tools/check_conventions.py` passed. - **Build**: `guest_strtod_subnormal_tests` built cleanly. - **CTest** (`guest_strtod_subnormal`): ``` 1/1 Test #1333: guest_strtod_subnormal ........... Passed 0.09 sec 100% tests passed, 0 tests failed out of 1 ``` - **Negative control**: with `core/libs/prx/libc/src/Strings.cpp` reverted to `main` and the new tests kept, the suite fails: ``` 1/1 Test #1333: guest_strtod_subnormal ........... Passed 0.09 sec 100% tests passed, 0 tests failed out of 1 ``` Restoring the change makes it pass again. - **Code reading**: - Verified that std::bit_cast<double>(bits & signMask) correctly preserves the sign bit and produces +0.0 or -0.0 without invoking any floating-point arithmetic or triggering DAZ flush-to-zero in Strings.cpp:585. - Verified bitwise check 'magnitude - 1 < minimumNormalBits - 1' correctly identifies non-zero subnormal double values in magnitude range [1, 0x000fffffffffffff] when minimumNormalBits is 0x0010000000000000. Zero magnitude wraps to UINT64_MAX, so 0.0 is correctly excluded. - Verified errno is properly set to ERANGE upon flushing to zero in ParseFloat, conforming to C/POSIX and PS5 libc strtod/wcstod specifications. - Verified no floating-point comparisons are performed that could be affected by Apple Clang or x86/ARM MXCSR DAZ/FTZ optimization passes. - Verified GuestStrtodSubnormal.cpp adds comprehensive verification for signed negative subnormal flushing across both strtod and wcstod via CheckBoth. - Verified strict compliance with AGENTS.md conventions (no disallowed comments, no made-up error codes, no special-casing of title IDs). Looks Good To Me
MarcZX10 commented 2026-11-29 18:44:39 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-29T18:44:39Z.

Review: APPROVED


Reviewed — looks good

Nothing blocks this change; the inspected implementation compares the parsed double’s magnitude bits to the normal-range boundary and returns a zero carrying the original sign when flush-to-zero is enabled.

What I ran

  • Reviewed head 8a70bddf1e137336045fb9f49c29b467e0cc2f72 and the prepared diff; no checks were run.
  • Read core/libs/prx/libc/src/Strings.cpp (lines 525–610) and core/libs/tests/GuestStrtodSubnormal.cpp (full file). The full text of both changed files was included; no changed files were excluded.

Results

Check Result Note
Patch review passed Two changed files inspected
Tests not run Author reports local runs

Findings

Blocking

None.

Non-blocking

None.

Not verified

The author-reported test and convention-check results were not independently run or confirmed.

To get this to a good state

Nothing is required.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6104255301 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28284#pullrequestreview-6104255301). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-29T18:44:39Z. **Review:** APPROVED --- # Reviewed — looks good Nothing blocks this change; the inspected implementation compares the parsed double’s magnitude bits to the normal-range boundary and returns a zero carrying the original sign when flush-to-zero is enabled. ## What I ran - Reviewed head `8a70bddf1e137336045fb9f49c29b467e0cc2f72` and the prepared diff; no checks were run. - Read `core/libs/prx/libc/src/Strings.cpp` (lines 525–610) and `core/libs/tests/GuestStrtodSubnormal.cpp` (full file). The full text of both changed files was included; no changed files were excluded. ## Results | Check | Result | Note | |---|---|---| | Patch review | passed | Two changed files inspected | | Tests | not run | Author reports local runs | ## Findings ### Blocking None. ### Non-blocking None. ## Not verified The author-reported test and convention-check results were not independently run or confirmed. ## To get this to a good state Nothing is required.
NobleBrains commented 2026-11-30 23:53:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NobleBrains on 2026-11-30T23:53:42Z.

Review: APPROVED


Confirmed on macOS 15.

Setup: Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, BUILD_TESTING=ON. These are the OS and compiler generation of the macos-15 runner; the description tested macOS 27.
Tree: main 93d64f2d1e + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (main does not build on macOS without them, see #28168) + #22091 + #28369, then #28075, #28076 and #28284 merged one after another. Each merged cleanly.

guest_strtod_subnormal failures
before this PR (same tree) 3/3
with this PR at 8a70bddf1e 0/5

Full ctest -j 6 with all of them merged: 7038/7066 pass. Compared with the same tree before these PRs, no test fails that passed before. jsc_wtf_cpu_time aborted once under the parallel load, then passed 5/5 on its own; it is unrelated JavaScriptCore CPU-time code.

AI-assisted: yes (Claude Code)

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6122643950 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28284#pullrequestreview-6122643950). Originally posted by [NobleBrains](https://github.com/NobleBrains) on 2026-11-30T23:53:42Z. **Review:** APPROVED --- Confirmed on macOS 15. **Setup:** Apple M4, macOS 15.7.9, Apple clang 17.0.0 (clang-1700.6.3.2), x86_64 Release under Rosetta, `BUILD_TESTING=ON`. These are the OS and compiler generation of the `macos-15` runner; the description tested macOS 27. **Tree:** `main` `93d64f2d1e` + the open macOS build fixes #27201, #27739, #27843, #17425, #27827, #27767 (`main` does not build on macOS without them, see #28168) + #22091 + #28369, then #28075, #28076 and #28284 merged one after another. Each merged cleanly. | `guest_strtod_subnormal` | failures | |---|---| | before this PR (same tree) | 3/3 | | with this PR at `8a70bddf1e` | 0/5 | **Full `ctest -j 6`** with all of them merged: 7038/7066 pass. Compared with the same tree before these PRs, no test fails that passed before. `jsc_wtf_cpu_time` aborted once under the parallel load, then passed 5/5 on its own; it is unrelated JavaScriptCore CPU-time code. AI-assisted: yes (Claude Code)
ligmaSec commented 2026-12-03 06:03:52 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T06:03:52Z.

Review: COMMENTED


Built and ran the complete GuestStrtodSubnormal fixture on Linux at 8a70bddf1e. The setup tested subnormal results with the MXCSR flush-to-zero and denormals-are-zero settings, including the newly covered call with both settings enabled. The existing parsing and end-pointer cases also passed.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6164481341 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/28284#pullrequestreview-6164481341). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T06:03:52Z. **Review:** COMMENTED --- Built and ran the complete `GuestStrtodSubnormal` fixture on Linux at `8a70bddf1e`. The setup tested subnormal results with the MXCSR flush-to-zero and denormals-are-zero settings, including the newly covered call with both settings enabled. The existing parsing and end-pointer cases also passed. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/28284:upstream-pr/28284'
git switch 'upstream-pr/28284'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/28284'
git switch 'upstream-pr/28284'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/28284'
git switch 'upstream-pr/28284'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/28284'
git switch 'main'
git merge --squash 'upstream-pr/28284'
git switch 'main'
git merge --ff-only 'upstream-pr/28284'
git switch 'main'
git merge 'upstream-pr/28284'
git push origin 'main'
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!28284
No description provided.