]> git.sesse.net Git - stockfish/commitdiff
Explicitly use delta psqt values when possible
authorMarco Costalba <mcostalba@gmail.com>
Sun, 28 Jun 2009 03:56:58 +0000 (05:56 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sun, 28 Jun 2009 04:30:13 +0000 (06:30 +0200)
Instead of add and subtract pqst values corrisponding to
the move starting and destination squares, do it in one
go with the helper function pst_delta<>()

This simplifies the code and also better documents that what
we need is a delta value, not an absolute one.

No functional change.

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

index cea4efb4bebd1a00deb723e4920d7306db09aa42..22845ba2a2a86f3f3cfb86c9ed0428fee21de582 100644 (file)
@@ -268,20 +268,23 @@ void MovePicker::score_noncaptures() {
   // First score by history, when no history is available then use
   // piece/square tables values. This seems to be better then a
   // random choice when we don't have an history for any move.
   // First score by history, when no history is available then use
   // piece/square tables values. This seems to be better then a
   // random choice when we don't have an history for any move.
-  Move m;
+  Piece piece;
+  Square from, to;
   int hs;
 
   for (int i = 0; i < numOfMoves; i++)
   {
   int hs;
 
   for (int i = 0; i < numOfMoves; i++)
   {
-      m = moves[i].move;
-      hs = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
+      from = move_from(moves[i].move);
+      to = move_to(moves[i].move);
+      piece = pos.piece_on(from);
+      hs = H.move_ordering_score(piece, to);
 
       // Ensure history is always preferred to pst
       if (hs > 0)
           hs += 1000;
 
       // pst based scoring
 
       // Ensure history is always preferred to pst
       if (hs > 0)
           hs += 1000;
 
       // pst based scoring
-      moves[i].score = hs + pos.mg_pst_delta(m);
+      moves[i].score = hs + pos.pst_delta<Position::MidGame>(piece, from, to);
   }
 }
 
   }
 }
 
index e7a5732bb9d3f49b8dd201173a7029211d99518d..1179f6f98a8aa61733cb81df7cdecae906c4a042 100644 (file)
@@ -735,7 +735,8 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
     assert(color_of_piece_on(from) == us);
     assert(color_of_piece_on(to) == them || piece_on(to) == EMPTY);
 
     assert(color_of_piece_on(from) == us);
     assert(color_of_piece_on(to) == them || piece_on(to) == EMPTY);
 
-    PieceType piece = type_of_piece_on(from);
+    Piece piece = piece_on(from);
+    PieceType pt = type_of_piece(piece);
 
     st->capture = type_of_piece_on(to);
 
 
     st->capture = type_of_piece_on(to);
 
@@ -745,23 +746,21 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
     // Move the piece
     Bitboard move_bb = make_move_bb(from, to);
     do_move_bb(&(byColorBB[us]), move_bb);
     // Move the piece
     Bitboard move_bb = make_move_bb(from, to);
     do_move_bb(&(byColorBB[us]), move_bb);
-    do_move_bb(&(byTypeBB[piece]), move_bb);
+    do_move_bb(&(byTypeBB[pt]), move_bb);
     do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
 
     board[to] = board[from];
     board[from] = EMPTY;
 
     // Update hash key
     do_move_bb(&(byTypeBB[0]), move_bb); // HACK: byTypeBB[0] == occupied squares
 
     board[to] = board[from];
     board[from] = EMPTY;
 
     // Update hash key
-    st->key ^= zobrist[us][piece][from] ^ zobrist[us][piece][to];
+    st->key ^= zobrist[us][pt][from] ^ zobrist[us][pt][to];
 
     // Update incremental scores
 
     // Update incremental scores
