1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 09:13:08 +00:00

Micro-optimize castling handling in do_move()

And better self-document the code.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2011-06-11 12:41:20 +01:00
parent 735cac5d53
commit b21a5e2f06
4 changed files with 35 additions and 36 deletions

View file

@ -59,7 +59,7 @@ const string move_to_uci(Move m, bool chess960) {
return from == SQ_E1 ? "e1c1" : "e8c8"; return from == SQ_E1 ? "e1c1" : "e8c8";
if (move_is_promotion(m)) if (move_is_promotion(m))
promotion = char(tolower(piece_type_to_char(move_promotion_piece(m)))); promotion = char(tolower(piece_type_to_char(promotion_piece_type(m))));
return square_to_string(from) + square_to_string(to) + promotion; return square_to_string(from) + square_to_string(to) + promotion;
} }
@ -157,7 +157,7 @@ const string move_to_san(Position& pos, Move m) {
if (move_is_promotion(m)) if (move_is_promotion(m))
{ {
san += '='; san += '=';
san += piece_type_to_char(move_promotion_piece(m)); san += piece_type_to_char(promotion_piece_type(m));
} }
} }

View file

@ -161,7 +161,7 @@ inline bool move_is_long_castle(Move m) {
return move_is_castle(m) && (move_to(m) < move_from(m)); return move_is_castle(m) && (move_to(m) < move_from(m));
} }
inline PieceType move_promotion_piece(Move m) { inline PieceType promotion_piece_type(Move m) {
return PieceType(((m >> 12) & 3) + 2); return PieceType(((m >> 12) & 3) + 2);
} }

View file

