+ StateInfo state[PLY_MAX_PLUS_2], *st = state;
+ TTEntry* tte;
+ int ply = 1;
+
+ assert(pv[0] != MOVE_NONE && move_is_legal(pos, pv[0]));
+
+ pos.do_move(pv[0], *st++);
+
+ while ( (tte = TT.retrieve(pos.get_key())) != NULL
+ && tte->move() != MOVE_NONE
+ && move_is_legal(pos, tte->move())
+ && ply < PLY_MAX
+ && (!pos.is_draw() || ply < 2))
+ {
+ pv[ply] = tte->move();
+ pos.do_move(pv[ply++], *st++);
+ }
+ pv[ply] = MOVE_NONE;
+
+ do pos.undo_move(pv[--ply]); while (ply);
+ }
+
+ // insert_pv_in_tt() is called at the end of a search iteration, and inserts
+ // the PV back into the TT. This makes sure the old PV moves are searched
+ // first, even if the old TT entries have been overwritten.
+
+ void RootMove::insert_pv_in_tt(Position& pos) {
+
+ StateInfo state[PLY_MAX_PLUS_2], *st = state;
+ TTEntry* tte;
+ Key k;
+ Value v, m = VALUE_NONE;
+ int ply = 0;
+
+ assert(pv[0] != MOVE_NONE && move_is_legal(pos, pv[0]));
+
+ do {
+ k = pos.get_key();
+ tte = TT.retrieve(k);
+
+ // Don't overwrite exsisting correct entries
+ if (!tte || tte->move() != pv[ply])
+ {
+ v = (pos.is_check() ? VALUE_NONE : evaluate(pos, m));
+ TT.store(k, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, pv[ply], v, m);
+ }
+ pos.do_move(pv[ply], *st++);
+
+ } while (pv[++ply] != MOVE_NONE);
+
+ do pos.undo_move(pv[--ply]); while (ply);
+ }
+
+ // pv_info_to_uci() returns a string with information on the current PV line
+ // formatted according to UCI specification and eventually writes the info
+ // to a log file. It is called at each iteration or after a new pv is found.
+
+ std::string RootMove::pv_info_to_uci(const Position& pos, Value alpha, Value beta, int pvLine) {
+
+ std::stringstream s, l;
+ Move* m = pv;
+
+ while (*m != MOVE_NONE)
+ l << *m++ << " ";
+
+ s << "info depth " << Iteration // FIXME
+ << " seldepth " << int(m - pv)
+ << " multipv " << pvLine + 1
+ << " score " << value_to_uci(pv_score)
+ << (pv_score >= beta ? " lowerbound" : pv_score <= alpha ? " upperbound" : "")
+ << " time " << current_search_time()
+ << " nodes " << pos.nodes_searched()
+ << " nps " << nps(pos)
+ << " pv " << l.str();
+
+ if (UseLogFile && pvLine == 0)
+ {
+ ValueType t = pv_score >= beta ? VALUE_TYPE_LOWER :
+ pv_score <= alpha ? VALUE_TYPE_UPPER : VALUE_TYPE_EXACT;
+
+ LogFile << pretty_pv(pos, current_search_time(), Iteration, pv_score, t, pv) << endl;
+ }
+ return s.str();
+ }