]> git.sesse.net Git - stockfish/commitdiff
Position: Unify and templetize mg_pst() and eg_pst()
authorMarco Costalba <mcostalba@gmail.com>
Fri, 20 Feb 2009 11:21:52 +0000 (12:21 +0100)
committerMarco Costalba <mcostalba@gmail.com>
Fri, 20 Feb 2009 21:50:35 +0000 (22:50 +0100)
Also templetize compute_value() can be simpler now that
the above is templetized too.

No functional change.

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

index d5f28218ea8aa53c53846a02516157edc8e0b2ae..435384cf93740bea491d47e4f3112e39dd5abe8d 100644 (file)
@@ -210,8 +210,8 @@ void Position::from_fen(const std::string& fen) {
   key = compute_key();
   pawnKey = compute_pawn_key();
   materialKey = compute_material_key();
   key = compute_key();
   pawnKey = compute_pawn_key();
   materialKey = compute_material_key();
-  mgValue = compute_value(MidGame);
-  egValue = compute_value(EndGame);
+  mgValue = compute_value<MidGame>();
+  egValue = compute_value<EndGame>();
   npMaterial[WHITE] = compute_non_pawn_material(WHITE);
   npMaterial[BLACK] = compute_non_pawn_material(BLACK);
 }
   npMaterial[WHITE] = compute_non_pawn_material(WHITE);
   npMaterial[BLACK] = compute_non_pawn_material(BLACK);
 }
