- const int QueenContactCheckBonus = 3;
- const int DiscoveredCheckBonus = 3;
- const int QueenCheckBonus = 2;
- const int RookCheckBonus = 1;
- const int BishopCheckBonus = 1;
- const int KnightCheckBonus = 1;
-
- // InitKingDanger[] contains bonuses based on the position of the defending
- // king.
- const int InitKingDanger[64] = {
- 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][] contains the actual king danger weighted scores
- 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];
-
- // Sizes of pawn and material hash tables
- const int PawnTableSize = 16384;
- const int MaterialTableSize = 1024;
-
- // Function prototypes
- template<bool HasPopCnt>
- Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID);
-
- template<Color Us, bool HasPopCnt>
- void evaluate_pieces_of_color(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool HasPopCnt>
- void evaluate_king(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- void evaluate_threats(const Position& pos, EvalInfo& ei);
-
- template<Color Us, bool HasPopCnt>
- void evaluate_space(const Position& pos, EvalInfo& ei);
-
- template<Color Us>
- void evaluate_passed_pawns(const Position& pos, EvalInfo& ei);
-
- void evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
- void evaluate_trapped_bishop_a7h7(const Position& pos, Square s, Color us, EvalInfo& ei);
- void evaluate_trapped_bishop_a1h1(const Position& pos, Square s, Color us, EvalInfo& ei);
- inline 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
-////
-
-/// 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, EvalInfo& ei, int threadID) {
-
- return CpuHasPOPCNT ? do_evaluate<true>(pos, ei, threadID)
- : do_evaluate<false>(pos, ei, threadID);
-}
-
-namespace {
-
-template<bool HasPopCnt>
-Value do_evaluate(const Position& pos, EvalInfo& ei, int threadID) {
-
- Bitboard b;
- ScaleFactor factor[2];
-
- assert(pos.is_ok());
- assert(threadID >= 0 && threadID < MAX_THREADS);
- assert(!pos.is_check());
-
- memset(&ei, 0, sizeof(EvalInfo));
-
- // Initialize by reading the incrementally updated scores included in the
- // position object (material + piece square tables)
- ei.value = pos.value();
-
- // Probe the material hash table
- ei.mi = MaterialTable[threadID]->get_material_info(pos);
- ei.value += ei.mi->material_value();
-
- // If we have a specialized evaluation function for the current material
- // configuration, call it and return
- if (ei.mi->specialized_eval_exists())
- return ei.mi->evaluate(pos);
-
- // After get_material_info() call that modifies them
- factor[WHITE] = ei.mi->scale_factor(pos, WHITE);
- factor[BLACK] = ei.mi->scale_factor(pos, BLACK);
-
- // Probe the pawn hash table
- ei.pi = PawnTable[threadID]->get_pawn_info(pos);
- ei.value += apply_weight(ei.pi->pawns_value(), Weights[PawnStructure]);
-
- // Initialize king attack bitboards and king attack zones for both sides
- ei.attackedBy[WHITE][KING] = pos.attacks_from<KING>(pos.king_square(WHITE));
- ei.attackedBy[BLACK][KING] = pos.attacks_from<KING>(pos.king_square(BLACK));
- ei.kingZone[WHITE] = ei.attackedBy[BLACK][KING] | (ei.attackedBy[BLACK][KING] >> 8);
- ei.kingZone[BLACK] = ei.attackedBy[WHITE][KING] | (ei.attackedBy[WHITE][KING] << 8);
-
- // Initialize pawn attack bitboards for both sides
- ei.attackedBy[WHITE][PAWN] = ei.pi->pawn_attacks(WHITE);
- b = ei.attackedBy[WHITE][PAWN] & ei.attackedBy[BLACK][KING];
- if (b)
- ei.kingAttackersCount[WHITE] = count_1s_max_15<HasPopCnt>(b)/2;
-
- ei.attackedBy[BLACK][PAWN] = ei.pi->pawn_attacks(BLACK);
- b = ei.attackedBy[BLACK][PAWN] & ei.attackedBy[WHITE][KING];
- if (b)
- ei.kingAttackersCount[BLACK] = count_1s_max_15<HasPopCnt>(b)/2;
-
- // Evaluate pieces
- evaluate_pieces_of_color<WHITE, HasPopCnt>(pos, ei);
- evaluate_pieces_of_color<BLACK, HasPopCnt>(pos, ei);
-
- // Kings. Kings are evaluated after all other pieces for both sides,
- // because we need complete attack information for all pieces when computing
- // the king safety evaluation.
- evaluate_king<WHITE, HasPopCnt>(pos, ei);
- evaluate_king<BLACK, HasPopCnt>(pos, ei);
-
- // Evaluate tactical threats, we need full attack info including king
- evaluate_threats<WHITE>(pos, ei);
- evaluate_threats<BLACK>(pos, ei);