]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
builtins.c (fold_builtin): Add new builtin optimizations for sqrt and/or cbrt.
authorKaveh R. Ghazi <ghazi@caip.rutgers.edu>
Thu, 25 Mar 2004 17:51:17 +0000 (17:51 +0000)
committerKaveh Ghazi <ghazi@gcc.gnu.org>
Thu, 25 Mar 2004 17:51:17 +0000 (17:51 +0000)
* builtins.c (fold_builtin): Add new builtin optimizations for
sqrt and/or cbrt.
* fold-const.c (fold): Likewise.

testsuite:
* gcc.dg/torture/builtin-explog-1.c: Add new cases.
* gcc.dg/torture/builtin-math-1.c: Likewise.
* builtin-power-1.c: New test.

From-SVN: r79959

gcc/ChangeLog
gcc/builtins.c
gcc/fold-const.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/torture/builtin-explog-1.c
gcc/testsuite/gcc.dg/torture/builtin-math-1.c
gcc/testsuite/gcc.dg/torture/builtin-power-1.c [new file with mode: 0644]

index 5a7d4d24bc3eb662916f778b0746d8bf13931890..d314638bd73cfa3420a56efc84a6640bb8ef7357 100644 (file)
@@ -1,3 +1,9 @@
+2004-03-25  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * builtins.c (fold_builtin): Add new builtin optimizations for
+       sqrt and/or cbrt.
+       * fold-const.c (fold): Likewise.
+
 2004-03-25  David Edelsohn  <edelsohn@gnu.org>
 
        * config/rs6000/rs6000.c (rs6000_always_hint): New variable.
index fd1b2950da42271871de762ebd7d2d7833e9a07d..f0ac04f89f9aa85c14bf136194199ca8ba4929a1 100644 (file)
@@ -6690,6 +6690,29 @@ fold_builtin (tree exp)
              return build_function_call_expr (expfn, arglist);
            }
 
+         /* Optimize sqrt(Nroot(x)) -> pow(x,1/(2*N)).  */
+         if (flag_unsafe_math_optimizations && BUILTIN_ROOT_P (fcode))
+           {
+             tree powfn = mathfn_built_in (type, BUILT_IN_POW);
+             
+             if (powfn)
+               {
+                 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
+                 tree tree_root;
+                 /* The inner root was either sqrt or cbrt.  */
+                 REAL_VALUE_TYPE dconstroot =
+                   BUILTIN_SQRT_P (fcode) ? dconsthalf : dconstthird;
+                 
+                 /* Adjust for the outer root.  */
+                 dconstroot.exp--;
+                 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
+                 tree_root = build_real (type, dconstroot);
+                 arglist = tree_cons (NULL_TREE, arg0,
+                                      build_tree_list (NULL_TREE, tree_root));
+                 return build_function_call_expr (powfn, arglist);
+               }
+           }
+
          /* Optimize sqrt(pow(x,y)) = pow(x,y*0.5).  */
          if (flag_unsafe_math_optimizations
              && (fcode == BUILT_IN_POW
@@ -6708,6 +6731,56 @@ fold_builtin (tree exp)
        }
       break;
 
+    case BUILT_IN_CBRT:
+    case BUILT_IN_CBRTF:
+    case BUILT_IN_CBRTL:
+      if (validate_arglist (arglist, REAL_TYPE, VOID_TYPE))
+       {
+         tree arg = TREE_VALUE (arglist);
+         const enum built_in_function fcode = builtin_mathfn_code (arg);
+
+         /* Optimize cbrt of constant value.  */
+         if (real_zerop (arg) || real_onep (arg) || real_minus_onep (arg))
+           return arg;
+
+         /* Optimize cbrt(expN(x)) -> expN(x/3).  */
+         if (flag_unsafe_math_optimizations && BUILTIN_EXPONENT_P (fcode))
+           {
+             tree expfn = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
+             const REAL_VALUE_TYPE third_trunc =
+               real_value_truncate (TYPE_MODE (type), dconstthird);
+             arg = fold (build (MULT_EXPR, type,
+                                TREE_VALUE (TREE_OPERAND (arg, 1)),
+                                build_real (type, third_trunc)));
+             arglist = build_tree_list (NULL_TREE, arg);
+             return build_function_call_expr (expfn, arglist);
+           }
+
+         /* Optimize cbrt(sqrt(x)) -> pow(x,1/6).  */
+         /* We don't optimize cbrt(cbrt(x)) -> pow(x,1/9) because if
+             x is negative pow will error but cbrt won't.  */
+         if (flag_unsafe_math_optimizations && BUILTIN_SQRT_P (fcode))
+           {
+             tree powfn = mathfn_built_in (type, BUILT_IN_POW);
+
+             if (powfn)
+               {
+                 tree arg0 = TREE_VALUE (TREE_OPERAND (arg, 1));
+                 tree tree_root;
+                 REAL_VALUE_TYPE dconstroot = dconstthird;
+
+                 dconstroot.exp--;
+                 dconstroot = real_value_truncate (TYPE_MODE (type), dconstroot);
+                 tree_root = build_real (type, dconstroot);
+                 arglist = tree_cons (NULL_TREE, arg0,
+                                      build_tree_list (NULL_TREE, tree_root));
+                 return build_function_call_expr (powfn, arglist);
+               }
+             
+           }
+       }
+      break;
+
     case BUILT_IN_SIN:
     case BUILT_IN_SINF:
     case BUILT_IN_SINL:
index 417d1a45c8ea930a4627a35cd8d707abfdfb2a11..e8c36f029ee740eb598f598f3127a2a75709c29a 100644 (file)
@@ -6389,23 +6389,24 @@ fold (tree expr)
              enum built_in_function fcode0 = builtin_mathfn_code (arg0);
              enum built_in_function fcode1 = builtin_mathfn_code (arg1);
 
-             /* Optimizations of sqrt(...)*sqrt(...).  */
-             if (fcode0 == fcode1 && BUILTIN_SQRT_P (fcode0))
+             /* Optimizations of root(...)*root(...).  */
+             if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
                {
-                 tree sqrtfn, arg, arglist;
+                 tree rootfn, arg, arglist;
                  tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
                  tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
 
                  /* Optimize sqrt(x)*sqrt(x) as x.  */
-                 if (operand_equal_p (arg00, arg10, 0)
+                 if (BUILTIN_SQRT_P (fcode0)
+                     && operand_equal_p (arg00, arg10, 0)
                      && ! HONOR_SNANS (TYPE_MODE (type)))
                    return arg00;
 
-                 /* Optimize sqrt(x)*sqrt(y) as sqrt(x*y).  */
-                 sqrtfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
+                 /* Optimize root(x)*root(y) as root(x*y).  */
+                 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
                  arg = fold (build (MULT_EXPR, type, arg00, arg10));
                  arglist = build_tree_list (NULL_TREE, arg);
-                 return build_function_call_expr (sqrtfn, arglist);
+                 return build_function_call_expr (rootfn, arglist);
                }
 
              /* Optimize expN(x)*expN(y) as expN(x+y).  */
index 572f5273e7f8c7df2ff0ba6525affe5db002f258..afbf937c80b4305fbc7f215c97279bbc333e57e2 100644 (file)
@@ -1,3 +1,9 @@
+2004-03-25  Kaveh R. Ghazi  <ghazi@caip.rutgers.edu>
+
+       * gcc.dg/torture/builtin-explog-1.c: Add new cases.
+       * gcc.dg/torture/builtin-math-1.c: Likewise.
+       * builtin-power-1.c: New test.
+
 2004-03-24  Andreas Schwab  <schwab@suse.de>
 
        * lib/prune.exp (prune_gcc_output): Ignore errata warning from
index 59ba1d095c8c5af61d919999d05fe5366a654be3..4d7de84cbb5d4d9cec3a500618fd25ca726e66ba 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2003  Free Software Foundation.
+/* Copyright (C) 2003, 2004  Free Software Foundation.
 
    Verify that built-in math function constant folding of log & exp is
    correctly performed by the compiler.
@@ -119,6 +119,17 @@ void test(double d1, double d2, float f1, float f2,
   LOG_CBRT(log2);
   LOG_CBRT(log10);
   
+  /* Test cbrt(expN(x)) -> expN(x/3).  */
+#define CBRT_EXP(EXP) \
+ extern void link_failure_cbrt_##EXP(void); \
+ if (cbrt(EXP(d1)) != EXP(d1/3.0) || cbrtf(EXP##f(f1)) != EXP##f(f1/3.0F) \
+  || cbrtl(EXP##l(ld1)) != EXP##l(ld1/3.0L)) link_failure_cbrt_##EXP()
+    
+  CBRT_EXP(exp);
+  CBRT_EXP(exp2);
+  CBRT_EXP(exp10);
+  CBRT_EXP(pow10);
+  
   /* Test logN(pow(x,y)) -> y*logN(x).  */
 #define LOG_POW(LOG, POW) \
  extern void link_failure_##LOG##_##POW(void); \
index 47b91d7e25d7e4aab3a8e8d6c0686b1bd2aad756..99fef8d7825e2602540d1589f4f8945015a09f79 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2002, 2003  Free Software Foundation.
+/* Copyright (C) 2002, 2003, 2004  Free Software Foundation.
 
    Verify that built-in math function constant folding of constant
    arguments is correctly performed by the compiler.
@@ -18,6 +18,15 @@ void test (float f, double d, long double ld)
   if (sqrt (1.0) != 1.0)
     link_error ();
 
+  if (cbrt (0.0) != 0.0)
+    link_error ();
+
+  if (cbrt (1.0) != 1.0)
+    link_error ();
+
+  if (cbrt (-1.0) != -1.0)
+    link_error ();
+
   if (exp (0.0) != 1.0)
     link_error ();
 
@@ -55,6 +64,15 @@ void test (float f, double d, long double ld)
   if (sqrtf (1.0F) != 1.0F)
     link_error ();
 
+  if (cbrtf (0.0F) != 0.0F)
+    link_error ();
+
+  if (cbrtf (1.0F) != 1.0F)
+    link_error ();
+
+  if (cbrtf (-1.0F) != -1.0F)
+    link_error ();
+
   if (expf (0.0F) != 1.0F)
     link_error ();
 
@@ -92,6 +110,15 @@ void test (float f, double d, long double ld)
   if (sqrtl (1.0L) != 1.0L)
     link_error ();
 
+  if (cbrtl (0.0L) != 0.0L)
+    link_error ();
+
+  if (cbrtl (1.0L) != 1.0L)
+    link_error ();
+
+  if (cbrtl (-1.0L) != -1.0L)
+    link_error ();
+
   if (expl (0.0L) != 1.0L)
     link_error ();
 
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-power-1.c b/gcc/testsuite/gcc.dg/torture/builtin-power-1.c
new file mode 100644 (file)
index 0000000..4556611
--- /dev/null
@@ -0,0 +1,105 @@
+/* Copyright (C) 2004  Free Software Foundation.
+
+   Verify that built-in folding of various math "power" functions is
+   correctly performed by the compiler.
+
+   Written by Kaveh Ghazi, 2004-03-11.  */
+
+/* { dg-do link } */
+/* { dg-options "-ffast-math" } */
+
+#include "../builtins-config.h"
+
+#ifdef HAVE_C99_RUNTIME
+#define C99CODE(CODE) CODE
+#else
+#define C99CODE(CODE) 0
+#endif
+
+#define PROTOTYPE(FN) extern double FN(double); extern float FN##f(float); \
+  extern long double FN##l(long double);
+#define PROTOTYPE2(FN) extern double FN(double, double); \
+  extern float FN##f(float, float); \
+  extern long double FN##l(long double, long double);
+
+PROTOTYPE(sqrt)
+PROTOTYPE(cbrt)
+PROTOTYPE2(pow)
+
+void test(double d1, double d2, double d3,
+         float f1, float f2, float f3,
+         long double ld1, long double ld2, long double ld3)
+{
+  /* Test N1root(N2root(x)) -> pow(x,1/(N1*N2)).  */
+  /* E.g. sqrt(cbrt(x)) -> pow(x,1/6).  */
+#define ROOT_ROOT(FN1,N1,FN2,N2) \
+ extern void link_failure_##FN1##_##FN2(void); \
+ if (FN1(FN2(d1)) != pow(d1,1.0/(N1*N2)) \
+     || C99CODE (FN1##f(FN2##f(f1)) != powf(f1,1.0F/(N1*N2))) \
+     || C99CODE (FN1##l(FN2##l(ld1)) != powl(ld1,1.0L/(N1*N2)))) \
+    link_failure_##FN1##_##FN2()
+
+  ROOT_ROOT(sqrt,2,sqrt,2);
+  ROOT_ROOT(sqrt,2,cbrt,3);
+  ROOT_ROOT(cbrt,3,sqrt,2);
+  /*ROOT_ROOT(cbrt,3,cbrt,3); Intentionally not implemented.  */
+
+  /* Test pow(Nroot(x),y) -> pow(x,y/N).  */
+#define POW_ROOT(FN,N) \
+ extern void link_failure_pow_##FN(void); \
+ if (pow(FN(d1), d2) != pow(d1,d2/N) \
+     || powf(FN##f(f1),f2) != powf(f1,f2/N) \
+     || powl(FN##l(ld1),ld2) != powl(ld1,ld2/N)) \
+    link_failure_pow_##FN()
+
+  POW_ROOT(sqrt,2);
+  /*POW_ROOT(cbrt,3); Intentionally not implemented.  */
+
+  /* Test Nroot(pow(x,y)) -> pow(x,y/N).  */
+#define ROOT_POW(FN,N) \
+ extern void link_failure_##FN##_pow(void); \
+ if (FN(pow(d1, d2)) != pow(d1,d2/N) \
+     || FN##f(powf(f1,f2)) != powf(f1,f2/N) \
+     || FN##l(powl(ld1,ld2)) != powl(ld1,ld2/N)) \
+    link_failure_##FN##_pow()
+
+  ROOT_POW(sqrt,2);
+  /*ROOT_POW(cbrt,3); Intentionally not implemented.  */
+
+  /* Test pow(pow(x,y),z) -> pow(x,y*z).  */
+#define POW_POW \
+ extern void link_failure_pow_pow(void); \
+ if (pow(pow(d1, d2), d3) != pow(d1,d2*d3) \
+     || powf(powf(f1,f2),f3) != powf(f1,f2*f3) \
+     || powl(powl(ld1,ld2),ld3) != powl(ld1,ld2*ld3)) \
+    link_failure_pow_pow()
+
+  POW_POW;
+
+  /* Test Nroot(x)*Nroot(y) -> Nroot(x*y).  */
+#define ROOT_X_ROOT(FN) \
+ extern void link_failure_root_x_root(void); \
+ if (FN(d1)*FN(d2) != FN(d1*d2) \
+     || FN##f(f1)*FN##f(f2) != FN##f(f1*f2) \
+     || FN##l(ld1)*FN##l(ld2) != FN##l(ld1*ld2)) \
+    link_failure_root_x_root()
+
+  ROOT_X_ROOT(sqrt);
+  ROOT_X_ROOT(cbrt);
+  
+  /* Test pow(x,y)*pow(x,z) -> pow(x,y+z).  */
+#define POW_X_POW \
+ extern void link_failure_pow_x_pow(void); \
+ if (pow(d1,d2)*pow(d1,d3) != pow(d1,d2+d3) \
+     || powf(f1,f2)*powf(f1,f3) != powf(f1,f2+f3) \
+     || powl(ld1,ld2)*powl(ld1,ld3) != powl(ld1,ld2+ld3)) \
+    link_failure_pow_x_pow()
+
+  POW_X_POW;
+  
+}
+
+int main (void)
+{
+  return 0;
+}