From 0c8659721f80e4ffb56cee0557c5f62799b68d19 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Mon, 20 Oct 2008 10:22:29 +0200 Subject: [PATCH] Fix a bug in king discoveries checks Introduced in "Add a generate_piece_checks() specialization for the king" Signed-off-by: Marco Costalba --- src/movegen.cpp | 102 +++++++++++++++++++++++------------------------- 1 file changed, 49 insertions(+), 53 deletions(-) diff --git a/src/movegen.cpp b/src/movegen.cpp index 50f8b488..02268bff 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -60,61 +60,10 @@ namespace { template int generate_piece_checks(const Position&, Bitboard, Bitboard, Square, MoveStack*, int); + int generate_piece_checks_king(const Position&, Square, Bitboard, Square, MoveStack*, int); template int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int); - - - /// Templates with specializations are defined here to avoid lookup issues - - template - int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc, - Square ksq, MoveStack* mlist, int n) { - // Discovered checks - Bitboard b = target & dc; - while (b) - { - Square from = pop_1st_bit(&b); - Bitboard bb = pos.piece_attacks(from) & pos.empty_squares(); - while (bb) - { - Square to = pop_1st_bit(&bb); - mlist[n++].move = make_move(from, to); - } - } - // Direct checks - b = target & ~dc; - Bitboard checkSqs = pos.piece_attacks(ksq) & pos.empty_squares(); - while (b) - { - Square from = pop_1st_bit(&b); - Bitboard bb = pos.piece_attacks(from) & checkSqs; - while (bb) - { - Square to = pop_1st_bit(&bb); - mlist[n++].move = make_move(from, to); - } - } - return n; - } - - - template<> // Special case the King - int generate_piece_checks(const Position& pos, Bitboard, Bitboard dc, - Square ksq, MoveStack* mlist, int n) { - if (bit_is_set(dc, ksq)) - { - Bitboard bb = pos.piece_attacks(ksq) - & pos.empty_squares() - & ~QueenPseudoAttacks[ksq]; - while (bb) - { - Square to = pop_1st_bit(&bb); - mlist[n++].move = make_move(ksq, to); - } - } - return n; - } } @@ -218,7 +167,7 @@ int generate_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { n = generate_piece_checks(pos, b, dc, ksq, mlist, n); // Hopefully we always have a king ;-) - n = generate_piece_checks(pos, b, dc, pos.king_square(us), mlist, n); + n = generate_piece_checks_king(pos, pos.king_square(us), dc, ksq, mlist, n); // TODO: Castling moves! @@ -813,6 +762,53 @@ namespace { return n; } + template + int generate_piece_checks(const Position& pos, Bitboard target, Bitboard dc, + Square ksq, MoveStack* mlist, int n) { + // Discovered checks + Bitboard b = target & dc; + while (b) + { + Square from = pop_1st_bit(&b); + Bitboard bb = pos.piece_attacks(from) & pos.empty_squares(); + while (bb) + { + Square to = pop_1st_bit(&bb); + mlist[n++].move = make_move(from, to); + } + } + // Direct checks + b = target & ~dc; + Bitboard checkSqs = pos.piece_attacks(ksq) & pos.empty_squares(); + while (b) + { + Square from = pop_1st_bit(&b); + Bitboard bb = pos.piece_attacks(from) & checkSqs; + while (bb) + { + Square to = pop_1st_bit(&bb); + mlist[n++].move = make_move(from, to); + } + } + return n; + } + + int generate_piece_checks_king(const Position& pos, Square from, Bitboard dc, + Square ksq, MoveStack* mlist, int n) { + if (bit_is_set(dc, from)) + { + Bitboard b = pos.piece_attacks(from) + & pos.empty_squares() + & ~QueenPseudoAttacks[ksq]; + while (b) + { + Square to = pop_1st_bit(&b); + mlist[n++].move = make_move(from, to); + } + } + return n; + } + template int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned, -- 2.39.2