Author: Gerd Isenberg
Date: 09:31:29 06/01/04
Go up one level in this thread
On June 01, 2004 at 11:45:53, Volker Böhm wrote:
>Hi,
>
>I am using attack-tables. For some code I whant to know on witch fields the king
>could escape. An escape field is a field not attacked by opponent and not
>occupied by an own piece.
>I am building up an bit array that hold a bit for every adjacent field. The bit
>is set if the king can move to this field. For generation of this bit-field I
>use 8 compares with 8 jumps and would like to optimize this.
>
>Some years ago I programmed intel assembler and remembered a trick doing this
>without any jumps. Now I wonder if it is able add code that makes the compiler
>use this trick. Here it is:
>
>Istead of
>int bitfield = 0;
>if (q8 != 0) bitfield++;
>bitfield << 1;
>if (q7 != 0) bitfield++;
>...
>
>{aternative:
>int bitfield = 0;
>if (q8 != 0) bitfield += (1 << 7);
>if (q7 != 0) bitfield += (1 << 6);
>...
>}
>
>In assembler I could write (i am not quite sure if my assembler syntax is
>correct any more):
>
>XOR AX, AX
>CMP Q1, 0
>RCL AX, 1
>CMP Q2, 0
>RCL AX, 1
>...
>
>{
>alternative:
>XOR AX, AX
>CMP Q1, 0
>ADC AX, 0
>SHL AX, 1
>CMP Q2, 0
>ADC AX, 0
>...
>}
>
>As the result of a compare sets the carry-flag you can rotate the carry-flag
>into your bit-field.
>
>
>Anybody knows how to "tell" the compiler to use the RCL trick?
>
>Volker
Hi Volker,
Not sure, i guess current compiler are not aware of the RCL trick.
So you may still use (inline) assembler for best perfomance.
Something like this in C may be result in a chain of cmp, setnz and lea
instructions:
bitfield = (q8 != 0);
bitfield <<= 1;
bitfield += (q7 != 0);
bitfield <<= 1;
...
bitfield += (q0 != 0);
or more compact:
bitfield = ...((((((q8!=0)<<1)+(q7!=0))<<1)+(q6!=0))<<1)...
Btw. have you considered using bitboards?
Cheers,
Gerd
This page took 0.01 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.