Asm optimisation
Posted: Tue Feb 05, 2008 9:29 am
Right, I'm not an asm programmer, and I *think* that this is a situation in which hand crafted asm code may just outdo the compiler. Possibly.
The situation: Im writing a chess engine using bitboards. These are 64-bit representations of a predicate, for example "which tiles contain white pieces?".
There is a specific function which is used extremely often when calculating the movement potential of rooks, queens and bishops. We extract the rank that the piece in question is on, from two bitboards - one which describes if there are ANY pieces in each tile, and one which describes if there are any OPPOSING pieces in each tile. Given these ranks as 8-bit integers, and an integer giving the "file" the piece in question is located, we need to find all possible moves for that piece.
The C++ code I have below works, but I reckon its possible that an experienced ASM programmer could improve some/much of it. What do you reckon? Can the compiler be beaten?
EDIT: Forgot to mention that the ranks are stored MSB first, so file "1" on a chessboard is represented by the most significant bit of a byte, and the file "8" (on the right of the chessboard) by the LSB.
It will be interesting to see the results...
Cheers,
James
The situation: Im writing a chess engine using bitboards. These are 64-bit representations of a predicate, for example "which tiles contain white pieces?".
There is a specific function which is used extremely often when calculating the movement potential of rooks, queens and bishops. We extract the rank that the piece in question is on, from two bitboards - one which describes if there are ANY pieces in each tile, and one which describes if there are any OPPOSING pieces in each tile. Given these ranks as 8-bit integers, and an integer giving the "file" the piece in question is located, we need to find all possible moves for that piece.
The C++ code I have below works, but I reckon its possible that an experienced ASM programmer could improve some/much of it. What do you reckon? Can the compiler be beaten?
EDIT: Forgot to mention that the ranks are stored MSB first, so file "1" on a chessboard is represented by the most significant bit of a byte, and the file "8" (on the right of the chessboard) by the LSB.
Code: Select all
unsigned char rankComparison(unsigned char allPieces,
unsigned char enemyPieces,
int file)
{
// Look right.
unsigned char toRet = 0;
// Find the first one bit.
for (int i = file+1; i < 8; i++)
{
if ( ((allPieces << i) & 0x80) == 0)
{
toRet |= 0x80 >> i;
}
else
{
// Here we check enemyPieces. If an enemy piece is here then we can
// add a 1 in the move byte. Else it's a friendly piece and we can't
// move there.
if ((enemyPieces << i) & 0x80)
toRet |= 0x80 >> i;
break;
}
}
// Look left.
for (int i = file-1; i >= 0; i--)
{
if ( ((allPieces << i) & 0x80) == 0)
toRet |= 0x80 >> i;
else
{
// Here we check enemyPieces. If an enemy piece is here then we can
// add a 1 in the move byte. Else it's a friendly piece and we can't
// move there.
if ((enemyPieces << i) & 0x80)
toRet |= 0x80 >> i;
break;
}
}
return toRet;
}
Cheers,
James