Cut in half the time spent in pos.draw() that accounts
for a whopping 1% of total time !
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
/// Position::is_draw() tests whether the position is drawn by material,
/// repetition, or the 50 moves rule. It does not detect stalemates, this
/// must be done by the search.
/// Position::is_draw() tests whether the position is drawn by material,
/// repetition, or the 50 moves rule. It does not detect stalemates, this
/// must be done by the search.
+template<bool SkipRepetition>
bool Position::is_draw() const {
// Draw by material?
bool Position::is_draw() const {
// Draw by material?
return true;
// Draw by repetition?
return true;
// Draw by repetition?
- for (int i = 4, e = Min(Min(st->gamePly, st->rule50), st->pliesFromNull); i <= e; i += 2)
- if (history[st->gamePly - i] == st->key)
- return true;
+ if (!SkipRepetition)
+ for (int i = 4, e = Min(Min(st->gamePly, st->rule50), st->pliesFromNull); i <= e; i += 2)
+ if (history[st->gamePly - i] == st->key)
+ return true;
+// Explicit template instantiations
+template bool Position::is_draw<false>() const;
+template bool Position::is_draw<true>() const;
+
/// Position::is_mate() returns true or false depending on whether the
/// side to move is checkmated.
/// Position::is_mate() returns true or false depending on whether the
/// side to move is checkmated.
// Game termination checks
bool is_mate() const;
// Game termination checks
bool is_mate() const;
+ template<bool SkipRepetition> bool is_draw() const;
// Number of plies from starting position
int startpos_ply_counter() const;
// Number of plies from starting position
int startpos_ply_counter() const;
// Step 2. Check for aborted search and immediate draw
if (( StopRequest
// Step 2. Check for aborted search and immediate draw
if (( StopRequest
+ || pos.is_draw<false>()
|| ss->ply > PLY_MAX) && !RootNode)
return VALUE_DRAW;
|| ss->ply > PLY_MAX) && !RootNode)
return VALUE_DRAW;
ss->ply = (ss-1)->ply + 1;
// Check for an instant draw or maximum ply reached
ss->ply = (ss-1)->ply + 1;
// Check for an instant draw or maximum ply reached
- if (ss->ply > PLY_MAX || pos.is_draw())
+ if (pos.is_draw<true>() || ss->ply > PLY_MAX)
return VALUE_DRAW;
// Decide whether or not to include checks, this fixes also the type of
return VALUE_DRAW;
// Decide whether or not to include checks, this fixes also the type of
&& pos.move_is_pl(tte->move())
&& pos.pl_move_is_legal(tte->move(), pos.pinned_pieces(pos.side_to_move()))
&& ply < PLY_MAX
&& pos.move_is_pl(tte->move())
&& pos.pl_move_is_legal(tte->move(), pos.pinned_pieces(pos.side_to_move()))
&& ply < PLY_MAX
- && (!pos.is_draw() || ply < 2))
+ && (!pos.is_draw<false>() || ply < 2))
{
pv[ply] = tte->move();
pos.do_move(pv[ply++], *st++);
{
pv[ply] = tte->move();
pos.do_move(pv[ply++], *st++);