- assert(is_ok(m));
- assert(&newSt != st);
-
- thisThread->nodes.fetch_add(1, std::memory_order_relaxed);
- Key k = st->key ^ Zobrist::side;
-
- // Copy some fields of the old state to our new StateInfo object except the
- // ones which are going to be recalculated from scratch anyway and then switch
- // our state pointer to point to the new (ready to be updated) state.
- std::memcpy(&newSt, st, offsetof(StateInfo, key));
- newSt.previous = st;
- st = &newSt;
-
- // Increment ply counters. In particular, rule50 will be reset to zero later on
- // in case of a capture or a pawn move.
- ++gamePly;
- ++st->rule50;
- ++st->pliesFromNull;
-
- Color us = sideToMove;
- Color them = ~us;
- Square from = from_sq(m);
- Square to = to_sq(m);
- Piece pc = piece_on(from);
- Piece captured = type_of(m) == ENPASSANT ? make_piece(them, PAWN) : piece_on(to);
-
- assert(color_of(pc) == us);
- assert(captured == NO_PIECE || color_of(captured) == (type_of(m) != CASTLING ? them : us));
- assert(type_of(captured) != KING);
-
- if (type_of(m) == CASTLING)
- {
- assert(pc == make_piece(us, KING));
- assert(captured == make_piece(us, ROOK));
-
- Square rfrom, rto;
- do_castling<true>(us, from, to, rfrom, rto);
-
- st->psq += PSQT::psq[captured][rto] - PSQT::psq[captured][rfrom];
- k ^= Zobrist::psq[captured][rfrom] ^ Zobrist::psq[captured][rto];
- captured = NO_PIECE;
- }
-
- if (captured)
- {
- Square capsq = to;
-
- // If the captured piece is a pawn, update pawn hash key, otherwise
- // update non-pawn material.
- if (type_of(captured) == PAWN)
- {
- if (type_of(m) == ENPASSANT)
- {
- capsq -= pawn_push(us);
-
- assert(pc == make_piece(us, PAWN));
- assert(to == st->epSquare);
- assert(relative_rank(us, to) == RANK_6);
- assert(piece_on(to) == NO_PIECE);
- assert(piece_on(capsq) == make_piece(them, PAWN));
-
- board[capsq] = NO_PIECE; // Not done by remove_piece()
- }
-
- st->pawnKey ^= Zobrist::psq[captured][capsq];
- }
- else
- st->nonPawnMaterial[them] -= PieceValue[MG][captured];
-
- // Update board and piece lists
- remove_piece(captured, capsq);
-
- // Update material hash key and prefetch access to materialTable
- k ^= Zobrist::psq[captured][capsq];
- st->materialKey ^= Zobrist::psq[captured][pieceCount[captured]];
- prefetch(thisThread->materialTable[st->materialKey]);
-
- // Update incremental scores
- st->psq -= PSQT::psq[captured][capsq];
-
- // Reset rule 50 counter
- st->rule50 = 0;
- }
-
- // Update hash key
- k ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
-
- // Reset en passant square
- if (st->epSquare != SQ_NONE)
- {
- k ^= Zobrist::enpassant[file_of(st->epSquare)];
- st->epSquare = SQ_NONE;
- }
-
- // Update castling rights if needed
- if (st->castlingRights && (castlingRightsMask[from] | castlingRightsMask[to]))
- {
- int cr = castlingRightsMask[from] | castlingRightsMask[to];
- k ^= Zobrist::castling[st->castlingRights & cr];
- st->castlingRights &= ~cr;
- }
-
- // Move the piece. The tricky Chess960 castling is handled earlier
- if (type_of(m) != CASTLING)
- move_piece(pc, from, to);
-
- // If the moving piece is a pawn do some special extra work
- if (type_of(pc) == PAWN)
- {
- // Set en-passant square if the moved pawn can be captured
- if ( (int(to) ^ int(from)) == 16
- && (attacks_from<PAWN>(to - pawn_push(us), us) & pieces(them, PAWN)))
- {
- st->epSquare = to - pawn_push(us);
- k ^= Zobrist::enpassant[file_of(st->epSquare)];
- }
-
- else if (type_of(m) == PROMOTION)
- {
- Piece promotion = make_piece(us, promotion_type(m));
-
- assert(relative_rank(us, to) == RANK_8);
- assert(type_of(promotion) >= KNIGHT && type_of(promotion) <= QUEEN);
-
- remove_piece(pc, to);
- put_piece(promotion, to);
-
- // Update hash keys
- k ^= Zobrist::psq[pc][to] ^ Zobrist::psq[promotion][to];
- st->pawnKey ^= Zobrist::psq[pc][to];
- st->materialKey ^= Zobrist::psq[promotion][pieceCount[promotion]-1]
- ^ Zobrist::psq[pc][pieceCount[pc]];
-
- // Update incremental score
- st->psq += PSQT::psq[promotion][to] - PSQT::psq[pc][to];
-
- // Update material
- st->nonPawnMaterial[us] += PieceValue[MG][promotion];
- }
-
- // Update pawn hash key and prefetch access to pawnsTable
- st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
- prefetch2(thisThread->pawnsTable[st->pawnKey]);
-
- // Reset rule 50 draw counter
- st->rule50 = 0;
- }
-
- // Update incremental scores
- st->psq += PSQT::psq[pc][to] - PSQT::psq[pc][from];
-
- // Set capture piece
- st->capturedPiece = captured;
-
- // Update the key with the final value
- st->key = k;
-
- // Calculate checkers bitboard (if move gives check)
- st->checkersBB = givesCheck ? attackers_to(square<KING>(them)) & pieces(us) : 0;
+ // Is there a discovered check?
+ if (blockers_for_king(~sideToMove) & from)
+ return !aligned(from, to, square<KING>(~sideToMove)) || type_of(m) == CASTLING;
+
+ switch (type_of(m))
+ {
+ case NORMAL :
+ return false;
+
+ case PROMOTION :
+ return attacks_bb(promotion_type(m), to, pieces() ^ from) & square<KING>(~sideToMove);
+
+ // En passant capture with check? We have already handled the case of direct
+ // checks and ordinary discovered check, so the only case we need to handle
+ // is the unusual case of a discovered check through the captured pawn.
+ case EN_PASSANT : {
+ Square capsq = make_square(file_of(to), rank_of(from));
+ Bitboard b = (pieces() ^ from ^ capsq) | to;
+
+ return (attacks_bb<ROOK>(square<KING>(~sideToMove), b) & pieces(sideToMove, QUEEN, ROOK))
+ | (attacks_bb<BISHOP>(square<KING>(~sideToMove), b)
+ & pieces(sideToMove, QUEEN, BISHOP));
+ }
+ default : //CASTLING
+ {
+ // Castling is encoded as 'king captures the rook'
+ Square rto = relative_square(sideToMove, to > from ? SQ_F1 : SQ_D1);