Removing the need for separates evaluation/generation functs
Posted: 06 Dec 2005, 19:07
Hi,
After I red a post on CCC by Anthony, I thought about a solution to the recurent need of different functions for the white and black pieces.
Thoses doubled functions are needed to avoid the overhead of branching where the code need to be different depending on witch side is on move.
As an example we can see something like this in the move generation function for the pawns :
In order to avoid this engine's programmer often (tell me if i'm wrong) use two differents function for the withe and black on move:
This will make the engine more efficient since all the test about the colors are removed and raplaced by a single test in the calling function. However this duplication of code has some well know draw backs. Principaly, we have to make any change to the function two times, this make such code prone to errors.
This is essentially what Anthony was trying to avoid (I think). While reading his post it appear to me that the first function could be made as a template like this :
then the calling code would be like this :
This way we have the best of both worlds. We only have one function to maintain and the conditional branching are eliminated by compiler optimisations because he know at compile time the vallue of sideToMove. The compiler actually compile two function.
I don't think this solution has any drawbacks/overhead, but I never tried it. What is your opinion ?
Mathieu Pag
After I red a post on CCC by Anthony, I thought about a solution to the recurent need of different functions for the white and black pieces.
Thoses doubled functions are needed to avoid the overhead of branching where the code need to be different depending on witch side is on move.
As an example we can see something like this in the move generation function for the pawns :
- Code: Select all
void genPawnsMoves()
{
// stuff
BitBoard toSquares;
if (sideToMove == white)
{
toSquares <<= 8;
}
else
{
toSquares >>= 8;
}
// stuff
}
In order to avoid this engine's programmer often (tell me if i'm wrong) use two differents function for the withe and black on move:
- Code: Select all
void genWhitePawnsMoves()
{
// stuff
BitBoard toSquares;
toSquares <<= 8;
// stuff
}
void genBlackPawnsMoves()
{
// stuff
BitBoard toSquares;
toSquares <<= 8;
// stuff
}
This will make the engine more efficient since all the test about the colors are removed and raplaced by a single test in the calling function. However this duplication of code has some well know draw backs. Principaly, we have to make any change to the function two times, this make such code prone to errors.
This is essentially what Anthony was trying to avoid (I think). While reading his post it appear to me that the first function could be made as a template like this :
- Code: Select all
template <int color>
void genPawnsMoves()
{
// stuff
BitBoard toSquares;
if (color == white)
{
toSquares <<= 8;
}
else
{
toSquares >>= 8;
}
// stuff
}
then the calling code would be like this :
- Code: Select all
if (sideToMove == white)
{
genPawnsMoves<WHITE>();
}
else
{
genPawnsMoves<BLACK>();
}
This way we have the best of both worlds. We only have one function to maintain and the conditional branching are eliminated by compiler optimisations because he know at compile time the vallue of sideToMove. The compiler actually compile two function.
I don't think this solution has any drawbacks/overhead, but I never tried it. What is your opinion ?
Mathieu Pag