Bug 57245 - Floating-point constant truncation ignores -frounding-math
Summary: Floating-point constant truncation ignores -frounding-math
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: tree-optimization (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: ---
Assignee: Richard Biener
URL:
Keywords: wrong-code
: 80450 (view as bug list)
Depends on: 103735
Blocks: 16989 84407
  Show dependency treegraph
 
Reported: 2013-05-10 20:35 UTC by Joseph S. Myers
Modified: 2023-11-04 05:46 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2013-05-13 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 2013-05-10 20:35:07 UTC
When a floating-point constant is converted to a narrower floating-point type, this conversion is meant to respect the current rounding direction, but GCC does the conversion at compile time even with -frounding-math.  Example (tested x86_64 and ARM):

float f;
void func (void) { f = 1.3L; }

fold_convert_const_real_from_real or its caller should avoid such conversions if -frounding-math, unless the conversion is exact.
Comment 1 Richard Biener 2013-05-13 08:44:45 UTC
Confirmed.  Note that some callers may expect a constant result anyway
(see the TREE_OVERFLOW setting which seems to be overflow indicating...).
So fixing this may not be easy without fallout.
Comment 2 Joseph S. Myers 2017-04-20 10:32:57 UTC
*** Bug 80450 has been marked as a duplicate of this bug. ***
Comment 3 nsz 2017-04-20 10:56:38 UTC
note that this may cause the omission of underflow, overflow and inexact
exceptions too (so in principle it's an invalid transformation even
without -frounding-math but with -ftrapping-math ):

float x,y;
void f(void)
{
  x = 0x1p-1000; // truncated to 0
  y = 0x1p1000; // truncated to inf
}
Comment 4 Richard Biener 2021-10-27 12:31:17 UTC
I have a patch fixing fold_convert_const_real_from_real
Comment 5 GCC Commits 2021-10-28 09:29:02 UTC
The master branch has been updated by Richard Biener <rguenth@gcc.gnu.org>:

https://gcc.gnu.org/g:a84b9d5373c7e67fd0ab2a412c22162cdf969c91

commit r12-4764-ga84b9d5373c7e67fd0ab2a412c22162cdf969c91
Author: Richard Biener <rguenther@suse.de>
Date:   Wed Oct 27 14:27:40 2021 +0200

    middle-end/57245 - honor -frounding-math in real truncation
    
    The following honors -frounding-math when converting a FP constant
    to another FP type.
    
    2021-10-27  Richard Biener  <rguenther@suse.de>
    
            PR middle-end/57245
            * fold-const.c (fold_convert_const_real_from_real): Honor
            -frounding-math if the conversion is not exact.
            * simplify-rtx.c (simplify_const_unary_operation): Do not
            simplify FLOAT_TRUNCATE with sign dependent rounding.
    
            * gcc.dg/torture/fp-double-convert-float-1.c: New testcase.
Comment 6 Richard Biener 2021-10-28 09:29:40 UTC
This aspect is fixed.  For fp<->int converts see PR84407.