Fast operations on floating point numbers?

Peter Barada peter@the-baradas.com
Thu Dec 4 14:16:00 GMT 2003


>I'm not completely sure whether this question belongs to a C/C++ newsgroup,
>but it is probably compiler-specific, so I'm trying my luck here.
>
>I'm developing a numerical simulation code which contains operations
>like this one
>
>double a;
>int flipsign;
>
>[...]
>
>if (flipsign)
>   a = -a;
>
>in an inner loop which is executed billions of times.
>I suspect that the conditional slows the execution down,
>and the "a=-a" statement might not be optimal as well.

If flipsign is only refered to inside the(hevily executed) loop, you
could try:

double a, flip;
int flipsign;

flip = flipsign ? -1.0 : 1.0;

for (;;) {
  ...
  a *= flip;
  ...
}

And get rid of the conditional...

-- 
Peter Barada
peter@the-baradas.com



More information about the Gcc mailing list