Bug 52907 - Underflowing floating point expressions wrongly folded to zero
Summary: Underflowing floating point expressions wrongly folded to zero
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-04-08 21:44 UTC by Joseph S. Myers
Modified: 2024-03-11 00:09 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2024-03-10 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph S. Myers 2012-04-08 21:44:25 UTC
fold-const.c:const_binop avoids folding overflowing floating-point operations on constant operands to constants if flag_trapping_math:

      /* Don't constant fold this floating point operation if
         the result has overflowed and flag_trapping_math.  */
      if (flag_trapping_math
          && MODE_HAS_INFINITIES (mode)
          && REAL_VALUE_ISINF (result)
          && !REAL_VALUE_ISINF (d1)
          && !REAL_VALUE_ISINF (d2))
        return NULL_TREE;

However, there is no such check for underflow.  This has the effect of miscompiling various code in glibc's libm that expects operations on constant values to be usable to produce an overflow or underflow exception when required.  In particular, this appears to cause <http://sourceware.org/bugzilla/show_bug.cgi?id=10846>.  Testcase, tested x86 and x86_64 (-O2 -ftrapping-math, wrongly compiled to return 0 rather than doing the given computation):

static const double a = 1e-300;
double f (void) { return a * a; }
Comment 1 Richard Biener 2012-04-10 12:18:12 UTC
Does real.c even communicate this fact?
Comment 2 jsm-csl@polyomino.org.uk 2012-04-23 20:02:50 UTC
On Tue, 10 Apr 2012, rguenth at gcc dot gnu.org wrote:

> Does real.c even communicate this fact?

You can identify underflow from the result of multiplication or division 
being zero or subnormal (unless an operand is zero or infinite), just as 
overflow is identified from an infinite result.  Information about 
subnormal results may not be conveniently communicated by real.c, but 
simply handling zero results here would be enough for the use cases in 
glibc.
Comment 3 Andrew Pinski 2024-03-11 00:09:46 UTC
Still happens.