- 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
- clear_bit(&byColorBB[us], kfrom);
- clear_bit(&byTypeBB[KING], kfrom);
- clear_bit(&occupied, kfrom);
- clear_bit(&byColorBB[us], rfrom);
- clear_bit(&byTypeBB[ROOK], rfrom);
- clear_bit(&occupied, rfrom);
-
- // Put pieces on destination squares
- set_bit(&byColorBB[us], kto);
- set_bit(&byTypeBB[KING], kto);
- set_bit(&occupied, kto);
- set_bit(&byColorBB[us], rto);
- set_bit(&byTypeBB[ROOK], rto);
- set_bit(&occupied, 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