]> git.sesse.net Git - stockfish/blobdiff - src/evaluate.cpp
Cache king shelter info in pawns structure
[stockfish] / src / evaluate.cpp
index c397671cb408d53293b76967260b2d292591f6f1..88ab5ff67f5ef079b76bd1d68fa2c95c4009ec9d 100644 (file)
@@ -1,7 +1,7 @@
 /*
   Stockfish, a UCI chess playing engine derived from Glaurung 2.1
   Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
-  Copyright (C) 2008 Marco Costalba
+  Copyright (C) 2008-2009 Marco Costalba
 
   Stockfish is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -490,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);
+  }
 }
 
 
@@ -502,6 +505,8 @@ void quit_eval() {
   {
       delete PawnTable[i];
       delete MaterialTable[i];
+      PawnTable[i] = NULL;
+      MaterialTable[i] = NULL;
   }
 }
 
@@ -566,25 +571,25 @@ namespace {
     ei.egMobility += Sign[us] * EgBonus[Piece][mob];
 
     // Bishop and Knight outposts
-    if (  (Piece != BISHOP && Piece != KNIGHT) // compile time condition
-        || !p.square_is_weak(s, them))
-        return mob;
-
-    // Initial bonus based on square
-    Value v, bonus;
-    v = bonus = OutpostBonus[Piece][relative_square(us, s)];
-
-    // Increase bonus if supported by pawn, especially if the opponent has
-    // no minor piece which can exchange the outpost piece
-    if (v && (p.pawn_attacks(them, s) & p.pawns(us)))
+    if (   (Piece == BISHOP || Piece == KNIGHT) // compile time condition
+        && p.square_is_weak(s, them))
     {
-        bonus += v / 2;
-        if (   p.piece_count(them, KNIGHT) == 0
-            && (SquaresByColorBB[square_color(s)] & p.bishops(them)) == EmptyBoardBB)
-            bonus += v;
+        // Initial bonus based on square
+        Value v, bonus;
+        v = bonus = OutpostBonus[Piece][relative_square(us, s)];
+
+        // Increase bonus if supported by pawn, especially if the opponent has
+        // no minor piece which can exchange the outpost piece
+        if (v && (p.pawn_attacks(them, s) & p.pawns(us)))
+        {
+            bonus += v / 2;
+            if (   p.piece_count(them, KNIGHT) == 0
+                   && (SquaresByColorBB[square_color(s)] & p.bishops(them)) == EmptyBoardBB)
+                bonus += v;
+        }
+        ei.mgValue += Sign[us] * bonus;
+        ei.egValue += Sign[us] * bonus;
     }
-    ei.mgValue += Sign[us] * bonus;
-    ei.egValue += Sign[us] * bonus;
     return mob;
   }
 
@@ -626,63 +631,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))
-            {
-                ei.mgValue += Sign[us] * RookOpenFileBonus;
-                ei.egValue += Sign[us] * RookOpenFileBonus;
-            }
-            else
+            // Open and half-open files
+            f = square_file(s);
+            if (ei.pi->file_is_half_open(us, f))
             {
-                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);
+            }
         }
     }
   }
@@ -704,11 +709,19 @@ namespace {
     // King shelter
     if (relative_rank(us, s) <= RANK_4)
     {
-        Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
-        Rank r = square_rank(s);
-        for (int i = 1; i < 4; i++)
-            shelter += count_1s_8bit(shiftRowsDown(pawns, r+i*sign)) * (128>>i);
-
+        // Shelter cache lookup
+        shelter = ei.pi->kingShelter(us, s);
+        if (shelter == -1)
+        {
+            shelter = 0;
+            Bitboard pawns = p.pawns(us) & this_and_neighboring_files_bb(s);
+            Rank r = square_rank(s);
+            for (int i = 1; i < 4; i++)
+                shelter += count_1s_8bit(shiftRowsDown(pawns, r+i*sign)) * (128>>i);
+
+            // Cache shelter value in pawn info
+            ei.pi->setKingShelter(us, s, shelter);
+        }
         ei.mgValue += sign * Value(shelter);
     }
 
@@ -779,7 +792,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);
                     }