- const int QueenContactCheckBonus = 6;
- const int RookContactCheckBonus = 4;
- const int QueenCheckBonus = 3;
- const int RookCheckBonus = 2;
- const int BishopCheckBonus = 1;
- const int KnightCheckBonus = 1;
-
- // InitKingDanger[Square] contains penalties based on the position of the
- // defending king, indexed by king's square (from white's point of view).
- const int InitKingDanger[] = {
- 2, 0, 2, 5, 5, 2, 0, 2,
- 2, 2, 4, 8, 8, 4, 2, 2,
- 7, 10, 12, 12, 12, 12, 10, 7,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15,
- 15, 15, 15, 15, 15, 15, 15, 15
- };
-
- // KingDangerTable[Color][attackUnits] contains the actual king danger
- // weighted scores, indexed by color and by a calculated integer number.
- Score KingDangerTable[2][128];
-
- // Pawn and material hash tables, indexed by the current thread id.
- // Note that they will be initialized at 0 being global variables.
- MaterialInfoTable* MaterialTable[MAX_THREADS];
- PawnInfoTable* PawnTable[MAX_THREADS];
-
- // Function prototypes
- template<bool HasPopCnt>
- Value do_evaluate(const Position& pos, Value margins[]);
-
- template<Color Us, bool HasPopCnt>
- void init_eval_info(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool HasPopCnt>
- Score evaluate_pieces_of_color(const Position& pos, EvalInfo& ei, Score& mobility);
-
- template<Color Us, bool HasPopCnt>
- Score evaluate_king(const Position& pos, EvalInfo& ei, Value margins[]);
-
- template<Color Us>
- Score evaluate_threats(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool HasPopCnt>
- int evaluate_space(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- Score evaluate_passed_pawns(const Position& pos, EvalInfo& ei);
-
- Score apply_weight(Score v, Score weight);
- Value scale_by_game_phase(const Score& v, Phase ph, const ScaleFactor sf[]);
- Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
- void init_safety();
-}
-
-
-////
-//// Functions
-////
-
-
-/// Prefetches in pawn hash tables
-
-void prefetchPawn(Key key, int threadID) {
-
- PawnTable[threadID]->prefetch(key);
-}
-
-
-/// evaluate() is the main evaluation function. It always computes two
-/// values, an endgame score and a middle game score, and interpolates
-/// between them based on the remaining material.
-Value evaluate(const Position& pos, Value margins[]) {
-
- return CpuHasPOPCNT ? do_evaluate<true>(pos, margins)
- : do_evaluate<false>(pos, margins);
-}
-
-namespace {
-
-template<bool HasPopCnt>
-Value do_evaluate(const Position& pos, Value margins[]) {
-
- EvalInfo ei;
- ScaleFactor factor[2];
- Score mobilityWhite, mobilityBlack;
-
- assert(pos.is_ok());
- assert(pos.thread() >= 0 && pos.thread() < MAX_THREADS);
- assert(!pos.is_check());
-
- // Initialize value by reading the incrementally updated scores included
- // in the position object (material + piece square tables).
- Score bonus = pos.value();
-
- // margins[color] is the uncertainty estimation of position's evaluation
- // and typically is used by the search for pruning decisions.
- margins[WHITE] = margins[BLACK] = VALUE_ZERO;
-
- // Probe the material hash table
- MaterialInfo* mi = MaterialTable[pos.thread()]->get_material_info(pos);
- bonus += mi->material_value();
-
- // If we have a specialized evaluation function for the current material
- // configuration, call it and return.
- if (mi->specialized_eval_exists())
- return mi->evaluate(pos);
-
- // After get_material_info() call that modifies them
- factor[WHITE] = mi->scale_factor(pos, WHITE);
- factor[BLACK] = mi->scale_factor(pos, BLACK);
-
- // Probe the pawn hash table
- ei.pi = PawnTable[pos.thread()]->get_pawn_info(pos);
- bonus += apply_weight(ei.pi->pawns_value(), Weights[PawnStructure]);
-
- // Initialize attack and king safety bitboards
- init_eval_info<WHITE, HasPopCnt>(pos, ei);
- init_eval_info<BLACK, HasPopCnt>(pos, ei);
-
- // Evaluate pieces and mobility
- bonus += evaluate_pieces_of_color<WHITE, HasPopCnt>(pos, ei, mobilityWhite)
- - evaluate_pieces_of_color<BLACK, HasPopCnt>(pos, ei, mobilityBlack);
-
- bonus += apply_weight(mobilityWhite - mobilityBlack, Weights[Mobility]);
-
- // Evaluate kings after all other pieces because we need complete attack
- // information when computing the king safety evaluation.
- bonus += evaluate_king<WHITE, HasPopCnt>(pos, ei, margins)
- - evaluate_king<BLACK, HasPopCnt>(pos, ei, margins);
-
- // Evaluate tactical threats, we need full attack information including king
- bonus += evaluate_threats<WHITE>(pos, ei)
- - evaluate_threats<BLACK>(pos, ei);
-
- // Evaluate passed pawns, we need full attack information including king
- bonus += evaluate_passed_pawns<WHITE>(pos, ei)
- - evaluate_passed_pawns<BLACK>(pos, ei);
-
- Phase phase = mi->game_phase();
-
- // Evaluate space for both sides, only in middle-game.
- if (phase > PHASE_ENDGAME && mi->space_weight() > 0)
- {
- int s = evaluate_space<WHITE, HasPopCnt>(pos, ei) - evaluate_space<BLACK, HasPopCnt>(pos, ei);
- bonus += apply_weight(make_score(s * mi->space_weight(), 0), Weights[Space]);