Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Optimizing C code for speed - Saving Into A Variable First DOES Work

Author: Robert Hyatt

Date: 13:54:26 01/03/03

Go up one level in this thread


On January 03, 2003 at 16:08:05, Graham Laight wrote:

>On January 03, 2003 at 13:37:25, Robert Hyatt wrote:
>
>>On January 03, 2003 at 10:00:51, Graham Laight wrote:
>>
>>
>>Here is a snippet of assembly produced by gcc, for the test
>>
>>if (x[1]==3 || x[1]==7) return(1);
>>else return(0);
>>
>>        movl    x+4, %eax
>>        cmpl    $3, %eax
>>        sete    %dl
>>        cmpl    $7, %eax
>>        sete    %al
>>        orl     %edx, %eax
>>        testl   $1, %eax
>>        sete    %al
>>        movzbl  %al, %eax
>>        ret
>>
>>Note that X+4 (x[1]) is copied to eax and compared against twice.  This is
>>_exactly_ what happens when you do it the other was by putting x[1] into a
>>temp first...
>>
>>no difference whatsoever...
>>
>>Notice that the compiler also avoids two jumps by saving the results of the two
>>comparisons (sete instructions) and then or'ing the two results together and
>>then
>>using that to compute the proper return value without a jump at all.
>>
>>Compilers are pretty good...
>
>That's 10 assembly commands for 10 C instructions (see count below).
>
>On a 1 Ghz machine that was doing nothing else, that's a theoretical 100 million
>loops per second.
>
>I admit - I'm impressed!

It is even better than that.  In a loop, along with the loop counters, some of
the above instructions execute for free by being paired with other instructions
that
don't have a dependency.

The above is also why Matt and I have pointed out many times that just because a
program
has if then else statements, it does _not_ have to have branches, as
you-know-who seems to
believe...


>
>-g
>
>Count
>=====
>
>if (x[1]==3 || x[1]==7) return(1);
> 1  2   3 4  5  6  7 8    9
>
>else return(0);
>         10
>
>
>1        movl    x+4, %eax
>2        cmpl    $3, %eax
>3        sete    %dl
>4        cmpl    $7, %eax
>5        sete    %al
>6        orl     %edx, %eax
>7        testl   $1, %eax
>8        sete    %al
>9        movzbl  %al, %eax
>10       ret



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.