Bitboards and inCheck
Posted: 03 Jan 2007, 11:19
Happy 2007 to everybody.
I have a question to the use of bitboards to check whether one side is in check. I currently use some code like that:
This looks expensive and the function is a bit to big for my taste to inline. Will the compiler inline the whole code or only the side relevant part? Is there something fundamentally better to test if the king is in check?
I use this function to check whether the king is in check or to see if a square is attacked, therefore the optional variable int square. I know that I could remove the if(square < 0) line if I would make separate functions, but I doubt if it's worth it.
I have a question to the use of bitboards to check whether one side is in check. I currently use some code like that:
- Code: Select all
bool inCheck(const Position& poos, bool side, int square = -1);
bool inCheck(const Position& ppos, bool side, int square)
{
register Bitboard attackers, all_attacks;
all_attacks = 0;
if (side == WHITE) {
if (square < 0) square = ppos.WhiteKing;
attackers = Attacks::BishopAttacks(ppos.occupied, square);
all_attacks = attackers & ppos.BlackBishops;
all_attacks |= attackers & ppos.BlackQueens;
attackers = Attacks::RookAttacks(ppos.occupied, square);
all_attacks |= attackers & ppos.BlackRooks;
all_attacks |= attackers & ppos.BlackQueens;
all_attacks |= knight_attacks[square] & ppos.BlackKnights;
all_attacks |= wpawn_attacks[square] & ppos.BlackPawns;
all_attacks |= king_attacks[square] & set_mask[ppos.BlackKing];
}
else {
if (square < 0) square = ppos.BlackKing;
attackers = Attacks::BishopAttacks(ppos.occupied, square);
all_attacks = attackers & ppos.WhiteBishops;
all_attacks |= attackers & ppos.WhiteQueens;
attackers = Attacks::RookAttacks(ppos.occupied, square);
all_attacks |= attackers & ppos.WhiteRooks;
all_attacks |= attackers & ppos.WhiteQueens;
all_attacks |= knight_attacks[square] & ppos.WhiteKnights;
all_attacks |= bpawn_attacks[square] & ppos.WhitePawns;
all_attacks |= king_attacks[square] & set_mask[ppos.WhiteKing];
}
return (all_attacks) ? true : false;
}
This looks expensive and the function is a bit to big for my taste to inline. Will the compiler inline the whole code or only the side relevant part? Is there something fundamentally better to test if the king is in check?
I use this function to check whether the king is in check or to see if a square is attacked, therefore the optional variable int square. I know that I could remove the if(square < 0) line if I would make separate functions, but I doubt if it's worth it.