Skip to content

fix(libc): return infinity from exp and cosh on overflow in every rounding mode - #28177

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
kiansaeidnia:fix/libc-exp-cosh-overflow-rounding
Dec 5, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
kiansaeidnia:fix/libc-exp-cosh-overflow-rounding

Conversation

@kiansaeidnia

@kiansaeidnia kiansaeidnia commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Part of #23411.

What

exp and cosh returned the largest finite double on overflow when rounding down or toward zero, because their overflow branches returned the run-time product huge * huge. cosh then also left errno alone, because it sets ERANGE only for an infinite result.

Both now evaluate the product for its flags and return +inf, as #24160 did for exp2. This is the reading in the rounding-mode section of #23411: on overflow the libc.prx bundled with PPSA10122 returns +inf with ERANGE, FE_OVERFLOW and FE_INEXACT in all four modes. The underflow branches are unchanged, since that reading has them follow the rounding mode.

expf and coshf are not touched; open #10113, #21714 and #11109 hold them.

The only callers that can pass exp an overflowing argument are outside Math.cpp: sinh, cosh and the erf tail stay below the threshold. Open #28071 adds callers of exp and cosh for the complex functions; in the two directed modes those would now see +inf where they saw DBL_MAX.

In the thread, the overflow rows were asked for on 19 November and exp2 followed as #24160. I found no PR or branch for exp or cosh since, so I took these two; say so if that work is still in progress and I will close this.

tools/pr_overlap.py could not compare #6295 and #21714, which conflict with main. I read both diffs: #6295 adds the nextafter family and #21714 changes expf and tgammaf; neither changes exp or cosh.

Tested

Windows 11, MinGW-w64 GCC 16.2, Release. guest_math, guest_math_exact, guest_math_elementary, guest_complex_trig and guest_complex_trig_long pass. The new CheckExpCoshOverflowRounding checks exp(709.9), cosh(711) and cosh(-711) in the four rounding modes, and pins exp(-745.2) and cosh(710.4) as they were. Without the Math.cpp change guest_math aborts in that check.

I do not have the bundled libc.prx, so the module's behaviour is taken from #23411 and was not run here.

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

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Improved exp and cosh behavior when results overflow, ensuring they return positive infinity consistently.
    • Improved consistency of results and floating-point status flags across rounding modes for extreme exp and cosh inputs.

…nding mode

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>

@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 811ab75440:

  • Conventions: tools/check_conventions.py passed.
  • Build: guest_math_tests built cleanly.
  • CTest (guest_math):
    1/1 Test #1329: guest_math .......................   Passed    0.01 sec
    100% tests passed, 0 tests failed out of 1
    
  • Negative control: with core/libs/prx/libc/src/Math.cpp reverted to main and the new tests kept, the suite fails:
    1/1 Test #1329: guest_math .......................Subprocess aborted***Exception:   0.12 sec
    0% tests passed, 1 tests failed out of 1
    
    Restoring the change makes it pass again.
  • Code reading:
    • Verified core/libs/prx/libc/src/Math.cpp:1717-1723 (exp_nid_postfix) sets errno = ERANGE, evaluates volatile huge * huge to raise FE_OVERFLOW and FE_INEXACT across all rounding modes, and returns +infinity
    • Verified core/libs/prx/libc/src/Math.cpp:3765-3773 (cosh_nid_postfix) evaluates volatile huge * huge to raise FE_OVERFLOW and FE_INEXACT, sets result to +infinity, and correctly sets errno = ERANGE via std::isinf(result) across all rounding modes
    • Verified core/libs/tests/GuestMath.cpp:832-857 restores floating-point environment via RestoreFloatingPointState, iterates over all four rounding modes (FE_TONEAREST, FE_UPWARD, FE_DOWNWARD, FE_TOWARDZERO), and validates errno, return values (+inf), and exception flags
    • Checked AGENTS.md rules: no prohibited comments added, conventions gate passed, no extra untracked files added, Conventional Commit title used
    • Checked LP64 vs LLP64 portability and verified types (double, volatile double, std::size_t) are portable across Linux and Windows

Looks Good To Me

@coderabbitai

coderabbitai Bot commented Nov 29, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Changes: Exponential and hyperbolic cosine overflow

Layer / File(s) Summary
Overflow handling and rounding tests
core/libs/prx/libc/src/Math.cpp, core/libs/tests/GuestMath.cpp
The exp and cosh overflow paths evaluate the overflow product through a volatile temporary and explicitly return or assign positive infinity. Tests check overflow results, flags, and errno across rounding modes, plus upward-rounding underflow and a finite cosh result.

Possibly related issues

  • Issue 23411 surveys math flags and errno for the bundled libc, including coshf overflow, and the PR states that its rounding-mode behavior informs this change.

