+ template<Color Us>
+ Score evaluate(const Position& pos, Pawns::Entry* e) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+ const Square Up = (Us == WHITE ? DELTA_N : DELTA_S);
+ const Square Right = (Us == WHITE ? DELTA_NE : DELTA_SW);
+ const Square Left = (Us == WHITE ? DELTA_NW : DELTA_SE);
+
+ Bitboard b;
+ Square s;
+ File f;
+ bool passed, isolated, doubled, opposed, connected, backward, candidate;
+ Score value = SCORE_ZERO;
+ const Square* pl = pos.list<PAWN>(Us);
+
+ Bitboard ourPawns = pos.pieces(Us, PAWN);
+ Bitboard theirPawns = pos.pieces(Them, PAWN);
+
+ e->passedPawns[Us] = e->candidatePawns[Us] = 0;
+ e->kingSquares[Us] = SQ_NONE;
+ e->semiopenFiles[Us] = 0xFF;
+ e->pawnAttacks[Us] = shift_bb<Right>(ourPawns) | shift_bb<Left>(ourPawns);
+ e->pawnsOnSquares[Us][BLACK] = popcount<Max15>(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));
+
+ f = file_of(s);
+
+ // This file cannot be semi-open
+ e->semiopenFiles[Us] &= ~(1 << f);
+
+ // Our rank plus previous one
+ b = rank_bb(s) | rank_bb(s - pawn_push(Us));
+
+ // Flag the pawn as passed, isolated, doubled or
+ // connected (but not the backward one).
+ connected = ourPawns & adjacent_files_bb(f) & b;
+ isolated = !(ourPawns & adjacent_files_bb(f));
+ doubled = ourPawns & forward_bb(Us, s);
+ opposed = theirPawns & forward_bb(Us, s);
+ passed = !(theirPawns & passed_pawn_mask(Us, s));
+
+ // Test for backward pawn.
+ // If the pawn is passed, isolated, or connected it cannot be
+ // backward. If there are friendly pawns behind on adjacent files
+ // or if it can capture an enemy pawn it cannot be backward either.
+ if ( (passed | isolated | connected)
+ || (ourPawns & pawn_attack_span(Them, s))
+ || (pos.attacks_from<PAWN>(s, Us) & theirPawns))
+ backward = false;
+ else
+ {
+ // We now know that there are no friendly pawns beside or behind this
+ // pawn on adjacent files. We now check whether the pawn is
+ // backward by looking in the forward direction on the adjacent
+ // files, and picking the closest pawn there.
+ b = pawn_attack_span(Us, s) & (ourPawns | theirPawns);
+ b = pawn_attack_span(Us, s) & rank_bb(backmost_sq(Us, b));
+
+ // If we have an enemy pawn in the same or next rank, the pawn is
+ // backward because it cannot advance without being captured.
+ backward = (b | shift_bb<Up>(b)) & theirPawns;
+ }
+
+ assert(opposed | passed | (pawn_attack_span(Us, s) & theirPawns));
+
+ // A not-passed pawn is a candidate to become passed, if it is free to
+ // advance and if the number of friendly pawns beside or behind this
+ // pawn on adjacent files is higher than or equal to the number of
+ // enemy pawns in the forward direction on the adjacent files.
+ candidate = !(opposed | passed | backward | isolated)
+ && (b = pawn_attack_span(Them, s + pawn_push(Us)) & ourPawns) != 0
+ && popcount<Max15>(b) >= popcount<Max15>(pawn_attack_span(Us, s) & theirPawns);
+
+ // Passed pawns will be properly scored in evaluation because we need
+ // full attack info to evaluate passed pawns. Only the frontmost passed
+ // pawn on each file is considered a true passed pawn.
+ if (passed && !doubled)
+ e->passedPawns[Us] |= s;
+
+ // Score this pawn
+ if (isolated)
+ value -= Isolated[opposed][f];
+
+ if (doubled)
+ value -= Doubled[f];
+
+ if (backward)
+ value -= Backward[opposed][f];
+
+ if (connected)
+ value += Connected[f][relative_rank(Us, s)];
+
+ if (candidate)
+ {
+ value += CandidatePassed[relative_rank(Us, s)];
+
+ if (!doubled)
+ e->candidatePawns[Us] |= s;
+ }
+ }
+
+ // In endgame it's better to have pawns on both wings. So give a bonus according
+ // to file distance between left and right outermost pawns.
+ if (pos.count<PAWN>(Us) > 1)
+ {
+ b = e->semiopenFiles[Us] ^ 0xFF;
+ value += PawnsFileSpan * int(msb(b) - lsb(b));
+ }
+
+ return value;
+ }