CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Join Date
    Nov 2010
    Posts
    105

    Question speed of floating point multiplication vs. division

    Long time ago I heard that the speed of floating point multiplication is much faster than division. Is it still the case today? (On a not very powerful netbook) Thanks!

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: speed of floating point multiplication vs. division

    Yes but there are so many other things to consider as well when predicting the execution speed. Here's some reading http://www.agner.org/optimize/instruction_tables.pdf
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: speed of floating point multiplication vs. division

    What I learned when I was doing CPU architecture research is that not only is division costly (about 40 cycles), vs a multiplication (about 10 cycles), divisions are not "pipe-able".

    What this means is that once you order your processor to do the division, you'll have to wait until it is finished before starting a new division. Multiplications, on the other hand, you can request a new multiplication on every cycle. You'll still have to wait the full 10 cycles for each to finish. So for example, if you need to do 10 multiplications, you can spend the first 10 cycles requesting the multiplications, and the during the next ten cyces, you'll get 1 result per cycle.

    All of this is very theoretical of course. The rule of thumb is "division is kind of expensive", but quite frankly, as always, these kinds of little optizations are the last thingg you should worry about. I've NEVER seen an algorithm where it really made a change.

    If you must divide, then you must divide...
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  4. #4
    Join Date
    Nov 2010
    Posts
    105

    Re: speed of floating point multiplication vs. division

    Thank you both! How about integer multiplication vs. shift? For example <<2 vs. *4 ? Thanks!

  5. #5
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: speed of floating point multiplication vs. division

    You know, if it really matters, measure.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  6. #6
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: speed of floating point multiplication vs. division

    A modern compiler will replace (if it's possible) a multiplication/division by a 2 base with shifts. An example from a MSVC2005 debug(!) build
    Code:
      unsigned int a = 0x12345678;
    004113BE  mov         dword ptr [a],12345678h 
      a /= 2;
    004113C5  mov         eax,dword ptr [a] 
    004113C8  shr         eax,1 
    004113CA  mov         dword ptr [a],eax 
      a *= 2;
    004113CD  mov         eax,dword ptr [a] 
    004113D0  shl         eax,1 
    004113D2  mov         dword ptr [a],eax
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: speed of floating point multiplication vs. division

    Quote Originally Posted by S_M_A View Post
    A modern compiler will replace (if it's possible) a multiplication/division by a 2 base with shifts. An example from a MSVC2005 debug(!) build[code]
    Even a non-modern compiler will do this.

    Back in the world of 16-bit DOS systems (Visual C++ 1.x, Turbo C) , there was no guarantee that the machine had a floating point processor installed, so why introduce emulated FP when not necessary.

    I know that for Turbo C, Borland made sure that the underlying code did not use any emulated floating point unless it was necessary (and sometimes you had to force the linker to use the floating point emulation).

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: speed of floating point multiplication vs. division

    Yes I remember that as well but for integer operations I doubt that the co-processor was in question.

    The reason I wrote modern was merely that I neither remember nor can test how such an old compiler behaves.

    Edit: I just remembered that some years ago I cleaned up some old 16 bit stuff and before throwing it nostalgy made me make a virtual Win3.11 machine. Here's a screen dump
    Attached Images Attached Images
    Last edited by S_M_A; June 3rd, 2012 at 03:52 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  9. #9
    Join Date
    Nov 2010
    Posts
    105

    Re: speed of floating point multiplication vs. division

    Thanks again everyone!

  10. #10
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: speed of floating point multiplication vs. division

    Quote Originally Posted by S_M_A View Post
    The reason I wrote modern was merely that I neither remember nor can test how such an old compiler behaves.
    Mainly added for completeness: I think I can assure that MS C 6.0 (most probably early 90s, running under DOS) already did that "prefer SHL/SAR/SHR over MUL/IMUL/DIV/IDIV when possible" optimization. Probably even some earlier compilers did that as well, but I'm not definitely sure about that. IIRC they even did replace multiplications by constants with few one-bits with SHL/ADD combinations.

    BTW, haven't seen anything like your Win 3.11 screen shot for a really long time...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  11. #11
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: speed of floating point multiplication vs. division

    Quote Originally Posted by Eri523 View Post
    BTW, haven't seen anything like your Win 3.11 screen shot for a really long time...
    And we shall all be grateful for what has replaced it. Even though I remember that back in those days we all were very exited. The change from running codeview in a DOS screen to running the debugger in a proper window was great. Today that old environment felt very very akward to handle though...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: speed of floating point multiplication vs. division

    Quote Originally Posted by S_M_A View Post
    Edit: I just remembered that some years ago I cleaned up some old 16 bit stuff and before throwing it nostalgy made me make a virtual Win3.11 machine. Here's a screen dump
    Well S_M_A, that's a lot of dedication just to see what 16-bit Visual C++ handled divisions and multiplications.

    Regards,

    Paul McKenzie

  13. #13
    Join Date
    Jan 2009
    Posts
    1,689

    Re: speed of floating point multiplication vs. division

    Quote Originally Posted by acppdummy View Post
    Thank you both! How about integer multiplication vs. shift? For example <<2 vs. *4 ? Thanks!
    shift is ALWAYS faster. However, you shouldn't need to worry about that. The compiler is smart enough to know that and fix it for you. In fact, shift is so much faster that to multiply by six (which is not shiftable)

    Code:
    x = (y << 2) + (y << 1);
    is faster than
    Code:
    x = y * 6;
    But again, your compiler should do that for you. And I'm not sure this is still the case, I know it was the case on 486 and before, but processors have changed a lot. No matter how much processors change though, a bit shift will always be faster than a multiplication, its probably actually the fastest instruction their is. Of course, it's only useful for integers, so it doesn't affect the original query.

    Like everyone else said though, the pipeline and the cache have a much bigger impact.

  14. #14
    Join Date
    Nov 2010
    Posts
    105

    Re: speed of floating point multiplication vs. division

    Does the speed depend on how many bits are shifted? For example would >>16 take longer than >>1 ? Just curious. Thanks!

  15. #15
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: speed of floating point multiplication vs. division

    Quote Originally Posted by acppdummy View Post
    Does the speed depend on how many bits are shifted? For example would >>16 take longer than >>1 ? Just curious. Thanks!
    Not on any architecture I now of.

    It may depend on the size of the object being shifted though, in particular, for when the long long type that doesn't fit inside a register (eg, it is emulated). Though in this case, to be fair, the instruction doesn't actually exist, the assembly code is hand written by the compiler writers.
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured