Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Branchless code

Author: Gerd Isenberg

Date: 04:50:57 11/19/02

Go up one level in this thread


On November 19, 2002 at 07:02:54, Sune Fischer wrote:

>On November 19, 2002 at 06:36:08, Gerd Isenberg wrote:
>
>>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].
>
>ok.
>But is this different from ((bool)x)?
>Is (bool)x just 1&x or does it do x!=0, which is not the same of course.

x!=0

>
>Last question, does it matter if x is negative, will (-1) be false?

No, all != 0 is true.

>
>I always get warnings when casting to boolean, I never get warnings usually when
>casting, in fact I cast to get rid of those warnings, so something must be going
>on.

What do you assign to bool? Assigning relational or logical expressions should
be fine without any warning. Of course if you assign an int to bool the compiler
is right to warn you, because of the rather different value ranges.

Btw. the build in C++ type "bool" is often implemented with byte size.
Due to performace reasons, it might be advantageous to use an own typedef like

typedef int BOOL;

#ifndef TRUE
#define FALSE 0
#define TRUE 1
#endif

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.