]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
re PR middle-end/19775 (sqrt(pow(x,y)) != pow(x,y*0.5) (with -ffast-math))
authorRichard Guenther <rguenth@gcc.gnu.org>
Thu, 3 Feb 2005 17:47:33 +0000 (17:47 +0000)
committerRichard Biener <rguenth@gcc.gnu.org>
Thu, 3 Feb 2005 17:47:33 +0000 (17:47 +0000)
PR middle-end/19775

* builtins.c (fold_builtin_sqrt): Transform sqrt(pow(x,y))
to pow(fabs(x),y*0.5), not pow(x,y*0.5).

* gcc.dg/builtins-10.c: Disable test for invalid transformation
and one test we no longer optimize.
* gcc.dg/builtins-47.c: New testcase.

From-SVN: r94665

gcc/ChangeLog
gcc/builtins.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/builtins-10.c
gcc/testsuite/gcc.dg/builtins-47.c [new file with mode: 0644]

index 89d15d54b15e03495c6eea97d168bcddd3127703..1dc084d7fff6793afe169b75b0b41acb7e679445 100644 (file)
@@ -1,3 +1,11 @@
+2005-02-03  Richard Guenther  <rguenth@gcc.gnu.org>
+
+       PR middle-end/19775
+
+       * builtins.c (fold_builtin_sqrt): Transform
+       sqrt(pow(x,y)) to pow(fabs(x),y*0.5), not
+       pow(x,y*0.5).
+
 2005-02-01  Richard Earnshaw  <rearnsha@arm.com>
 
        PR target/16201
index 4cb42cc61e248f895a32492444cf833f2fd06517..a3e069e4baca087d2dde7d2d028c96c69b418094 100644 (file)
@@ -6584,7 +6584,7 @@ fold_builtin (tree exp)
              return build_function_call_expr (expfn, arglist);
            }
 
-         /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5).  */
+         /* Optimize sqrt(pow(x,y)) = pow(|x|,y*0.5).  */
          if (flag_unsafe_math_optimizations
              && (fcode == BUILT_IN_POW
                  || fcode == BUILT_IN_POWF
@@ -6593,8 +6593,11 @@ fold_builtin (tree exp)
              tree powfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
              tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
              tree arg1 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg, 1)));
-             tree narg1 = fold (build (MULT_EXPR, type, arg1,
-                                       build_real (type, dconsthalf)));
+             tree narg1;
+             if (!tree_expr_nonnegative_p (arg0))
+               arg0 = build1 (ABS_EXPR, type, arg0);
+             narg1 = fold (build (MULT_EXPR, type, arg1,
+                                  build_real (type, dconsthalf)));
              arglist = tree_cons (NULL_TREE, arg0,
                                   build_tree_list (NULL_TREE, narg1));
              return build_function_call_expr (powfn, arglist);
index ee8ddc8bea8a9433fad91d2555dad6de9bb3d76a..11a23843310e9b15618a93b36fb08a6a6025f9e7 100644 (file)
@@ -1,3 +1,12 @@
+2005-02-03  Richard Guenther  <rguenth@gcc.gnu.org>
+
+       PR middle-end/19775
+
+       * gcc.dg/builtins-10.c: Disable test for
+       invalid transformation and one test we no
+       longer optimize.
+       * gcc.dg/builtins-47.c: New testcase.
+
 2005-02-01  Alexandre Oliva  <aoliva@redhat.com>
 
        * g++.dg/parse/typename7.C: Adjust error messages.
index 9e5a4583fc3ade7e789b8b2d873edda033e71f24..7070f5eeb9470f81874210ad456ab2bda1e4537d 100644 (file)
@@ -14,11 +14,12 @@ extern double exp(double);
 extern double log(double);
 extern double sqrt(double);
 extern double pow(double,double);
+extern double fabs(double);
 
 void test(double x)
 {
-  if (sqrt(pow(x,4.0)) != x*x)
-    link_error ();
+  /*if (sqrt(pow(x,4.0)) != x*x)
+    link_error ();*/
 
   if (pow(sqrt(x),4.0) != x*x)
     link_error ();
@@ -29,7 +30,7 @@ void test(double x)
 
 void test2(double x, double y, double z)
 {
-  if (sqrt(pow(x,y)) != pow(x,y*0.5))
+  if (sqrt(pow(x,y)) != pow(fabs(x),y*0.5))
     link_error ();
 
   if (log(pow(x,y)) != y*log(x))
diff --git a/gcc/testsuite/gcc.dg/builtins-47.c b/gcc/testsuite/gcc.dg/builtins-47.c
new file mode 100644 (file)
index 0000000..79c4f84
--- /dev/null
@@ -0,0 +1,16 @@
+/* { dg-do run } */
+/* { dg-options "-ffast-math" } */
+
+extern double sqrt (double);
+extern double pow (double, double);
+extern void abort (void);
+
+int main ()
+{
+  double x = -1.0;
+  if (sqrt (pow (x, 2)) != 1.0)
+    abort();
+  if (sqrt (x*x) != 1.0)
+    abort();
+  return 0;
+}