From a44c5cf4f77b05a0385c127b7c26cf086a73120e Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 3 Dec 2011 11:56:11 +0100 Subject: [PATCH] Prefer 0 to EmptyBoardBB Easier and even faster or at least easier to optimize. No functional change. Signed-off-by: Marco Costalba --- src/bitbase.cpp | 2 +- src/bitboard.cpp | 10 +++++----- src/bitboard.h | 20 -------------------- src/endgame.cpp | 16 ++++++++-------- src/evaluate.cpp | 6 +++--- src/movegen.cpp | 4 ++-- src/pawns.cpp | 2 +- src/position.cpp | 6 +++--- src/types.h | 18 ++++++++++++++++++ 9 files changed, 41 insertions(+), 43 deletions(-) diff --git a/src/bitbase.cpp b/src/bitbase.cpp index 96ada8dd..5e31cc5c 100644 --- a/src/bitbase.cpp +++ b/src/bitbase.cpp @@ -151,7 +151,7 @@ namespace { // // Case 1: Stalemate if ( sideToMove == BLACK - && (bk_attacks() & ~(wk_attacks() | pawn_attacks())) == EmptyBoardBB) + && !(bk_attacks() & ~(wk_attacks() | pawn_attacks()))) return RESULT_DRAW; // Case 2: King can capture pawn diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 794f54cf..0e035b15 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -166,7 +166,7 @@ void bitboards_init() { ClearMaskBB[s] = ~SetMaskBB[s]; } - ClearMaskBB[SQ_NONE] = ~EmptyBoardBB; + ClearMaskBB[SQ_NONE] = ~0ULL; FileBB[FILE_A] = FileABB; RankBB[RANK_1] = Rank1BB; @@ -231,9 +231,9 @@ void bitboards_init() { for (Square s = SQ_A1; s <= SQ_H8; s++) { - BishopPseudoAttacks[s] = bishop_attacks_bb(s, EmptyBoardBB); - RookPseudoAttacks[s] = rook_attacks_bb(s, EmptyBoardBB); - QueenPseudoAttacks[s] = queen_attacks_bb(s, EmptyBoardBB); + BishopPseudoAttacks[s] = bishop_attacks_bb(s, 0); + RookPseudoAttacks[s] = rook_attacks_bb(s, 0); + QueenPseudoAttacks[s] = queen_attacks_bb(s, 0); } for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++) @@ -324,7 +324,7 @@ namespace { // all the attacks for each possible subset of the mask and so is 2 power // the number of 1s of the mask. Hence we deduce the size of the shift to // apply to the 64 or 32 bits word to get the index. - masks[s] = sliding_attacks(pt, s, EmptyBoardBB) & ~edges; + masks[s] = sliding_attacks(pt, s, 0) & ~edges; shifts[s] = (CpuIs64Bit ? 64 : 32) - count_1s(masks[s]); // Use Carry-Rippler trick to enumerate all subsets of masks[s] and diff --git a/src/bitboard.h b/src/bitboard.h index 1c8a8c22..83668278 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -23,26 +23,6 @@ #include "types.h" -const Bitboard EmptyBoardBB = 0; - -const Bitboard FileABB = 0x0101010101010101ULL; -const Bitboard FileBBB = FileABB << 1; -const Bitboard FileCBB = FileABB << 2; -const Bitboard FileDBB = FileABB << 3; -const Bitboard FileEBB = FileABB << 4; -const Bitboard FileFBB = FileABB << 5; -const Bitboard FileGBB = FileABB << 6; -const Bitboard FileHBB = FileABB << 7; - -const Bitboard Rank1BB = 0xFF; -const Bitboard Rank2BB = Rank1BB << (8 * 1); -const Bitboard Rank3BB = Rank1BB << (8 * 2); -const Bitboard Rank4BB = Rank1BB << (8 * 3); -const Bitboard Rank5BB = Rank1BB << (8 * 4); -const Bitboard Rank6BB = Rank1BB << (8 * 5); -const Bitboard Rank7BB = Rank1BB << (8 * 6); -const Bitboard Rank8BB = Rank1BB << (8 * 7); - extern Bitboard SquaresByColorBB[2]; extern Bitboard FileBB[8]; extern Bitboard NeighboringFilesBB[8]; diff --git a/src/endgame.cpp b/src/endgame.cpp index 4e6de809..2ebc8f97 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -376,7 +376,7 @@ Value Endgame::apply(const Position& pos) const { assert(pos.non_pawn_material(strongerSide) == 2*BishopValueMidgame); assert(pos.piece_count(weakerSide, KNIGHT) == 1); assert(pos.non_pawn_material(weakerSide) == KnightValueMidgame); - assert(pos.pieces(PAWN) == EmptyBoardBB); + assert(!pos.pieces(PAWN)); Value result = BishopValueEndgame; Square wksq = pos.king_square(strongerSide); @@ -428,7 +428,7 @@ ScaleFactor Endgame::apply(const Position& pos) const { // All pawns are on a single rook file ? if ( (pawnFile == FILE_A || pawnFile == FILE_H) - && (pawns & ~file_bb(pawnFile)) == EmptyBoardBB) + && !(pawns & ~file_bb(pawnFile))) { Square bishopSq = pos.piece_list(strongerSide, BISHOP)[0]; Square queeningSq = relative_square(strongerSide, make_square(pawnFile, RANK_8)); @@ -443,12 +443,12 @@ ScaleFactor Endgame::apply(const Position& pos) const { Rank rank; if (strongerSide == WHITE) { - for (rank = RANK_7; (rank_bb(rank) & pawns) == EmptyBoardBB; rank--) {} + for (rank = RANK_7; !(rank_bb(rank) & pawns); rank--) {} assert(rank >= RANK_2 && rank <= RANK_7); } else { - for (rank = RANK_2; (rank_bb(rank) & pawns) == EmptyBoardBB; rank++) {} + for (rank = RANK_2; !(rank_bb(rank) & pawns); rank++) {} rank = Rank(rank ^ 7); // HACK to get the relative rank assert(rank >= RANK_2 && rank <= RANK_7); } @@ -667,21 +667,21 @@ ScaleFactor Endgame::apply(const Position& pos) const { Bitboard pawns = pos.pieces(PAWN, strongerSide); // Are all pawns on the 'a' file? - if ((pawns & ~FileABB) == EmptyBoardBB) + if (!(pawns & ~FileABB)) { // Does the defending king block the pawns? if ( square_distance(ksq, relative_square(strongerSide, SQ_A8)) <= 1 || ( file_of(ksq) == FILE_A - && (in_front_bb(strongerSide, ksq) & pawns) == EmptyBoardBB)) + && !in_front_bb(strongerSide, ksq) & pawns)) return SCALE_FACTOR_ZERO; } // Are all pawns on the 'h' file? - else if ((pawns & ~FileHBB) == EmptyBoardBB) + else if (!(pawns & ~FileHBB)) { // Does the defending king block the pawns? if ( square_distance(ksq, relative_square(strongerSide, SQ_H8)) <= 1 || ( file_of(ksq) == FILE_H - && (in_front_bb(strongerSide, ksq) & pawns) == EmptyBoardBB)) + && !in_front_bb(strongerSide, ksq) & pawns)) return SCALE_FACTOR_ZERO; } return SCALE_FACTOR_NONE; diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 6b15f6c7..6d95fd58 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -462,8 +462,8 @@ namespace { // no minor piece which can exchange the outpost piece. if (bonus && bit_is_set(ei.attackedBy[Us][PAWN], s)) { - if ( pos.pieces(KNIGHT, Them) == EmptyBoardBB - && (SquaresByColorBB[color_of(s)] & pos.pieces(BISHOP, Them)) == EmptyBoardBB) + if ( !pos.pieces(KNIGHT, Them) + && !(SquaresByColorBB[color_of(s)] & pos.pieces(BISHOP, Them))) bonus += bonus + bonus / 2; else bonus += bonus / 2; @@ -488,7 +488,7 @@ namespace { const Color Them = (Us == WHITE ? BLACK : WHITE); const Square* pl = pos.piece_list(Us, Piece); - ei.attackedBy[Us][Piece] = EmptyBoardBB; + ei.attackedBy[Us][Piece] = 0; while ((s = *pl++) != SQ_NONE) { diff --git a/src/movegen.cpp b/src/movegen.cpp index dd849c15..ba1cc334 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -251,7 +251,7 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) { Color us = pos.side_to_move(); Square ksq = pos.king_square(us); Bitboard checkers = pos.checkers(); - Bitboard sliderAttacks = EmptyBoardBB; + Bitboard sliderAttacks = 0; assert(pos.piece_on(ksq) == make_piece(us, KING)); assert(checkers); @@ -485,7 +485,7 @@ namespace { b1 = pawns & pos.attacks_from(pos.ep_square(), Them); - assert(b1 != EmptyBoardBB); + assert(b1); while (b1) { diff --git a/src/pawns.cpp b/src/pawns.cpp index d5514e2e..3aede5ad 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -182,7 +182,7 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns, // pawn on neighboring files is higher or equal than the number of // enemy pawns in the forward direction on the neighboring files. candidate = !(opposed | passed | backward | isolated) - && (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != EmptyBoardBB + && (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0 && count_1s(b) >= count_1s(attack_span_mask(Us, s) & theirPawns); // Passed pawns will be properly scored in evaluation because we need diff --git a/src/position.cpp b/src/position.cpp index 2f18427d..bed08eba 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -89,7 +89,7 @@ CheckInfo::CheckInfo(const Position& pos) { checkSq[BISHOP] = pos.attacks_from(ksq); checkSq[ROOK] = pos.attacks_from(ksq); checkSq[QUEEN] = checkSq[BISHOP] | checkSq[ROOK]; - checkSq[KING] = EmptyBoardBB; + checkSq[KING] = 0; } @@ -931,7 +931,7 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI st->key = key; // Update checkers bitboard, piece must be already moved - st->checkersBB = EmptyBoardBB; + st->checkersBB = 0; if (moveIsCheck) { @@ -1697,7 +1697,7 @@ bool Position::pos_is_ok(int* failedStep) const { if (debugBitboards) { // The intersection of the white and black pieces must be empty - if ((pieces(WHITE) & pieces(BLACK)) != EmptyBoardBB) + if (!(pieces(WHITE) & pieces(BLACK))) return false; // The union of the white and black pieces must be equal to all diff --git a/src/types.h b/src/types.h index 841c794a..7040b5ad 100644 --- a/src/types.h +++ b/src/types.h @@ -158,6 +158,24 @@ typedef uint64_t Bitboard; const int PLY_MAX = 100; const int PLY_MAX_PLUS_2 = PLY_MAX + 2; +const Bitboard FileABB = 0x0101010101010101ULL; +const Bitboard FileBBB = FileABB << 1; +const Bitboard FileCBB = FileABB << 2; +const Bitboard FileDBB = FileABB << 3; +const Bitboard FileEBB = FileABB << 4; +const Bitboard FileFBB = FileABB << 5; +const Bitboard FileGBB = FileABB << 6; +const Bitboard FileHBB = FileABB << 7; + +const Bitboard Rank1BB = 0xFF; +const Bitboard Rank2BB = Rank1BB << (8 * 1); +const Bitboard Rank3BB = Rank1BB << (8 * 2); +const Bitboard Rank4BB = Rank1BB << (8 * 3); +const Bitboard Rank5BB = Rank1BB << (8 * 4); +const Bitboard Rank6BB = Rank1BB << (8 * 5); +const Bitboard Rank7BB = Rank1BB << (8 * 6); +const Bitboard Rank8BB = Rank1BB << (8 * 7); + enum ValueType { VALUE_TYPE_NONE = 0, VALUE_TYPE_UPPER = 1, -- 2.39.2