Compiler optimizations (I'm baffled)

Dave Dave_member at pathlink.com
Wed May 3 12:30:23 PDT 2006


Bruno Medeiros wrote:
> Walter Bright wrote:
>> Craig Black wrote:
>>>  This is
>>> because integer division is essentially floating point division under 
>>> the
>>> hood.
>>
>> Not exactly. What is true is that both kinds of division are based on 
>> the same 'shift and subtract' method (the same algorithm taught in 3rd 
>> grade for doing long division). If you're really interested in exactly 
>> how integer and floating point division work, get the Digital Mars CD 
>> which has complete source code to an IEEE 754 floating point emulator, 
>> as well as code to do integer division for long's.
>>
>> www.digitalmars.com/shop.html
> 
> I ran these tests and I got basicly the same results (the int division 
> is slower). I am very intrigued and confused. Can you (or someone else) 
> explain briefly why this is so?
> One would think it would be the other way around (float being slower) or 
> at least the same speed.
> 

If you take a look at the assembly for div(), idiv is being executed 
twice - once for the division and once for the modulo. If you replace 
the modulo with the explicit calculation then the perf. will improve a 
lot because imul & sub is faster than idiv.

It'd be nice if dmd would do this optimization for us because 
calculating the integer quotient and subsequent remainder for the same 
dividend and divisor would seem to be pretty common operations.



More information about the Digitalmars-d mailing list