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]

Re: [PATCH] Provide basic folding for __builtin_powi


> Ok for mainline with that change.  It looks like you've already
> got testsuite coverage for __builtin_powi with pr19402-*.c, but
> for extra credit feel free to another testcase or two (perhaps
> along the lines of builtins-8.c and/or builtins-14.c).

Testing finished, I'll commit the following later.


2005-02-10  Richard Guenther  <rguenth@gcc.gnu.org>

	* builtins.c (fold_builtin_powi): New function.
	(fold_builtin_1): Call it.

	* gcc.dg/builtins-52.c: New testcase.


Index: builtins.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/builtins.c,v
retrieving revision 1.418
diff -c -3 -p -r1.418 builtins.c
*** builtins.c	9 Feb 2005 21:56:30 -0000	1.418
--- builtins.c	10 Feb 2005 08:58:50 -0000
*************** static tree fold_builtin_cabs (tree, tre
*** 152,157 ****
--- 152,158 ----
  static tree fold_builtin_sqrt (tree, tree);
  static tree fold_builtin_cbrt (tree, tree);
  static tree fold_builtin_pow (tree, tree, tree);
+ static tree fold_builtin_powi (tree, tree, tree);
  static tree fold_builtin_sin (tree);
  static tree fold_builtin_cos (tree, tree, tree);
  static tree fold_builtin_tan (tree);
*************** fold_builtin_pow (tree fndecl, tree argl
*** 6948,6953 ****
--- 6949,7001 ----
    return NULL_TREE;
  }

+ /* Fold a builtin function call to powi, powif, or powil.  Return
+    NULL_TREE if no simplification can be made.  */
+ static tree
+ fold_builtin_powi (tree fndecl ATTRIBUTE_UNUSED, tree arglist, tree type)
+ {
+   tree arg0 = TREE_VALUE (arglist);
+   tree arg1 = TREE_VALUE (TREE_CHAIN (arglist));
+
+   if (!validate_arglist (arglist, REAL_TYPE, INTEGER_TYPE, VOID_TYPE))
+     return NULL_TREE;
+
+   /* Optimize pow(1.0,y) = 1.0.  */
+   if (real_onep (arg0))
+     return omit_one_operand (type, build_real (type, dconst1), arg1);
+
+   if (host_integerp (arg1, 0))
+     {
+       HOST_WIDE_INT c = TREE_INT_CST_LOW (arg1);
+
+       /* Evaluate powi at compile-time.  */
+       if (TREE_CODE (arg0) == REAL_CST
+ 	  && ! TREE_CONSTANT_OVERFLOW (arg0))
+ 	{
+ 	  REAL_VALUE_TYPE x;
+ 	  x = TREE_REAL_CST (arg0);
+ 	  real_powi (&x, TYPE_MODE (type), &x, c);
+ 	  return build_real (type, x);
+ 	}
+
+       /* Optimize pow(x,0) = 1.0.  */
+       if (c == 0)
+ 	return omit_one_operand (type, build_real (type, dconst1),
+ 				 arg0);
+
+       /* Optimize pow(x,1) = x.  */
+       if (c == 1)
+ 	return arg0;
+
+       /* Optimize pow(x,-1) = 1.0/x.  */
+       if (c == -1)
+ 	return fold (build2 (RDIV_EXPR, type,
+ 			     build_real (type, dconst1), arg0));
+     }
+
+   return NULL_TREE;
+ }
+
  /* A subroutine of fold_builtin to fold the various exponent
     functions.  EXP is the CALL_EXPR of a call to a builtin function.
     VALUE is the value which will be raised to a power.  */
*************** fold_builtin_1 (tree exp, bool ignore)
*** 7969,7974 ****
--- 8017,8027 ----
      case BUILT_IN_POWL:
        return fold_builtin_pow (fndecl, arglist, type);

+     case BUILT_IN_POWI:
+     case BUILT_IN_POWIF:
+     case BUILT_IN_POWIL:
+       return fold_builtin_powi (fndecl, arglist, type);
+
      case BUILT_IN_INF:
      case BUILT_IN_INFF:
      case BUILT_IN_INFL:


/* { dg-do link } */
/* { dg-options "-ffast-math" } */

extern void link_error(void);

void test(double x, int n)
{
  if (__builtin_powi(x,-1.0) != 1.0/x)
    link_error ();
  if (__builtin_powi(x,0.0) != 1.0)
    link_error ();
  if (__builtin_powi(x,1.0) != x)
    link_error ();
  if (__builtin_powi(1.0,n) != 1.0)
    link_error ();
}

int main()
{
  test(7.3, 2);
  return 0;
}



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