Skip to content

Commit

Permalink
Rewrite some bitboard init code
Browse files Browse the repository at this point in the history
And move the static function Position::attacks_from() to
bitboard code renaming it attacks_bb()

No functional change.
  • Loading branch information
mcostalba committed Nov 30, 2013
1 parent 6ea5dc2 commit 034a2b0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 35 deletions.
25 changes: 12 additions & 13 deletions src/bitboard.cpp
Expand Up @@ -212,24 +212,23 @@ void Bitboards::init() {
init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);

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<BISHOP>(s, 0);
PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0);
}
PseudoAttacks[QUEEN][s1] = PseudoAttacks[BISHOP][s1] = attacks_bb<BISHOP>(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]);
}
}
}


Expand Down
10 changes: 10 additions & 0 deletions src/bitboard.h
Expand Up @@ -254,6 +254,16 @@ inline Bitboard attacks_bb(Square s, Bitboard occ) {
return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
}

inline Bitboard attacks_bb(Piece p, Square s, Bitboard occ) {

switch (type_of(p))
{
case BISHOP: return attacks_bb<BISHOP>(s, occ);
case ROOK : return attacks_bb<ROOK>(s, occ);
case QUEEN : return attacks_bb<BISHOP>(s, occ) | attacks_bb<ROOK>(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.
Expand Down
19 changes: 1 addition & 18 deletions src/position.cpp
Expand Up @@ -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<BISHOP>(s, occ);
case ROOK : return attacks_bb<ROOK>(s, occ);
case QUEEN : return attacks_bb<BISHOP>(s, occ) | attacks_bb<ROOK>(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 {
Expand Down Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions src/position.h
Expand Up @@ -114,7 +114,6 @@ class Position {
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<PieceType> Bitboard attacks_from(Square s) const;
template<PieceType> Bitboard attacks_from(Square s, Color c) const;

Expand Down Expand Up @@ -304,7 +303,7 @@ inline Bitboard Position::attacks_from<PAWN>(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 {
Expand Down
4 changes: 2 additions & 2 deletions src/search.cpp
Expand Up @@ -1351,7 +1351,7 @@ namespace {
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;

Expand Down Expand Up @@ -1395,7 +1395,7 @@ namespace {
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
Expand Down

0 comments on commit 034a2b0

Please sign in to comment.