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

Complex arithmetic improvements?



Hello all,

I was writing code a while ago to implement complex arithmetic in C when I
happened upon the gcc extension __complex__.

While it works fine, I looked at the egcs-1.03a source to see how it was
done and found the methods in egcs-1.0.3a/gcc/optabs.c starting from line
1235. It seems to use rather naive methods to do things like multiply and
divide: 

Line 1266: /* (a+ib) * (c+id) = (ac-bd) + i(ad+cb) */ 

this can be improved to

            = (ac - bd) + i[ (a+b)(c+d) - ac - bd ]

which is one multiply less but one addition and two subtractions more. 
This is usually faster as multiply is quite slow. Figures, anyone? All I
remember from my assembly programming days on a 68000 was that it was much
slower.

Line 1344: /* (a+ib) / (c+id) = ((ac+bd)/(cc+dd)) + i((bc-ad)/(cc+dd)) */

this can be improved to prevent avoidable {under,over}flows and loss of
precisions to

if |c| >= |d|, = ([ a + b(d/c) ] + i[ b - a(d/c) ]) / ( c + d(d/c) ) 


if |c| < |d|,  = ([ a(c/d) + b ] + i[ b(c/d) - a ]) / ( c(c/d) + d ) 

(There also seems to be an absolute function further down in the code for
which improvements can be made (I think).)

I don't have any expertise with egcs or indeed any other compiler to be
confident enough to actually implement these improvements so I'll leave it
to others.

All this and more on complex arithmetics implementations can be found in
Numerical Recipes in C, 2nd ed., section 5.4 which also references the
mighty Knuth's Seminumerical Algorithms.

Regards,
 Peter

PS could you cc any discussions to me as I'm not subscribe though I can
monitor the archives on cynus.com.



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