- Bitboard Rank3BB, Rank8BB;
- SquareDelta DELTA_N, DELTA_NE, DELTA_NW;
- Color us, them;
- typedef Bitboard (*Shift_fn)(Bitboard b);
- Shift_fn forward, forward_left, forward_right;
- };
- const PawnOffsets WhitePawnOffsets = { Rank3BB, Rank8BB, DELTA_N, DELTA_NE, DELTA_NW, WHITE,
- BLACK, &forward_white, forward_left_white, forward_right_white };
-
- const PawnOffsets BlackPawnOffsets = { Rank6BB, Rank1BB, DELTA_S, DELTA_SE, DELTA_SW, BLACK,
- WHITE, &forward_black, &forward_left_black, &forward_right_black };
-
- int generate_pawn_captures(const PawnOffsets& ofs, const Position& pos, MoveStack* mlist);
- int generate_white_pawn_noncaptures(const Position&, MoveStack*);
- int generate_black_pawn_noncaptures(const Position&, MoveStack*);
- int generate_piece_moves(PieceType, const Position&, MoveStack*, Color side, Bitboard t);
- int generate_castle_moves(const Position&, MoveStack*, Color us);
-
- int generate_piece_checks(PieceType pce, const Position& pos, Bitboard target,
- Bitboard dc, Square ksq, MoveStack* mlist, int n);
-
- int generate_pawn_checks(const PawnOffsets& ofs, const Position& pos, Bitboard dc,
- Square ksq, MoveStack* mlist, int n);
+ // Helper templates
+ template<CastlingSide Side>
+ MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
+
+ template<Color Us, Rank, Bitboard, SquareDelta>
+ MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
+
+ template<Color, Color, Bitboard, SquareDelta, SquareDelta, SquareDelta>
+ MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
+
+ template<Color, Color, Bitboard, Bitboard, SquareDelta, SquareDelta, SquareDelta>
+ MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
+
+ template<Color, Color, Bitboard, Bitboard, SquareDelta>
+ MoveStack* generate_pawn_checks(const Position&, Bitboard, Square, MoveStack*);
+
+ template<Color Us, SquareDelta Direction>
+ inline Bitboard move_pawns(Bitboard p) {
+
+ if (Direction == DELTA_N)
+ return Us == WHITE ? p << 8 : p >> 8;
+ else if (Direction == DELTA_NE)
+ return Us == WHITE ? p << 9 : p >> 7;
+ else if (Direction == DELTA_NW)
+ return Us == WHITE ? p << 7 : p >> 9;
+ else
+ return p;
+ }
+
+ // Template generate_piece_checks() with specializations
+ template<PieceType>
+ MoveStack* generate_piece_checks(const Position&, MoveStack*, Color, Bitboard, Square);
+
+ template<>
+ inline MoveStack* generate_piece_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
+
+ if (us == WHITE)
+ return generate_pawn_checks<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_N>(p, dc, ksq, m);
+ else
+ return generate_pawn_checks<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_S>(p, dc, ksq, m);
+
+ }
+
+ // Template generate_piece_moves() with specializations and overloads
+ template<PieceType>
+ MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard);
+
+ template<>
+ MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
+
+ template<PieceType Piece, MoveType Type>
+ inline MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us) {
+
+ assert(Piece == PAWN);
+
+ if (Type == CAPTURE)
+ return (us == WHITE ? generate_pawn_captures<WHITE, BLACK, Rank8BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
+ : generate_pawn_captures<BLACK, WHITE, Rank1BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
+ else
+ return (us == WHITE ? generate_pawn_noncaptures<WHITE, BLACK, Rank8BB, Rank3BB, DELTA_NE, DELTA_NW, DELTA_N>(p, m)
+ : generate_pawn_noncaptures<BLACK, WHITE, Rank1BB, Rank6BB, DELTA_SE, DELTA_SW, DELTA_S>(p, m));
+ }
+
+ template<PieceType>
+ MoveStack* generate_piece_moves(const Position&, MoveStack*, Color us, Bitboard, Bitboard);
+
+ template<>
+ inline MoveStack* generate_piece_moves<PAWN>(const Position& p, MoveStack* m,
+ Color us, Bitboard t, Bitboard pnd) {
+ if (us == WHITE)
+ return generate_pawn_blocking_evasions<WHITE, RANK_8, Rank3BB, DELTA_N>(p, pnd, t, m);
+ else
+ return generate_pawn_blocking_evasions<BLACK, RANK_1, Rank6BB, DELTA_S>(p, pnd, t, m);
+ }