-/// 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);
- }
-
- // 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);
-
- // If one side has only a king, check whether exists any unstoppable passed pawn
- if (!pos.non_pawn_material(WHITE) || !pos.non_pawn_material(BLACK))
- bonus += evaluate_unstoppable_pawns<HasPopCnt>(pos, ei);
-
- // Evaluate space for both sides, only in middle-game.
- if (mi->space_weight())
- {
- 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]);
- }
-
- // Scale winning side if position is more drawish that what it appears
- ScaleFactor sf = eg_value(bonus) > VALUE_DRAW ? mi->scale_factor(pos, WHITE)
- : mi->scale_factor(pos, BLACK);
- Phase phase = mi->game_phase();
-
- // If we don't already have an unusual scale factor, check for opposite
- // colored bishop endgames, and use a lower scale for those.
- if ( phase < PHASE_MIDGAME
- && pos.opposite_colored_bishops()
- && sf == SCALE_FACTOR_NORMAL)
- {
- // Only the two bishops ?
- if ( pos.non_pawn_material(WHITE) == BishopValueMidgame
- && pos.non_pawn_material(BLACK) == BishopValueMidgame)
- {
- // Check for KBP vs KB with only a single pawn that is almost
- // certainly a draw or at least two pawns.
- bool one_pawn = (pos.piece_count(WHITE, PAWN) + pos.piece_count(BLACK, PAWN) == 1);
- sf = one_pawn ? ScaleFactor(8) : ScaleFactor(32);
- }
- else
- // Endgame with opposite-colored bishops, but also other pieces. Still
- // a bit drawish, but not as drawish as with only the two bishops.
- sf = ScaleFactor(50);