From: Marco Costalba Date: Sun, 20 Sep 2009 08:43:28 +0000 (+0100) Subject: Cleanup piece_attacks_square() functions X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=f74f42b2982e25e180417948771843e8bb6bd4b7 Cleanup piece_attacks_square() functions Most of them are not required to be public and are used in one place only so remove them and use its definitions. Also rename piece_attacks_square() in piece_attacks() to be aligned to the current naming policy. No functional change. Signed-off-by: Marco Costalba --- diff --git a/src/movegen.cpp b/src/movegen.cpp index c7f729e5..7bddeb6e 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -557,7 +557,7 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) { } // Luckly we can handle all the other pieces in one go - return ( pos.piece_attacks_square(pos.piece_on(from), from, to) + return ( bit_is_set(pos.piece_attacks(pc, from), to) && pos.pl_move_is_legal(m, pinned) && !move_is_promotion(m)); } diff --git a/src/position.cpp b/src/position.cpp index 162c9423..c4798fb5 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -392,23 +392,22 @@ Bitboard Position::attackers_to(Square s) const { | (piece_attacks(s) & pieces(KING)); } -/// Position::piece_attacks_square() tests whether the piece on square f -/// attacks square t. +/// Position::piece_attacks() computes a bitboard of all attacks +/// of a given piece put in a given square. -bool Position::piece_attacks_square(Piece p, Square f, Square t) const { +Bitboard Position::piece_attacks(Piece p, Square s) const { - assert(square_is_ok(f)); - assert(square_is_ok(t)); + assert(square_is_ok(s)); switch (p) { - case WP: return pawn_attacks_square(f, t, WHITE); - case BP: return pawn_attacks_square(f, t, BLACK); - case WN: case BN: return piece_attacks_square(f, t); - case WB: case BB: return piece_attacks_square(f, t); - case WR: case BR: return piece_attacks_square(f, t); - case WQ: case BQ: return piece_attacks_square(f, t); - case WK: case BK: return piece_attacks_square(f, t); + case WP: return pawn_attacks(s, WHITE); + case BP: return pawn_attacks(s, BLACK); + case WN: case BN: return piece_attacks(s); + case WB: case BB: return piece_attacks(s); + case WR: case BR: return piece_attacks(s); + case WQ: case BQ: return piece_attacks(s); + case WK: case BK: return piece_attacks(s); default: break; } return false; @@ -427,7 +426,7 @@ bool Position::move_attacks_square(Move m, Square s) const { assert(square_is_occupied(f)); - if (piece_attacks_square(piece_on(f), t, s)) + if (bit_is_set(piece_attacks(piece_on(f), t), s)) return true; // Move the piece and scan for X-ray attacks behind it diff --git a/src/position.h b/src/position.h index de6d186b..d47d84b7 100644 --- a/src/position.h +++ b/src/position.h @@ -198,13 +198,9 @@ public: // Attack information to a given square Bitboard attackers_to(Square s) const; Bitboard attackers_to(Square s, Color c) const; - template Bitboard piece_attacks(Square s) const; + Bitboard piece_attacks(Piece p, Square s) const; Bitboard pawn_attacks(Square s, Color c) const; - - // Attack information to a given square from another given square - template Bitboard piece_attacks_square(Square f, Square t) const; // Dispatch at compile-time - bool piece_attacks_square(Piece p, Square f, Square t) const; // Dispatch at run-time - bool pawn_attacks_square(Square f, Square t, Color c) const; + template Bitboard piece_attacks(Square s) const; // Properties of moves bool pl_move_is_legal(Move m) const; @@ -474,15 +470,6 @@ inline bool Position::is_check() const { return st->checkersBB != EmptyBoardBB; } -inline bool Position::pawn_attacks_square(Square f, Square t, Color c) const { - return bit_is_set(pawn_attacks(f, c), t); -} - -template -inline Bitboard Position::piece_attacks_square(Square f, Square t) const { - return bit_is_set(piece_attacks(f), t); -} - inline Bitboard Position::attackers_to(Square s, Color c) const { return attackers_to(s) & pieces_of_color(c); diff --git a/src/search.cpp b/src/search.cpp index 049328e1..acd5d1da 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -2153,7 +2153,9 @@ namespace { // the second move is assumed to be a move from the current position. bool connected_moves(const Position& pos, Move m1, Move m2) { + Square f1, t1, f2, t2; + Piece p; assert(move_is_ok(m1)); assert(move_is_ok(m2)); @@ -2179,11 +2181,12 @@ namespace { return true; // Case 4: The destination square for m2 is attacked by the moving piece in m1 - if (pos.piece_attacks_square(pos.piece_on(t1), t1, t2)) + p = pos.piece_on(t1); + if (bit_is_set(pos.piece_attacks(p, t1), t2)) return true; // Case 5: Discovered check, checking piece is the piece moved in m1 - if ( piece_is_slider(pos.piece_on(t1)) + if ( piece_is_slider(p) && bit_is_set(squares_between(t1, pos.king_square(pos.side_to_move())), f2) && !bit_is_set(squares_between(t1, pos.king_square(pos.side_to_move())), t2)) { @@ -2191,19 +2194,19 @@ namespace { Color us = pos.side_to_move(); Square ksq = pos.king_square(us); clear_bit(&occ, f2); - if (pos.type_of_piece_on(t1) == BISHOP) + if (type_of_piece(p) == BISHOP) { if (bit_is_set(bishop_attacks_bb(ksq, occ), t1)) return true; } - else if (pos.type_of_piece_on(t1) == ROOK) + else if (type_of_piece(p) == ROOK) { if (bit_is_set(rook_attacks_bb(ksq, occ), t1)) return true; } else { - assert(pos.type_of_piece_on(t1) == QUEEN); + assert(type_of_piece(p) == QUEEN); if (bit_is_set(queen_attacks_bb(ksq, occ), t1)) return true; }