-    st->mgValue -= pst<MidGame>(us, piece, from);
-    st->mgValue += pst<MidGame>(us, piece, to);
-    st->egValue -= pst<EndGame>(us, piece, from);
-    st->egValue += pst<EndGame>(us, piece, to);
+    st->mgValue += pst_delta<MidGame>(piece, from, to);
+    st->egValue += pst_delta<EndGame>(piece, from, to);
 
     // If the moving piece was a king, update the king square
 
     // If the moving piece was a king, update the king square
-    if (piece == KING)
+    if (pt == KING)
         kingSquare[us] = to;
 
     // Reset en passant square
         kingSquare[us] = to;
 
     // Reset en passant square
@@ -772,7 +771,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
     }
 
     // If the moving piece was a pawn do some special extra work
     }
 
     // If the moving piece was a pawn do some special extra work
-    if (piece == PAWN)
+    if (pt == PAWN)
     {
         // Reset rule 50 draw counter
         st->rule50 = 0;
     {
         // Reset rule 50 draw counter
         st->rule50 = 0;
@@ -793,7 +792,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
     }
 
     // Update piece lists
     }
 
     // Update piece lists
-    pieceList[us][piece][index[from]] = to;
+    pieceList[us][pt][index[from]] = to;
     index[to] = index[from];
 
     // Update castle rights, try to shortcut a common case
     index[to] = index[from];
 
     // Update castle rights, try to shortcut a common case