@@ -758,10 +758,10 @@ void Position::do_move(Move m, UndoInfo& u) {
     key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
 
     // Update incremental scores
     key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
 
     // Update incremental scores
-    mgValue -= mg_pst(us, piece, from);
-    mgValue += mg_pst(us, piece, to);
-    egValue -= eg_pst(us, piece, from);
-    egValue += eg_pst(us, piece, to);
+    mgValue -= pst<MidGame>(us, piece, from);
+    mgValue += pst<MidGame>(us, piece, to);
+    egValue -= pst<EndGame>(us, piece, from);
+    egValue += pst<EndGame>(us, piece, to);
 
     // If the moving piece was a king, update the king square
     if (piece == KING)
 
     // If the moving piece was a king, update the king square
     if (piece == KING)
@@ -851,8 +851,8 @@ void Position::do_capture_move(Move m, PieceType capture, Color them, Square to)
         pawnKey ^= zobrist[them][PAWN][to];
 
     // Update incremental scores
         pawnKey ^= zobrist[them][PAWN][to];
 
     // Update incremental scores
-    mgValue -= mg_pst(them, capture, to);
-    egValue -= eg_pst(them, capture, to);
+    mgValue -= pst<MidGame>(them, capture, to);
+    egValue -= pst<EndGame>(them, capture, to);
 
     assert(!move_promotion(m) || capture != PAWN);
 
 
     assert(!move_promotion(m) || capture != PAWN);
 
@@ -939,14 +939,14 @@ void Position::do_castle_move(Move m) {
   index[rto] = tmp;
 
   // Update incremental scores
   index[rto] = tmp;
 
   // Update incremental scores
-  mgValue -= mg_pst(us, KING, kfrom);
-  mgValue += mg_pst(us, KING, kto);
-  egValue -= eg_pst(us, KING, kfrom);
-  egValue += eg_pst(us, KING, kto);
-  mgValue -= mg_pst(us, ROOK, rfrom);
-  mgValue += mg_pst(us, ROOK, rto);
-  egValue -= eg_pst(us, ROOK, rfrom);
-  egValue += eg_pst(us, ROOK, rto);
+  mgValue -= pst<MidGame>(us, KING, kfrom);
+  mgValue += pst<MidGame>(us, KING, kto);
+  egValue -= pst<EndGame>(us, KING, kfrom);
+  egValue += pst<EndGame>(us, KING, kto);
+  mgValue -= pst<MidGame>(us, ROOK, rfrom);
+  mgValue += pst<MidGame>(us, ROOK, rto);
+  egValue -= pst<EndGame>(us, ROOK, rfrom);
+  egValue += pst<EndGame>(us, ROOK, rto);
 
   // Update hash key
   key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
 
   // Update hash key
   key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
@@ -1039,10 +1039,10 @@ void Position::do_promotion_move(Move m, UndoInfo &u) {
   index[to] = pieceCount[us][promotion] - 1;
 
   // Update incremental scores
   index[to] = pieceCount[us][promotion] - 1;
 
   // Update incremental scores
-  mgValue -= mg_pst(us, PAWN, from);
-  mgValue += mg_pst(us, promotion, to);
-  egValue -= eg_pst(us, PAWN, from);
-  egValue += eg_pst(us, promotion, to);
+  mgValue -= pst<MidGame>(us, PAWN, from);
+  mgValue += pst<MidGame>(us, promotion, to);
+  egValue -= pst<EndGame>(us, PAWN, from);
+  egValue += pst<EndGame>(us, promotion, to);
 
   // Update material
   npMaterial[us] += piece_value_midgame(promotion);
 
   // Update material
   npMaterial[us] += piece_value_midgame(promotion);
@@ -1133,12 +1133,12 @@ void Position::do_ep_move(Move m) {
   pawnKey ^= zobrist[them][PAWN][capsq];
 
   // Update incremental scores
   pawnKey ^= zobrist[them][PAWN][capsq];
 
   // Update incremental scores
-  mgValue -= mg_pst(them, PAWN, capsq);
-  mgValue -= mg_pst(us, PAWN, from);
-  mgValue += mg_pst(us, PAWN, to);
-  egValue -= eg_pst(them, PAWN, capsq);
-  egValue -= eg_pst(us, PAWN, from);
-  egValue += eg_pst(us, PAWN, to);
+  mgValue -= pst<MidGame>(them, PAWN, capsq);
+  mgValue -= pst<MidGame>(us, PAWN, from);
+  mgValue += pst<MidGame>(us, PAWN, to);
+  egValue -= pst<EndGame>(them, PAWN, capsq);
+  egValue -= pst<EndGame>(us, PAWN, from);
+  egValue += pst<EndGame>(us, PAWN, to);
 
   // Reset en passant square
   epSquare = SQ_NONE;
 
   // Reset en passant square
   epSquare = SQ_NONE;
@@ -1811,13 +1811,12 @@ Key Position::compute_material_key() const {
 }
 
 
 }
 
 
-/// Position::compute_mg_value() and Position::compute_eg_value() compute the
-/// incremental scores for the middle game and the endgame. These functions
-/// are used to initialize the incremental scores when a new position is set
-/// up, and to verify that the scores are correctly updated by do_move
-/// and undo_move when the program is running in debug mode.
-
-Value Position::compute_value(GamePhase p) const {
+/// Position::compute_value() compute the incremental scores for the middle
+/// game and the endgame. These functions are used to initialize the incremental
+/// scores when a new position is set up, and to verify that the scores are correctly
+/// updated by do_move and undo_move when the program is running in debug mode.
+template<Position::GamePhase Phase>
+Value Position::compute_value() const {
 
   Value result = Value(0);
   Bitboard b;
 
   Value result = Value(0);
   Bitboard b;
@@ -1831,11 +1830,11 @@ Value Position::compute_value(GamePhase p) const {
           {
               s = pop_1st_bit(&b);
               assert(piece_on(s) == piece_of_color_and_type(c, pt));
           {
               s = pop_1st_bit(&b);
               assert(piece_on(s) == piece_of_color_and_type(c, pt));
-              result += (p == MidGame ? mg_pst(c, pt, s) : eg_pst(c, pt, s));
+              result += pst<Phase>(c, pt, s);
           }
       }
 
           }
       }
 
-  const Value TempoValue = (p == MidGame ? TempoValueMidgame : TempoValueEndgame);
+  const Value TempoValue = (Phase == MidGame ? TempoValueMidgame : TempoValueEndgame);
   result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2;
   return result;
 }
   result += (side_to_move() == WHITE)? TempoValue / 2 : -TempoValue / 2;
   return result;
 }
@@ -2057,8 +2056,8 @@ void Position::flipped_copy(const Position &pos) {
   materialKey = compute_material_key();
 
   // Incremental scores
   materialKey = compute_material_key();
 
   // Incremental scores
-  mgValue = compute_value(MidGame);
-  egValue = compute_value(EndGame);
+  mgValue = compute_value<MidGame>();
+  egValue = compute_value<EndGame>();
 
   // Material
   npMaterial[WHITE] = compute_non_pawn_material(WHITE);
 
   // Material
   npMaterial[WHITE] = compute_non_pawn_material(WHITE);
@@ -2187,10 +2186,10 @@ bool Position::is_ok(int* failedStep) const {
   if (failedStep) (*failedStep)++;
   if (debugIncrementalEval)
   {
   if (failedStep) (*failedStep)++;
   if (debugIncrementalEval)
   {
-      if (mgValue != compute_value(MidGame))
+      if (mgValue != compute_value<MidGame>())
           return false;
 
           return false;
 
-      if (egValue != compute_value(EndGame))
+      if (egValue != compute_value<EndGame>())
           return false;
   }
 
           return false;
   }
 
index 11b9e2de13991e75fc2d94c28d5123dfded556dd..f3bb011274e04616809c4b82018047c19d17c4d8 100644 (file)
@@ -319,9 +319,8 @@ private:
       MidGame,
       EndGame
   };
       MidGame,
       EndGame
   };
-  Value mg_pst(Color c, PieceType pt, Square s) const;
-  Value eg_pst(Color c, PieceType pt, Square s) const;
-  Value compute_value(GamePhase p) const;
+  template<GamePhase> Value pst(Color c, PieceType pt, Square s) const;
+  template<GamePhase> Value compute_value() const;
   Value compute_non_pawn_material(Color c) const;
 
   // Bitboards
   Value compute_non_pawn_material(Color c) const;
 
   // Bitboards
@@ -633,8 +632,10 @@ inline Key Position::get_material_key() const {
   return materialKey;
 }
 
   return materialKey;
 }
 
-inline Value Position::mg_pst(Color c, PieceType pt, Square s) const {
-  return MgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
+template<Position::GamePhase Phase>
+inline Value Position::pst(Color c, PieceType pt, Square s) const {
+  return (Phase == MidGame ? MgPieceSquareTable[piece_of_color_and_type(c, pt)][s]
+                           : EgPieceSquareTable[piece_of_color_and_type(c, pt)][s]);
 }
 
 inline Value Position::mg_pst_delta(Move m) const {
 }
 
 inline Value Position::mg_pst_delta(Move m) const {
@@ -642,10 +643,6 @@ inline Value Position::mg_pst_delta(Move m) const {
         -MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
 }
 
         -MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
 }
 
-inline Value Position::eg_pst(Color c, PieceType pt, Square s) const {
-  return EgPieceSquareTable[piece_of_color_and_type(c, pt)][s];
-}
-
 inline Value Position::mg_value() const {
   return mgValue;
 }
 inline Value Position::mg_value() const {
   return mgValue;
 }