]> git.sesse.net Git - stockfish/commitdiff
Speed up polynomial material imbalance loop
authorMarco Costalba <mcostalba@gmail.com>
Sat, 8 Aug 2009 12:06:50 +0000 (13:06 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 8 Aug 2009 13:12:04 +0000 (14:12 +0100)
Access pos.piece_count() only once and avoid some
branches in the inner loop.

Profiling with VTune shows a 20% speed improvement in
get_material_info(), and it is also a bit more cleaned
up this way ;-)

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
src/material.cpp

index a8ecb51a5a3377a6a7153951e4e13c53e89c528a..27ea5923b492fc9caeb548a29e8dab6c1369da91 100644 (file)
@@ -263,8 +263,10 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
   }
 
   // Evaluate the material balance
   }
 
   // Evaluate the material balance
-
-  const int bishopsPair_count[2] = { pos.piece_count(WHITE, BISHOP) > 1, pos.piece_count(BLACK, BISHOP) > 1 };
+  const int pieceCount[2][6] = { { pos.piece_count(WHITE, BISHOP) > 1, pos.piece_count(WHITE, PAWN), pos.piece_count(WHITE, KNIGHT),
+                                   pos.piece_count(WHITE, BISHOP), pos.piece_count(WHITE, ROOK), pos.piece_count(WHITE, QUEEN) },
+                                 { pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT),
+                                   pos.piece_count(BLACK, BISHOP), pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
   Color c, them;
   int sign;
   int matValue = 0;
   Color c, them;
   int sign;
   int matValue = 0;
@@ -297,29 +299,26 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) {
     // Redundancy of major pieces, formula based on Kaufman's paper
     // "The Evaluation of Material Imbalances in Chess"
     // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
     // Redundancy of major pieces, formula based on Kaufman's paper
     // "The Evaluation of Material Imbalances in Chess"
     // http://mywebpages.comcast.net/danheisman/Articles/evaluation_of_material_imbalance.htm
-    if (pos.piece_count(c, ROOK) >= 1)
-        matValue -= sign * ((pos.piece_count(c, ROOK) - 1) * RedundantRookPenalty + pos.piece_count(c, QUEEN) * RedundantQueenPenalty);
+    if (pieceCount[c][ROOK] >= 1)
+        matValue -= sign * ((pieceCount[c][ROOK] - 1) * RedundantRookPenalty + pieceCount[c][QUEEN] * RedundantQueenPenalty);
 
     // Second-degree polynomial material imbalance by Tord Romstad
     //
     // We use NO_PIECE_TYPE as a place holder for the bishop pair "extended piece",
     // this allow us to be more flexible in defining bishop pair bonuses.
     them = opposite_color(c);
 
     // Second-degree polynomial material imbalance by Tord Romstad
     //
     // We use NO_PIECE_TYPE as a place holder for the bishop pair "extended piece",
     // this allow us to be more flexible in defining bishop pair bonuses.
     them = opposite_color(c);
-    for (PieceType pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; pt1++)
+    for (int pt1 = NO_PIECE_TYPE; pt1 <= QUEEN; pt1++)
     {
     {
-        int c1, c2, c3;
-        c1 = sign * (pt1 != NO_PIECE_TYPE ? pos.piece_count(c, pt1) : bishopsPair_count[c]);
+        int c1 = sign * pieceCount[c][pt1];
         if (!c1)
             continue;
 
         matValue += c1 * LinearCoefficients[pt1];
 
         if (!c1)
             continue;
 
         matValue += c1 * LinearCoefficients[pt1];
 
-        for (PieceType pt2 = NO_PIECE_TYPE; pt2 <= pt1; pt2++)
+        for (int pt2 = NO_PIECE_TYPE; pt2 <= pt1; pt2++)
         {
         {
-            c2 = (pt2 != NO_PIECE_TYPE ? pos.piece_count(c,    pt2) : bishopsPair_count[c]);
-            c3 = (pt2 != NO_PIECE_TYPE ? pos.piece_count(them, pt2) : bishopsPair_count[them]);
-            matValue += c1 * c2 * QuadraticCoefficientsSameColor[pt1][pt2];
-            matValue += c1 * c3 * QuadraticCoefficientsOppositeColor[pt1][pt2];
+            matValue += c1 * pieceCount[c][pt2] * QuadraticCoefficientsSameColor[pt1][pt2];
+            matValue += c1 * pieceCount[them][pt2] * QuadraticCoefficientsOppositeColor[pt1][pt2];
         }
     }
   }
         }
     }
   }