You inspired me to think of an own bitboard attack function.
It is somewhere between a traditional for loop and the modern magic.
It is not compiled. Read it as pseudo code.
It is not tested. Could it work?
And will it be slow or fast?
- Code: Select all
typedef uint64_t Bitboard;
Bitboard initB[64];
Bitboard maskB[64];
/*
= init >>= 9 *= 0x5005 &= mask at |= bb
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . 1 1 . . 1 . . . 1 . . . 1 . . . 1 . . . 1 . .
. . 1 . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . .
. . . s . . . . . . . 1 . 1 . . 1 1 . . 1 . . . . . . s . . . . . . . s . . . .
. . 1 . 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 . 1 . . .
. . . . . . . . . . . 1 . 1 . . . 1 1 . . 1 . . . 1 . . . 1 . . . 1 . . . 1 . .
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
*/
Bitboard bishopAttacks( int sq, const Bitboard &free )
{
Bitboard bb = initB[sq];
Bitboard at = bb;
for ( int i = 0; i < 7 && bb != 0; ++i )
{
bb >>= 9;
bb &= 0x007f7f7f7f7f7f7f7f;
bb *= 0x00050005; // 00000000000001010000000000000101
bb &= maskB[sq];
at |= bb;
bb &= free;
}
return at;
}
/*
. . . . . . . .
. . . . . . . .
. . . . . . . .
. . 1 . 1 . . .
. . .sq . . . .
. . 1 . 1 . . .
. . . . . . . .
. . . . . . . .
*/
makeInitB()
{
for ( int sq = 0; sq < 64; ++sq )
{
Bitboard bb = (Bitboard)1 << sq;
initB[sq] = (bb >> 9) & 0x7f7f7f7f7f7f7f7f;
initB[sq] |= (bb >> 7) & 0xfefefefefefefefe;
initB[sq] |= (bb << 9) & 0xfefefefefefefefe;
initB[sq] |= (bb << 7) & 0x7f7f7f7f7f7f7f7f;
}
}
/*
. . . . . . . 1
1 . . . . . 1 .
. 1 . . . 1 . .
. . 1 . 1 . . .
. . .sq . . . .
. . 1 . 1 . . .
. 1 . . . 1 . .
1 . . . . . 1 .
*/
makeMaskB()
{
for ( int sq = 0; sq < 64; ++sq )
{
maskB[sq] = 0;
for ( int i = sq - 9; i >= 0 && i % 8 != 7; i -= 9 )
{
maskB[sq] |= (Bitboard)1 << i;
}
for ( int i = sq - 7; i >= 0 && i % 8 != 0; i -= 7 )
{
maskB[sq] |= (Bitboard)1 << i;
}
for ( int i = sq + 9; i < 64 && i % 8 != 0; i += 9 )
{
maskB[sq] |= (Bitboard)1 << i;
}
for ( int i = sq + 7; i < 64 && i % 8 != 7; i += 7 )
{
maskB[sq] |= (Bitboard)1 << i;
}
}
}
What do you think?
Harald