Skip to content

fix(libc): quiet signalling NaNs in round as FreeBSD msun's s_round.c does - #12006

Closed
xXPBSXx wants to merge 1 commit into
boykopovar:mainfrom
xXPBSXx:libm/round-snan
Closed

xXPBSXx wants to merge 1 commit into
boykopovar:mainfrom
xXPBSXx:libm/round-snan

Conversation

@xXPBSXx

@xXPBSXx xXPBSXx commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

What

round returned the host's result. On Windows (UCRT) that passes a signalling NaN through unchanged. FreeBSD 11.4's s_round.c returns it quieted, with its payload and sign: it returns x + x for infinities and for NaNs with a zero high mantissa word, and otherwise goes through floor, which returns x + x for a NaN (s_floor.c). For example, round(0x7ff0000000000001) is 0x7ff8000000000001 there and 0x7ff0000000000001 here.

round now returns x + x for a NaN argument and calls the host otherwise, as trunc, ceil, floor and nearbyint do since #10370. The effect is limited to signalling NaNs on Windows/UCRT: on Linux with glibc 2.39 the host already quiets them (0 differences in 4,000,000 random arguments), and no result for any other argument changes. This claims no benefit for any title. The libc.prx that titles ship has not been measured for round; #10370 compared its eight functions with titles' copies, this change follows msun's source only. roundf is unchanged: it matched msun on both hosts in the same 4,000,000 arguments.

The test guest_math gets CheckRoundNaNBits: 22 arguments with the bits of a local build of msun's round as the expected values (signalling NaNs of both signs with payloads, quiet NaNs of both signs, both infinities, both zeros, 1, 2.5, -2.5, 0.5, -0.5, 1.5, -1.5, the largest double below 0.5 and its negative, the smallest subnormal and DBL_MAX).

Tested

  • Windows 10 Pro 19045, MinGW-w64 GCC 15.2.0 (UCRT), Release, this head on main e0cd1259a9 (ninja exit code 0): guest_math passes 5 of 5 runs; guest_math_exact and guest_dinkumware_internals pass.
  • With Math.cpp of main (the change reverted) and the new test, guest_math fails on Windows (exit code 0xc0000409) with "Guest round failed for 7ff0000000000001: got 7ff0000000000001, expected 7ff8000000000001".
  • Five deliberate defects in round_nid_postfix, each making guest_math fail on Windows with the failing argument in the message: the NaN check removed, the check true only for positive NaNs, true only for quiet NaNs, a NaN returned as 0, and trunc instead of round.
  • Ubuntu 24.04 under WSL2 (a virtualised kernel, not bare-metal), GCC 13.3, Release (-O3): guest_math passes 5 of 5 runs, and guest_math_exact and guest_dinkumware_internals pass. It passes before and after the change, because glibc already quiets the NaN, so the NaN checks have no effect there; of the five defects only the NaN-to-0 and the trunc ones fail on Linux.
  • The expected values come from a local build of FreeBSD 11.4's lib/msun (BSD licence; not in the repository) with all its symbols renamed, compared on random arguments with the host's round; that measures these hosts against msun, not a console or a title.
  • Not run: macOS, bare-metal Linux, a sanitizer build, a console or a title, roundl and the floating-point exception flags.

Overlap

No open PR changes round_nid_postfix, and round has no other open or merged PR; #10370 (merged) did the same for trunc, ceil, floor and nearbyint. 49 of the 1,321 open PRs touch Math.cpp or GuestMath.cpp, and 4 more list 100 or more files and were checked as well; git merge-tree of this head with each of the 53: 45 merge without a conflict and 8 conflict, all 8 of which already conflict with main on their own. None conflicts only with this change, so there is no Depends on.

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: none
  • GPUs tested: n/a
  • AI-assisted: yes

round returned the host's result. The Windows UCRT passes a signalling
NaN through unchanged, while msun's round returns it quieted, with its
payload and sign (through floor, which returns x + x for a NaN). Return
x + x for a NaN as trunc, ceil, floor and nearbyint here already do;
every other argument still goes to the host. The new test pins 22
inputs to the bits a local build of msun returned.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Oct 27, 2026 •

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 6085 core/libs/prx/libc/src/Math.cpp
PR 6295 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 8937 core/libs/prx/libc/src/Math.cpp
PR 9867 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10113 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10222 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10226 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10229 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10245 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10247 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10250 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10253 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10265 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 10349 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 12053 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp
PR 12122 core/libs/prx/libc/src/Math.cpp
core/libs/tests/GuestMath.cpp

@github-actions github-actions Bot added the libc label Oct 27, 2026
@JJenkx

JJenkx commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

A runtime check of this head (e5686ca265) against the libc.prx that titles ship, which suggests the titles don't quiet signalling NaNs in round.

Setup: a scratch loader runs the decrypted libc.prx of PPSA07923 natively on Windows 11 and calls its round and roundf by NID. It compares exact result bits (NaN payloads included) and errno with this PR's Windows build and with main d13b3d7c55 (both WinLibs GCC 15.2.0 UCRT r7, Release; guest_math passes on this head).

argument (bits) titles' round main this PR
0x7ff0000000000001 (sNaN) 0x7ff0000000000001 0x7ff0000000000001 0x7ff8000000000001
0xfff0000000000001 (sNaN) 0xfff0000000000001 0xfff0000000000001 0xfff8000000000001
0x7ff4000000000000 (sNaN) 0x7ff4000000000000 0x7ff4000000000000 0x7ffc000000000000
quiet NaNs, ±inf, -0, ±0.5 unchanged / as expected same same

errno stays 7 everywhere on all three. So on this title the result is the input NaN unchanged, which main already matches. With this PR, round differs from the titles on exactly these three arguments. roundf on 0x7f800001, 0xff800001 and 0x7fa00000 also returns the input unchanged on the titles, and main and this PR agree with that.

Update: the libc.prx of PPSA05916 and of PPSA13934 (an FMA-contracted build) give the same results. main matches all three titles on every argument above, so this applies across both families of title copies. I haven't checked a console. If the msun source quiets them but the titles' build doesn't (for example because the compiler turned x + x into a move, or the titles use a different round), the titles' copy is probably the better target here.

AI-assisted: yes (Claude Code).

@xXPBSXx

xXPBSXx commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Thank you for running this differential. This change rested only on msun's s_round.c and a host measurement, and our description said the titles were not measured. Your data show that the titles return a signalling NaN from round unchanged, which main already matches, so the change would move main away from them; we are closing this PR and leaving the branch in place.

AI-assisted (Claude Code).

@xXPBSXx xXPBSXx closed this Oct 28, 2026
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.

2 participants