@@ -808,7 +807,7 @@ void Position::do_move(Move m, StateInfo& newSt, Bitboard dcCandidates) {
     // Update checkers bitboard, piece must be already moved
     st->checkersBB = EmptyBoardBB;
     Square ksq = king_square(them);
     // Update checkers bitboard, piece must be already moved
     st->checkersBB = EmptyBoardBB;
     Square ksq = king_square(them);
-    switch (piece)
+    switch (pt)
     {
     case PAWN:   update_checkers<PAWN>(&(st->checkersBB), ksq, from, to, dcCandidates);   break;
     case KNIGHT: update_checkers<KNIGHT>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
     {
     case PAWN:   update_checkers<PAWN>(&(st->checkersBB), ksq, from, to, dcCandidates);   break;
     case KNIGHT: update_checkers<KNIGHT>(&(st->checkersBB), ksq, from, to, dcCandidates); break;
@@ -923,9 +922,11 @@ void Position::do_castle_move(Move m) {
   set_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
 
   // Update board array
   set_bit(&(byTypeBB[0]), rto); // HACK: byTypeBB[0] == occupied squares
 
   // Update board array
+  Piece king = piece_of_color_and_type(us, KING);
+  Piece rook = piece_of_color_and_type(us, ROOK);
   board[kfrom] = board[rfrom] = EMPTY;
   board[kfrom] = board[rfrom] = EMPTY;
-  board[kto] = piece_of_color_and_type(us, KING);
-  board[rto] = piece_of_color_and_type(us, ROOK);
+  board[kto] = king;
+  board[rto] = rook;
 
   // Update king square
   kingSquare[us] = kto;
 
   // Update king square
   kingSquare[us] = kto;
@@ -938,14 +939,10 @@ void Position::do_castle_move(Move m) {
   index[rto] = tmp;
 
   // Update incremental scores
   index[rto] = tmp;
 
   // Update incremental scores
-  st->mgValue -= pst<MidGame>(us, KING, kfrom);
-  st->mgValue += pst<MidGame>(us, KING, kto);
-  st->egValue -= pst<EndGame>(us, KING, kfrom);
-  st->egValue += pst<EndGame>(us, KING, kto);
-  st->mgValue -= pst<MidGame>(us, ROOK, rfrom);
-  st->mgValue += pst<MidGame>(us, ROOK, rto);
-  st->egValue -= pst<EndGame>(us, ROOK, rfrom);
-  st->egValue += pst<EndGame>(us, ROOK, rto);
+  st->mgValue += pst_delta<MidGame>(king, kfrom, kto);
+  st->egValue += pst_delta<EndGame>(king, kfrom, kto);
+  st->mgValue += pst_delta<MidGame>(rook, rfrom, rto);
+  st->egValue += pst_delta<EndGame>(rook, rfrom, rto);
 
   // Update hash key
   st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
 
   // Update hash key
   st->key ^= zobrist[us][KING][kfrom] ^ zobrist[us][KING][kto];
@@ -1121,12 +1118,11 @@ void Position::do_ep_move(Move m) {
   st->pawnKey ^= zobrist[them][PAWN][capsq];
 
   // Update incremental scores
   st->pawnKey ^= zobrist[them][PAWN][capsq];
 
   // Update incremental scores
+  Piece pawn = piece_of_color_and_type(us, PAWN);
+  st->mgValue += pst_delta<MidGame>(pawn, from, to);
+  st->egValue += pst_delta<EndGame>(pawn, from, to);
   st->mgValue -= pst<MidGame>(them, PAWN, capsq);
   st->mgValue -= pst<MidGame>(them, PAWN, capsq);
-  st->mgValue -= pst<MidGame>(us, PAWN, from);
-  st->mgValue += pst<MidGame>(us, PAWN, to);
   st->egValue -= pst<EndGame>(them, PAWN, capsq);
   st->egValue -= pst<EndGame>(them, PAWN, capsq);
-  st->egValue -= pst<EndGame>(us, PAWN, from);
-  st->egValue += pst<EndGame>(us, PAWN, to);
 
   // Reset en passant square
   st->epSquare = SQ_NONE;
 
   // Reset en passant square
   st->epSquare = SQ_NONE;
index 9737d7614981756382ab2fc150f149fa24f95c2b..9730d3dc24b253d276fdb8ffe2e0771cc91a8087 100644 (file)
@@ -127,6 +127,11 @@ class Position {
   friend class EndgameFunctions;
 
 public:
   friend class EndgameFunctions;
 
 public:
+  enum GamePhase {
+      MidGame,
+      EndGame
+  };
+
   // Constructors
   Position() {};
   Position(const Position& pos);
   // Constructors
   Position() {};
   Position(const Position& pos);
@@ -272,7 +277,7 @@ public:
   Value eg_value() const;
   Value non_pawn_material(Color c) const;
   Phase game_phase() const;
   Value eg_value() const;
   Value non_pawn_material(Color c) const;
   Phase game_phase() const;
-  Value mg_pst_delta(Move m) const;
+  template<GamePhase> Value pst_delta(Piece piece, Square from, Square to) const;
 
   // Game termination checks
   bool is_mate() const;
 
   // Game termination checks
   bool is_mate() const;
@@ -328,10 +333,6 @@ private:
   Key compute_material_key() const;
 
   // Computing incremental evaluation scores and material counts
   Key compute_material_key() const;
 
   // Computing incremental evaluation scores and material counts
-  enum GamePhase {
-      MidGame,
-      EndGame
-  };
   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;
   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;
@@ -622,15 +623,16 @@ inline Key Position::get_material_key() const {
   return st->materialKey;
 }
 
   return st->materialKey;
 }
 
-template<Position::GamePhase Phase>
+template<Position::GamePhase Ph>
 inline Value Position::pst(Color c, PieceType pt, Square s) const {
 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]);
+  return (Ph == 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 {
-  return MgPieceSquareTable[piece_on(move_from(m))][move_to(m)]
-        -MgPieceSquareTable[piece_on(move_from(m))][move_from(m)];
+template<Position::GamePhase Ph>
+inline Value Position::pst_delta(Piece piece, Square from, Square to) const {
+  return (Ph == MidGame ? MgPieceSquareTable[piece][to] - MgPieceSquareTable[piece][from]
+                        : EgPieceSquareTable[piece][to] - EgPieceSquareTable[piece][from]);
 }
 
 inline Value Position::mg_value() const {
 }
 
 inline Value Position::mg_value() const {