Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Branchless code

Author: Gerd Isenberg

Date: 03:36:08 11/19/02

Go up one level in this thread


On November 19, 2002 at 05:28:03, Sune Fischer wrote:

>On November 19, 2002 at 04:57:50, Gerd Isenberg wrote:
>>Therefore it is often possible to replace some conditional assignments or simple
>>if[else] statements by some cheap test,cmp,setcc,shift,add or lea instructions:
>>
>>c =  a > b ? 32 : 16;
>>c = 16 + 16*(a>b);
>
>is that legal, will a>b (true or false) always be 1 or 0?

Yes, boolean expressions with relational and/or logical operators produce a
[0,1] result.

>
>Because usually expressions like:
>if (x) {...}
>just means x!=0.
>

If you write (x != 0) the boolean result is [0,1].

>
>>// color [0,1]  ==> white,black
>>delta = color == WHITE ? 8 : -8;
>
>how about:
>
>delta = -8*(((color)<<1)-1)

should procuce the same code as

delta = 8 - 16*color;
or
delta = 8*(1 - 2*color);

Gerd




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.