From a492a9dd079d95faf136a744ff6d47a3d109ad68 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 29 Jan 2012 09:54:17 +0100 Subject: [PATCH] Bitwise operator overloads between Bitboard and Square Yes, we try to be fancy here ;-) No functional change. Signed-off-by: Marco Costalba --- src/bitbase.cpp | 8 ++-- src/bitboard.cpp | 12 ++--- src/bitboard.h | 30 ++++-------- src/evaluate.cpp | 6 +-- src/move.cpp | 2 +- src/movegen.cpp | 10 ++-- src/pawns.cpp | 2 +- src/position.cpp | 119 ++++++++++++++++++++++++----------------------- src/search.cpp | 21 ++++----- 9 files changed, 98 insertions(+), 112 deletions(-) diff --git a/src/bitbase.cpp b/src/bitbase.cpp index 402f3fbb..c06ca521 100644 --- a/src/bitbase.cpp +++ b/src/bitbase.cpp @@ -134,8 +134,8 @@ namespace { return RESULT_INVALID; // Check if a king can be captured - if ( bit_is_set(wk_attacks(), blackKingSquare) - || (bit_is_set(pawn_attacks(), blackKingSquare) && sideToMove == WHITE)) + if ( (wk_attacks() & blackKingSquare) + || ((pawn_attacks() & blackKingSquare) && sideToMove == WHITE)) return RESULT_INVALID; // The position is an immediate win if it is white to move and the @@ -144,7 +144,7 @@ namespace { && sideToMove == WHITE && whiteKingSquare != pawnSquare + DELTA_N && ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1 - || bit_is_set(wk_attacks(), pawnSquare + DELTA_N))) + || (wk_attacks() & (pawnSquare + DELTA_N)))) return RESULT_WIN; // Check for known draw positions @@ -156,7 +156,7 @@ namespace { // Case 2: King can capture pawn if ( sideToMove == BLACK - && bit_is_set(bk_attacks(), pawnSquare) && !bit_is_set(wk_attacks(), pawnSquare)) + && (bk_attacks() & pawnSquare) && !(wk_attacks() & pawnSquare)) return RESULT_DRAW; // Case 3: Black king in front of white pawn diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 1ac32293..a5b80b0b 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -77,7 +77,7 @@ void print_bitboard(Bitboard b) { { std::cout << "+---+---+---+---+---+---+---+---+" << '\n'; for (File f = FILE_A; f <= FILE_H; f++) - std::cout << "| " << (bit_is_set(b, make_square(f, r)) ? "X " : " "); + std::cout << "| " << ((b & make_square(f, r)) ? "X " : " "); std::cout << "|\n"; } @@ -213,7 +213,7 @@ void bitboards_init() { Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]); if (square_is_ok(to) && square_distance(s, to) < 3) - set_bit(&StepAttacksBB[make_piece(c, pt)][s], to); + StepAttacksBB[make_piece(c, pt)][s] |= to; } Square RDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W }; @@ -231,12 +231,12 @@ void bitboards_init() { for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++) for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++) - if (bit_is_set(PseudoAttacks[QUEEN][s1], s2)) + if (PseudoAttacks[QUEEN][s1] & s2) { Square delta = (s2 - s1) / square_distance(s1, s2); for (Square s = s1 + delta; s != s2; s += delta) - set_bit(&BetweenBB[s1][s2], s); + BetweenBB[s1][s2] |= s; } } @@ -252,9 +252,9 @@ namespace { square_is_ok(s) && square_distance(s, s - deltas[i]) == 1; s += deltas[i]) { - set_bit(&attack, s); + attack |= s; - if (bit_is_set(occupied, s)) + if (occupied & s) break; } diff --git a/src/bitboard.h b/src/bitboard.h index e6abd39f..c80d549f 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -47,31 +47,19 @@ extern Bitboard AttackSpanMask[2][64]; extern Bitboard PseudoAttacks[6][64]; -/// Functions for testing whether a given bit is set in a bitboard, and for -/// setting and clearing bits. +/// Overloads of bitwise operators between a Bitboard and a Square 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) { +inline Bitboard operator&(Bitboard b, Square s) { return b & SquareBB[s]; } -inline void set_bit(Bitboard* b, Square s) { - *b |= SquareBB[s]; +inline Bitboard& operator|=(Bitboard& b, Square s) { + return b |= SquareBB[s], b; } -inline void xor_bit(Bitboard* b, Square s) { - *b ^= SquareBB[s]; -} - - -/// Functions used to update a bitboard after a move. This is faster -/// then calling a sequence of clear_bit() + set_bit() - -inline Bitboard make_move_bb(Square from, Square to) { - return SquareBB[from] | SquareBB[to]; -} - -inline void do_move_bb(Bitboard* b, Bitboard move_bb) { - *b ^= move_bb; +inline Bitboard& operator^=(Bitboard& b, Square s) { + return b ^= SquareBB[s], b; } @@ -217,8 +205,8 @@ inline bool squares_aligned(Square s1, Square s2, Square s3) { /// the same color of the given square. inline Bitboard same_color_squares(Square s) { - return bit_is_set(0xAA55AA55AA55AA55ULL, s) ? 0xAA55AA55AA55AA55ULL - : ~0xAA55AA55AA55AA55ULL; + return Bitboard(0xAA55AA55AA55AA55ULL) & s ? 0xAA55AA55AA55AA55ULL + : ~0xAA55AA55AA55AA55ULL; } diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 4419a64b..745d1a30 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -453,7 +453,7 @@ namespace { // Increase bonus if supported by pawn, especially if the opponent has // no minor piece which can exchange the outpost piece. - if (bonus && bit_is_set(ei.attackedBy[Us][PAWN], s)) + if (bonus && (ei.attackedBy[Us][PAWN] & s)) { if ( !pos.pieces(KNIGHT, Them) && !(same_color_squares(s) & pos.pieces(BISHOP, Them))) @@ -514,7 +514,7 @@ namespace { // Decrease score if we are attacked by an enemy pawn. Remaining part // of threat evaluation must be done later when we have full attack info. - if (bit_is_set(ei.attackedBy[Them][PAWN], s)) + if (ei.attackedBy[Them][PAWN] & s) score -= ThreatenedByPawnPenalty[Piece]; // Bishop and knight outposts squares @@ -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))) - xor_bit(&candidates, s); + 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 4b1285e4..a60506fd 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()); - xor_bit(&attackers, from); + attackers ^= from; ambiguousMove = ambiguousFile = ambiguousRank = false; while (attackers) diff --git a/src/movegen.cpp b/src/movegen.cpp index 441ab70c..e2cc7b99 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -74,7 +74,7 @@ namespace { if (pos.is_chess960()) { Bitboard occ = pos.occupied_squares(); - xor_bit(&occ, rfrom); + occ ^= rfrom; if (pos.attackers_to(kto, occ) & enemies) return mlist; } @@ -121,7 +121,7 @@ namespace { // Knight-promotion is the only one that can give a direct check not // already included in the queen-promotion. - if (Type == MV_QUIET_CHECK && bit_is_set(StepAttacksBB[W_KNIGHT][to], ksq)) + if (Type == MV_QUIET_CHECK && (StepAttacksBB[W_KNIGHT][to] & ksq)) (*mlist++).move = make_promotion(to - Delta, to, KNIGHT); else (void)ksq; // Silence a warning under MSVC @@ -219,7 +219,7 @@ namespace { // An en passant capture can be an evasion only if the checking piece // is the double pushed pawn and so is in the target. Otherwise this // is a discovery check and we are forced to do otherwise. - if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - UP)) + if (Type == MV_EVASION && !(target & (pos.ep_square() - UP))) return mlist; b1 = pawnsNotOn7 & pos.attacks_from(pos.ep_square(), Them); @@ -255,7 +255,7 @@ namespace { && !(PseudoAttacks[Pt][from] & target)) continue; - if (ci.dcCandidates && bit_is_set(ci.dcCandidates, from)) + if (ci.dcCandidates && (ci.dcCandidates & from)) continue; b = pos.attacks_from(from) & target; @@ -430,7 +430,7 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) { // If queen and king are far or not on a diagonal line we can safely // remove all the squares attacked in the other direction becuase are // not reachable by the king anyway. - if (squares_between(ksq, checksq) || !bit_is_set(PseudoAttacks[BISHOP][checksq], ksq)) + if (squares_between(ksq, checksq) || !(PseudoAttacks[BISHOP][checksq] & ksq)) sliderAttacks |= PseudoAttacks[QUEEN][checksq]; // Otherwise we need to use real rook attacks to check if king is safe diff --git a/src/pawns.cpp b/src/pawns.cpp index d000209f..950994ef 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -188,7 +188,7 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns, // full attack info to evaluate passed pawns. Only the frontmost passed // pawn on each file is considered a true passed pawn. if (passed && !doubled) - set_bit(&(pi->passedPawns[Us]), s); + pi->passedPawns[Us] |= s; // Score this pawn if (isolated) diff --git a/src/position.cpp b/src/position.cpp index abc0e6f2..5ee0107f 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -426,10 +426,11 @@ bool Position::move_attacks_square(Move m, Square s) const { // Update occupancy as if the piece is moving occ = occupied_squares(); - do_move_bb(&occ, make_move_bb(from, to)); + occ ^= from; + occ ^= to; // The piece moved in 'to' attacks the square 's' ? - if (bit_is_set(attacks_from(piece, to, occ), s)) + if (attacks_from(piece, to, occ) & s) return true; // Scan for possible X-ray attackers behind the moved piece @@ -470,9 +471,9 @@ 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); - xor_bit(&b, from); - xor_bit(&b, capsq); - set_bit(&b, to); + b ^= from; + b ^= capsq; + b |= to; return !(rook_attacks_bb(ksq, b) & pieces(ROOK, QUEEN, them)) && !(bishop_attacks_bb(ksq, b) & pieces(BISHOP, QUEEN, them)); @@ -487,7 +488,7 @@ bool Position::pl_move_is_legal(Move m, Bitboard pinned) const { // A non-king move is legal if and only if it is not pinned or it // is moving along the ray towards or away from the king. return !pinned - || !bit_is_set(pinned, from) + || !(pinned & from) || squares_aligned(from, to_sq(m), king_square(us)); } @@ -597,7 +598,7 @@ bool Position::is_pseudo_legal(const Move m) const { return false; } } - else if (!bit_is_set(attacks_from(pc, from), to)) + else if (!(attacks_from(pc, from) & to)) return false; // Evasions generator already takes care to avoid some kind of illegal moves @@ -610,7 +611,7 @@ bool Position::is_pseudo_legal(const Move m) const { if (type_of(piece_on(from)) == KING) { Bitboard b = occupied_squares(); - xor_bit(&b, from); + b ^= from; if (attackers_to(to_sq(m), b) & pieces(~us)) return false; } @@ -624,7 +625,7 @@ bool Position::is_pseudo_legal(const Move m) const { // Our move must be a blocking evasion or a capture of the checking piece target = squares_between(checksq, king_square(us)) | checkers(); - if (!bit_is_set(target, to_sq(m))) + if (!(target & to_sq(m))) return false; } } @@ -646,11 +647,11 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { PieceType pt = type_of(piece_on(from)); // Direct check ? - if (bit_is_set(ci.checkSq[pt], to)) + if (ci.checkSq[pt] & to) return true; // Discovery check ? - if (ci.dcCandidates && bit_is_set(ci.dcCandidates, from)) + if (ci.dcCandidates && (ci.dcCandidates & from)) { // For pawn and king moves we need to verify also direction if ( (pt != PAWN && pt != KING) @@ -669,8 +670,8 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { // Promotion with check ? if (is_promotion(m)) { - xor_bit(&b, from); - return bit_is_set(attacks_from(Piece(promotion_piece_type(m)), to, b), ksq); + b ^= from; + return attacks_from(Piece(promotion_piece_type(m)), to, b) & ksq; } // En passant capture with check ? We have already handled the case @@ -680,9 +681,9 @@ 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)); - xor_bit(&b, from); - xor_bit(&b, capsq); - set_bit(&b, to); + b ^= from; + b ^= capsq; + b |= to; return (rook_attacks_bb(ksq, b) & pieces(ROOK, QUEEN, us)) ||(bishop_attacks_bb(ksq, b) & pieces(BISHOP, QUEEN, us)); } @@ -702,11 +703,11 @@ bool Position::move_gives_check(Move m, const CheckInfo& ci) const { kto = relative_square(us, SQ_C1); rto = relative_square(us, SQ_D1); } - 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); + b ^= kfrom; + b ^= rfrom; + b |= rto; + b |= kto; + return rook_attacks_bb(rto, b) & ksq; } return false; @@ -801,9 +802,9 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI st->npMaterial[them] -= PieceValueMidgame[capture]; // Remove the captured piece - xor_bit(&byColorBB[them], capsq); - xor_bit(&byTypeBB[capture], capsq); - xor_bit(&occupied, capsq); + byColorBB[them] ^= capsq; + byTypeBB[capture] ^= capsq; + occupied ^= capsq; // Update piece list, move the last piece at index[capsq] position and // shrink the list. @@ -851,10 +852,10 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI prefetch((char*)TT.first_entry(k)); // Move the piece - Bitboard move_bb = make_move_bb(from, to); - do_move_bb(&byColorBB[us], move_bb); - do_move_bb(&byTypeBB[pt], move_bb); - do_move_bb(&occupied, move_bb); + Bitboard from_to_bb = SquareBB[from] | SquareBB[to]; + byColorBB[us] ^= from_to_bb; + byTypeBB[pt] ^= from_to_bb; + occupied ^= from_to_bb; board[to] = board[from]; board[from] = NO_PIECE; @@ -883,8 +884,8 @@ 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 - xor_bit(&byTypeBB[PAWN], to); - set_bit(&byTypeBB[promotion], to); + byTypeBB[PAWN] ^= to; + byTypeBB[promotion] |= to; board[to] = make_piece(us, promotion); // Update piece lists, move the last pawn at index[to] position @@ -940,11 +941,11 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool moveI else { // Direct checks - if (bit_is_set(ci.checkSq[pt], to)) - st->checkersBB = SquareBB[to]; + if (ci.checkSq[pt] & to) + st->checkersBB |= to; // Discovery checks - if (ci.dcCandidates && bit_is_set(ci.dcCandidates, from)) + if (ci.dcCandidates && (ci.dcCandidates & from)) { if (pt != ROOK) st->checkersBB |= attacks_from(king_square(them)) & pieces(ROOK, QUEEN, us); @@ -999,8 +1000,8 @@ void Position::undo_move(Move m) { assert(promotion >= KNIGHT && promotion <= QUEEN); // Replace the promoted piece with the pawn - xor_bit(&byTypeBB[promotion], to); - set_bit(&byTypeBB[PAWN], to); + byTypeBB[promotion] ^= to; + byTypeBB[PAWN] |= to; board[to] = make_piece(us, PAWN); // Update piece lists, move the last promoted piece at index[to] position @@ -1016,10 +1017,10 @@ void Position::undo_move(Move m) { } // Put the piece back at the source square - Bitboard move_bb = make_move_bb(to, from); - do_move_bb(&byColorBB[us], move_bb); - do_move_bb(&byTypeBB[pt], move_bb); - do_move_bb(&occupied, move_bb); + Bitboard from_to_bb = SquareBB[from] | SquareBB[to]; + byColorBB[us] ^= from_to_bb; + byTypeBB[pt] ^= from_to_bb; + occupied ^= from_to_bb; board[from] = board[to]; board[to] = NO_PIECE; @@ -1044,9 +1045,9 @@ void Position::undo_move(Move m) { } // Restore the captured piece - set_bit(&byColorBB[them], capsq); - set_bit(&byTypeBB[capture], capsq); - set_bit(&occupied, capsq); + byColorBB[them] |= capsq; + byTypeBB[capture] |= capsq; + occupied |= capsq; board[capsq] = make_piece(them, capture); @@ -1100,20 +1101,20 @@ void Position::do_castle_move(Move m) { assert(piece_on(rfrom) == make_piece(us, ROOK)); // Remove pieces from source squares - 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); + byColorBB[us] ^= kfrom; + byTypeBB[KING] ^= kfrom; + occupied ^= kfrom; + byColorBB[us] ^= rfrom; + byTypeBB[ROOK] ^= rfrom; + 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); + byColorBB[us] |= kto; + byTypeBB[KING] |= kto; + occupied |= kto; + byColorBB[us] |= rto; + byTypeBB[ROOK] |= rto; + occupied |= rto; // Update board Piece king = make_piece(us, KING); @@ -1268,13 +1269,13 @@ int Position::see(Move m) const { assert(type_of(piece_on(capQq)) == PAWN); // Remove the captured pawn - xor_bit(&occ, capQq); + 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. - xor_bit(&occ, from); + occ ^= from; attackers = attackers_to(to, occ); // If the opponent has no attackers we are finished @@ -1378,9 +1379,9 @@ void Position::put_piece(Piece p, Square s) { index[s] = pieceCount[c][pt]++; pieceList[c][pt][index[s]] = s; - set_bit(&byTypeBB[pt], s); - set_bit(&byColorBB[c], s); - set_bit(&occupied, s); + byTypeBB[pt] |= s; + byColorBB[c] |= s; + occupied |= s; } diff --git a/src/search.cpp b/src/search.cpp index fa12ee71..1131fae7 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1363,8 +1363,7 @@ split_point_start: // At split points actual search starts from here return true; // Rule 2. Queen contact check is very dangerous - if ( type_of(pc) == QUEEN - && bit_is_set(kingAtt, to)) + if (type_of(pc) == QUEEN && (kingAtt & to)) return true; // Rule 3. Creating new double threats with checks @@ -1419,23 +1418,21 @@ split_point_start: // At split points actual search starts from here // Case 3: Moving through the vacated square p2 = pos.piece_on(f2); - if ( piece_is_slider(p2) - && bit_is_set(squares_between(f2, t2), f1)) + if (piece_is_slider(p2) && (squares_between(f2, t2) & f1)) return true; // Case 4: The destination square for m2 is defended by the moving piece in m1 p1 = pos.piece_on(t1); - if (bit_is_set(pos.attacks_from(p1, t1), t2)) + if (pos.attacks_from(p1, t1) & t2) return true; // Case 5: Discovered check, checking piece is the piece moved in m1 ksq = pos.king_square(pos.side_to_move()); - if ( piece_is_slider(p1) - && bit_is_set(squares_between(t1, ksq), f2)) + if (piece_is_slider(p1) && (squares_between(t1, ksq) & f2)) { Bitboard occ = pos.occupied_squares(); - xor_bit(&occ, f2); - if (bit_is_set(pos.attacks_from(p1, t1, occ), ksq)) + occ ^= f2; + if (pos.attacks_from(p1, t1, occ) & ksq) return true; } return false; @@ -1505,9 +1502,9 @@ split_point_start: // At split points actual search starts from here // Case 3: If the moving piece in the threatened move is a slider, don't // prune safe moves which block its ray. - if ( piece_is_slider(pos.piece_on(tfrom)) - && bit_is_set(squares_between(tfrom, tto), mto) - && pos.see_sign(m) >= 0) + if ( piece_is_slider(pos.piece_on(tfrom)) + && (squares_between(tfrom, tto) & mto) + && pos.see_sign(m) >= 0) return true; return false; -- 2.39.2