} // namespace Zobrist
+namespace {
+
+/// next_attacker() is an helper function used by see() to locate the least
+/// valuable attacker for the side to move, remove the attacker we just found
+/// from the 'occupied' bitboard and scan for new X-ray attacks behind it.
+
+template<int Pt> FORCE_INLINE
+PieceType next_attacker(const Bitboard* bb, const Square& to, const Bitboard& stmAttackers,
+ Bitboard& occupied, Bitboard& attackers) {
+
+ if (stmAttackers & bb[Pt])
+ {
+ Bitboard b = stmAttackers & bb[Pt];
+ occupied ^= b & ~(b - 1);
+
+ if (Pt == PAWN || Pt == BISHOP || Pt == QUEEN)
+ attackers |= attacks_bb<BISHOP>(to, occupied) & (bb[BISHOP] | bb[QUEEN]);
+
+ if (Pt == ROOK || Pt == QUEEN)
+ attackers |= attacks_bb<ROOK>(to, occupied) & (bb[ROOK] | bb[QUEEN]);
+
+ return (PieceType)Pt;
+ }
+ return next_attacker<Pt+1>(bb, to, stmAttackers, occupied, attackers);
+}
+
+template<> FORCE_INLINE
+PieceType next_attacker<KING>(const Bitboard*, const Square&, const Bitboard&, Bitboard&, Bitboard&) {
+ return KING; // No need to update bitboards, it is the last cycle
+}
+
+} // namespace
+
+
/// CheckInfo c'tor
CheckInfo::CheckInfo(const Position& pos) {
int Position::see(Move m) const {
Square from, to;
- Bitboard occupied, attackers, stmAttackers, b;
+ Bitboard occupied, attackers, stmAttackers;
int swapList[32], slIndex = 1;
PieceType captured;
Color stm;
swapList[slIndex] = -swapList[slIndex - 1] + PieceValue[Mg][captured];
slIndex++;
- // Locate the least valuable attacker for the side to move. The loop
- // below looks like it is potentially infinite, but it isn't. We know
- // that the side to move still has at least one attacker left.
- for (captured = PAWN; (b = stmAttackers & pieces(captured)) == 0; captured++)
- assert(captured < KING);
+ // Locate and remove from 'occupied' the next least valuable attacker
+ captured = next_attacker<PAWN>(byTypeBB, to, stmAttackers, occupied, attackers);
- // Remove the attacker we just found from the 'occupied' bitboard,
- // and scan for new X-ray attacks behind the attacker.
- occupied ^= (b & (~b + 1));
- attackers |= (attacks_bb<ROOK>(to, occupied) & pieces(ROOK, QUEEN))
- | (attacks_bb<BISHOP>(to, occupied) & pieces(BISHOP, QUEEN));
-
- attackers &= occupied; // Cut out pieces we've already done
+ attackers &= occupied; // Remove the just found attacker
stm = ~stm;
stmAttackers = attackers & pieces(stm);
- // Stop before processing a king capture
- if (captured == KING && stmAttackers)
+ if (captured == KING)
{
- assert(slIndex < 32);
- swapList[slIndex++] = QueenValueMg * 16;
+ // Stop before processing a king capture
+ if (stmAttackers)
+ swapList[slIndex++] = QueenValueMg * 16;
+
break;
}