Skip to content

Commit

Permalink
Fix an off-by-one bug in extract_pv_from_tt
Browse files Browse the repository at this point in the history
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
  • Loading branch information
mcostalba committed May 17, 2014
1 parent e46a72d commit 5e03734
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1399,18 +1399,18 @@ 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();

do {
pv.push_back(m);

assert(MoveList<LEGAL>(pos).contains(pv[ply]));
assert(MoveList<LEGAL>(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;

Expand All @@ -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]);
}


Expand All @@ -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<LEGAL>(pos).contains(pv[ply]));
assert(MoveList<LEGAL>(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]);
}


Expand Down

0 comments on commit 5e03734

Please sign in to comment.