]> git.sesse.net Git - stockfish/commitdiff
Start searching for a repetition from the 4th ply behind
authorAram Tumanian <aram_tumanian@ukr.net>
Fri, 11 Nov 2016 15:46:21 +0000 (17:46 +0200)
committerMarco Costalba <mcostalba@gmail.com>
Sat, 19 Nov 2016 09:20:28 +0000 (10:20 +0100)
A position can never repeat the one on the previous move.
Thus we can start searching for a repetition from the 4th
ply behind. In the case:

 std::min(st->rule50, st->pliesFromNull) < 4

We don't need to do any more calculations. This case happens
very often - in more than a half of all calls of the function.

No functional change.

src/position.cpp

index 325ffd1114892aa0e4f1598acc74bc877d430adf..fd518488e3384c38397243bd0610dab6134af73f 100644 (file)
@@ -1079,14 +1079,20 @@ bool Position::is_draw() const {
   if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
       return true;
 
   if (st->rule50 > 99 && (!checkers() || MoveList<LEGAL>(*this).size()))
       return true;
 
-  StateInfo* stp = st;
-  for (int i = 2, e = std::min(st->rule50, st->pliesFromNull); i <= e; i += 2)
-  {
+  int e = std::min(st->rule50, st->pliesFromNull);
+
+  if (e < 4)
+    return false;
+
+  StateInfo* stp = st->previous->previous;
+
+  do {
       stp = stp->previous->previous;
 
       if (stp->key == st->key)
           return true; // Draw at first repetition
       stp = stp->previous->previous;
 
       if (stp->key == st->key)
           return true; // Draw at first repetition
-  }
+
+  } while ((e -= 2) >= 4);
 
   return false;
 }
 
   return false;
 }