From 875a8079bc142ca92027b07427d72c03fe5268a5 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 29 Jan 2012 09:11:03 +0100 Subject: [PATCH] Replace clear_bit() with xor_bit() This allows to retire ClearMaskBB[] and use just one SquareBB[] array to set and clear a bit. No functional change. Signed-off-by: Marco Costalba --- src/bitboard.cpp | 14 ++++---------- src/bitboard.h | 43 ++++++++++++++++++------------------------- src/evaluate.cpp | 2 +- src/move.cpp | 2 +- src/movegen.cpp | 2 +- src/position.cpp | 44 ++++++++++++++++++++++---------------------- src/search.cpp | 2 +- src/types.h | 1 + 8 files changed, 49 insertions(+), 61 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 21cd34a2..1ac32293 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -25,6 +25,8 @@ #include "bitcount.h" #include "rkiss.h" +CACHE_LINE_ALIGNMENT + Bitboard RMasks[64]; Bitboard RMagics[64]; Bitboard* RAttacks[64]; @@ -35,9 +37,7 @@ Bitboard BMagics[64]; Bitboard* BAttacks[64]; int BShifts[64]; -Bitboard SetMaskBB[65]; -Bitboard ClearMaskBB[65]; - +Bitboard SquareBB[64]; Bitboard FileBB[8]; Bitboard RankBB[8]; Bitboard AdjacentFilesBB[8]; @@ -48,7 +48,6 @@ Bitboard BetweenBB[64][64]; Bitboard SquaresInFrontMask[2][64]; Bitboard PassedPawnMask[2][64]; Bitboard AttackSpanMask[2][64]; - Bitboard PseudoAttacks[6][64]; uint8_t BitCount8Bit[256]; @@ -157,12 +156,7 @@ void bitboards_init() { BitCount8Bit[b] = (uint8_t)popcount(b); for (Square s = SQ_A1; s <= SQ_H8; s++) - { - SetMaskBB[s] = 1ULL << s; - ClearMaskBB[s] = ~SetMaskBB[s]; - } - - ClearMaskBB[SQ_NONE] = ~0ULL; + SquareBB[s] = 1ULL << s; FileBB[FILE_A] = FileABB; RankBB[RANK_1] = Rank1BB; diff --git a/src/bitboard.h b/src/bitboard.h index 94579124..e6abd39f 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -23,50 +23,43 @@ #include "types.h" +extern Bitboard RMasks[64]; +extern Bitboard RMagics[64]; +extern Bitboard* RAttacks[64]; +extern int RShifts[64]; + +extern Bitboard BMasks[64]; +extern Bitboard BMagics[64]; +extern Bitboard* BAttacks[64]; +extern int BShifts[64]; + +extern Bitboard SquareBB[64]; extern Bitboard FileBB[8]; +extern Bitboard RankBB[8]; extern Bitboard AdjacentFilesBB[8]; extern Bitboard ThisAndAdjacentFilesBB[8]; -extern Bitboard RankBB[8]; extern Bitboard InFrontBB[2][8]; - -extern Bitboard SetMaskBB[65]; -extern Bitboard ClearMaskBB[65]; - extern Bitboard StepAttacksBB[16][64]; extern Bitboard BetweenBB[64][64]; - extern Bitboard SquaresInFrontMask[2][64]; extern Bitboard PassedPawnMask[2][64]; extern Bitboard AttackSpanMask[2][64]; - -extern uint64_t RMagics[64]; -extern int RShifts[64]; -extern Bitboard RMasks[64]; -extern Bitboard* RAttacks[64]; - -extern uint64_t BMagics[64]; -extern int BShifts[64]; -extern Bitboard BMasks[64]; -extern Bitboard* BAttacks[64]; - extern Bitboard PseudoAttacks[6][64]; -extern uint8_t BitCount8Bit[256]; - /// Functions for testing whether a given bit is set in a bitboard, and for /// setting and clearing bits. inline Bitboard bit_is_set(Bitboard b, Square s) { - return b & SetMaskBB[s]; + return b & SquareBB[s]; } inline void set_bit(Bitboard* b, Square s) { - *b |= SetMaskBB[s]; + *b |= SquareBB[s]; } -inline void clear_bit(Bitboard* b, Square s) { - *b &= ClearMaskBB[s]; +inline void xor_bit(Bitboard* b, Square s) { + *b ^= SquareBB[s]; } @@ -74,7 +67,7 @@ inline void clear_bit(Bitboard* b, Square s) { /// then calling a sequence of clear_bit() + set_bit() inline Bitboard make_move_bb(Square from, Square to) { - return SetMaskBB[from] | SetMaskBB[to]; + return SquareBB[from] | SquareBB[to]; } inline void do_move_bb(Bitboard* b, Bitboard move_bb) { @@ -216,7 +209,7 @@ inline Bitboard attack_span_mask(Color c, Square s) { inline bool squares_aligned(Square s1, Square s2, Square s3) { return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3]) - & ( SetMaskBB[s1] | SetMaskBB[s2] | SetMaskBB[s3]); + & ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]); } diff --git a/src/evaluate.cpp b/src/evaluate.cpp index ad37455f..4419a64b 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -944,7 +944,7 @@ namespace { // Check if (without even considering any obstacles) we're too far away or doubled if ( pliesToQueen[winnerSide] + 3 <= pliesToGo || (squares_in_front_of(loserSide, s) & pos.pieces(PAWN, loserSide))) - clear_bit(&candidates, s); + xor_bit(&candidates, s); } // If any candidate is already a passed pawn it _may_ promote in time. We give up. diff --git a/src/move.cpp b/src/move.cpp index e52a8e59..4b1285e4 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -98,7 +98,7 @@ const string move_to_san(Position& pos, Move m) { // Disambiguation if we have more then one piece with destination 'to' // note that for pawns is not needed because starting file is explicit. attackers = pos.attackers_to(to) & pos.pieces(pt, pos.side_to_move()); - clear_bit(&attackers, from); + xor_bit(&attackers, from); ambiguousMove = ambiguousFile = ambiguousRank = false; while (attackers) diff --git a/src/movegen.cpp b/src/movegen.cpp index bc5b9cde..441ab70c 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -74,7 +74,7 @@ namespace { if (pos.is_chess960()) { Bitboard occ = pos.occupied_squares(); - clear_bit(&occ, rfrom); + xor_bit(&occ, rfrom); if (pos.attackers_to(kto, occ) & enemies) return mlist; } diff --git a/src/position.cpp b/src/position.cpp index 756977ce..abc0e6f2 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -470,8 +470,8 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const { assert(piece_on(capsq) == make_piece(them, PAWN)); assert(piece_on(to) == NO_PIECE); - clear_bit(&b, from); - clear_bit(&b, capsq); + xor_bit(&b, from); + xor_bit(&b, capsq); set_bit(&b, to); return !(rook_attacks_bb(ksq, b) & pieces(ROOK, QUEEN, them)) @@ -610,7 +610,7 @@ bool Position::is_pseudo_legal(const Move m) const { if (type_of(piece_on(from)) == KING) { Bitboard b = occupied_squares(); - clear_bit(&b, from); + xor_bit(&b, from); if (attackers_to(to_sq(m), b) & pieces(~us)) return false; } @@ -669,7 +669,7 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { // Promotion with check ? if (is_promotion(m)) { - clear_bit(&b, from); + xor_bit(&b, from); return bit_is_set(attacks_from(Piece(promotion_piece_type(m)), to, b), ksq); } @@ -680,8 +680,8 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { if (is_enpassant(m)) { Square capsq = make_square(file_of(to), rank_of(from)); - clear_bit(&b, from); - clear_bit(&b, capsq); + xor_bit(&b, from); + xor_bit(&b, capsq); set_bit(&b, to); return (rook_attacks_bb(ksq, b) & pieces(ROOK, QUEEN, us)) ||(bishop_attacks_bb(ksq, b) & pieces(BISHOP, QUEEN, us)); @@ -702,8 +702,8 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { kto = relative_square(us, SQ_C1); rto = relative_square(us, SQ_D1); } - clear_bit(&b, kfrom); - clear_bit(&b, rfrom); + xor_bit(&b, kfrom); + xor_bit(&b, rfrom); set_bit(&b, rto); set_bit(&b, kto); return bit_is_set(rook_attacks_bb(rto, b), ksq); @@ -801,9 +801,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI st->npMaterial[them] -= PieceValueMidgame[capture]; // Remove the captured piece - clear_bit(&byColorBB[them], capsq); - clear_bit(&byTypeBB[capture], capsq); - clear_bit(&occupied, capsq); + xor_bit(&byColorBB[them], capsq); + xor_bit(&byTypeBB[capture], capsq); + xor_bit(&occupied, capsq); // Update piece list, move the last piece at index[capsq] position and // shrink the list. @@ -883,7 +883,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI assert(promotion >= KNIGHT && promotion <= QUEEN); // Replace the pawn with the promoted piece - clear_bit(&byTypeBB[PAWN], to); + xor_bit(&byTypeBB[PAWN], to); set_bit(&byTypeBB[promotion], to); board[to] = make_piece(us, promotion); @@ -941,7 +941,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI { // Direct checks if (bit_is_set(ci.checkSq[pt], to)) - st->checkersBB = SetMaskBB[to]; + st->checkersBB = SquareBB[to]; // Discovery checks if (ci.dcCandidates && bit_is_set(ci.dcCandidates, from)) @@ -999,7 +999,7 @@ void Position::undo_move(Move m) { assert(promotion >= KNIGHT && promotion <= QUEEN); // Replace the promoted piece with the pawn - clear_bit(&byTypeBB[promotion], to); + xor_bit(&byTypeBB[promotion], to); set_bit(&byTypeBB[PAWN], to); board[to] = make_piece(us, PAWN); @@ -1100,12 +1100,12 @@ void Position::do_castle_move(Move m) { 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); + xor_bit(&byColorBB[us], kfrom); + xor_bit(&byTypeBB[KING], kfrom); + xor_bit(&occupied, kfrom); + xor_bit(&byColorBB[us], rfrom); + xor_bit(&byTypeBB[ROOK], rfrom); + xor_bit(&occupied, rfrom); // Put pieces on destination squares set_bit(&byColorBB[us], kto); @@ -1268,13 +1268,13 @@ int Position::see(Move m) const { assert(type_of(piece_on(capQq)) == PAWN); // Remove the captured pawn - clear_bit(&occ, capQq); + xor_bit(&occ, capQq); capturedType = PAWN; } // Find all attackers to the destination square, with the moving piece // removed, but possibly an X-ray attacker added behind it. - clear_bit(&occ, from); + xor_bit(&occ, from); attackers = attackers_to(to, occ); // If the opponent has no attackers we are finished diff --git a/src/search.cpp b/src/search.cpp index 36bd0869..fa12ee71 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1434,7 +1434,7 @@ split_point_start: // At split points actual search starts from here && bit_is_set(squares_between(t1, ksq), f2)) { Bitboard occ = pos.occupied_squares(); - clear_bit(&occ, f2); + xor_bit(&occ, f2); if (bit_is_set(pos.attacks_from(p1, t1, occ), ksq)) return true; } diff --git a/src/types.h b/src/types.h index eaa9a25b..85fddc27 100644 --- a/src/types.h +++ b/src/types.h @@ -333,6 +333,7 @@ const Value QueenValueEndgame = Value(0x9FE); extern const Value PieceValueMidgame[17]; // Indexed by Piece or PieceType extern const Value PieceValueEndgame[17]; extern int SquareDistance[64][64]; +extern uint8_t BitCount8Bit[256]; inline Color operator~(Color c) { return Color(c ^ 1); -- 2.39.2