- Square kBefore = from_sq(m);
- Square rBefore = to_sq(m);
-
- // Find after-castle squares for king and rook
- if (rBefore > kBefore) // O-O
- {
- kAfter = relative_square(us, SQ_G1);
- rAfter = relative_square(us, SQ_F1);
- }
- else // O-O-O
- {
- kAfter = relative_square(us, SQ_C1);
- rAfter = relative_square(us, SQ_D1);
- }
-
- kfrom = Do ? kBefore : kAfter;
- rfrom = Do ? rBefore : rAfter;
-
- kto = Do ? kAfter : kBefore;
- rto = Do ? rAfter : rBefore;
-
- assert(piece_on(kfrom) == make_piece(us, KING));
- assert(piece_on(rfrom) == make_piece(us, ROOK));
-
- // Remove pieces from source squares
- byTypeBB[ALL_PIECES] ^= kfrom;
- byTypeBB[KING] ^= kfrom;
- byColorBB[us] ^= kfrom;
- byTypeBB[ALL_PIECES] ^= rfrom;
- byTypeBB[ROOK] ^= rfrom;
- byColorBB[us] ^= rfrom;
-
- // Put pieces on destination squares
- byTypeBB[ALL_PIECES] |= kto;
- byTypeBB[KING] |= kto;
- byColorBB[us] |= kto;
- byTypeBB[ALL_PIECES] |= rto;
- byTypeBB[ROOK] |= rto;
- byColorBB[us] |= rto;
-
- // Update board
- Piece king = make_piece(us, KING);
- Piece rook = make_piece(us, ROOK);
+ Bitboard k_from_to_bb = SquareBB[kfrom] ^ SquareBB[kto];
+ Bitboard r_from_to_bb = SquareBB[rfrom] ^ SquareBB[rto];
+ byTypeBB[KING] ^= k_from_to_bb;
+ byTypeBB[ROOK] ^= r_from_to_bb;
+ byTypeBB[ALL_PIECES] ^= k_from_to_bb ^ r_from_to_bb;
+ byColorBB[us] ^= k_from_to_bb ^ r_from_to_bb;
+
+ // Could be from == to, so first set NO_PIECE then KING and ROOK