]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Fix a small bug in king safety
[stockfish] / src / evaluate.cpp
index 4745da0210a921112852c3a3cb69192200660481..c29fd5e651a46bd5a46af414b256f2923f6a029d 100644 (file)
@@ -289,6 +289,7 @@ namespace {
 
   void evaluate_space(const Position &p, Color us, EvalInfo &ei);
   inline Value apply_weight(Value v, int w);
+  Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]);
 
   int count_1s_8bit(Bitboard b);
 
@@ -489,7 +490,10 @@ void init_eval(int threads) {
   }
 
   for (Bitboard b = 0ULL; b < 256ULL; b++)
-      BitCount8Bit[b] = count_1s(b);
+  {
+      assert(count_1s(b) == int(uint8_t(count_1s(b))));
+      BitCount8Bit[b] = (uint8_t)count_1s(b);
+  }
 }
 
 
@@ -527,22 +531,6 @@ void read_weights(Color us) {
 }
 
 
-/// 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(Value mv, Value ev, Phase ph, const ScaleFactor sf[]) {
-
-  assert(mv > -VALUE_INFINITE && mv < VALUE_INFINITE);
-  assert(ev > -VALUE_INFINITE && ev < VALUE_INFINITE);
-  assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME);
-
-  ev = apply_scale_factor(ev, sf[(ev > Value(0) ? WHITE : BLACK)]);
-
-  Value result = Value(int((mv * ph + ev * (128 - ph)) / 128));
-  return Value(int(result) & ~(GrainSize - 1));
-}
-
 namespace {
 
   // evaluate_common() computes terms common to all pieces attack
@@ -641,63 +629,63 @@ namespace {
                 evaluate_trapped_bishop_a1h1(pos, s, us, ei);
         }
 
-        if (Piece != ROOK && Piece != QUEEN)
-            continue;
-
-        // Queen or rook on 7th rank
-        them = opposite_color(us);
-
-        if (   relative_rank(us, s) == RANK_7
-            && relative_rank(us, pos.king_square(them)) == RANK_8)
+        if (Piece == ROOK || Piece == QUEEN)
         {
-            ei.mgValue += Sign[us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
-            ei.egValue += Sign[us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
+            // Queen or rook on 7th rank
+            them = opposite_color(us);
+
+            if (   relative_rank(us, s) == RANK_7
+                && relative_rank(us, pos.king_square(them)) == RANK_8)
+            {
+                ei.mgValue += Sign[us] * (Piece == ROOK ? MidgameRookOn7thBonus : MidgameQueenOn7thBonus);
+                ei.egValue += Sign[us] * (Piece == ROOK ? EndgameRookOn7thBonus : EndgameQueenOn7thBonus);
+            }
         }
 
         // Special extra evaluation for rooks
-        if (Piece != ROOK)
-            continue;
-
-        // Open and half-open files
-        f = square_file(s);
-        if (ei.pi->file_is_half_open(us, f))
+        if (Piece == ROOK)
         {
-            if (ei.pi->file_is_half_open(them, f))
+            // Open and half-open files
+            f = square_file(s);
+            if (ei.pi->file_is_half_open(us, f))
             {
-                ei.mgValue += Sign[us] * RookOpenFileBonus;
-                ei.egValue += Sign[us] * RookOpenFileBonus;
-            }
-            else
-            {
-                ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
-                ei.egValue += Sign[us] * RookHalfOpenFileBonus;
+                if (ei.pi->file_is_half_open(them, f))
+                {
+                    ei.mgValue += Sign[us] * RookOpenFileBonus;
+                    ei.egValue += Sign[us] * RookOpenFileBonus;
+                }
+                else
+                {
+                    ei.mgValue += Sign[us] * RookHalfOpenFileBonus;
+                    ei.egValue += Sign[us] * RookHalfOpenFileBonus;
+                }
             }
-        }
 
-        // Penalize rooks which are trapped inside a king. Penalize more if
-        // king has lost right to castle.
-        if (mob > 6 || ei.pi->file_is_half_open(us, f))
-            continue;
+            // Penalize rooks which are trapped inside a king. Penalize more if
+            // king has lost right to castle.
+            if (mob > 6 || ei.pi->file_is_half_open(us, f))
+                continue;
 
-        ksq = pos.king_square(us);
+            ksq = pos.king_square(us);
 
-        if (    square_file(ksq) >= FILE_E
-            &&  square_file(s) > square_file(ksq)
-            && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
-        {
-            // Is there a half-open file between the king and the edge of the board?
-            if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
-                ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
-                                                : Sign[us] *  (TrappedRookPenalty - mob * 16);
-        }
-        else if (    square_file(ksq) <= FILE_D
-                 &&  square_file(s) < square_file(ksq)
-                 && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
-        {
-            // Is there a half-open file between the king and the edge of the board?
-            if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
-                ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
-                                                : Sign[us] * (TrappedRookPenalty - mob * 16);
+            if (    square_file(ksq) >= FILE_E
+                &&  square_file(s) > square_file(ksq)
+                && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
+            {
+                // Is there a half-open file between the king and the edge of the board?
+                if (!ei.pi->has_open_file_to_right(us, square_file(ksq)))
+                    ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
+                                                    : Sign[us] *  (TrappedRookPenalty - mob * 16);
+            }
+            else if (    square_file(ksq) <= FILE_D
+                    &&  square_file(s) < square_file(ksq)
+                    && (relative_rank(us, ksq) == RANK_1 || square_rank(ksq) == square_rank(s)))
+            {
+                // Is there a half-open file between the king and the edge of the board?
+                if (!ei.pi->has_open_file_to_left(us, square_file(ksq)))
+                    ei.mgValue -= pos.can_castle(us)? Sign[us] * ((TrappedRookPenalty - mob * 16) / 2)
+                                                    : Sign[us] * (TrappedRookPenalty - mob * 16);
+            }
         }
     }
   }
@@ -794,7 +782,7 @@ namespace {
                         if (    bit_is_set(p.piece_attacks<QUEEN>(from), to)
                             && !bit_is_set(p.pinned_pieces(them), from)
                             && !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us))
-                            && !(rook_attacks_bb(to, occ & ClearMaskBB[from]) & p.rooks_and_queens(us)))
+                            && !(bishop_attacks_bb(to, occ & ClearMaskBB[from]) & p.bishops_and_queens(us)))
 
                             ei.mateThreat[them] = make_move(from, to);
                     }
@@ -1159,6 +1147,23 @@ 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.
+
+  Value scale_by_game_phase(Value mv, Value ev, Phase ph, const ScaleFactor sf[]) {
+
+    assert(mv > -VALUE_INFINITE && mv < VALUE_INFINITE);
+    assert(ev > -VALUE_INFINITE && ev < VALUE_INFINITE);
+    assert(ph >= PHASE_ENDGAME && ph <= PHASE_MIDGAME);
+
+    ev = apply_scale_factor(ev, sf[(ev > Value(0) ? WHITE : BLACK)]);
+
+    Value result = Value(int((mv * ph + ev * (128 - ph)) / 128));
+    return Value(int(result) & ~(GrainSize - 1));
+  }
+
+
   // count_1s_8bit() counts the number of nonzero bits in the 8 least
   // significant bits of a Bitboard. This function is used by the king
   // shield evaluation.