]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
fold-const.c (tree_expr_nonnegative_p): Add support for floating point constants...
authorRoger Sayle <roger@eyesopen.com>
Thu, 12 Jun 2003 12:53:01 +0000 (12:53 +0000)
committerRoger Sayle <sayle@gcc.gnu.org>
Thu, 12 Jun 2003 12:53:01 +0000 (12:53 +0000)
* fold-const.c (tree_expr_nonnegative_p): Add support for
floating point constants, addition and multiplication.

* gcc.dg/builtins-21.c: New test case.

From-SVN: r67828

gcc/ChangeLog
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/builtins-21.c [new file with mode: 0644]

index b738ffb6eece564bd23a5341522bced8c93e00cb..87d86d7217d5ce27624a8426439b93c55b2f8aa0 100644 (file)
@@ -1,3 +1,8 @@
+2003-06-12  Roger Sayle  <roger@eyesopen.com>
+
+       * fold-const.c (tree_expr_nonnegative_p): Add support for
+       floating point constants, addition and multiplication.
+
 2003-06-12  J"orn Rennecke <joern.rennecke@superh.com>
 
        * sh.md (adddi3_compact, subdi3_compact): Add earlyclobber
index 927b4d7410c5ddf0e3b33caa80f0152d838e60e8..b3271a2c6dadc33e4547738961e1ca913d2dcfdc 100644 (file)
@@ -8014,9 +8014,29 @@ tree_expr_nonnegative_p (t)
         C[LT]Z_DEFINED_VALUE_AT_ZERO is set, since what we're
         computing here is a user-visible property.  */
       return 0;
-      
+
     case INTEGER_CST:
       return tree_int_cst_sgn (t) >= 0;
+
+    case REAL_CST:
+      return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
+
+    case PLUS_EXPR:
+      return FLOAT_TYPE_P (TREE_TYPE (t))
+            && tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
+            && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
+
+    case MULT_EXPR:
+      if (FLOAT_TYPE_P (TREE_TYPE (t)))
+       {
+         /* x * x for floating point x is always non-negative.  */
+         if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
+           return 1;
+         return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
+                && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
+       }
+      return 0;
+
     case TRUNC_DIV_EXPR:
     case CEIL_DIV_EXPR:
     case FLOOR_DIV_EXPR:
index acd4e47aab1eec8b28b9ed76e377368b29b60fc0..704694ab33c69e16400192e07d17ccc1d44b067b 100644 (file)
@@ -1,3 +1,7 @@
+2003-06-12  Roger Sayle  <roger@eyesopen.com>
+
+       * gcc.dg/builtins-21.c: New test case.
+
 2003-06-11  Kelley Cook  <kelleycook@wideopenwest.com>
 
        * lib/gcc.exp (gcc_target_compile): Put TOOL_OPTIONS at front of
diff --git a/gcc/testsuite/gcc.dg/builtins-21.c b/gcc/testsuite/gcc.dg/builtins-21.c
new file mode 100644 (file)
index 0000000..38d0c4f
--- /dev/null
@@ -0,0 +1,55 @@
+/* Copyright (C) 2003  Free Software Foundation.
+
+   Verify that built-in math function constant folding doesn't
+   cause any problems for the compiler.
+
+   Written by Roger Sayle, 7th June 2003.  */
+
+/* { dg-do compile } */
+/* { dg-options "-O2 -ffast-math" } */
+
+double test1(double x)
+{
+  return fabs(x*x);
+}
+
+double test2(double x)
+{
+  return fabs(sqrt(x)+2.0);
+}
+
+double test3(double x)
+{
+  return fabs(3.0*exp(x));
+}
+
+float test1f(float x)
+{
+  return fabsf(x*x);
+}
+
+float test2f(float x)
+{
+  return fabsf(sqrtf(x)+2.0f);
+}
+
+float test3f(float x)
+{
+  return fabsf(3.0f*expf(x));
+}
+
+long double test1l(long double x)
+{
+  return fabsl(x*x);
+}
+
+long double test2l(long double x)
+{
+  return fabsl(sqrtl(x)+2.0l);
+}
+
+long double test3l(long double x)
+{
+  return fabsl(3.0l*expl(x));
+}
+