- // Calculate our parametrized parameters at compile time
- const Color Them = (Us == WHITE ? BLACK : WHITE);
- const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
- const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
- const SquareDelta TDELTA_N = (Us == WHITE ? DELTA_N : DELTA_S);
- const SquareDelta TDELTA_S = (Us == WHITE ? DELTA_S : DELTA_N);
-
- Square to;
- Bitboard b1, b2, b3;
- Bitboard pawns = pos.pieces(PAWN, Us);
-
- if (dc & pawns)
- {
- Bitboard empty = pos.empty_squares();
-
- // Pawn moves which gives 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.
- b1 = pawns & ~file_bb(ksq);
-
- // Discovered checks, single pawn pushes, no promotions
- b2 = b3 = move_pawns<Us, DELTA_N>(b1 & dc) & empty & ~TRank8BB;
- SERIALIZE_MOVES_D(b3, -TDELTA_N);
-
- // Discovered checks, double pawn pushes
- b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty;
- SERIALIZE_MOVES_D(b3, -TDELTA_N -TDELTA_N);
- }
-
- // Direct checks. These are possible only for pawns on neighboring files
- // and in the two ranks that, after the push, are in front of the enemy king.
- b1 = pawns & neighboring_files_bb(ksq) & ~dc;
-
- // We can get false positives if (ksq + x) is not in [0,63] range but
- // is not a problem, they will be filtered out later.
- b2 = b1 & (rank_bb(ksq + 2 * TDELTA_S) | rank_bb(ksq + 3 * TDELTA_S));
- if (!b2)
- return mlist;
-
- // Direct checks, single pawn pushes
- Bitboard empty = pos.empty_squares();
- b2 = move_pawns<Us, DELTA_N>(b1) & empty;
- b3 = b2 & pos.attacks_from<PAWN>(ksq, Them);
- SERIALIZE_MOVES_D(b3, -TDELTA_N);
-
- // Direct checks, double pawn pushes
- b3 = move_pawns<Us, DELTA_N>(b2 & TRank3BB) & empty & pos.attacks_from<PAWN>(ksq, Them);
- SERIALIZE_MOVES_D(b3, -TDELTA_N -TDELTA_N);
- return mlist;
- }
-
- template<PieceType Piece>
- MoveStack* generate_piece_checks(const Position& pos, MoveStack* mlist, Color us,
- Bitboard dc, Square ksq) {
-
- Bitboard target = pos.pieces(Piece, us);
-
- // Discovered checks
- Bitboard b = target & dc;
- while (b)
- {
- Square from = pop_1st_bit(&b);
- Bitboard bb = pos.attacks_from<Piece>(from) & pos.empty_squares();
- if (Piece == KING)
- bb &= ~QueenPseudoAttacks[ksq];
-
- SERIALIZE_MOVES(bb);
- }
-
- // Direct checks
- b = target & ~dc;
- Bitboard checkSqs = pos.attacks_from<Piece>(ksq) & pos.empty_squares();
- if (Piece == KING || !checkSqs)
- return mlist;
-
- while (b)
- {
- Square from = pop_1st_bit(&b);
- if ( (Piece == QUEEN && !(QueenPseudoAttacks[from] & checkSqs))
- || (Piece == ROOK && !(RookPseudoAttacks[from] & checkSqs))
- || (Piece == BISHOP && !(BishopPseudoAttacks[from] & checkSqs)))
- continue;
-
- Bitboard bb = pos.attacks_from<Piece>(from) & checkSqs;
- SERIALIZE_MOVES(bb);
- }
- return mlist;
+ from = pop_1st_bit(&b);
+ switch (type_of(pos.piece_on(from)))
+ {
+ case PAWN: /* Will be generated togheter with pawns direct checks */ break;
+ case KNIGHT: mlist = generate_discovered_checks<KNIGHT>(pos, mlist, from); break;
+ case BISHOP: mlist = generate_discovered_checks<BISHOP>(pos, mlist, from); break;
+ case ROOK: mlist = generate_discovered_checks<ROOK>(pos, mlist, from); break;
+ case KING: mlist = generate_discovered_checks<KING>(pos, mlist, from); break;
+ default: assert(false); break;
+ }