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: 10:37:25 01/03/03

Go up one level in this thread


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...



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.