From 5e03734eacc8e52a6c92be19e73b69ab3e398518 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sat, 17 May 2014 22:56:35 +0200 Subject: [PATCH] Fix an off-by-one bug in extract_pv_from_tt At root we start counting plies from 1, instead pv[] array starts from 0. So the variable 'ply' we use in extract_pv_from_tt to index pv[] is misnamed, indeed it is not the real ply, but ply-1. The fix is to leave ply name in extract_pv_from_tt but assign it the correct start value and consequentely change all the references to pv[]. Instead in insert_pv_in_tt it's simpler to rename the misnamed 'ply' in 'idx'. The off-by-one bug was unhidden when trying to use 'ply' for what it should have been, for instance in this position: position fen 8/6R1/8/3k4/8/8/8/2K5 w - - 0 1 at depth 24 mate line is erroneusly truncated due to value_from_tt() using the wrong ply. Spotted by Ronald de Man. bench: 8732553 --- src/search.cpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/search.cpp b/src/search.cpp index 6a4e352f..4ac4c086 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -1399,8 +1399,8 @@ void RootMove::extract_pv_from_tt(Position& pos) { StateInfo state[MAX_PLY_PLUS_6], *st = state; const TTEntry* tte; - int ply = 0; - Move m = pv[0]; + int ply = 1; // At root ply is 1... + Move m = pv[0]; // ...instead pv[] array starts from 0 Value expectedScore = score; pv.clear(); @@ -1408,9 +1408,9 @@ void RootMove::extract_pv_from_tt(Position& pos) { do { pv.push_back(m); - assert(MoveList(pos).contains(pv[ply])); + assert(MoveList(pos).contains(pv[ply - 1])); - pos.do_move(pv[ply++], *st++); + pos.do_move(pv[ply++ - 1], *st++); tte = TT.probe(pos.key()); expectedScore = -expectedScore; @@ -1419,11 +1419,11 @@ void RootMove::extract_pv_from_tt(Position& pos) { && pos.pseudo_legal(m = tte->move()) // Local copy, TT could change && pos.legal(m, pos.pinned_pieces(pos.side_to_move())) && ply < MAX_PLY - && (!pos.is_draw() || ply < 2)); + && (!pos.is_draw() || ply <= 2)); pv.push_back(MOVE_NONE); // Must be zero-terminating - while (ply) pos.undo_move(pv[--ply]); + while (--ply) pos.undo_move(pv[ply - 1]); } @@ -1435,21 +1435,21 @@ void RootMove::insert_pv_in_tt(Position& pos) { StateInfo state[MAX_PLY_PLUS_6], *st = state; const TTEntry* tte; - int ply = 0; + int idx = 0; // Ply starts from 1, we need to start from 0 do { tte = TT.probe(pos.key()); - if (!tte || tte->move() != pv[ply]) // Don't overwrite correct entries - TT.store(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[ply], VALUE_NONE); + if (!tte || tte->move() != pv[idx]) // Don't overwrite correct entries + TT.store(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE); - assert(MoveList(pos).contains(pv[ply])); + assert(MoveList(pos).contains(pv[idx])); - pos.do_move(pv[ply++], *st++); + pos.do_move(pv[idx++], *st++); - } while (pv[ply] != MOVE_NONE); + } while (pv[idx] != MOVE_NONE); - while (ply) pos.undo_move(pv[--ply]); + while (idx) pos.undo_move(pv[--idx]); } -- 2.39.2