A little benchmark in D and C

safety0ff via digitalmars-d-ldc digitalmars-d-ldc at puremagic.com
Sun Jul 6 01:30:01 PDT 2014


On Friday, 4 July 2014 at 15:55:50 UTC, David Nadlinger wrote:
>
> Would be interesting to look at the asm to see what is going on 
> here.

I reversed the gcc assembly output, gcc compiles the run() 
function as follows:

void run() {
     unsigned i = 10;
     for (;; i += 2)
     {
//    movl    $0x55555555, %ebp // magic contant for division via 
multiplication
//    mull    %ebp // edx = mulhi (magic, i)
//    shrl    %edx // edx >>= 1 (part of magic division)
//    leal    (%rdx,%rdx,2), %eax // eax = 3*rdx
//    if (i - eax != 0) // i - 3 * rdx = remainder
//        continue
         if ((i & 3) != 0) // above comments show how this line is 
implemented
             continue;     // see ch.10 section 3 of hacker's 
delight 2nd ed.
         if (i % 5 != 0)
             continue;
         if (i % 6 != 0)
             continue;
         if (i % 7 != 0)
             continue;
         if (isEvenlyDivisible(8, i, t))
             break;
     }

     printf("%d\n", i);
}

Without the division by 3 optimization the code runs slower with 
that check in place. Basically there's some epic wins in 
unrolling that function.


More information about the digitalmars-d-ldc mailing list