Generalize incremental computation for hand pieces
authorFabian Fichter <ianfab@users.noreply.github.com>
Sun, 29 Aug 2021 09:16:53 +0000 (11:16 +0200)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sun, 29 Aug 2021 21:46:23 +0000 (23:46 +0200)
This allows to apply incremental NNUE computation not only to drop variants,
but also to S-Chess gating and drop variants with blast captures.

src/nnue/features/half_ka_v2_variants.cpp
src/position.cpp

index 9e7a990..a025de2 100644 (file)
@@ -84,11 +84,11 @@ namespace Stockfish::Eval::NNUE::Features {
       Piece pc = dp.piece[i];
       if (dp.from[i] != SQ_NONE)
         removed.push_back(make_index(perspective, dp.from[i], pc, oriented_ksq, pos));
-      else if (pos.piece_drops() && dp.dirty_num == 1)
+      else if (dp.handPiece[i] != NO_PIECE)
         removed.push_back(make_index(perspective, dp.handCount[i], dp.handPiece[i], oriented_ksq, pos));
       if (dp.to[i] != SQ_NONE)
         added.push_back(make_index(perspective, dp.to[i], pc, oriented_ksq, pos));
-      else if (pos.captures_to_hand() && i == 1)
+      else if (dp.handPiece[i] != NO_PIECE)
         added.push_back(make_index(perspective, dp.handCount[i] - 1, dp.handPiece[i], oriented_ksq, pos));
     }
   }
index a809cd0..8342845 100644 (file)
@@ -1434,7 +1434,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
               dp.handCount[1] = pieceCountInHand[color_of(pieceToHand)][type_of(pieceToHand)];
           }
       }
-      else
+      else if (Eval::useNNUE)
           dp.handPiece[1] = NO_PIECE;
 
       // Update material hash key and prefetch access to materialTable
@@ -1591,6 +1591,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
           {
               // Promoting pawn to SQ_NONE, promoted piece from SQ_NONE
               dp.to[0] = SQ_NONE;
+              dp.handPiece[0] = NO_PIECE;
               dp.piece[dp.dirty_num] = promotion;
               dp.handPiece[dp.dirty_num] = NO_PIECE;
               dp.from[dp.dirty_num] = SQ_NONE;
@@ -1625,6 +1626,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
       {
           // Promoting piece to SQ_NONE, promoted piece from SQ_NONE
           dp.to[0] = SQ_NONE;
+          dp.handPiece[0] = NO_PIECE;
           dp.piece[dp.dirty_num] = promotion;
           dp.handPiece[dp.dirty_num] = NO_PIECE;
           dp.from[dp.dirty_num] = SQ_NONE;
@@ -1651,6 +1653,7 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
       {
           // Demoting piece to SQ_NONE, demoted piece from SQ_NONE
           dp.to[0] = SQ_NONE;
+          dp.handPiece[0] = NO_PIECE;
           dp.piece[dp.dirty_num] = demotion;
           dp.handPiece[dp.dirty_num] = NO_PIECE;
           dp.from[dp.dirty_num] = SQ_NONE;