+ // evaluate_threats<>() assigns bonuses according to the type of attacking piece
+ // and the type of attacked one.
+
+ template<Color Us>
+ void evaluate_threats(const Position& pos, EvalInfo& ei) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+
+ Bitboard b;
+ Score bonus = SCORE_ZERO;
+
+ // Enemy pieces not defended by a pawn and under our attack
+ Bitboard weakEnemies = pos.pieces_of_color(Them)
+ & ~ei.attackedBy[Them][PAWN]
+ & ei.attackedBy[Us][0];
+ if (!weakEnemies)
+ return;
+
+ // Add bonus according to type of attacked enemy pieces and to the
+ // type of attacking piece, from knights to queens. Kings are not
+ // considered because are already special handled in king evaluation.
+ for (PieceType pt1 = KNIGHT; pt1 < KING; pt1++)
+ {
+ b = ei.attackedBy[Us][pt1] & weakEnemies;
+ if (b)
+ for (PieceType pt2 = PAWN; pt2 < KING; pt2++)
+ if (b & pos.pieces(pt2))
+ bonus += ThreatBonus[pt1][pt2];
+ }
+ ei.value += Sign[Us] * bonus;
+ }
+
+
+ // evaluate_pieces_of_color<>() assigns bonuses and penalties to all the
+ // pieces of a given color.
+
+ template<Color Us, bool HasPopCnt>
+ void evaluate_pieces_of_color(const Position& pos, EvalInfo& ei) {
+
+ const Color Them = (Us == WHITE ? BLACK : WHITE);
+
+ // Do not include in mobility squares protected by enemy pawns or occupied by our pieces
+ const Bitboard no_mob_area = ~(ei.attackedBy[Them][PAWN] | pos.pieces_of_color(Us));
+
+ evaluate_pieces<KNIGHT, Us, HasPopCnt>(pos, ei, no_mob_area);
+ evaluate_pieces<BISHOP, Us, HasPopCnt>(pos, ei, no_mob_area);
+ evaluate_pieces<ROOK, Us, HasPopCnt>(pos, ei, no_mob_area);
+ evaluate_pieces<QUEEN, Us, HasPopCnt>(pos, ei, no_mob_area);
+
+ // Sum up all attacked squares
+ ei.attackedBy[Us][0] = ei.attackedBy[Us][PAWN] | ei.attackedBy[Us][KNIGHT]
+ | ei.attackedBy[Us][BISHOP] | ei.attackedBy[Us][ROOK]
+ | ei.attackedBy[Us][QUEEN] | ei.attackedBy[Us][KING];