Skip to content

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

Open
ibrahimmaq14 wants to merge 1 commit into
boykopovar:mainfrom
ibrahimmaq14:fix/libc-strtod-apple-clang-ftz
Open

ibrahimmaq14 wants to merge 1 commit into
boykopovar:mainfrom
ibrahimmaq14:fix/libc-strtod-apple-clang-ftz

Conversation

@ibrahimmaq14

@ibrahimmaq14 ibrahimmaq14 commented Nov 29, 2026 •

Copy link
Copy Markdown

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.

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: FTZ subnormal parsing

Layer / File(s) Summary
FTZ parsing and regression check
core/libs/prx/libc/src/Strings.cpp, core/libs/tests/GuestStrtodSubnormal.cpp
The FTZ check detects magnitudes below the minimum normal value and preserves the input sign when returning zero. The test asserts that parsing -1e-310 yields negative zero and sets errno to ERANGE.

Possibly related issues

  • boykopovar/AnyPS5#27647 reports the same failing guest_strtod_subnormal test. This PR changes the FTZ parsing check for that case, but does not trace the issue’s proposed MXCSR mask factor.

Suggested labels: libc, prx

Suggested reviewers: aasishdairelsahayagrinspan, rocklouis055, b-howell

Change: Bug fix

Merge Risk: Minimal · up to 8a70b

This change makes strtod flush decimal subnormals to signed zero with ERANGE when DAZ/FTZ is enabled, fixing the reported test failure. No actionable merge-blocking risk was found. The affected scope is small and covered by a targeted regression check.

Security architecture risk: Minimal · up to 8a70b

The fix makes decimal subnormal parsing behave as intended without adding an entry point or expanding what callers can access. Exact zero and hexadecimal parsing are unaffected by the changed branch.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 01e3593a-e430-4f29-a4ea-cd2e3925a4e9
Commits

Reviewing files that changed from the base of the PR and between 1f27e2a and 8a70bdd.

Files selected for processing: 2

  • core/libs/prx/libc/src/Strings.cpp
  • core/libs/tests/GuestStrtodSubnormal.cpp

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

@PrivacyOnion PrivacyOnion left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@JJenkx

JJenkx commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Linux check at exact head 8a70bddf1e13 (Arch Linux, RX 6650 XT, Release):

ctest -R strtod: 1/1 passed (guest_strtod_subnormal). No Vulkan instance is involved, so no validation claim.

AI-assisted: yes

@ycbuzi

ycbuzi commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Tested 8a70bddf1e merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Windows Server 2022 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 99% tests passed, 2 tests failed out of 7462; 99% tests passed, 2 tests failed out of 7462. 2 of the failing tests fail on main too. run
  • Windows Server 2025 (GitHub-hosted runner), full build, WinLibs GCC 15.2.0: 100% tests passed, 0 tests failed out of 7462; 100% tests passed, 0 tests failed out of 7462. run

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@github-actions

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 8965 core/libs/prx/libc/src/Strings.cpp

@NobleBrains NobleBrains left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 ligmaSec left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@Plyxen Plyxen left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I read the diff at 8a70bddf1e. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at 6c6b29320fe together with other open PRs (so not an isolated build).

  • guest_strtod_subnormal: pass on both.
  • Control (the same tree with only this PR's source change taken out): it passes too on both, as expected: the body describes an Apple Clang optimization, and GCC did not have the problem. So this shows no regression, not the fix.

Not checked: macOS.

AI-assisted: yes (Claude Code)

@NobleBrains

Copy link
Copy Markdown
Contributor

Host: macOS 15.7.9, Apple M4, x86_64 Release build under Rosetta 2, BUILD_TESTING=ON. The tree was main e8f09d2877 plus the open macOS build fixes #27201, #27767, #27739 and #27843, so it is not a build of this branch alone. This PR's diff (gh pr diff, head 8a70bddf1e) was applied on top. The other two PRs I was testing in the same tree touch different files.

guest_strtod_subnormal: aborts without this PR (FTZ flushes an inexact decimal subnormal), passes with it. I removed only this diff again to check, and the test aborted again.

guest_math still aborts on this host with the PR applied. Before the PR it aborted at GuestMath.cpp:5478 (an lgammaf_r check that expects flags 0x9fe0). I did not check whether the line is the same with the PR, so I only note that this PR does not make that test pass.

The full ctest -j2 run (7152 tests) had no other new failure from this change. I did not test natively on arm64 or on Intel hardware, and no title was run.

AI-assisted: yes (Claude Code)

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants