Re: "Shifting" floating point numbers



woessner@xxxxxxxxx wrote:
Does anyone know of a fast way to multiply floating point numbers by
powers of two? Conceptually, all you need to do is add to the
mantissa. But can I write C code (or x86 assembly) to accomplish this
without a full-blown multiply?

My advice: don't bother. The chance that you'll beat the combination of FPU and compiler is small, and a trick like this tends to break down when you least expect it.

For example, if you want to do this the right way, you have to deal with the infinities, denormalized numbers, NaNs and last but not least, zero. But of course you can't, since that would make your code far too slow.

For example, I'd like to be able to do the following very quickly:

double x;
double y;

x = 42.13;
y = (1 << 9) * x;

Verify that you really need to do this faster than you're doing it now first. Then see if there's some way to avoid doing the multiplication altogether (at least in tight loops) by changing the way you handle your data. Finally, if all these options are exhausted, you'd best go with x86 assembler and read up on the format of double precision numbers. Try comp.lang.asm.x86. When you're at this point, C (even nonportable C) is unlikely to help you enough.

S.
.



Relevant Pages