Velocity Reviews > "Shifting" floating point numbers

# "Shifting" floating point numbers

woessner@gmail.com
Guest
Posts: n/a

 03-21-2006
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?

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;

Bill

santosh
Guest
Posts: n/a

 03-21-2006
(E-Mail Removed) 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?
>
> 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;

If you know the floating point representation of your implementation
and underlying hardware, you can copy the float value to an unsiged
long variable, shift the appropriate bits and copy the value back to
the float variable. All this implies that you'll have to know details

These days, FPU's are quite fast, so a direct multiply on the float
should be efficient enough unless you're code is time critical.

Skarmander
Guest
Posts: n/a

 03-21-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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

S.

Eric Sosman
Guest
Posts: n/a

 03-21-2006

(E-Mail Removed) wrote On 03/21/06 13:45,:
> 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?
>
> 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;

#include <math.h>
...
y = ldexp(x, 9);

No guarantees about relative speed, though: You'll
need to measure on the platform(s) of interest.

--
(E-Mail Removed)

Pierre Maurette
Guest
Posts: n/a

 03-21-2006
(E-Mail Removed), le 21/03/2006 a écrit :
> 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.

Mantissa ? Exponent, no ?

> But can I write C code (or x86 assembly) to accomplish this
> without a full-blown multiply?

[I consider Intel FPU, double / real8]
A problem is in the position of the exponent in the double. From bit 52
to bit 62. You can't use this way in order to have faster code, just
for fun.

In assembly, you have two ways:

- FIMUL, multiply a FP data by an integer.

- FSCALE, that do just what you want, multiply very quickly by a power
of 2 You need just to have the power (9, not 2^9 !) in ST(1) and do
FSCALE.

--
Pierre Maurette

Keith Thompson
Guest
Posts: n/a

 03-21-2006
Pierre Maurette <(E-Mail Removed)> writes:
> (E-Mail Removed), le 21/03/2006 a écrit :
>> 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.

> Mantissa ? Exponent, no ?
>
>> But can I write C code (or x86 assembly) to accomplish this
>> without a full-blown multiply?

> [I consider Intel FPU, double / real8]
> A problem is in the position of the exponent in the double. From bit
> 52 to bit 62. You can't use this way in order to have faster code,
> just for fun.
>
> In assembly, you have two ways:
>
> - FIMUL, multiply a FP data by an integer.
>
> - FSCALE, that do just what you want, multiply very quickly by a power
> of 2 You need just to have the power (9, not 2^9 !) in ST(1) and
> do FSCALE.

have no way of knowing whether your description is accurate. I make
mistakes every now and then; when I make them in a forum full of
experts on what I'm talking about, they'll be corrected.

And, of course, it's off-topic. There are newsgroups where such
things are topical; comp.lang.asm.x86 is probably one of them.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Pierre Maurette
Guest
Posts: n/a

 03-21-2006
Keith Thompson, le 21/03/2006 a écrit :
[...]
> And, of course, it's off-topic. There are newsgroups where such
> things are topical; comp.lang.asm.x86 is probably one of them.

I agree your remark. But its was an answer to an off-topic question.

--
Pierre Maurette

Keith Thompson
Guest
Posts: n/a

 03-21-2006
(E-Mail Removed) writes:
> 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?
>
> 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;

Compile the code with whatever optimization options are appropriate,
and look at the assembly language. If your compiler generates code
that uses something other than (and presumably faster than) a
floating-point multiply, then you're all set; there's no need to do
source-level optimization if the compiler will do it for you. If the
compiler emits an ordinary floating-point multiply instruction, then
it may mean that that's the best way to do the multiplication.

It's also possible that you can optimize this better than your
compiler can, but that's not the way to bet (unless you've actually
measured it).

And always remember the Rules of Optimization:

Rule 1: Don't do it.
Rule 2 (for experts only): Don't do it yet.

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.

Jordan Abel
Guest
Posts: n/a

 03-21-2006
On 2006-03-21, Pierre Maurette <(E-Mail Removed)> wrote:
> - FSCALE, that do just what you want, multiply very quickly by a power
> of 2 You need just to have the power (9, not 2^9 !) in ST(1) and do
> FSCALE.

This instruction may be available to C as the "scalbn" function (and if
not, scalbn will be a software implementation that does the same thing),
and I suspect this is exactly what he wants.

C99 only, of course.

Here's my system's implementation of it for i387

RCSID("\$FreeBSD: src/lib/msun/i387/s_scalbn.S,v 1.7 1999/08/28 00:06:13 peter Exp \$")

ENTRY(scalbn)
fildl 12(%esp)
fldl 4(%esp)
fscale
fstp %st(1)
ret

Jordan Abel
Guest
Posts: n/a

 03-21-2006
On 2006-03-21, Pierre Maurette <(E-Mail Removed)> wrote:
> Keith Thompson, le 21/03/2006 a écrit :
> [...]
>> And, of course, it's off-topic. There are newsgroups where such
>> things are topical; comp.lang.asm.x86 is probably one of them.

> I agree your remark. But its was an answer to an off-topic question.

Not really. scalbn() [and its friends] are the on-topic answer.