Im game programming and as you know that means time is a huge concern. Im going through my functions and trying to speed them up since they all run many many times a second. So my question is which is faster, a muliplication of a variable or an unrolled addition.

ex: total = 2 * x;
or total = x+x;

does anyone know for a fact?
Also, what difference will it make between a float or an int x. Is it only better to unroll with an int, etc?

Finally, if im multiplying by a bigger number does it become less effective. Like 5*x is faster than x+x+x+x+x but 2*x is slower than x+x. The higher the coefficient the more additions it would need and the more calls to the variable it makes so that sounds bad. So basically is there any knowledgeable person who could fill me in on this stuff?

2. bitshifting is the way to go:

so x * 2 becomes x >> 2

x * 64 becomes x >> 6

ints are faster than floats but not by that much, and conversions take ages. So if a function needs a float inputed use floats don't use ints and cast them into floats.

3. Yes, in terms of clocks per instruction a 486+ only needs a single clock pulse to do an addition between two registers. However it takes anywere between 13 and 42 clocks to do a multiplication. Wiether it is faster to use unrolled multiplications does depend on the number of times it must be multiplied. I wouldnt use it for more than multipling by 3. However there is a much more sensible way of achieving this effect. This is by using shifts, have a look at the >> and << operators in C. To do a fast mulitipication using these simply find the number 2 must be raised to get the muliplying effect you want and shift by this number i.e. 2 * x == x << 1. This is because 2 ^ 1 = 2.

You can also add the results of two or more of these shifts to get any multiplication i.e. x * 320 == (x >> 8) + (x >> 6)

4. ## Floats

Does this shifting stuff work for floats?

5. yes, shifting works on floats...but one thing worth mentioning is that a lot of the math in game programming isn't multiplying a variable by a constant, it's multiplying two variables, which you can't achieve by shifting (I don't think...)

6. Originally posted by Clyde
bitshifting is the way to go:

so x * 2 becomes x >> 2

x * 64 becomes x >> 6
This is wrong. The correct way is this.

x * 2 is equivalent to x << 1
x * 64 is equivalent to x << 6

when you shift left you are basically raising 2^x so in the first case x is shifted one, so to say 2^1 = 2; in the second 2^6 = 64;

When you shift RIGHT like in Clyde's example you are DIVIDING. This is important to notice and will give you unwanted effects I am sure.

7. I can't get it to Bit shift floats without giving a compiling error. Are you sure it is possible?.....

float a = 2.5;

a << 1 == 5.0?

or error?

8. Well in C you can trick the compiler to make the float act like an integer. Take the following code snippet to test the first bit whether it is 1 or 0.

Code:
```int main( void )
{
float num = 1.0;
int    *x = 0;

x = (int *)&num;

// Test the first bit...
if( *x & ( 1 << 0 ) )
{
printf( "1" );
}

return 0;
}```

9. "This is wrong. The correct way is this.

x * 2 is equivalent to x << 1
x * 64 is equivalent to x << 6 "

Ooops, *decides to stop giving advice, when he clearly knows diddly*.

Sorry unregestered.

10. just for clarification on the last post, he meant x * 64, not x * 6

11. Originally posted by Clyde
"This is wrong. The correct way is this.

x * 2 is equivalent to x << 1
x * 64 is equivalent to x << 6 "

Ooops, *decides to stop giving advice, when he clearly knows diddly*.

Sorry unregestered.
BTW: I wasn't trying to sound like a smart .........., just wanted to correct you won't make any mistakes in the future.

blackrat364: thanks for the typo change.

12. floating point numbers look like this for the most part:
seeeeeeeemmmmmmmmmmmmmmmmmmmmmmm
the s is the sign bit, the eee's are the exponent bits, and the mmmm's are the mantissa bits. a float is really:
2^e*(1+f) * (s?-1:1)
a shift screws this up. shifts on floats or doubles won't do any multiplying or dividing. if you're working with floats, just multiply or divide like normal.