Multiplication in GCC 2.953

This is a discussion on Multiplication in GCC 2.953 within the C Programming forums, part of the General Programming Boards category; Hi, as far as I understand when I multiply two 32 bit integer numbers, the processor produces a 64 bit ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    3

    Multiplication in GCC 2.953

    Hi, as far as I understand when I multiply two 32 bit integer numbers, the processor produces a 64 bit result, but then the compiler truncates the upper 32 bits to produce a 32 bit value. Is there a way in gcc to avoid this truncating? The problem is that I need to multiply two potentially big 32bit numbers(I don't want to use long long because it's slow) and need to verify whether their product fits into 32 bit integer.

    Thanks !

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    I don't know about a solution offered by GCC, but you could write a function for such multiplication. Let it receive two 32 bit variables, design a 64 bit datastructure and store the result of the multiplication in that datastructure.

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    3
    Well, my performance grows by at least 20% if I use ints instead of (long long)

    Right now I'm doing the following:
    long long result = (long long)a * (long long) b;
    if (result > MAX_INT) then overflow
    else no overflow.

    but that's still not fast enough, considering that the CPU can do it all for me..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GCC and SSE multplication
    By Kernel Sanders in forum C Programming
    Replies: 5
    Last Post: 09-19-2008, 05:08 PM
  2. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  3. Replies: 4
    Last Post: 09-02-2007, 08:47 PM
  4. Compiles on gcc 3.3 but not on gcc 4.0.3
    By cunnus88 in forum C++ Programming
    Replies: 5
    Last Post: 03-29-2007, 12:24 PM
  5. gcc
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 21
    Last Post: 10-22-2003, 03:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21