Thread: Addition vs. Multiplication

  1. #1
    Unregistered
    Guest

    Addition vs. Multiplication

    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. #2
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    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.
    Last edited by Clyde; 07-08-2002 at 05:23 PM.

  3. #3
    Registered User VBprogrammer's Avatar
    Join Date
    Mar 2002
    Posts
    175
    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)
    VC++ 6

  4. #4
    Unregistered
    Guest

    Floats

    Does this shifting stuff work for floats?

  5. #5
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    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. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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.
    Last edited by MrWizard; 07-08-2002 at 07:58 PM.

  7. #7
    Unregistered
    Guest
    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. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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;
    }
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    The Earth is not flat. Clyde's Avatar
    Join Date
    Mar 2002
    Posts
    1,403
    "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. #10
    Pursuing knowledge confuted's Avatar
    Join Date
    Jun 2002
    Posts
    1,916
    just for clarification on the last post, he meant x * 64, not x * 6

  11. #11
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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. #12
    Just because ygfperson's Avatar
    Join Date
    Jan 2002
    Posts
    2,490
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Repeated Addition
    By rocksteady in forum C Programming
    Replies: 15
    Last Post: 12-11-2007, 07:05 AM
  2. Help me find a multiplication toy.
    By QuestionC in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 08-24-2007, 12:27 PM
  3. ...multiplication using stacks
    By iiwhitexb0iii in forum C Programming
    Replies: 1
    Last Post: 10-09-2006, 01:28 AM
  4. multiplication table
    By SpEkTrE in forum C Programming
    Replies: 2
    Last Post: 12-09-2003, 04:46 PM
  5. addition, multiplication errors??
    By bigB8210 in forum C Programming
    Replies: 3
    Last Post: 07-15-2003, 12:05 AM