Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: bool versus BOOL in C++

Author: Koundinya Veluri

Date: 11:08:58 09/27/03

Go up one level in this thread


On September 27, 2003 at 03:42:07, Gerd Isenberg wrote:

>I use color (WHITE, BLACK) with int type, e.g. as array index.
>Is there a way in C++ to assign the color to a bool (bool isBlack(int color))
>only by moving or using a partial byte register, e.g. color is in EAX, so the
>bool is already in AL?
>
>The compiler is not aware of the value range with only two values even with enum
>(int) type, which map perfectly to bool. Therefore the compiler will perform
>additional TEST and SETNZ instructions.
>
>If you use an int-BOOL and stay with probably ambiguous "TRUE"-values (all != 0)
>that is not necessary.

It's the compiler's fault if it can't recognize the enum case :). But anyway
using my compiler I tested some things. The same assembly code is generated for
these two functions:

#define WHITE (0)
#define BLACK (1)

int isBlack1(int c)
{
    return c != WHITE;
}

bool isBlack2(int c)
{
    return c != WHITE;
}

The code generated is:

; c = ecx
	xor	eax, eax
	test	ecx, ecx
	setne	al

For the function returning bool, the best I could do is return c & 1, which
generates one and operation. Since the same code is generated for isBlack1() and
isBlack2(), I see no advantage in using BOOL.

If you are saying that no test is necessary for isBlack1(), then that's the same
as returning c. But then there is no point to the function isBlack1(). You can
just use the color directly in the calling function. Also, there will be a
problem in isWhite1() where basically a conversion to bool will be necessary.

I also tried making the colors bool but when I use a bool as an array index,
this compiler generates a movzx to zero the rest of the register and then uses
the whole register as the index. Instead couldn't it just use the partial
register as the index? That would be a possible solution too, if that worked.

Koundinya



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.