+ enum CastlingSide {
+ KING_SIDE,
+ QUEEN_SIDE
+ };
+
+ enum MoveType {
+ CAPTURE,
+ NON_CAPTURE
+ };
+
+ // Functions
+ bool castling_is_check(const Position&, CastlingSide);
+
+ // Helper templates
+ template<CastlingSide Side>
+ MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist);
+
+ template<Color Us>
+ MoveStack* generate_pawn_blocking_evasions(const Position&, Bitboard, Bitboard, MoveStack*);
+
+ template<Color Us>
+ MoveStack* generate_pawn_captures(const Position& pos, MoveStack* mlist);
+
+ template<Color Us, SquareDelta Diagonal>
+ MoveStack* generate_pawn_captures_diagonal(MoveStack* mlist, Bitboard pawns, Bitboard enemyPieces, bool promotion);
+
+ template<Color Us>
+ MoveStack* generate_pawn_noncaptures(const Position& pos, MoveStack* mlist);
+
+ template<Color Us>
+ 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) {
+
+ return (us == WHITE ? generate_pawn_checks<WHITE>(p, dc, ksq, m)
+ : generate_pawn_checks<BLACK>(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>(p, m)
+ : generate_pawn_captures<BLACK>(p, m));
+ else
+ return (us == WHITE ? generate_pawn_noncaptures<WHITE>(p, m)
+ : generate_pawn_noncaptures<BLACK>(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) {
+
+ return (us == WHITE ? generate_pawn_blocking_evasions<WHITE>(p, pnd, t, m)
+ : generate_pawn_blocking_evasions<BLACK>(p, pnd, t, m));
+ }