Fast operations on floating point numbers?

Martin Reinecke martin@MPA-Garching.MPG.DE
Thu Dec 4 10:11:00 GMT 2003


Hi,

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.

What is, according to your experience, the most efficient way to negate
a floating point number? Should one write
  a=-a;
or
  a*=-1;
or would it be better just to flip the sign bit by hand with something like this
   #include<ieee754.h>

   ieee754_double a;
   a.negative ^= 1;

In the latter case, I could replace the above example by

   a.negative ^= flipsign;

which avoids the conditional and should be rather effective.


It would be great to have some intrinsics around for dealing with this issue,
but I guess not many people are bit by this problem, and since it is not covered
by the C and C++ standards, this is unlikely to happen.

Similarly, there is the math library function ldexp(), which multiplies a double
with a given power of two. For some reason it is extremely slow, but in principle
it should just add a number to the exponent of the double (and do a few sanity checks).

Does gcc offer a way to perform this kind of operations efficiently (on machines
adhering to IEEE754)?

Thanks in advance,
   Martin



More information about the Gcc mailing list