This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Fix problem in FP constant folding


Hi,

The bottom line is that -ftrapping-math is not always honored:

function p return Float is
  X : Float;
  Y : Float;
  Result : Float;
begin
   X := Float'Last;
   Y := Float'Last;
   Result := X + Y;
   return Result;
end;

eric@linux:~/gcc -O -S p.adb -ftrapping-math
_ada_p:
.LFB3:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        flds    .LC0
        popl    %ebp
        ret

Excerpt from t31.ccp:

<bb 0>:
  x_1 = 3.4028234663852885981170418348451692544e+38;
  y_2 = 3.4028234663852885981170418348451692544e+38;
  result_3 =  Inf;
  result_4 =  Inf;
  result_5 =  Inf;
  return  Inf;

Tested on x86_64-suse-linux.  OK for mainline?


2005-11-10  Eric Botcazou  <ebotcazou@adacore.com>

	* fold-const.c (const_binop): Don't constant fold the operation
	if the result has overflowed and flag_trapping_math.
	* simplify-rtx.c (simplify_const_binary_operation): Likewise.


-- 
Eric Botcazou
Index: fold-const.c
===================================================================
--- fold-const.c	(revision 106481)
+++ fold-const.c	(working copy)
@@ -1537,6 +1537,16 @@ const_binop (enum tree_code code, tree a
       inexact = real_arithmetic (&value, code, &d1, &d2);
       real_convert (&result, mode, &value);
 
+      /* 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;
+
       /* Don't constant fold this floating point operation if the
 	 result may dependent upon the run-time rounding mode and
 	 flag_rounding_math is set, or if GCC's software emulation
Index: simplify-rtx.c
===================================================================
--- simplify-rtx.c	(revision 106481)
+++ simplify-rtx.c	(working copy)
@@ -2242,6 +2242,17 @@ simplify_const_binary_operation (enum rt
 				     &f0, &f1);
 	  real_convert (&result, mode, &value);
 
+	  /* 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 (f0)
+	      && !REAL_VALUE_ISINF (f1))
+	    /* Overflow plus exception.  */
+	    return 0;
+
 	  /* Don't constant fold this floating point operation if the
 	     result may dependent upon the run-time rounding mode and
 	     flag_rounding_math is set, or if GCC's software emulation

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]