Skip to content

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

Open
Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/28177
Open

Thimo188 wants to merge 1 commit into
mainfrom
upstream-pr/28177

Conversation

@Thimo188

@Thimo188 Thimo188 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Imported from boykopovar/AnyPS5#28177, originally opened by kiansaeidnia.

Source commit: 811ab75440cc6f06c07a0b24cae245e491b13762. The import branch preserves this fetched upstream PR snapshot. The description and validation below are copied from upstream; no new code changes or test runs were made for this import. Original reviews, discussions, and attachments remain at the linked upstream PR.


Part of boykopovar#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 boykopovar#24160 did for exp2. This is the reading in the rounding-mode section of boykopovar#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 boykopovar#10113, boykopovar#21714 and boykopovar#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 boykopovar#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 boykopovar#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 boykopovar#6295 and boykopovar#21714, which conflict with main. I read both diffs: boykopovar#6295 adds the nextafter family and boykopovar#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 boykopovar#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 the handling of very large and very small inputs to exponential and hyperbolic cosine calculations, including behavior under different rounding modes.
    • Overflow results and related error reporting are now more consistent, helping avoid unexpected numeric results in applications that rely on these calculations.

…nding mode

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

coderabbitai Bot commented Dec 2, 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 behavior and rounding-mode tests
core/libs/prx/libc/src/Math.cpp, core/libs/tests/GuestMath.cpp
The exp and cosh overflow paths use volatile intermediate products and explicitly produce positive infinity. Tests check overflow and underflow behavior, finite cosh results, errno, and floating-point exception flags under all four rounding modes.

Suggested reviewers: jjenkx, derkarussellbremser

Change: Bug fix

Merge Risk: Minimal · up to 811ab

This change makes exp and cosh return positive infinity on overflow in every rounding mode, and adds tests for that behavior. No concrete merge-blocking risk was found.

Security architecture risk: Minimal · up to 811ab

Guest libc now returns positive infinity from the affected exp and cosh overflow branches in all four rounding modes. Callers that previously received the largest finite double under downward or toward-zero rounding will see a different result, but the change does not expand access or require deployment or data migration.

Security review details

Security Blast Radius

  • inferred — The demonstrated production effect is limited to callers that already reach the two overflow branches. Those callers can observe a different numeric result and, for cosh, a different errno value; the changed code adds no privileged operation or external data destination.
Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Usage-based review receipt

  • Mode: Continue automatically
  • Reviewed files: 2
  • Waived: $0.50 (charged $0.00)
  • View usage details

Note

This review exceeded your plan’s limits and used usage-based reviews—free during trial, billed after paid activation unless disabled. Manage usage-based reviews.

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: c8a0e91e-e1a2-4b0c-8322-2021f225ea48
Commits

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

Files selected for processing: 2

  • core/libs/prx/libc/src/Math.cpp
  • core/libs/tests/GuestMath.cpp

Limit details: You’ve used all 10 included reviews currently available.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants