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] fold tan(x)/sin(x) as 1.0/cos(x)


Hello!

Attached patch to mainline gcc adds missing optimization to fold-const.c:

2004-03-12 Uros Bizjak <uros@kss-loka.si>

      * fold-const.c (fold <RDIV_EXPR>): With
      flag_unsafe_math_optimizations fold tan(x)/sin(x) as 1.0/cos(x).

* gcc.dg/builtins-20.c: Also check tan(x)/sin(x).

Uros.
? testsuite/gcc.dg/builtins-20.s
Index: ChangeLog
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/ChangeLog,v
retrieving revision 2.3144
diff -u -r2.3144 ChangeLog
--- ChangeLog	12 Mar 2004 07:14:39 -0000	2.3144
+++ ChangeLog	12 Mar 2004 10:49:44 -0000
@@ -1,3 +1,8 @@
+2004-03-12  Uros Bizjak  <uros@kss-loka.si>
+
+	* fold-const.c (fold <RDIV_EXPR>): With
+	flag_unsafe_math_optimizations fold tan(x)/sin(x) as 1.0/cos(x).
+
 2004-03-11  Richard Henderson  <rth@redhat.com>
 
 	* config/alpha/alpha.c (xfloating_ops, vax_cvt_ops): New.
Index: fold-const.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/fold-const.c,v
retrieving revision 1.344
diff -u -r1.344 fold-const.c
--- fold-const.c	11 Mar 2004 17:45:01 -0000	1.344
+++ fold-const.c	12 Mar 2004 10:50:01 -0000
@@ -6894,6 +6894,34 @@
 		}
 	    }
 
+	  /* Optimize tan(x)/sin(x) as 1.0/cos(x).  */
+	  if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
+	       || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
+	       || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL))
+	      && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
+				  TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
+	    {
+	      tree cosfn;
+
+	      if (fcode0 == BUILT_IN_TAN)
+		cosfn = implicit_built_in_decls[BUILT_IN_COS];
+	      else if (fcode0 == BUILT_IN_TANF)
+		cosfn = implicit_built_in_decls[BUILT_IN_COSF];
+	      else if (fcode0 == BUILT_IN_TANL)
+		cosfn = implicit_built_in_decls[BUILT_IN_COSL];
+	      else
+		cosfn = NULL_TREE;
+
+	      if (cosfn != NULL_TREE)
+		{
+		  tree tmp = TREE_OPERAND (arg0, 1);
+		  tmp = build_function_call_expr (cosfn, tmp);
+		  return fold (build (RDIV_EXPR, type,
+				      build_real (type, dconst1),
+				      tmp));
+		}
+	    }
+
 	  /* Optimize pow(x,c)/x as pow(x,c-1).  */
 	  if (fcode0 == BUILT_IN_POW
 	      || fcode0 == BUILT_IN_POWF
Index: testsuite/ChangeLog
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/testsuite/ChangeLog,v
retrieving revision 1.3598
diff -u -r1.3598 ChangeLog
--- testsuite/ChangeLog	11 Mar 2004 22:53:51 -0000	1.3598
+++ testsuite/ChangeLog	12 Mar 2004 10:50:04 -0000
@@ -1,3 +1,7 @@
+2004-03-12  Uros Bizjak  <uros@kss-loka.si>
+
+	* gcc.dg/builtins-20.c: Also check tan(x)/sin(x).
+
 2004-03-11  Ulrich Weigand  <uweigand@de.ibm.com>
 
 	PR target/14262
Index: testsuite/gcc.dg/builtins-20.c
===================================================================
RCS file: /cvsroot/gcc/gcc/gcc/testsuite/gcc.dg/builtins-20.c,v
retrieving revision 1.3
diff -u -r1.3 builtins-20.c
--- testsuite/gcc.dg/builtins-20.c	24 Dec 2003 06:52:27 -0000	1.3
+++ testsuite/gcc.dg/builtins-20.c	12 Mar 2004 10:50:22 -0000
@@ -23,6 +23,9 @@
   if (cos(x)/sin(x) != 1.0/tan(x))
     link_error ();
 
+  if (tan(x)/sin(x) != 1.0/cos(x))
+    link_error ();
+
   if (tan(x)*cos(x) != sin(x))
     link_error ();
 
@@ -51,6 +54,9 @@
   if (cosf(x)/sinf(x) != 1.0f/tanf(x))
     link_error ();
 
+  if (tanf(x)/sinf(x) != 1.0f/cosf(x))
+    link_error ();
+
   if (tanf(x)*cosf(x) != sinf(x))
     link_error ();
 
@@ -79,6 +85,9 @@
     link_error ();
 
   if (cosl(x)/sinl(x) != 1.0l/tanl(x))
+    link_error ();
+
+  if (tanl(x)/sinl(x) != 1.0l/cosl(x))
     link_error ();
 
   if (tanl(x)*cosl(x) != sinl(x))

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