const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, RANK_1, DELTA_S, DELTA_SE, DELTA_SW, BLACK, WHITE,
&forward_black, &forward_left_black, &forward_right_black };
- int generate_pawn_captures(const PawnOffsets&, const Position&, MoveStack*);
- int generate_pawn_noncaptures(const PawnOffsets&, const Position&, MoveStack*);
- int generate_pawn_checks(const PawnOffsets&, const Position&, Bitboard, Square, MoveStack*, int);
int generate_castle_moves(const Position&, MoveStack*, Color);
- int generate_pawn_blocking_evasions(const PawnOffsets&, const Position&, Bitboard, Bitboard, MoveStack*, int);
+
+ template<Color>
+ int generate_pawn_captures(const Position&, MoveStack*);
+
+ template<Color>
+ int generate_pawn_noncaptures(const Position&, MoveStack*);
+
+ template<Color>
+ int generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*, int);
+
+ template<Color>
+ int generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
template<PieceType>
int generate_piece_moves(const Position&, MoveStack*, Color, Bitboard);
template<PieceType>
int generate_piece_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*, int);
+
+
+ /// Templates are defined here to avoid lookup issues with specializations
+
+ template<PieceType Piece>
+ int generate_piece_moves(const Position &pos, MoveStack *mlist,
+ Color side, Bitboard target) {
+ int n = 0;
+ for (int i = 0; i < pos.piece_count(side, Piece); i++)
+ {
+ Square from = pos.piece_list(side, Piece, i);
+ Bitboard b = pos.piece_attacks<Piece>(from) & target;
+ while (b)
+ {
+ Square to = pop_1st_bit(&b);
+ mlist[n++].move = make_move(from, to);
+ }
+ }
+ return n;
+ }
+
+
+ template<PieceType Piece>
+ 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<Piece>(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<Piece>(ksq) & pos.empty_squares();
+ while (b)
+ {
+ Square from = pop_1st_bit(&b);
+ Bitboard bb = pos.piece_attacks<Piece>(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<KING>(const Position& pos, Bitboard, Bitboard dc,
+ Square ksq, MoveStack* mlist, int n) {
+ if (bit_is_set(dc, ksq))
+ {
+ Bitboard bb = pos.piece_attacks<KING>(ksq)
+ & pos.empty_squares()
+ & ~QueenPseudoAttacks[ksq];
+ while (bb)
+ {
+ Square to = pop_1st_bit(&bb);
+ mlist[n++].move = make_move(ksq, to);
+ }
+ }
+ return n;
+ }
+
+
+ template<PieceType Piece>
+ int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
+ Bitboard blockSquares, MoveStack* mlist, int n) {
+ while (b)
+ {
+ Square from = pop_1st_bit(&b);
+ Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
+ while (bb)
+ {
+ Square to = pop_1st_bit(&bb);
+ mlist[n++].move = make_move(from, to);
+ }
+ }
+ return n;
+ }
}
int n;
if (us == WHITE)
- n = generate_pawn_captures(WhitePawnOffsets, pos, mlist);
+ n = generate_pawn_captures<WHITE>(pos, mlist);
else
- n = generate_pawn_captures(BlackPawnOffsets, pos, mlist);
+ n = generate_pawn_captures<BLACK>(pos, mlist);
n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
int n;
if (us == WHITE)
- n = generate_pawn_noncaptures(WhitePawnOffsets, pos, mlist);
+ n = generate_pawn_noncaptures<WHITE>(pos, mlist);
else
- n = generate_pawn_noncaptures(BlackPawnOffsets, pos, mlist);
+ n = generate_pawn_noncaptures<BLACK>(pos, mlist);
n += generate_piece_moves<KNIGHT>(pos, mlist+n, us, target);
n += generate_piece_moves<BISHOP>(pos, mlist+n, us, target);
// Pawn moves
if (us == WHITE)
- n = generate_pawn_checks(WhitePawnOffsets, pos, dc, ksq, mlist, 0);
+ n = generate_pawn_checks<WHITE>(pos, dc, ksq, mlist, 0);
else
- n = generate_pawn_checks(BlackPawnOffsets, pos, dc, ksq, mlist, 0);
+ n = generate_pawn_checks<BLACK>(pos, dc, ksq, mlist, 0);
// Pieces moves
Bitboard b = pos.knights(us);
if (b)
n = generate_piece_checks<QUEEN>(pos, b, dc, ksq, mlist, n);
- // King moves
- Square from = pos.king_square(us);
- if (bit_is_set(dc, from))
- {
- b = pos.piece_attacks<KING>(from) & pos.empty_squares() & ~QueenPseudoAttacks[ksq];
- while (b)
- {
- Square to = pop_1st_bit(&b);
- mlist[n++].move = make_move(from, to);
- }
- }
+ // Hopefully we always have a king ;-)
+ n = generate_piece_checks<KING>(pos, b, dc, pos.king_square(us), mlist, n);
// TODO: Castling moves!
// Pawn moves. Because a blocking evasion can never be a capture, we
// only generate pawn pushes.
if (us == WHITE)
- n = generate_pawn_blocking_evasions(WhitePawnOffsets, pos, not_pinned, blockSquares, mlist, n);
+ n = generate_pawn_blocking_evasions<WHITE>(pos, not_pinned, blockSquares, mlist, n);
else
- n = generate_pawn_blocking_evasions(BlackPawnOffsets, pos, not_pinned, blockSquares, mlist, n);
+ n = generate_pawn_blocking_evasions<BLACK>(pos, not_pinned, blockSquares, mlist, n);
// Pieces moves
b1 = pos.knights(us) & not_pinned;
namespace {
- int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
+ template<Color C>
+ int generate_pawn_captures(const Position& pos, MoveStack* mlist) {
+
+ static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
Bitboard pawns = pos.pawns(ofs.us);
Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
}
- int generate_pawn_noncaptures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist) {
+ template<Color C>
+ int generate_pawn_noncaptures(const Position& pos, MoveStack* mlist) {
+
+ static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
Bitboard pawns = pos.pawns(ofs.us);
Bitboard enemyPieces = pos.pieces_of_color(ofs.them);
}
- template<PieceType Piece>
- int generate_piece_moves(const Position &pos, MoveStack *mlist,
- Color side, Bitboard target) {
-
- Square from, to;
- Bitboard b;
- int n = 0;
-
- for (int i = 0; i < pos.piece_count(side, Piece); i++)
- {
- from = pos.piece_list(side, Piece, i);
- b = pos.piece_attacks<Piece>(from) & target;
- while (b)
- {
- to = pop_1st_bit(&b);
- mlist[n++].move = make_move(from, to);
- }
- }
- return n;
- }
-
-
int generate_castle_moves(const Position &pos, MoveStack *mlist, Color us) {
int n = 0;
return n;
}
- template<PieceType Piece>
- 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<Piece>(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<Piece>(ksq) & pos.empty_squares();
- while (b)
- {
- Square from = pop_1st_bit(&b);
- Bitboard bb = pos.piece_attacks<Piece>(from) & checkSqs;
- while (bb)
- {
- Square to = pop_1st_bit(&bb);
- mlist[n++].move = make_move(from, to);
- }
- }
- return n;
- }
- int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
+ template<Color C>
+ int generate_pawn_checks(const Position& pos, Bitboard dc, Square ksq, MoveStack* mlist, int n)
{
+ static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
+
// Pawn moves which give discovered check. This is possible only if the
// pawn is not on the same file as the enemy king, because we don't
// generate captures.
return n;
}
+ template<Color C>
+ int generate_pawn_blocking_evasions(const Position& pos, Bitboard not_pinned,
+ Bitboard blockSquares, MoveStack* mlist, int n) {
- template<PieceType Piece>
- int generate_piece_blocking_evasions(const Position& pos, Bitboard b,
- Bitboard blockSquares, MoveStack* mlist, int n) {
- while (b)
- {
- Square from = pop_1st_bit(&b);
- Bitboard bb = pos.piece_attacks<Piece>(from) & blockSquares;
- while (bb)
- {
- Square to = pop_1st_bit(&bb);
- mlist[n++].move = make_move(from, to);
- }
- }
- return n;
- }
-
+ static const PawnOffsets ofs = (C == WHITE ? WhitePawnOffsets : BlackPawnOffsets);
- int generate_pawn_blocking_evasions(const PawnOffsets& ofs, const Position& pos, Bitboard not_pinned,
- Bitboard blockSquares, MoveStack* mlist, int n) {
// Find non-pinned pawns
Bitboard b1 = pos.pawns(ofs.us) & not_pinned;