Skip to content

Commit

Permalink
Tidy up
Browse files Browse the repository at this point in the history
No functional change

Closes #1148
  • Loading branch information
Rocky640 authored and zamar committed Jul 3, 2017
1 parent 69eb391 commit 6d24ef8
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 52 deletions.
12 changes: 6 additions & 6 deletions src/bitboard.cpp
Expand Up @@ -30,11 +30,11 @@ Bitboard SquareBB[SQUARE_NB];
Bitboard FileBB[FILE_NB];
Bitboard RankBB[RANK_NB];
Bitboard AdjacentFilesBB[FILE_NB];
Bitboard InFrontBB[COLOR_NB][RANK_NB];
Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
Bitboard LineBB[SQUARE_NB][SQUARE_NB];
Bitboard DistanceRingBB[SQUARE_NB][8];
Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
Expand Down Expand Up @@ -163,14 +163,14 @@ void Bitboards::init() {
AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);

for (Rank r = RANK_1; r < RANK_8; ++r)
InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
ForwardRanksBB[WHITE][r] = ~(ForwardRanksBB[BLACK][r + 1] = ForwardRanksBB[BLACK][r] | RankBB[r]);

for (Color c = WHITE; c <= BLACK; ++c)
for (Square s = SQ_A1; s <= SQ_H8; ++s)
{
ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
PawnAttackSpan[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
PassedPawnMask[c][s] = ForwardBB[c][s] | PawnAttackSpan[c][s];
ForwardFileBB [c][s] = ForwardRanksBB[c][rank_of(s)] & FileBB[file_of(s)];
PawnAttackSpan[c][s] = ForwardRanksBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
PassedPawnMask[c][s] = ForwardFileBB [c][s] | PawnAttackSpan[c][s];
}

for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
Expand Down
24 changes: 12 additions & 12 deletions src/bitboard.h
Expand Up @@ -65,11 +65,11 @@ extern Bitboard SquareBB[SQUARE_NB];
extern Bitboard FileBB[FILE_NB];
extern Bitboard RankBB[RANK_NB];
extern Bitboard AdjacentFilesBB[FILE_NB];
extern Bitboard InFrontBB[COLOR_NB][RANK_NB];
extern Bitboard ForwardRanksBB[COLOR_NB][RANK_NB];
extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB];
extern Bitboard LineBB[SQUARE_NB][SQUARE_NB];
extern Bitboard DistanceRingBB[SQUARE_NB][8];
extern Bitboard ForwardBB[COLOR_NB][SQUARE_NB];
extern Bitboard ForwardFileBB[COLOR_NB][SQUARE_NB];
extern Bitboard PassedPawnMask[COLOR_NB][SQUARE_NB];
extern Bitboard PawnAttackSpan[COLOR_NB][SQUARE_NB];
extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB];
Expand Down Expand Up @@ -179,28 +179,28 @@ inline Bitboard between_bb(Square s1, Square s2) {
}


/// in_front_bb() returns a bitboard representing all the squares on all the ranks
/// forward_ranks_bb() returns a bitboard representing all the squares on all the ranks
/// in front of the given one, from the point of view of the given color. For
/// instance, in_front_bb(BLACK, RANK_3) will return the squares on ranks 1 and 2.
/// instance, forward_ranks_bb(BLACK, SQ_D3) will return the 16 squares on ranks 1 and 2.

