+ #undef V
+
+ template<Color Us>
+ Score evaluate(const Position& pos, Pawns::Entry* e) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+ const Square Up = (Us == WHITE ? NORTH : SOUTH);
+ const Square Right = (Us == WHITE ? NORTH_EAST : SOUTH_WEST);
+ const Square Left = (Us == WHITE ? NORTH_WEST : SOUTH_EAST);
+
+ Bitboard b, neighbours, stoppers, doubled, supported, phalanx;
+ Bitboard lever, leverPush;
+ Square s;
+ bool opposed, backward;
+ Score score = SCORE_ZERO;
+ const Square* pl = pos.squares<PAWN>(Us);
+
+ Bitboard ourPawns = pos.pieces( Us, PAWN);
+ Bitboard theirPawns = pos.pieces(Them, PAWN);
+
+ e->passedPawns[Us] = e->pawnAttacksSpan[Us] = 0;
+ e->semiopenFiles[Us] = 0xFF;
+ e->kingSquares[Us] = SQ_NONE;
+ e->pawnAttacks[Us] = shift<Right>(ourPawns) | shift<Left>(ourPawns);
+ e->pawnsOnSquares[Us][BLACK] = popcount(ourPawns & DarkSquares);
+ e->pawnsOnSquares[Us][WHITE] = pos.count<PAWN>(Us) - e->pawnsOnSquares[Us][BLACK];
+
+ // Loop through all pawns of the current color and score each pawn
+ while ((s = *pl++) != SQ_NONE)
+ {
+ assert(pos.piece_on(s) == make_piece(Us, PAWN));
+
+ File f = file_of(s);
+
+ e->semiopenFiles[Us] &= ~(1 << f);
+ e->pawnAttacksSpan[Us] |= pawn_attack_span(Us, s);
+
+ // Flag the pawn
+ 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];
+ doubled = ourPawns & (s - Up);
+ neighbours = ourPawns & adjacent_files_bb(f);
+ phalanx = neighbours & rank_bb(s);
+ supported = neighbours & rank_bb(s - Up);
+
+ // A pawn is backward when it is behind all pawns of the same color on the
+ // adjacent files and cannot be safely advanced.
+ if (!neighbours || lever || relative_rank(Us, s) >= RANK_5)
+ backward = false;
+ else
+ {
+ // Find the backmost rank with neighbours or stoppers
+ b = rank_bb(backmost_sq(Us, neighbours | stoppers));
+
+ // The pawn is backward when it cannot safely progress to that rank:
+ // either there is a stopper in the way on this rank, or there is a
+ // stopper on adjacent file which controls the way to that rank.
+ backward = (b | shift<Up>(b & adjacent_files_bb(f))) & stoppers;
+
+ assert(!(backward && (forward_ranks_bb(Them, s + Up) & neighbours)));
+ }
+
+ // Passed pawns will be properly scored in evaluation because we need
+ // full attack info to evaluate them. Include also not passed pawns
+ // which could become passed after one or two pawn pushes when are
+ // not attacked more times than defended.
+ if ( !(stoppers ^ lever ^ leverPush)
+ && !(ourPawns & forward_file_bb(Us, s))
+ && popcount(supported) >= popcount(lever)
+ && popcount(phalanx) >= popcount(leverPush))
+ e->passedPawns[Us] |= s;
+
+ else if ( stoppers == SquareBB[s + Up]
+ && relative_rank(Us, s) >= RANK_5)
+ {
+ b = shift<Up>(supported) & ~theirPawns;
+ while (b)
+ if (!more_than_one(theirPawns & PawnAttacks[Us][pop_lsb(&b)]))
+ e->passedPawns[Us] |= s;
+ }
+
+ // Score this pawn
+ if (supported | phalanx)
+ score += Connected[opposed][!!phalanx][popcount(supported)][relative_rank(Us, s)];
+
+ else if (!neighbours)
+ score -= Isolated[opposed];
+
+ else if (backward)
+ score -= Backward[opposed];
+
+ if (doubled && !supported)
+ score -= Doubled;
+
+ if (lever)
+ score += Lever[relative_rank(Us, s)];
+ }
+
+ return score;
+ }
+
+} // namespace