From 034a2b04f2fc1017721b4f3fc12895e5f8a190bd Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 30 Nov 2013 10:27:23 +0100 Subject: [PATCH] Rewrite some bitboard init code And move the static function Position::attacks_from() to bitboard code renaming it attacks_bb() No functional change. --- src/bitboard.cpp | 25 ++++++++++++------------- src/bitboard.h | 10 ++++++++++ src/position.cpp | 19 +------------------ src/position.h | 3 +-- src/search.cpp | 4 ++-- 5 files changed, 26 insertions(+), 35 deletions(-) diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 3683b363..a6502330 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -212,24 +212,23 @@ void Bitboards::init() { init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index); init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index); - for (Square s = SQ_A1; s <= SQ_H8; ++s) + for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1) { - PseudoAttacks[QUEEN][s] = PseudoAttacks[BISHOP][s] = attacks_bb(s, 0); - PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0); - } + PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb(s1, 0); + PseudoAttacks[QUEEN][s1] |= PseudoAttacks[ ROOK][s1] = attacks_bb< ROOK>(s1, 0); - for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1) for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2) - if (PseudoAttacks[QUEEN][s1] & s2) - { - Square delta = (s2 - s1) / square_distance(s1, s2); + { + Piece pc = (PseudoAttacks[BISHOP][s1] & s2) ? W_BISHOP : + (PseudoAttacks[ROOK][s1] & s2) ? W_ROOK : NO_PIECE; - for (Square s = s1 + delta; s != s2; s += delta) - BetweenBB[s1][s2] |= s; + if (pc == NO_PIECE) + continue; - PieceType pt = (PseudoAttacks[BISHOP][s1] & s2) ? BISHOP : ROOK; - LineBB[s1][s2] = (PseudoAttacks[pt][s1] & PseudoAttacks[pt][s2]) | s1 | s2; - } + LineBB[s1][s2] = (attacks_bb(pc, s1, 0) & attacks_bb(pc, s2, 0)) | s1 | s2; + BetweenBB[s1][s2] = attacks_bb(pc, s1, SquareBB[s2]) & attacks_bb(pc, s2, SquareBB[s1]); + } + } } diff --git a/src/bitboard.h b/src/bitboard.h index e95bf0d2..34900578 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -254,6 +254,16 @@ inline Bitboard attacks_bb(Square s, Bitboard occ) { return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index(s, occ)]; } +inline Bitboard attacks_bb(Piece p, Square s, Bitboard occ) { + + switch (type_of(p)) + { + case BISHOP: return attacks_bb(s, occ); + case ROOK : return attacks_bb(s, occ); + case QUEEN : return attacks_bb(s, occ) | attacks_bb(s, occ); + default : return StepAttacksBB[p][s]; + } +} /// lsb()/msb() finds the least/most significant bit in a nonzero bitboard. /// pop_lsb() finds and clears the least significant bit in a nonzero bitboard. diff --git a/src/position.cpp b/src/position.cpp index c40b1199..ca7c4600 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -455,23 +455,6 @@ Bitboard Position::attackers_to(Square s, Bitboard occ) const { } -/// Position::attacks_from() computes a bitboard of all attacks of a given piece -/// put in a given square. Slider attacks use occ bitboard as occupancy. - -Bitboard Position::attacks_from(Piece p, Square s, Bitboard occ) { - - assert(is_ok(s)); - - switch (type_of(p)) - { - case BISHOP: return attacks_bb(s, occ); - case ROOK : return attacks_bb(s, occ); - case QUEEN : return attacks_bb(s, occ) | attacks_bb(s, occ); - default : return StepAttacksBB[p][s]; - } -} - - /// Position::legal() tests whether a pseudo-legal move is legal bool Position::legal(Move m, Bitboard pinned) const { @@ -672,7 +655,7 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const { switch (type_of(m)) { case PROMOTION: - return attacks_from(Piece(promotion_type(m)), to, pieces() ^ from) & ksq; + return attacks_bb(Piece(promotion_type(m)), to, pieces() ^ from) & ksq; // En passant capture with check ? We have already handled the case // of direct checks and ordinary discovered check, the only case we diff --git a/src/position.h b/src/position.h index c333e55a..cd297df7 100644 --- a/src/position.h +++ b/src/position.h @@ -114,7 +114,6 @@ public: Bitboard attackers_to(Square s) const; Bitboard attackers_to(Square s, Bitboard occ) const; Bitboard attacks_from(Piece p, Square s) const; - static Bitboard attacks_from(Piece p, Square s, Bitboard occ); template Bitboard attacks_from(Square s) const; template Bitboard attacks_from(Square s, Color c) const; @@ -304,7 +303,7 @@ inline Bitboard Position::attacks_from(Square s, Color c) const { } inline Bitboard Position::attacks_from(Piece p, Square s) const { - return attacks_from(p, s, byTypeBB[ALL_PIECES]); + return attacks_bb(p, s, byTypeBB[ALL_PIECES]); } inline Bitboard Position::attackers_to(Square s) const { diff --git a/src/search.cpp b/src/search.cpp index f1ab80f2..e235c3fc 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1351,7 +1351,7 @@ moves_loop: // When in check and at SpNode search starts from here return true; // Second's destination is defended by the first move's piece - Bitboard m1att = pos.attacks_from(pos.piece_on(m1to), m1to, pos.pieces() ^ m2from); + Bitboard m1att = attacks_bb(pos.piece_on(m1to), m1to, pos.pieces() ^ m2from); if (m1att & m2to) return true; @@ -1395,7 +1395,7 @@ moves_loop: // When in check and at SpNode search starts from here Piece pc = pos.piece_on(m1from); // The moved piece attacks the square 'tto' ? - if (pos.attacks_from(pc, m1to, occ) & m2to) + if (attacks_bb(pc, m1to, occ) & m2to) return true; // Scan for possible X-ray attackers behind the moved piece -- 2.39.2