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?


Peter Chang writes:
 > 
 > 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.

Another implementation with 3 multiples and 5 additions is
          a(c + d) - d(a + b) + i[a(c + d) - c(a - b)]

Alternatively, there is Golub's method, using 3 multiples and 5 additions:
         (a+b)(c-d) + ad-bc + i[ad+bc]
This is more useful for hardware implementations where the imaginary
part can be quickly computed, followed by the real part.

However, on most modern pipelined architectures, multiply is often as
fast as addition so the standard approach is usually best.



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