inline Bitboard in_front_bb(Color c, Rank r) {
return InFrontBB[c][r];
inline Bitboard forward_ranks_bb(Color c, Square s) {
return ForwardRanksBB[c][rank_of(s)];
}


/// forward_bb() returns a bitboard representing all the squares along the line
/// forward_file_bb() returns a bitboard representing all the squares along the line
/// in front of the given one, from the point of view of the given color:
/// ForwardBB[c][s] = in_front_bb(c, rank_of(s)) & file_bb(s)
/// ForwardFileBB[c][s] = forward_ranks_bb(c, s) & file_bb(s)

inline Bitboard forward_bb(Color c, Square s) {
return ForwardBB[c][s];
inline Bitboard forward_file_bb(Color c, Square s) {
return ForwardFileBB[c][s];
}


/// pawn_attack_span() returns a bitboard representing all the squares that can be
/// attacked by a pawn of the given color when it moves along its file, starting
/// from the given square:
/// PawnAttackSpan[c][s] = in_front_bb(c, rank_of(s)) & adjacent_files_bb(file_of(s));
/// PawnAttackSpan[c][s] = forward_ranks_bb(c, s) & adjacent_files_bb(file_of(s));

inline Bitboard pawn_attack_span(Color c, Square s) {
return PawnAttackSpan[c][s];
Expand All @@ -209,7 +209,7 @@ inline Bitboard pawn_attack_span(Color c, Square s) {

/// passed_pawn_mask() returns a bitboard mask which can be used to test if a
/// pawn of the given color and on the given square is a passed pawn:
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_bb(c, s)
/// PassedPawnMask[c][s] = pawn_attack_span(c, s) | forward_file_bb(c, s)

inline Bitboard passed_pawn_mask(Color c, Square s) {
return PassedPawnMask[c][s];
Expand Down
6 changes: 3 additions & 3 deletions src/endgame.cpp
Expand Up @@ -598,7 +598,7 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {

// If all pawns are ahead of the king, on a single rook file and
// the king is within one file of the pawns, it's a draw.
if ( !(pawns & ~in_front_bb(weakSide, rank_of(ksq)))
if ( !(pawns & ~forward_ranks_bb(weakSide, ksq))
&& !((pawns & ~FileABB) && (pawns & ~FileHBB))
&& distance<File>(ksq, lsb(pawns)) <= 1)
return SCALE_FACTOR_DRAW;
Expand Down Expand Up @@ -645,7 +645,7 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
if (relative_rank(strongSide, pawnSq) <= RANK_5)
return SCALE_FACTOR_DRAW;

Bitboard path = forward_bb(strongSide, pawnSq);
Bitboard path = forward_file_bb(strongSide, pawnSq);

if (path & pos.pieces(weakSide, KING))
return SCALE_FACTOR_DRAW;
Expand Down Expand Up @@ -780,7 +780,7 @@ ScaleFactor Endgame<KNPKB>::operator()(const Position& pos) const {

// King needs to get close to promoting pawn to prevent knight from blocking.
// Rules for this are very tricky, so just approximate.
if (forward_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
if (forward_file_bb(strongSide, pawnSq) & pos.attacks_from<BISHOP>(bishopSq))
return ScaleFactor(distance(weakKingSq, pawnSq));

return SCALE_FACTOR_NONE;
Expand Down
55 changes: 28 additions & 27 deletions src/evaluate.cpp
Expand Up @@ -88,7 +88,7 @@ namespace {
template<Color Us> void initialize();
template<Color Us> Score evaluate_king();
template<Color Us> Score evaluate_threats();
template<Color Us> Score evaluate_passer_pawns();
template<Color Us> Score evaluate_passed_pawns();
template<Color Us> Score evaluate_space();
template<Color Us, PieceType Pt> Score evaluate_pieces();
ScaleFactor evaluate_scale_factor(Value eg);
Expand Down Expand Up @@ -416,7 +416,7 @@ namespace {
: ~Bitboard(0) ^ Rank1BB ^ Rank2BB ^ Rank3BB);

const Square ksq = pos.square<KING>(Us);
Bitboard undefended, b, b1, b2, safe, other;
Bitboard kingOnlyDefended, b, b1, b2, safe, other;
int kingDanger;

// King shelter and enemy pawns storm
Expand All @@ -426,9 +426,9 @@ namespace {
if (kingAttackersCount[Them] > (1 - pos.count<QUEEN>(Them)))
{
// Find the attacked squares which are defended only by our king...
undefended = attackedBy[Them][ALL_PIECES]
& attackedBy[Us][KING]
& ~attackedBy2[Us];
kingOnlyDefended = attackedBy[Them][ALL_PIECES]
& attackedBy[Us][KING]
& ~attackedBy2[Us];

// ... and those which are not defended at all in the larger king ring
b = attackedBy[Them][ALL_PIECES] & ~attackedBy[Us][ALL_PIECES]
Expand All @@ -437,19 +437,19 @@ namespace {
// Initialize the 'kingDanger' variable, which will be transformed
// later into a king danger score. The initial value is based on the
// number and types of the enemy's attacking pieces, the number of
// attacked and undefended squares around our king and the quality of
// the pawn shelter (current 'score' value).
// attacked and weak squares around our king, the absence of queen and
// the quality of the pawn shelter (current 'score' value).
kingDanger = kingAttackersCount[Them] * kingAttackersWeight[Them]
+ 102 * kingAdjacentZoneAttacksCount[Them]
+ 201 * popcount(undefended)
+ 201 * popcount(kingOnlyDefended)
+ 143 * (popcount(b) + !!pos.pinned_pieces(Us))
- 848 * !pos.count<QUEEN>(Them)
- 9 * mg_value(score) / 8
+ 40;

// Analyse the safe enemy's checks which are possible on next move
safe = ~pos.pieces(Them);
safe &= ~attackedBy[Us][ALL_PIECES] | (undefended & attackedBy2[Them]);
safe &= ~attackedBy[Us][ALL_PIECES] | (kingOnlyDefended & attackedBy2[Them]);

b1 = pos.attacks_from< ROOK>(ksq);
b2 = pos.attacks_from<BISHOP>(ksq);
Expand Down Expand Up @@ -532,8 +532,7 @@ namespace {
const Square Up = (Us == WHITE ? NORTH : SOUTH);
const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
const Bitboard TRank2BB = (Us == WHITE ? Rank2BB : Rank7BB);
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);

Bitboard b, weak, defended, stronglyProtected, safeThreats;
Score score = SCORE_ZERO;
Expand Down Expand Up @@ -596,14 +595,15 @@ namespace {
score += ThreatByKing[more_than_one(b)];
}

// Bonus if some pawns can safely push and attack an enemy piece
b = pos.pieces(Us, PAWN) & ~TRank7BB;
b = shift<Up>(b | (shift<Up>(b & TRank2BB) & ~pos.pieces()));
// Find squares where our pawns can push on the next move
b = shift<Up>(pos.pieces(Us, PAWN)) & ~pos.pieces();
b |= shift<Up>(b & TRank3BB) & ~pos.pieces();

b &= ~pos.pieces()
& ~attackedBy[Them][PAWN]
// Keep only the squares which are not completely unsafe
b &= ~attackedBy[Them][PAWN]
& (attackedBy[Us][ALL_PIECES] | ~attackedBy[Them][ALL_PIECES]);

// Add a bonus for each new pawn threats from those squares
b = (shift<Left>(b) | shift<Right>(b))
& pos.pieces(Them)
& ~attackedBy[Us][PAWN];
Expand All @@ -617,13 +617,14 @@ namespace {
}


// evaluate_passer_pawns() evaluates the passed pawns and candidate passed
// evaluate_passed_pawns() evaluates the passed pawns and candidate passed
// pawns of the given color.

template<Tracing T> template<Color Us>
Score Evaluation<T>::evaluate_passer_pawns() {
Score Evaluation<T>::evaluate_passed_pawns() {

const Color Them = (Us == WHITE ? BLACK : WHITE);
const Square Up = (Us == WHITE ? NORTH : SOUTH);

Bitboard b, bb, squaresToQueen, defendedSquares, unsafeSquares;
Score score = SCORE_ZERO;
Expand All @@ -634,9 +635,9 @@ namespace {
{
Square s = pop_lsb(&b);

assert(!(pos.pieces(Them, PAWN) & forward_bb(Us, s + pawn_push(Us))));
assert(!(pos.pieces(Them, PAWN) & forward_file_bb(Us, s + Up)));

bb = forward_bb(Us, s) & (attackedBy[Them][ALL_PIECES] | pos.pieces(Them));
bb = forward_file_bb(Us, s) & (attackedBy[Them][ALL_PIECES] | pos.pieces(Them));
score -= HinderPassedPawn * popcount(bb);

int r = relative_rank(Us, s) - RANK_2;
Expand All @@ -646,25 +647,25 @@ namespace {

if (rr)
{
Square blockSq = s + pawn_push(Us);
Square blockSq = s + Up;

// Adjust bonus based on the king's proximity
ebonus += distance(pos.square<KING>(Them), blockSq) * 5 * rr
- distance(pos.square<KING>( Us), blockSq) * 2 * rr;

// If blockSq is not the queening square then consider also a second push
if (relative_rank(Us, blockSq) != RANK_8)
ebonus -= distance(pos.square<KING>(Us), blockSq + pawn_push(Us)) * rr;
ebonus -= distance(pos.square<KING>(Us), blockSq + Up) * rr;

// If the pawn is free to advance, then increase the bonus
if (pos.empty(blockSq))
{
// If there is a rook or queen attacking/defending the pawn from behind,
// consider all the squaresToQueen. Otherwise consider only the squares
// in the pawn's path attacked or occupied by the enemy.
defendedSquares = unsafeSquares = squaresToQueen = forward_bb(Us, s);
defendedSquares = unsafeSquares = squaresToQueen = forward_file_bb(Us, s);

bb = forward_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from<ROOK>(s);
bb = forward_file_bb(Them, s) & pos.pieces(ROOK, QUEEN) & pos.attacks_from<ROOK>(s);

if (!(pos.pieces(Us) & bb))
defendedSquares &= attackedBy[Us][ALL_PIECES];
Expand Down Expand Up @@ -692,7 +693,7 @@ namespace {

// Scale down bonus for candidate passers which need more than one
// pawn push to become passed or have a pawn in front of them.
if (!pos.pawn_passed(Us, s + pawn_push(Us)) || (pos.pieces(PAWN) & forward_bb(Us, s)))
if (!pos.pawn_passed(Us, s + Up) || (pos.pieces(PAWN) & forward_file_bb(Us, s)))
mbonus /= 2, ebonus /= 2;

score += make_score(mbonus, ebonus) + PassedFile[file_of(s)];
Expand Down Expand Up @@ -852,8 +853,8 @@ namespace {
score += evaluate_threats<WHITE>()
- evaluate_threats<BLACK>();

score += evaluate_passer_pawns<WHITE>()
- evaluate_passer_pawns<BLACK>();
score += evaluate_passed_pawns<WHITE>()
- evaluate_passed_pawns<BLACK>();

if (pos.non_pawn_material() >= SpaceThreshold)
score += evaluate_space<WHITE>()
Expand Down
8 changes: 4 additions & 4 deletions src/pawns.cpp
Expand Up @@ -126,7 +126,7 @@ namespace {
e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);

// Flag the pawn
opposed = theirPawns & forward_bb(Us, s);
opposed = theirPawns & forward_file_bb(Us, s);
stoppers = theirPawns & passed_pawn_mask(Us, s);
lever = theirPawns & PawnAttacks[Us][s];
leverPush = theirPawns & PawnAttacks[Us][s + Up];
Expand Down Expand Up @@ -158,7 +158,7 @@ namespace {
// which could become passed after one or two pawn pushes when are
// not attacked more times than defended.
if ( !(stoppers ^ lever ^ leverPush)
&& !(ourPawns & forward_bb(Us, s))
&& !(ourPawns & forward_file_bb(Us, s))
&& popcount(supported) >= popcount(lever)
&& popcount(phalanx) >= popcount(leverPush))
e->passedPawns[Us] |= s;
Expand Down Expand Up @@ -250,7 +250,7 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {

enum { BlockedByKing, Unopposed, BlockedByPawn, Unblocked };

Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, rank_of(ksq)) | rank_bb(ksq));
Bitboard b = pos.pieces(PAWN) & (forward_ranks_bb(Us, ksq) | rank_bb(ksq));
Bitboard ourPawns = b & pos.pieces(Us);
Bitboard theirPawns = b & pos.pieces(Them);
Value safety = MaxSafetyBonus;
Expand All @@ -261,7 +261,7 @@ Value Entry::shelter_storm(const Position& pos, Square ksq) {
b = ourPawns & file_bb(f);
Rank rkUs = b ? relative_rank(Us, backmost_sq(Us, b)) : RANK_1;

b = theirPawns & file_bb(f);
b = theirPawns & file_bb(f);
Rank rkThem = b ? relative_rank(Us, frontmost_sq(Them, b)) : RANK_1;

int d = std::min(f, FILE_H - f);
Expand Down

0 comments on commit 6d24ef8

Please sign in to comment.