Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: making code color independant

Author: Heiner Marxen

Date: 10:47:36 12/08/03

Go up one level in this thread


On December 08, 2003 at 12:28:55, Georg v. Zimmermann wrote:

>This has been recently discussed, but on a very sophisticated level, while I am
>looking for a simple solution.
>
>Right now I just have 2 makeMove() , 2 genAllMove(), 2 genCaptures() etc etc
>
>In there are statements like
>
>if (to >> 4 == 7) // is it a promotion ?
>{...}
>
>To make the code color independent, my first idea was to have
>
>if (to >> 4 == lastRank[onMove])
>{..}

That is the most natural approach IMHO.
If you want to stay simple, I would recommend this.

Those small arrays indexed by color (I assume color is 0 or 1),
can sometimes be replaced by unconditional expressions, which do not even
need a memory reference, by filling small bit groups into a single constant,
and shifting out the relevant part:
    lastRank[2] ={ 7, 0 }
translates to
    (0x70 >> (onMove*4)) & 0x0f

[ Looks ugly, but somehow I like it :-) ]

In this special case we an do even better than the general approach:
    (8 - onMove) & 07

Whether the involved shifting and masking is worth to avoid a memory reference
entirely depends on... a lot of circumstances: CPU, memory footprint etc.
With a macro you an always hide the actual implementation.


>and have such similar arrays for colorMask[2], fifthRank[2], e1Square[2],
>pawnDirection[2] etc.
>
>First question: how slow is this ? Is it comparable slow as having
>
>if (onMove)
>  if (to >> 4 == 7)
>  {...}
>else
>  if (to >> 4 == 0)
>  {...}
>
>which is horrible ?
>
>
>Then my next idea was to have a structure where I put all the relevant info in.
>struct colorDep
>{
>  int lastRank;
>  [...]
>};
>
>and then fill 2 such structures with the correct info for white and black. And
>have a pointer which always points to the correct one. Then I could do
>
>if (to >> 4 == cD->lastRank)
>{..}
>
>Is this faster as the array solution, as I think it is ?

Interesting!  Never tried this.
But instead of accessing the index each time, now the pointer has to be
accessed every time.  So you trade an absolute array address against a
structure offset.  Looks like a small improvement, but maybe not.

Cheers,
Heiner




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.