mirror of
https://github.com/sockspls/badfish
synced 2025-04-29 16:23:09 +00:00
Refactor Position::pinned_pieces() to use templates
Also better document this interesting function. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
parent
2f8961beef
commit
af59cb1d63
4 changed files with 49 additions and 44 deletions
|
@ -150,26 +150,16 @@ extern Bitboard QueenPseudoAttacks[64];
|
||||||
/// Functions for testing whether a given bit is set in a bitboard, and for
|
/// Functions for testing whether a given bit is set in a bitboard, and for
|
||||||
/// setting and clearing bits.
|
/// setting and clearing bits.
|
||||||
|
|
||||||
inline Bitboard set_mask_bb(Square s) {
|
|
||||||
// return 1ULL << s;
|
|
||||||
return SetMaskBB[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Bitboard clear_mask_bb(Square s) {
|
|
||||||
// return ~set_mask_bb(s);
|
|
||||||
return ClearMaskBB[s];
|
|
||||||
}
|
|
||||||
|
|
||||||
inline Bitboard bit_is_set(Bitboard b, Square s) {
|
inline Bitboard bit_is_set(Bitboard b, Square s) {
|
||||||
return b & set_mask_bb(s);
|
return b & SetMaskBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void set_bit(Bitboard *b, Square s) {
|
inline void set_bit(Bitboard *b, Square s) {
|
||||||
*b |= set_mask_bb(s);
|
*b |= SetMaskBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void clear_bit(Bitboard *b, Square s) {
|
inline void clear_bit(Bitboard *b, Square s) {
|
||||||
*b &= clear_mask_bb(s);
|
*b &= ClearMaskBB[s];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -778,7 +778,7 @@ namespace {
|
||||||
while (b)
|
while (b)
|
||||||
{
|
{
|
||||||
Square from, to = pop_1st_bit(&b);
|
Square from, to = pop_1st_bit(&b);
|
||||||
if (!(escapeSquares & ~queen_attacks_bb(to, occ & clear_mask_bb(s))))
|
if (!(escapeSquares & ~queen_attacks_bb(to, occ & ClearMaskBB[s])))
|
||||||
{
|
{
|
||||||
// We have a mate, unless the queen is pinned or there
|
// We have a mate, unless the queen is pinned or there
|
||||||
// is an X-ray attack through the queen.
|
// is an X-ray attack through the queen.
|
||||||
|
@ -787,8 +787,8 @@ namespace {
|
||||||
from = p.piece_list(them, QUEEN, i);
|
from = p.piece_list(them, QUEEN, i);
|
||||||
if ( bit_is_set(p.piece_attacks<QUEEN>(from), to)
|
if ( bit_is_set(p.piece_attacks<QUEEN>(from), to)
|
||||||
&& !bit_is_set(p.pinned_pieces(them), from)
|
&& !bit_is_set(p.pinned_pieces(them), from)
|
||||||
&& !(rook_attacks_bb(to, occ & clear_mask_bb(from)) & p.rooks_and_queens(us))
|
&& !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us))
|
||||||
&& !(rook_attacks_bb(to, occ & clear_mask_bb(from)) & p.rooks_and_queens(us)))
|
&& !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us)))
|
||||||
|
|
||||||
ei.mateThreat[them] = make_move(from, to);
|
ei.mateThreat[them] = make_move(from, to);
|
||||||
}
|
}
|
||||||
|
|
|
@ -296,40 +296,53 @@ void Position::copy(const Position &pos) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position:pinned_pieces() returns a bitboard of all pinned (against the
|
/// Position:pinned_pieces<>() returns a bitboard of all pinned (against the
|
||||||
/// king) pieces for the given color.
|
/// king) pieces for the given color and for the given pinner type.
|
||||||
|
template<PieceType Piece>
|
||||||
|
Bitboard Position::pinned_pieces(Color c, Square ksq) const {
|
||||||
|
|
||||||
Bitboard Position::pinned_pieces(Color c) const {
|
Square s;
|
||||||
Bitboard b1, b2, pinned, pinners, sliders;
|
Bitboard sliders, pinned = EmptyBoardBB;
|
||||||
Square ksq = king_square(c), s;
|
|
||||||
Color them = opposite_color(c);
|
if (Piece == ROOK) // Resolved at compile time
|
||||||
|
sliders = rooks_and_queens(opposite_color(c)) & RookPseudoAttacks[ksq];
|
||||||
|
else
|
||||||
|
sliders = bishops_and_queens(opposite_color(c)) & BishopPseudoAttacks[ksq];
|
||||||
|
|
||||||
pinned = EmptyBoardBB;
|
if (sliders && (sliders & ~checkersBB))
|
||||||
b1 = occupied_squares();
|
{
|
||||||
|
// Our king blockers are candidate pinned pieces
|
||||||
|
Bitboard candidate_pinned = piece_attacks<Piece>(ksq) & pieces_of_color(c);
|
||||||
|
|
||||||
sliders = rooks_and_queens(them) & ~checkers();
|
// Pinners are sliders, not checkers, that give check when
|
||||||
if(sliders & RookPseudoAttacks[ksq]) {
|
// candidate pinned are removed.
|
||||||
b2 = piece_attacks<ROOK>(ksq) & pieces_of_color(c);
|
Bitboard pinners = sliders & ~checkersBB;
|
||||||
pinners = rook_attacks_bb(ksq, b1 ^ b2) & sliders;
|
if (Piece == ROOK)
|
||||||
while(pinners) {
|
pinners &= rook_attacks_bb(ksq, occupied_squares() ^ candidate_pinned);
|
||||||
s = pop_1st_bit(&pinners);
|
else
|
||||||
pinned |= (squares_between(s, ksq) & b2);
|
pinners &= bishop_attacks_bb(ksq, occupied_squares() ^ candidate_pinned);
|
||||||
}
|
|
||||||
|
// Finally for each pinner find the corresponding pinned piece
|
||||||
|
// among the candidates.
|
||||||
|
while (pinners)
|
||||||
|
{
|
||||||
|
s = pop_1st_bit(&pinners);
|
||||||
|
pinned |= (squares_between(s, ksq) & candidate_pinned);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
sliders = bishops_and_queens(them) & ~checkers();
|
|
||||||
if(sliders & BishopPseudoAttacks[ksq]) {
|
|
||||||
b2 = piece_attacks<BISHOP>(ksq) & pieces_of_color(c);
|
|
||||||
pinners = bishop_attacks_bb(ksq, b1 ^ b2) & sliders;
|
|
||||||
while(pinners) {
|
|
||||||
s = pop_1st_bit(&pinners);
|
|
||||||
pinned |= (squares_between(s, ksq) & b2);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return pinned;
|
return pinned;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Position:pinned_pieces() returns a bitboard of all pinned (against the
|
||||||
|
/// king) pieces for the given color.
|
||||||
|
Bitboard Position::pinned_pieces(Color c) const {
|
||||||
|
|
||||||
|
Square ksq = king_square(c);
|
||||||
|
return pinned_pieces<ROOK>(c, ksq) | pinned_pieces<BISHOP>(c, ksq);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Position:discovered_check_candidates() returns a bitboard containing all
|
/// Position:discovered_check_candidates() returns a bitboard containing all
|
||||||
/// pieces for the given side which are candidates for giving a discovered
|
/// pieces for the given side which are candidates for giving a discovered
|
||||||
/// check. The code is almost the same as the function for finding pinned
|
/// check. The code is almost the same as the function for finding pinned
|
||||||
|
|
|
@ -197,6 +197,8 @@ public:
|
||||||
// Bitboards for pinned pieces and discovered check candidates
|
// Bitboards for pinned pieces and discovered check candidates
|
||||||
Bitboard discovered_check_candidates(Color c) const;
|
Bitboard discovered_check_candidates(Color c) const;
|
||||||
Bitboard pinned_pieces(Color c) const;
|
Bitboard pinned_pieces(Color c) const;
|
||||||
|
template<PieceType Piece>
|
||||||
|
Bitboard pinned_pieces(Color c, Square ksq) const;
|
||||||
|
|
||||||
// Checking pieces
|
// Checking pieces
|
||||||
Bitboard checkers() const;
|
Bitboard checkers() const;
|
||||||
|
@ -553,7 +555,7 @@ inline Bitboard Position::checkers() const {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::is_check() const {
|
inline bool Position::is_check() const {
|
||||||
return checkers() != EmptyBoardBB;
|
return checkersBB != EmptyBoardBB;
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool Position::pawn_attacks_square(Color c, Square f, Square t) const {
|
inline bool Position::pawn_attacks_square(Color c, Square f, Square t) const {
|
||||||
|
|
Loading…
Add table
Reference in a new issue