byTypeBB[capture] ^= capsq;
byColorBB[them] ^= capsq;
- // Update piece list, move the last piece at index[capsq] position and
- // shrink the list.
- //
- // WARNING: This is a not reversible operation. When we will reinsert the
- // captured piece in undo_move() we will put it at the end of the list and
- // not in its original place, it means index[] and pieceList[] are not
- // guaranteed to be invariant to a do_move() + undo_move() sequence.
- Square lastSquare = pieceList[them][capture][--pieceCount[them][capture]];
- index[lastSquare] = index[capsq];
- pieceList[them][capture][index[lastSquare]] = lastSquare;
- pieceList[them][capture][pieceCount[them][capture]] = SQ_NONE;
+ // Update piece lists
+ remove_piece(capsq, them, capture);
// Update material hash key and prefetch access to materialTable
k ^= Zobrist::psq[them][capture][capsq];
board[from] = NO_PIECE;
board[to] = pc;
- // Update piece lists, index[from] is not updated and becomes stale. This
- // works as long as index[] is accessed just by known occupied squares.
- index[to] = index[from];
- pieceList[us][pt][index[to]] = to;
+ move_piece(from, to, us, pt);
}
// If the moving piece is a pawn do some special extra work
byTypeBB[promotion] |= to;
board[to] = make_piece(us, promotion);
- // Update piece lists, move the last pawn at index[to] position
- // and shrink the list. Add a new promotion piece to the list.
- Square lastSquare = pieceList[us][PAWN][--pieceCount[us][PAWN]];
- index[lastSquare] = index[to];
- pieceList[us][PAWN][index[lastSquare]] = lastSquare;
- pieceList[us][PAWN][pieceCount[us][PAWN]] = SQ_NONE;
- index[to] = pieceCount[us][promotion];
- pieceList[us][promotion][index[to]] = to;
+ // Update piece lists and add a new promotion piece to the list
+ remove_piece(to, us, PAWN);
+ add_piece(to, us, promotion);
// Update hash keys
k ^= Zobrist::psq[us][PAWN][to] ^ Zobrist::psq[us][promotion][to];
st->pawnKey ^= Zobrist::psq[us][PAWN][to];
- st->materialKey ^= Zobrist::psq[us][promotion][pieceCount[us][promotion]++]
+ st->materialKey ^= Zobrist::psq[us][promotion][pieceCount[us][promotion]-1]
^ Zobrist::psq[us][PAWN][pieceCount[us][PAWN]];
// Update incremental score
byTypeBB[PAWN] |= to;
board[to] = make_piece(us, PAWN);
- // Update piece lists, move the last promoted piece at index[to] position
- // and shrink the list. Add a new pawn to the list.
- Square lastSquare = pieceList[us][promotion][--pieceCount[us][promotion]];
- index[lastSquare] = index[to];
- pieceList[us][promotion][index[lastSquare]] = lastSquare;
- pieceList[us][promotion][pieceCount[us][promotion]] = SQ_NONE;
- index[to] = pieceCount[us][PAWN]++;
- pieceList[us][PAWN][index[to]] = to;
+ // Update piece lists and add new pawn to the list
+ remove_piece(to, us, promotion);
+ add_piece(to, us, PAWN);
pt = PAWN;
}
board[to] = NO_PIECE;
board[from] = make_piece(us, pt);
- // Update piece lists, index[to] is not updated and becomes stale. This
- // works as long as index[] is accessed just by known occupied squares.
- index[from] = index[to];
- pieceList[us][pt][index[from]] = from;
+ move_piece(to, from, us, pt);
}
if (capture)
board[capsq] = make_piece(them, capture);
// Update piece list, add a new captured piece in capsq square
- index[capsq] = pieceCount[them][capture]++;
- pieceList[them][capture][index[capsq]] = capsq;
+ add_piece(capsq, them, capture);
}
// Finally point our state pointer back to the previous state
PieceType pt = type_of(p);
board[s] = p;
- index[s] = pieceCount[c][pt]++;
- pieceList[c][pt][index[s]] = s;
+ add_piece(s, c, pt);
byTypeBB[ALL_PIECES] |= s;
byTypeBB[pt] |= s;
// Helper functions
void do_castle(Square kfrom, Square kto, Square rfrom, Square rto);
Bitboard hidden_checkers(Square ksq, Color c) const;
+ void remove_piece(Square s, Color c, PieceType pt);
+ void add_piece(Square s, Color c, PieceType pt);
+ void move_piece(Square from, Square to, Color c, PieceType pt);
// Computing hash keys from scratch (for initialization and debugging)
Key compute_key() const;
return thisThread;
}
+inline void Position::add_piece(Square s, Color c, PieceType pt) {
+ index[s] = pieceCount[c][pt]++;
+ pieceList[c][pt][index[s]] = s;
+}
+
+inline void Position::move_piece(Square from, Square to, Color c, PieceType pt) {
+ // index[from] is not updated and becomes stale. This works as long
+ // as index[] is accessed just by known occupied squares.
+ index[to] = index[from];
+ pieceList[c][pt][index[to]] = to;
+}
+
+inline void Position::remove_piece(Square s, Color c, PieceType pt) {
+ // WARNING: This is not a reversible operation. If we remove a piece in
+ // do_move() and then replace it in undo_move() we will put it at the end of
+ // the list and not in its original place, it means index[] and pieceList[]
+ // are not guaranteed to be invariant to a do_move() + undo_move() sequence.
+ Square lastSquare = pieceList[c][pt][--pieceCount[c][pt]];
+ index[lastSquare] = index[s];
+ pieceList[c][pt][index[lastSquare]] = lastSquare;
+ pieceList[c][pt][pieceCount[c][pt]] = SQ_NONE;
+}
+
#endif // #ifndef POSITION_H_INCLUDED