Possibly related PRs

  • boykopovar/AnyPS5#10113: Changes expf overflow behavior to raise the overflow flag and return positive infinity, a related contract for a sibling function.
  • boykopovar/AnyPS5#28071: Ports complex exponential and hyperbolic functions whose scaled-exp paths interact with the changed exp overflow behavior.

Suggested reviewers: jjenkx, marczx10, derkarussellbremser

Change: Bug fix

Merge Risk: Minimal · up to 811ab

exp and cosh now return positive infinity on overflow in every rounding mode, and cosh also sets ERANGE. A regression test covers the four rounding modes. No merge-blocking risk was identified.

Security architecture risk: Minimal · up to 811ab

The change is limited to what existing callers receive on overflow. It does not introduce a new access path or authority, and the review found no material security or deployment risk from the change.

Retained concerns
No architecture-level concerns identified.

Security review details

Security Blast Radius

  • inferred — The direct effect is limited to existing callers whose inputs reach the changed overflow branches. The diff adds no production caller or input route; downstream guest callers are not comprehensively mapped.

Trust Boundaries and Controls

  • observed — The routed GuestMath entrypoint change adds a call to a regression test with fixed inputs and rounding modes, not a production dispatch path.
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: d9239175-4294-444f-87e9-86e070876dd0
Commits

Reviewing files that changed from the base of the PR and between ffc6f26 and 811ab75.

Files selected for processing: 2

  • core/libs/prx/libc/src/Math.cpp
  • core/libs/tests/GuestMath.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.

@JJenkx

JJenkx commented Nov 29, 2026

Copy link
Copy Markdown
Contributor

Linux check of the exact head 811ab75440cc; the description has no Linux results. Arch Linux 7.3.5 x86-64, GCC 16.2.1, Release, -DBUILD_TESTING=ON:

  • ctest -R '^guest_math' (this PR's GuestMath.cpp): 4 pass, 0 skipped.

AI-assisted: yes

@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 overflow paths return positive infinity and preserve overflow flags across rounding modes.

What I ran

Reviewed head 811ab75440cc6f06c07a0b24cae245e491b13762. I read review/description.md, review/commits.md, review/manifest.json, review/diff.patch, and the full text of both changed files, core/libs/prx/libc/src/Math.cpp and core/libs/tests/GuestMath.cpp. I used targeted repository searches for the changed functions and their supporting declarations. No commands or tests were run in this review.

Results

Check Result Note
Changed source and test review passed Both changed files read
Runtime tests not run Author reports Windows 11 / MinGW GCC 16.2 results
Bundled module behavior not run Author reports no local libc.prx

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently run the reported test suites or verify behavior against the bundled libc.prx. review/files.json was not available as a review artifact; review/manifest.json identifies both changed files and indicates their full text was included and read.

To get this to a good state

Nothing is required.

@kripotker kripotker 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.

Review: fix(libc): return infinity from exp and cosh on overflow in every rounding mode

Approve.

What I checked

  • Overflow branches. Both now evaluate huge * huge into a volatile, which still raises FE_OVERFLOW and FE_INEXACT, and return +inf independent of the rounding mode. This matches the #23411 reading the description cites and the exp2 change in #24160.
  • errno in cosh. It now gets ERANGE in the directed modes too, because std::isinf(result) is true.
  • Unchanged paths.
    • cosh's 0.5 * w * w path below the overflow threshold is untouched, and cosh(710.4) is pinned as before.
    • The underflow branch of exp is unchanged.
  • Sabotage check: two small branch changes plus a test.

What I ran

Windows 11, MSYS2 UCRT64 GCC 16.2.0, Release/Ninja, with PR head 811ab75's Math.cpp and GuestMath.cpp in a built tree:

Files ctest -R '^guest_math$'
PR's Math.cpp and GuestMath.cpp passes
main's Math.cpp with the PR's test fails at GuestMath.cpp:846, the new check

check_conventions.py: All conventions checks passed.

I don't have the bundled libc.prx, so the console behaviour is taken from #23411.

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 guest_math on Linux with the change applied. The fixture passed the exp and cosh overflow checks in all four rounding modes, including ERANGE, positive infinity, and the overflow and inexact flags. The large finite cosh and exp underflow 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 811ab75440. Windows 11 with GCC 15.2 and Linux (Ubuntu 24.04 in WSL2, GCC 13.3), merged into main at b97867d3ce6 together with other open PRs (so not an isolated build).

  • guest_math: pass on both.
  • Control (same tree and tests, the changed files of those PRs put back to main's): fails on both at GuestMath.cpp:914.

Not checked: a title.

AI-assisted: yes (Claude Code)

@github-actions

github-actions Bot commented Dec 5, 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 28071 core/libs/prx/libc/src/Math.cpp

@oneandonlydean
oneandonlydean merged commit 2c16c0c into boykopovar:main Dec 5, 2026
23 of 25 checks passed
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