- 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;
-
- // Used by NNUE
- st->accumulator.computed_accumulation = false;
- auto& dp = st->dirtyPiece;
- dp.dirty_num = 1;
-
- 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);
-
- 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));
- }
-
- st->pawnKey ^= Zobrist::psq[captured][capsq];
- }
- else
- st->nonPawnMaterial[them] -= PieceValue[MG][captured];
-
- if (Eval::useNNUE)
- {
- dp.dirty_num = 2; // 1 piece moved, 1 piece captured
- dp.piece[1] = captured;
- dp.from[1] = capsq;
- dp.to[1] = SQ_NONE;
- }
-
- // Update board and piece lists
- remove_piece(capsq);
-
- if (type_of(m) == ENPASSANT)
- board[capsq] = NO_PIECE;
-
- // 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]);
-
- // 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]))
- {
- k ^= Zobrist::castling[st->castlingRights];
- st->castlingRights &= ~(castlingRightsMask[from] | castlingRightsMask[to]);
- k ^= Zobrist::castling[st->castlingRights];
- }
-
- // Move the piece. The tricky Chess960 castling is handled earlier
- if (type_of(m) != CASTLING)
- {
- if (Eval::useNNUE)
- {
- dp.piece[0] = pc;
- dp.from[0] = from;
- dp.to[0] = to;
- }
-
- move_piece(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
- && (pawn_attacks_bb(us, to - pawn_push(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(to);
- put_piece(promotion, to);
-
- if (Eval::useNNUE)
- {
- // Promoting pawn to SQ_NONE, promoted piece from SQ_NONE
- dp.to[0] = SQ_NONE;
- dp.piece[dp.dirty_num] = promotion;
- dp.from[dp.dirty_num] = SQ_NONE;
- dp.to[dp.dirty_num] = to;
- dp.dirty_num++;
- }
-
- // 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 material
- st->nonPawnMaterial[us] += PieceValue[MG][promotion];
- }
-
- // Update pawn hash key
- st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to];
-
- // Reset rule 50 draw counter
- st->rule50 = 0;
- }
-
- // 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;
-
- sideToMove = ~sideToMove;
-
- // Update king attacks used for fast check detection
- set_check_info(st);
-
- // Calculate the repetition info. It is the ply distance from the previous
- // occurrence of the same position, negative in the 3-fold case, or zero
- // if the position was not repeated.
- st->repetition = 0;
- int end = std::min(st->rule50, st->pliesFromNull);
- if (end >= 4)
- {
- StateInfo* stp = st->previous->previous;
- for (int i = 4; i <= end; i += 2)
- {
- stp = stp->previous->previous;
- if (stp->key == st->key)
- {
- st->repetition = stp->repetition ? -i : i;
- break;
- }
- }
- }
-
- assert(pos_is_ok());
-}