Kaveh R. GHAZI - [PATCH]: fold cabs(x+xi) and (x,0)-(0,y) (original) (raw)

This is the mail archive of the gcc-patches@gcc.gnu.orgmailing 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]

This patch adds two bits of functionality. First I add folding of cabs(x+xi) -> fabs(x)*sqrt(2). We already do the same for hypot(x,x). I slightly reorganized fold_builtin_cabs to only check that TREE_CODE (arg) == COMPLEX_EXPR one time.

Second I add folding of complex expressions like (x,0)-(0,y) into (x,-y) and also (0,y)-(x,0) into (-x,y). We already do a similar folding for the analogous PLUS expression.

I also noticed two comment typos which I corrected below.

I'm doing these two seemingly unrelated functional bits together because the testcases I came up with make use of both transformations in some cases to resolve correctly.

Tested on sparc-sun-solaris2.10, no regressions.

Okay for mainline?

    Thanks,
    --Kaveh

2007-02-02 Kaveh R. Ghazi ghazi@caip.rutgers.edu

* builtins.c (fold_builtin_cabs): Fold cabs(x+xi) into
fabs(x)*sqrt(2).
* fold-const.c (fold_binary): Fix comment typos.  Fold complex
(x,0)-(0,y) into (x,-y).  Likewise (0,y)-(x,0) into (-x,y).

testsuite: * gcc.dg/builtins-54.c: Add more cases.

diff -rup orig/egcc-SVN20070202/gcc/builtins.c egcc-SVN20070202/gcc/builtins.c --- orig/egcc-SVN20070202/gcc/builtins.c 2007-02-02 12:46:46.000000000 -0500 +++ egcc-SVN20070202/gcc/builtins.c 2007-02-02 14:35:16.628127104 -0500 @@ -7143,13 +7143,30 @@ fold_builtin_cabs (tree arglist, tree ty type, mpfr_hypot))) return res;

/* Optimize cabs(-z) and cabs(conj(z)) as cabs(z). */ if (TREE_CODE (arg) == NEGATE_EXPR diff -rup orig/egcc-SVN20070202/gcc/fold-const.c egcc-SVN20070202/gcc/fold-const.c --- orig/egcc-SVN20070202/gcc/fold-const.c 2007-02-02 12:46:47.000000000 -0500 +++ egcc-SVN20070202/gcc/fold-const.c 2007-02-02 14:16:48.906222070 -0500 @@ -8923,7 +8923,7 @@ fold_binary (enum tree_code code, tree t

   /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
      to __complex__ ( x, y ).  This is not the same for SNaNs or

@@ -9231,6 +9231,43 @@ fold_binary (enum tree_code code, tree t else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0)) return negate_expr (fold_convert (type, arg1));

@@ -9410,7 +9447,7 @@ fold_binary (enum tree_code code, tree t }

   /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).

diff -rup orig/egcc-SVN20070202/gcc/testsuite/gcc.dg/builtins-54.c egcc-SVN20070202/gcc/testsuite/gcc.dg/builtins-54.c --- orig/egcc-SVN20070202/gcc/testsuite/gcc.dg/builtins-54.c 2006-05-31 20:00:17.000000000 -0400 +++ egcc-SVN20070202/gcc/testsuite/gcc.dg/builtins-54.c 2007-02-02 14:29:28.791920612 -0500 @@ -1,44 +1,146 @@ /* { dg-do link } / / { dg-options "-O2 -ffast-math" } */

+double fabs(double); +float fabsf(float); +long double fabsl(long double); double cabs(complex double); float cabsf(complex float); long double cabsl(complex long double);

void link_error (void);

-void test(complex double x) +void test(complex double x, double a, double b) { if (cabs(x) != cabs(-x)) link_error();

if (cabs(x) != cabs(~x)) link_error(); +

}

-void testf(complex float x) +void testf(complex float x, float a, float b) { if (cabsf(x) != cabsf(-x)) link_error();

if (cabsf(x) != cabsf(~x)) link_error(); +

}

-void testl(complex long double x) +void testl(complex long double x, long double a, long double b) { if (cabsl(x) != cabsl(-x)) link_error();

if (cabsl(x) != cabsl(~x)) link_error(); +

}

int main() {


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