- 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& margin);
-
- 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);
-
- template<bool HasPopCnt>
- Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
-
- inline Score apply_weight(Score v, Score weight);
- Value scale_by_game_phase(const Score& v, Phase ph, 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& margin) {
-
- return CpuHasPOPCNT ? do_evaluate<true>(pos, margin)
- : do_evaluate<false>(pos, margin);
-}
-
-namespace {
-
-template<bool HasPopCnt>
-Value do_evaluate(const Position& pos, Value& margin) {
-
- EvalInfo ei;
- Value margins[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[] store the uncertainty estimation of position's evaluation
- // that 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())
- {
- margin = VALUE_ZERO;
- return mi->evaluate(pos);