@ -270,7 +270,7 @@ bool Position::set_castling_rights(char token) {
for (Square sq = sqH; sq >= sqA; sq--) for (Square sq = sqH; sq >= sqA; sq--)
if (piece_on(sq) == rook) if (piece_on(sq) == rook)
{ {
do_allow_oo(c); set_castle_kingside(c);
initialKRFile = square_file(sq); initialKRFile = square_file(sq);
break; break;
} }
@ -280,7 +280,7 @@ bool Position::set_castling_rights(char token) {
for (Square sq = sqA; sq <= sqH; sq++) for (Square sq = sqA; sq <= sqH; sq++)
if (piece_on(sq) == rook) if (piece_on(sq) == rook)
{ {
do_allow_ooo(c); set_castle_queenside(c);
initialQRFile = square_file(sq); initialQRFile = square_file(sq);
break; break;
} }
@ -290,12 +290,12 @@ bool Position::set_castling_rights(char token) {
File rookFile = File(token - 'A') + FILE_A; File rookFile = File(token - 'A') + FILE_A;
if (rookFile < initialKFile) if (rookFile < initialKFile)
{ {
do_allow_ooo(c); set_castle_queenside(c);
initialQRFile = rookFile; initialQRFile = rookFile;
} }
else else
{ {
do_allow_oo(c); set_castle_kingside(c);
initialKRFile = rookFile; initialKRFile = rookFile;
} }
} }
@ -637,7 +637,7 @@ bool Position::move_is_pl(const Move m) const {
return move_is_pl_slow(m); return move_is_pl_slow(m);
// Is not a promotion, so promotion piece must be empty // Is not a promotion, so promotion piece must be empty
if (move_promotion_piece(m) - 2 != PIECE_TYPE_NONE) if (promotion_piece_type(m) - 2 != PIECE_TYPE_NONE)
return false; return false;
// If the from square is not occupied by a piece belonging to the side to // If the from square is not occupied by a piece belonging to the side to
@ -788,7 +788,7 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const {
{ {
clear_bit(&b, from); clear_bit(&b, from);
switch (move_promotion_piece(m)) switch (promotion_piece_type(m))
{ {
case KNIGHT: case KNIGHT:
return bit_is_set(attacks_from<KNIGHT>(to), ci.ksq); return bit_is_set(attacks_from<KNIGHT>(to), ci.ksq);
@ -949,13 +949,12 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
st->epSquare = SQ_NONE; st->epSquare = SQ_NONE;
} }
// Update castle rights, try to shortcut a common case // Update castle rights if needed
int cm = castleRightsMask[from] & castleRightsMask[to]; if ( st->castleRights != CASTLES_NONE
if (cm != ALL_CASTLES && ((cm & st->castleRights) != st->castleRights)) && (castleRightsMask[from] & castleRightsMask[to]) != ALL_CASTLES)
{ {
key ^= zobCastle[st->castleRights]; key ^= zobCastle[st->castleRights];
st->castleRights &= castleRightsMask[from]; st->castleRights &= castleRightsMask[from] & castleRightsMask[to];
st->castleRights &= castleRightsMask[to];
key ^= zobCastle[st->castleRights]; key ^= zobCastle[st->castleRights];
} }
@ -998,7 +997,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI
if (pm) // promotion ? if (pm) // promotion ?
{ {
PieceType promotion = move_promotion_piece(m); PieceType promotion = promotion_piece_type(m);
assert(promotion >= KNIGHT && promotion <= QUEEN); assert(promotion >= KNIGHT && promotion <= QUEEN);
@ -1278,7 +1277,7 @@ void Position::undo_move(Move m) {
if (pm) // promotion ? if (pm) // promotion ?
{ {
PieceType promotion = move_promotion_piece(m); PieceType promotion = promotion_piece_type(m);
pt = PAWN; pt = PAWN;
assert(promotion >= KNIGHT && promotion <= QUEEN); assert(promotion >= KNIGHT && promotion <= QUEEN);
@ -1851,10 +1850,10 @@ void Position::flip() {
sideToMove = opposite_color(pos.side_to_move()); sideToMove = opposite_color(pos.side_to_move());
// Castling rights // Castling rights
if (pos.can_castle_kingside(WHITE)) do_allow_oo(BLACK); if (pos.can_castle_kingside(WHITE)) set_castle_kingside(BLACK);
if (pos.can_castle_queenside(WHITE)) do_allow_ooo(BLACK); if (pos.can_castle_queenside(WHITE)) set_castle_queenside(BLACK);
if (pos.can_castle_kingside(BLACK)) do_allow_oo(WHITE); if (pos.can_castle_kingside(BLACK)) set_castle_kingside(WHITE);
if (pos.can_castle_queenside(BLACK)) do_allow_ooo(WHITE); if (pos.can_castle_queenside(BLACK)) set_castle_queenside(WHITE);
initialKFile = pos.initialKFile; initialKFile = pos.initialKFile;
initialKRFile = pos.initialKRFile; initialKRFile = pos.initialKRFile;

View file

@ -257,8 +257,8 @@ private:
void clear(); void clear();
void detach(); void detach();
void put_piece(Piece p, Square s); void put_piece(Piece p, Square s);
void do_allow_oo(Color c); void set_castle_kingside(Color c);
void do_allow_ooo(Color c); void set_castle_queenside(Color c);
bool set_castling_rights(char token); bool set_castling_rights(char token);
bool move_is_pl_slow(const Move m) const; bool move_is_pl_slow(const Move m) const;
@ -406,16 +406,24 @@ inline Square Position::king_square(Color c) const {
return pieceList[c][KING][0]; return pieceList[c][KING][0];
} }
inline bool Position::can_castle_kingside(Color side) const { inline bool Position::can_castle_kingside(Color c) const {
return st->castleRights & (1+int(side)); return st->castleRights & (WHITE_OO << c);
} }
inline bool Position::can_castle_queenside(Color side) const { inline bool Position::can_castle_queenside(Color c) const {
return st->castleRights & (4+4*int(side)); return st->castleRights & (WHITE_OOO << c);
} }
inline bool Position::can_castle(Color side) const { inline bool Position::can_castle(Color c) const {
return can_castle_kingside(side) || can_castle_queenside(side); return st->castleRights & ((WHITE_OO | WHITE_OOO) << c);
}
inline void Position::set_castle_kingside(Color c) {
st->castleRights |= (WHITE_OO << c);
}
inline void Position::set_castle_queenside(Color c) {
st->castleRights |= (WHITE_OOO << c);
} }
inline Square Position::initial_kr_square(Color c) const { inline Square Position::initial_kr_square(Color c) const {
@ -542,12 +550,4 @@ inline int Position::thread() const {
return threadID; return threadID;
} }
inline void Position::do_allow_oo(Color c) {
st->castleRights |= (1 + int(c));
}
inline void Position::do_allow_ooo(Color c) {
st->castleRights |= (4 + 4*int(c));
}
#endif // !defined(POSITION_H_INCLUDED) #endif // !defined(POSITION_H_INCLUDED)