Author: Gerd Isenberg
Date: 23:10:16 09/27/03
Go up one level in this thread
On September 27, 2003 at 14:08:58, Koundinya Veluri wrote: >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. Ok, it may depend on the callee with inlined functions. IIRC the ms-compiler does some optimization with int: if ( isBlack(c)) ... ; c = ecx test ecx, ecx jnz _isblack instead of ; c = ecx xor eax, eax test ecx, ecx setne al .... test al, al jnz _isblack I agree that it is a compiler "fault" not to use the zero flag immediatly. Same for boolean mapping with bit testing. > >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. You are right, a dumb sample... I mean immediate conditional statements with these boolean inliners, instead if ( color == WHITE ) if ( color.isWhite() ) >Also, there will be a >problem in isWhite1() where basically a conversion to bool will be necessary. > Yes, one may use "!c" or "c^1" or "1-c". My point with "bool" is, that there are often not necessary SETxx instructions. >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. > No, x86 addressing modes only use 32-bit registers in 32-bit mode. On Opteron one may use 32- and 64-bit registers in 64-bit mode. Signed int32 is sign extended at runtime and need some extra time. Gerd >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.