@@ -52,19 +52,65 @@ namespace {
5252 EVASION
5353 };
5454
55- // Helper templates
5655 template <CastlingSide Side>
5756 MoveStack* generate_castle_moves (const Position&, MoveStack*);
5857
5958 template <Color Us, MoveType Type>
6059 MoveStack* generate_pawn_moves (const Position&, MoveStack*, Bitboard, Square);
6160
62- // Template generate_piece_moves (captures and non-captures) with specializations and overloads
63- template <PieceType>
64- MoveStack* generate_piece_moves (const Position&, MoveStack*, Color, Bitboard);
61+ template <PieceType Piece>
62+ inline MoveStack* generate_discovered_checks (const Position& pos, MoveStack* mlist, Square from) {
63+
64+ assert (Piece != QUEEN);
65+
66+ Bitboard b = pos.attacks_from <Piece>(from) & pos.empty_squares ();
67+ if (Piece == KING)
68+ {
69+ Square ksq = pos.king_square (opposite_color (pos.side_to_move ()));
70+ b &= ~QueenPseudoAttacks[ksq];
71+ }
72+ SERIALIZE_MOVES (b);
73+ return mlist;
74+ }
75+
76+ template <PieceType Piece>
77+ inline MoveStack* generate_direct_checks (const Position& pos, MoveStack* mlist, Color us,
78+ Bitboard dc, Square ksq) {
79+ assert (Piece != KING);
80+
81+ Bitboard checkSqs, b;
82+ Square from;
83+ const Square* ptr = pos.piece_list_begin (us, Piece);
84+
85+ if ((from = *ptr++) == SQ_NONE)
86+ return mlist;
87+
88+ checkSqs = pos.attacks_from <Piece>(ksq) & pos.empty_squares ();
89+
90+ do
91+ {
92+ if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
93+ || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
94+ || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
95+ continue ;
96+
97+ if (dc && bit_is_set (dc, from))
98+ continue ;
99+
100+ b = pos.attacks_from <Piece>(from) & checkSqs;
101+ SERIALIZE_MOVES (b);
102+
103+ } while ((from = *ptr++) != SQ_NONE);
104+
105+ return mlist;
106+ }
65107
66108 template <>
67- MoveStack* generate_piece_moves<KING>(const Position&, MoveStack*, Color, Bitboard);
109+ inline MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m, Color us, Bitboard dc, Square ksq) {
110+
111+ return (us == WHITE ? generate_pawn_moves<WHITE, CHECK>(p, m, dc, ksq)
112+ : generate_pawn_moves<BLACK, CHECK>(p, m, dc, ksq));
113+ }
68114
69115 template <PieceType Piece, MoveType Type>
70116 inline MoveStack* generate_piece_moves (const Position& p, MoveStack* m, Color us, Bitboard t) {
@@ -76,20 +122,35 @@ namespace {
76122 : generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE));
77123 }
78124
79- // Templates for non-capture checks generation
80-
81125 template <PieceType Piece>
82- MoveStack* generate_discovered_checks (const Position&, MoveStack*, Square);
126+ inline MoveStack* generate_piece_moves (const Position& pos , MoveStack* mlist, Color us, Bitboard target) {
83127
84- template <PieceType>
85- MoveStack* generate_direct_checks (const Position&, MoveStack*, Color, Bitboard, Square);
128+ Bitboard b;
129+ Square from;
130+ const Square* ptr = pos.piece_list_begin (us, Piece);
131+
132+ if (*ptr != SQ_NONE)
133+ {
134+ do {
135+ from = *ptr;
136+ b = pos.attacks_from <Piece>(from) & target;
137+ SERIALIZE_MOVES (b);
138+ } while (*++ptr != SQ_NONE);
139+ }
140+ return mlist;
141+ }
86142
87143 template <>
88- inline MoveStack* generate_direct_checks<PAWN >(const Position& p , MoveStack* m , Color us, Bitboard dc, Square ksq ) {
144+ inline MoveStack* generate_piece_moves<KING >(const Position& pos , MoveStack* mlist , Color us, Bitboard target ) {
89145
90- return (us == WHITE ? generate_pawn_moves<WHITE, CHECK>(p, m, dc, ksq)
91- : generate_pawn_moves<BLACK, CHECK>(p, m, dc, ksq));
146+ Bitboard b;
147+ Square from = pos.king_square (us);
148+
149+ b = pos.attacks_from <KING>(from) & target;
150+ SERIALIZE_MOVES (b);
151+ return mlist;
92152 }
153+
93154}
94155
95156
@@ -415,35 +476,6 @@ bool move_is_legal(const Position& pos, const Move m, Bitboard pinned) {
415476
416477namespace {
417478
418- template <PieceType Piece>
419- MoveStack* generate_piece_moves (const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
420-
421- Bitboard b;
422- Square from;
423- const Square* ptr = pos.piece_list_begin (us, Piece);
424-
425- if (*ptr != SQ_NONE)
426- {
427- do {
428- from = *ptr;
429- b = pos.attacks_from <Piece>(from) & target;
430- SERIALIZE_MOVES (b);
431- } while (*++ptr != SQ_NONE);
432- }
433- return mlist;
434- }
435-
436- template <>
437- MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) {
438-
439- Bitboard b;
440- Square from = pos.king_square (us);
441-
442- b = pos.attacks_from <KING>(from) & target;
443- SERIALIZE_MOVES (b);
444- return mlist;
445- }
446-
447479 template <SquareDelta Delta>
448480 inline Bitboard move_pawns (Bitboard p) {
449481
@@ -607,53 +639,6 @@ namespace {
607639 return mlist;
608640 }
609641
610- template <PieceType Piece>
611- MoveStack* generate_discovered_checks (const Position& pos, MoveStack* mlist, Square from) {
612-
613- assert (Piece != QUEEN);
614-
615- Bitboard b = pos.attacks_from <Piece>(from) & pos.empty_squares ();
616- if (Piece == KING)
617- {
618- Square ksq = pos.king_square (opposite_color (pos.side_to_move ()));
619- b &= ~QueenPseudoAttacks[ksq];
620- }
621- SERIALIZE_MOVES (b);
622- return mlist;
623- }
624-
625- template <PieceType Piece>
626- MoveStack* generate_direct_checks (const Position& pos, MoveStack* mlist, Color us,
627- Bitboard dc, Square ksq) {
628- assert (Piece != KING);
629-
630- Bitboard checkSqs, b;
631- Square from;
632- const Square* ptr = pos.piece_list_begin (us, Piece);
633-
634- if ((from = *ptr++) == SQ_NONE)
635- return mlist;
636-
637- checkSqs = pos.attacks_from <Piece>(ksq) & pos.empty_squares ();
638-
639- do
640- {
641- if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
642- || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
643- || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
644- continue ;
645-
646- if (dc && bit_is_set (dc, from))
647- continue ;
648-
649- b = pos.attacks_from <Piece>(from) & checkSqs;
650- SERIALIZE_MOVES (b);
651-
652- } while ((from = *ptr++) != SQ_NONE);
653-
654- return mlist;
655- }
656-
657642 template <CastlingSide Side>
658643 MoveStack* generate_castle_moves (const Position& pos, MoveStack* mlist) {
659644
0 commit comments