]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Prefetch pawn hash key
[stockfish] / src / evaluate.cpp
index d291e46b6c8c21c7816952adf89c311874cbcbc5..375c2369a543e8d392b0e4c138f87af97c069fe2 100644 (file)
@@ -29,7 +29,6 @@
 #include "evaluate.h"
 #include "material.h"
 #include "pawns.h"
-#include "scale.h"
 #include "thread.h"
 #include "ucioption.h"
 
@@ -148,9 +147,6 @@ namespace {
 
   #undef S
 
-  // Bonus for unstoppable passed pawns
-  const Value UnstoppablePawnValue = Value(0x500);
-
   // Rooks and queens on the 7th rank (modified by Joona Kiiski)
   const Score RookOn7thBonus  = make_score(47, 98);
   const Score QueenOn7thBonus = make_score(27, 54);
@@ -233,10 +229,6 @@ namespace {
   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);
@@ -259,7 +251,6 @@ namespace {
   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);
@@ -273,6 +264,14 @@ namespace {
 //// 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.
@@ -338,10 +337,6 @@ Value do_evaluate(const Position& pos, EvalInfo& ei) {
   evaluate_passed_pawns<WHITE>(pos, ei);
   evaluate_passed_pawns<BLACK>(pos, ei);
 
-  // If one side has only a king, check whether exsists any unstoppable passed pawn
-  if (!pos.non_pawn_material(WHITE) || !pos.non_pawn_material(BLACK))
-      evaluate_unstoppable_pawns(pos, ei);
-
   Phase phase = ei.mi->game_phase();
 
   // Middle-game specific evaluation terms
@@ -373,8 +368,8 @@ Value do_evaluate(const Position& pos, EvalInfo& ei) {
   // colored bishop endgames, and use a lower scale for those
   if (   phase < PHASE_MIDGAME
       && pos.opposite_colored_bishops()
-      && (   (factor[WHITE] == SCALE_FACTOR_NORMAL && eg_value(ei.value) > Value(0))
-          || (factor[BLACK] == SCALE_FACTOR_NORMAL && eg_value(ei.value) < Value(0))))
+      && (   (factor[WHITE] == SCALE_FACTOR_NORMAL && eg_value(ei.value) > VALUE_ZERO)
+          || (factor[BLACK] == SCALE_FACTOR_NORMAL && eg_value(ei.value) < VALUE_ZERO)))
   {
       ScaleFactor sf;
 
@@ -421,9 +416,9 @@ void init_eval(int threads) {
         continue;
     }
     if (!PawnTable[i])
-        PawnTable[i] = new PawnInfoTable(PawnTableSize);
+        PawnTable[i] = new PawnInfoTable();
     if (!MaterialTable[i])
-        MaterialTable[i] = new MaterialInfoTable(MaterialTableSize);
+        MaterialTable[i] = new MaterialInfoTable();
   }
 }
 
@@ -635,7 +630,7 @@ namespace {
     const Color Them = (Us == WHITE ? BLACK : WHITE);
 
     Bitboard b;
-    Score bonus = make_score(0, 0);
+    Score bonus = SCORE_ZERO;
 
     // Enemy pieces not defended by a pawn and under our attack
     Bitboard weakEnemies =  pos.pieces_of_color(Them)
@@ -691,15 +686,11 @@ namespace {
 
     Bitboard undefended, b, b1, b2, safe;
     bool sente;
-    int attackUnits, shelter = 0;
+    int attackUnits;
     const Square ksq = pos.king_square(Us);
 
     // King shelter
-    if (relative_rank(Us, ksq) <= RANK_4)
-    {
-        shelter = ei.pi->get_king_shelter(pos, Us, ksq);
-        ei.value += Sign[Us] * make_score(shelter, 0);
-    }
+    ei.value += Sign[Us] * ei.pi->king_shelter(pos, Us, ksq);
 
     // King safety. This is quite complicated, and is almost certainly far
     // from optimally tuned.
@@ -726,7 +717,7 @@ namespace {
         attackUnits =  Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
                      + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s_max_15<HasPopCnt>(undefended))
                      + InitKingDanger[relative_square(Us, ksq)]
-                     - shelter / 32;
+                     - mg_value(ei.pi->king_shelter(pos, Us, ksq)) / 32;
 
         // Analyse enemy's safe queen contact checks. First find undefended
         // squares around the king attacked by enemy queen...
@@ -788,7 +779,7 @@ namespace {
     const Color Them = (Us == WHITE ? BLACK : WHITE);
 
     Bitboard squaresToQueen, defendedSquares, unsafeSquares, supportingPawns;
-    Bitboard b = ei.pi->passed_pawns() & pos.pieces_of_color(Us);
+    Bitboard b = ei.pi->passed_pawns(Us);
 
     while (b)
     {
@@ -822,7 +813,7 @@ namespace {
                 // add all X-ray attacks by the rook or queen. Otherwise consider only
                 // the squares in the pawn's path attacked or occupied by the enemy.
                 if (   (squares_behind(Us, s) & pos.pieces(ROOK, QUEEN, Them))
-                    && (squares_behind(Us, s) & pos.pieces(ROOK, QUEEN, Them) & pos.attacks_from<QUEEN>(s)))
+                    && (squares_behind(Us, s) & pos.pieces(ROOK, QUEEN, Them) & pos.attacks_from<ROOK>(s)))
                     unsafeSquares = squaresToQueen;
                 else
                     unsafeSquares = squaresToQueen & (ei.attacked_by(Them) | pos.pieces_of_color(Them));
@@ -861,8 +852,7 @@ namespace {
         // value if the other side has a rook or queen.
         if (square_file(s) == FILE_A || square_file(s) == FILE_H)
         {
-            if (   pos.non_pawn_material(Them) <= KnightValueMidgame
-                && pos.piece_count(Them, KNIGHT) <= 1)
+            if (pos.non_pawn_material(Them) <= KnightValueMidgame)
                 ebonus += ebonus / 4;
             else if (pos.pieces(ROOK, QUEEN, Them))
                 ebonus -= ebonus / 4;
@@ -875,98 +865,6 @@ namespace {
   }
 
 
-  // evaluate_unstoppable_pawns() evaluates the unstoppable passed pawns for both sides
-
-  void evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei) {
-
-    int movesToGo[2] = {0, 0};
-    Square pawnToGo[2] = {SQ_NONE, SQ_NONE};
-
-    for (Color c = WHITE; c <= BLACK; c++)
-    {
-        // Skip evaluation if other side has non-pawn pieces
-        if (pos.non_pawn_material(opposite_color(c)))
-            continue;
-
-        Bitboard b = ei.pi->passed_pawns() & pos.pieces_of_color(c);
-
-        while (b)
-        {
-            Square s = pop_1st_bit(&b);
-            Square queeningSquare = relative_square(c, make_square(square_file(s), RANK_8));
-            int d =  square_distance(s, queeningSquare)
-                   - (relative_rank(c, s) == RANK_2) // Double pawn push
-                   - square_distance(pos.king_square(opposite_color(c)), queeningSquare)
-                   + int(c != pos.side_to_move());
-
-            // Do we protect the path to queening ?
-            bool pathDefended = (ei.attacked_by(c) & squares_in_front_of(c, s)) == squares_in_front_of(c, s);
-
-            if (d < 0 || pathDefended)
-            {
-                int mtg = RANK_8 - relative_rank(c, s);
-                int blockerCount = count_1s_max_15(squares_in_front_of(c, s) & pos.occupied_squares());
-                mtg += blockerCount;
-                d += blockerCount;
-                if ((d < 0 || pathDefended) && (!movesToGo[c] || movesToGo[c] > mtg))
-                {
-                    movesToGo[c] = mtg;
-                    pawnToGo[c] = s;
-                }
-            }
-        }
-    }
-
-    // Neither side has an unstoppable passed pawn?
-    if (!(movesToGo[WHITE] | movesToGo[BLACK]))
-        return;
-
-    // Does only one side have an unstoppable passed pawn?
-    if (!movesToGo[WHITE] || !movesToGo[BLACK])
-    {
-        Color winnerSide = movesToGo[WHITE] ? WHITE : BLACK;
-        ei.value += make_score(0, Sign[winnerSide] * (UnstoppablePawnValue - Value(0x40 * movesToGo[winnerSide])));
-    }
-    else
-    {   // Both sides have unstoppable pawns! Try to find out who queens
-        // first. We begin by transforming 'movesToGo' to the number of
-        // plies until the pawn queens for both sides.
-        movesToGo[WHITE] *= 2;
-        movesToGo[BLACK] *= 2;
-        movesToGo[pos.side_to_move()]--;
-
-        Color winnerSide = movesToGo[WHITE] < movesToGo[BLACK] ? WHITE : BLACK;
-        Color loserSide = opposite_color(winnerSide);
-
-        // If one side queens at least three plies before the other, that side wins
-        if (movesToGo[winnerSide] <= movesToGo[loserSide] - 3)
-            ei.value += Sign[winnerSide] * make_score(0, UnstoppablePawnValue - Value(0x40 * (movesToGo[winnerSide]/2)));
-
-        // If one side queens one ply before the other and checks the king or attacks
-        // the undefended opponent's queening square, that side wins. To avoid cases
-        // where the opponent's king could move somewhere before first pawn queens we
-        // consider only free paths to queen for both pawns.
-        else if (   !(squares_in_front_of(WHITE, pawnToGo[WHITE]) & pos.occupied_squares())
-                 && !(squares_in_front_of(BLACK, pawnToGo[BLACK]) & pos.occupied_squares()))
-        {
-            assert(movesToGo[loserSide] - movesToGo[winnerSide] == 1);
-
-            Square winnerQSq = relative_square(winnerSide, make_square(square_file(pawnToGo[winnerSide]), RANK_8));
-            Square loserQSq = relative_square(loserSide, make_square(square_file(pawnToGo[loserSide]), RANK_8));
-
-            Bitboard b = pos.occupied_squares();
-            clear_bit(&b, pawnToGo[winnerSide]);
-            clear_bit(&b, pawnToGo[loserSide]);
-            b = queen_attacks_bb(winnerQSq, b);
-
-            if (  (b & pos.pieces(KING, loserSide))
-                ||(bit_is_set(b, loserQSq) && !bit_is_set(ei.attacked_by(loserSide), loserQSq)))
-                ei.value += Sign[winnerSide] * make_score(0, UnstoppablePawnValue - Value(0x40 * (movesToGo[winnerSide]/2)));
-        }
-    }
-  }
-
-
   // evaluate_trapped_bishop_a7h7() determines whether a bishop on a7/h7
   // (a2/h2 for black) is trapped by enemy pawns, and assigns a penalty
   // if it is.
@@ -1066,9 +964,8 @@ namespace {
   }
 
 
-  // scale_by_game_phase() interpolates between a middle game and an endgame
-  // score, based on game phase.  It also scales the return value by a
-  // ScaleFactor array.
+  // scale_by_game_phase() interpolates between a middle game and an endgame score,
+  // based on game phase. It also scales the return value by a ScaleFactor array.
 
   Value scale_by_game_phase(const Score& v, Phase ph, const ScaleFactor sf[]) {
 
@@ -1076,9 +973,11 @@ namespace {
     assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE);
     assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME);
 
-    Value ev = apply_scale_factor(eg_value(v), sf[(eg_value(v) > Value(0) ? WHITE : BLACK)]);
+    Value eg = eg_value(v);
+    ScaleFactor f = sf[eg > VALUE_ZERO ? WHITE : BLACK];
+    Value ev = Value((eg * int(f)) / SCALE_FACTOR_NORMAL);
 
-    int result = (mg_value(v) * ph + ev * (128 - ph)) / 128;
+    int result = (mg_value(v) * int(ph) + ev * int(128 - ph)) / 128;
     return Value(result & ~(GrainSize - 1));
   }