1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-07-11 19:49:14 +00:00

Retire attackers_to(Square s, Color c)

Use the definition in the few places where is needed.

As a nice side effect there is also an optimization in
generate_evasions() where the bitboard of enemy pieces
is computed only once and out of a tight loop.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-09-20 10:47:59 +01:00
parent 6845397c5c
commit 84d6fe0f31
3 changed files with 16 additions and 21 deletions

View file

@ -254,12 +254,13 @@ MoveStack* generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pin
// Generate evasions for king // Generate evasions for king
Bitboard b1 = pos.piece_attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks; Bitboard b1 = pos.piece_attacks_from<KING>(ksq) & ~pos.pieces_of_color(us) & ~checkersAttacks;
Bitboard enemy = pos.pieces_of_color(them);
while (b1) while (b1)
{ {
to = pop_1st_bit(&b1); to = pop_1st_bit(&b1);
// Note that we can use attackers_to() only because we // Note that we can use attackers_to() only because we
// have already removed slider checkers. // have already removed slider checkers.
if (!pos.attackers_to(to, them)) if (!(pos.attackers_to(to) & enemy))
(*mlist++).move = make_move(ksq, to); (*mlist++).move = make_move(ksq, to);
} }
@ -441,7 +442,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
// is occupied or under attack. // is occupied or under attack.
for (s = Min(from, g1); s <= Max(from, g1); s++) for (s = Min(from, g1); s <= Max(from, g1); s++)
if ( (s != from && s != to && !pos.square_is_empty(s)) if ( (s != from && s != to && !pos.square_is_empty(s))
|| pos.attackers_to(s, them)) ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
illegal = true; illegal = true;
// Check if any of the squares between king and rook // Check if any of the squares between king and rook
@ -472,7 +473,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
for (s = Min(from, c1); s <= Max(from, c1); s++) for (s = Min(from, c1); s <= Max(from, c1); s++)
if( (s != from && s != to && !pos.square_is_empty(s)) if( (s != from && s != to && !pos.square_is_empty(s))
|| pos.attackers_to(s, them)) ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
illegal = true; illegal = true;
for (s = Min(to, d1); s <= Max(to, d1); s++) for (s = Min(to, d1); s <= Max(to, d1); s++)
@ -942,7 +943,7 @@ namespace {
// It is a bit complicated to correctly handle Chess960 // It is a bit complicated to correctly handle Chess960
for (s = Min(ksq, s1); s <= Max(ksq, s1); s++) for (s = Min(ksq, s1); s <= Max(ksq, s1); s++)
if ( (s != ksq && s != rsq && pos.square_is_occupied(s)) if ( (s != ksq && s != rsq && pos.square_is_occupied(s))
|| pos.attackers_to(s, them)) ||(pos.attackers_to(s) & pos.pieces_of_color(them)))
illegal = true; illegal = true;
for (s = Min(rsq, s2); s <= Max(rsq, s2); s++) for (s = Min(rsq, s2); s <= Max(rsq, s2); s++)

View file

@ -452,7 +452,7 @@ bool Position::move_attacks_square(Move m, Square s) const {
void Position::find_checkers() { void Position::find_checkers() {
Color us = side_to_move(); Color us = side_to_move();
st->checkersBB = attackers_to(king_square(us), opposite_color(us)); st->checkersBB = attackers_to(king_square(us)) & pieces_of_color(opposite_color(us));
} }
@ -509,7 +509,7 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const {
// If the moving piece is a king, check whether the destination // If the moving piece is a king, check whether the destination
// square is attacked by the opponent. // square is attacked by the opponent.
if (type_of_piece_on(from) == KING) if (type_of_piece_on(from) == KING)
return !attackers_to(move_to(m), opposite_color(us)); return !(attackers_to(move_to(m)) & pieces_of_color(opposite_color(us)));
// A non-king move is legal if and only if it is not pinned or it // A non-king move is legal if and only if it is not pinned or it
// is moving along the ray towards or away from the king. // is moving along the ray towards or away from the king.
@ -864,7 +864,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
// Update checkers bitboard, piece must be already moved // Update checkers bitboard, piece must be already moved
if (ep | pm) if (ep | pm)
st->checkersBB = attackers_to(king_square(them), us); st->checkersBB = attackers_to(king_square(them)) & pieces_of_color(us);
else else
{ {
st->checkersBB = EmptyBoardBB; st->checkersBB = EmptyBoardBB;
@ -1041,7 +1041,7 @@ void Position::do_castle_move(Move m) {
st->rule50 = 0; st->rule50 = 0;
// Update checkers BB // Update checkers BB
st->checkersBB = attackers_to(king_square(them), us); st->checkersBB = attackers_to(king_square(them)) & pieces_of_color(us);
// Finish // Finish
sideToMove = opposite_color(sideToMove); sideToMove = opposite_color(sideToMove);
@ -1358,12 +1358,12 @@ int Position::see(Square from, Square to) const {
while (true) while (true)
{ {
clear_bit(&occ, from); clear_bit(&occ, from);
attackers = (rook_attacks_bb(to, occ) & pieces(ROOK, QUEEN)) attackers = (rook_attacks_bb(to, occ) & pieces(ROOK, QUEEN))
| (bishop_attacks_bb(to, occ) & pieces(BISHOP, QUEEN)) | (bishop_attacks_bb(to, occ) & pieces(BISHOP, QUEEN))
| (piece_attacks_from<KNIGHT>(to) & pieces(KNIGHT)) | (piece_attacks_from<KNIGHT>(to) & pieces(KNIGHT))
| (piece_attacks_from<KING>(to) & pieces(KING)) | (piece_attacks_from<KING>(to) & pieces(KING))
| (pawn_attacks_from(to, WHITE) & pieces(PAWN, BLACK)) | (pawn_attacks_from(to, WHITE) & pieces(PAWN, BLACK))
| (pawn_attacks_from(to, BLACK) & pieces(PAWN, WHITE)); | (pawn_attacks_from(to, BLACK) & pieces(PAWN, WHITE));
if (from != SQ_NONE) if (from != SQ_NONE)
break; break;
@ -1923,7 +1923,7 @@ bool Position::is_ok(int* failedStep) const {
Color us = side_to_move(); Color us = side_to_move();
Color them = opposite_color(us); Color them = opposite_color(us);
Square ksq = king_square(them); Square ksq = king_square(them);
if (attackers_to(ksq, us)) if (attackers_to(ksq) & pieces_of_color(us))
return false; return false;
} }

View file

@ -197,7 +197,6 @@ public:
// Information about attacks to or from a given square // Information about attacks to or from a given square
Bitboard attackers_to(Square s) const; Bitboard attackers_to(Square s) const;
Bitboard attackers_to(Square s, Color c) const;
Bitboard piece_attacks_from(Piece p, Square s) const; Bitboard piece_attacks_from(Piece p, Square s) const;
Bitboard pawn_attacks_from(Square s, Color c) const; Bitboard pawn_attacks_from(Square s, Color c) const;
template<PieceType> Bitboard piece_attacks_from(Square s) const; template<PieceType> Bitboard piece_attacks_from(Square s) const;
@ -470,11 +469,6 @@ inline bool Position::is_check() const {
return st->checkersBB != EmptyBoardBB; return st->checkersBB != EmptyBoardBB;
} }
inline Bitboard Position::attackers_to(Square s, Color c) const {
return attackers_to(s) & pieces_of_color(c);
}
inline bool Position::pawn_is_passed(Color c, Square s) const { inline bool Position::pawn_is_passed(Color c, Square s) const {
return !(pieces(PAWN, opposite_color(c)) & passed_pawn_mask(c, s)); return !(pieces(PAWN, opposite_color(c)) & passed_pawn_mask(c, s));
} }