namespace {
- // Different node types, used as template parameter
- enum NodeType { NonPV, PV };
-
- // Set to true to force running with one thread. Used for debugging.
+ // Set to true to force running with one thread. Used for debugging
const bool FakeSplit = false;
- // Lookup table to check if a Piece is a slider and its access function
- const bool Slidings[18] = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1 };
- inline bool piece_is_slider(Piece p) { return Slidings[p]; }
+ // Different node types, used as template parameter
+ enum NodeType { NonPV, PV };
// RootMove struct is used for moves at the root of the tree. For each root
// move, we store two scores, a node count, and a PV (really a refutation
// in the case of moves which fail low). Value pv_score is normally set at
// -VALUE_INFINITE for all non-pv moves, while non_pv_score is computed
// according to the order in which moves are returned by MovePicker.
-
struct RootMove {
RootMove();
Move pv[PLY_MAX_PLUS_2];
};
-
// RootMoveList struct is just a vector of RootMove objects,
// with an handful of methods above the standard ones.
-
struct RootMoveList : public std::vector<RootMove> {
typedef std::vector<RootMove> Base;
int bestMoveChanges;
};
+ // MovePickerExt template class extends MovePicker and allows to choose at compile
+ // time the proper moves source according to the type of node. In the default case
+ // we simply create and use a standard MovePicker object.
+ template<bool SpNode, bool Root> struct MovePickerExt : public MovePicker {
- // Overload operator<<() to make it easier to print moves in a coordinate
- // notation compatible with UCI protocol.
- std::ostream& operator<<(std::ostream& os, Move m) {
+ MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
+ : MovePicker(p, ttm, d, h, ss, b) {}
- bool chess960 = (os.iword(0) != 0); // See set960()
- return os << move_to_uci(m, chess960);
- }
+ RootMoveList::iterator rm; // Dummy, needed to compile
+ };
+ // In case of a SpNode we use split point's shared MovePicker object as moves source
+ template<> struct MovePickerExt<true, false> : public MovePicker {
- // When formatting a move for std::cout we must know if we are in Chess960
- // or not. To keep using the handy operator<<() on the move the trick is to
- // embed this flag in the stream itself. Function-like named enum set960 is
- // used as a custom manipulator and the stream internal general-purpose array,
- // accessed through ios_base::iword(), is used to pass the flag to the move's
- // operator<<() that will read it to properly format castling moves.
- enum set960 {};
+ MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
+ : MovePicker(p, ttm, d, h, ss, b), mp(ss->sp->mp) {}
- std::ostream& operator<< (std::ostream& os, const set960& f) {
+ Move get_next_move() { return mp->get_next_move(); }
+
+ RootMoveList::iterator rm; // Dummy, needed to compile
+ MovePicker* mp;
+ };
+
+ // In case of a Root node we use RootMoveList as moves source
+ template<> struct MovePickerExt<false, true> : public MovePicker {
+
+ MovePickerExt(const Position&, Move, Depth, const History&, SearchStack*, Value);
+ Move get_next_move();
+
+ RootMoveList::iterator rm;
+ bool firstCall;
+ };
- os.iword(0) = int(f);
- return os;
- }
+ /// Constants
- /// Adjustments
+ // Lookup table to check if a Piece is a slider and its access function
+ const bool Slidings[18] = { 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1 };
+ inline bool piece_is_slider(Piece p) { return Slidings[p]; }
// Step 6. Razoring
/// Namespace variables
- // Book
- Book OpeningBook;
-
// Root move list
RootMoveList Rml;
// Skill level adjustment
int SkillLevel;
bool SkillLevelEnabled;
- RKISS RK;
// Node counters, used only by thread[0] but try to keep in different cache
// lines (64 bytes each) from the heavy multi-thread read accessed variables.
void poll(const Position& pos);
void wait_for_stop_or_ponderhit();
+ // Overload operator<<() to make it easier to print moves in a coordinate
+ // notation compatible with UCI protocol.
+ std::ostream& operator<<(std::ostream& os, Move m) {
- // MovePickerExt is an extended MovePicker class used to choose at compile time
- // the proper move source according to the type of node.
- template<bool SpNode, bool Root> struct MovePickerExt;
-
- // In Root nodes use RootMoveList as source. Score and sort the root moves
- // before to search them.
- template<> struct MovePickerExt<false, true> : public MovePicker {
-
- MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
- : MovePicker(p, ttm, d, h, ss, b), firstCall(true) {
- Move move;
- Value score = VALUE_ZERO;
-
- // Score root moves using standard ordering used in main search, the moves
- // are scored according to the order in which they are returned by MovePicker.
- // This is the second order score that is used to compare the moves when
- // the first orders pv_score of both moves are equal.
- while ((move = MovePicker::get_next_move()) != MOVE_NONE)
- for (rm = Rml.begin(); rm != Rml.end(); ++rm)
- if (rm->pv[0] == move)
- {
- rm->non_pv_score = score--;
- break;
- }
-
- Rml.sort();
- rm = Rml.begin();
- }
-
- Move get_next_move() {
-
- if (!firstCall)
- ++rm;
- else
- firstCall = false;
-
- return rm != Rml.end() ? rm->pv[0] : MOVE_NONE;
- }
-
- RootMoveList::iterator rm;
- bool firstCall;
- };
-
- // In SpNodes use split point's shared MovePicker object as move source
- template<> struct MovePickerExt<true, false> : public MovePicker {
-
- MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
- : MovePicker(p, ttm, d, h, ss, b), mp(ss->sp->mp) {}
-
- Move get_next_move() { return mp->get_next_move(); }
-
- RootMoveList::iterator rm; // Dummy, needed to compile
- MovePicker* mp;
- };
+ bool chess960 = (os.iword(0) != 0); // See set960()
+ return os << move_to_uci(m, chess960);
+ }
- // Default case, create and use a MovePicker object as source
- template<> struct MovePickerExt<false, false> : public MovePicker {
+ // When formatting a move for std::cout we must know if we are in Chess960
+ // or not. To keep using the handy operator<<() on the move the trick is to
+ // embed this flag in the stream itself. Function-like named enum set960 is
+ // used as a custom manipulator and the stream internal general-purpose array,
+ // accessed through ios_base::iword(), is used to pass the flag to the move's
+ // operator<<() that will read it to properly format castling moves.
+ enum set960 {};
- MovePickerExt(const Position& p, Move ttm, Depth d, const History& h, SearchStack* ss, Value b)
- : MovePicker(p, ttm, d, h, ss, b) {}
+ std::ostream& operator<< (std::ostream& os, const set960& f) {
- RootMoveList::iterator rm; // Dummy, needed to compile
- };
+ os.iword(0) = int(f);
+ return os;
+ }
} // namespace
bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) {
+ static Book book;
+
// Initialize global search-related variables
StopOnPonderhit = StopRequest = QuitRequest = AspirationFailLow = SendSearchedNodes = false;
NodesSincePoll = 0;
// Look for a book move
if (Options["OwnBook"].value<bool>())
{
- if (Options["Book File"].value<std::string>() != OpeningBook.name())
- OpeningBook.open(Options["Book File"].value<std::string>());
+ if (Options["Book File"].value<std::string>() != book.name())
+ book.open(Options["Book File"].value<std::string>());
- Move bookMove = OpeningBook.get_move(pos, Options["Best Book Move"].value<bool>());
+ Move bookMove = book.get_move(pos, Options["Best Book Move"].value<bool>());
if (bookMove != MOVE_NONE)
{
if (Limits.ponder)
excludedMove = ss->excludedMove;
posKey = excludedMove ? pos.get_exclusion_key() : pos.get_key();
- tte = TT.retrieve(posKey);
+ tte = TT.probe(posKey);
ttMove = tte ? tte->move() : MOVE_NONE;
// At PV nodes we check for exact scores, while at non-PV nodes we check for
ss->skipNullMove = false;
ttMove = ss->bestMove;
- tte = TT.retrieve(posKey);
+ tte = TT.probe(posKey);
}
split_point_start: // At split points actual search starts from here
// Transposition table lookup. At PV nodes, we don't use the TT for
// pruning, but only for move ordering.
- tte = TT.retrieve(pos.get_key());
+ tte = TT.probe(pos.get_key());
ttMove = (tte ? tte->move() : MOVE_NONE);
if (!PvNode && tte && ok_to_use_TT(tte, ttDepth, beta, ss->ply))
assert(MultiPV > 1);
+ static RKISS rk;
+
// Rml list is already sorted by pv_score in descending order
int s;
int max_s = -VALUE_INFINITE;
// PRNG sequence should be non deterministic
for (int i = abs(get_system_time() % 50); i > 0; i--)
- RK.rand<unsigned>();
+ rk.rand<unsigned>();
// Choose best move. For each move's score we add two terms both dependent
// on wk, one deterministic and bigger for weaker moves, and one random,
break;
// This is our magical formula
- s += ((max - s) * wk + var * (RK.rand<unsigned>() % wk)) / 128;
+ s += ((max - s) * wk + var * (rk.rand<unsigned>() % wk)) / 128;
if (s > max_s)
{
pos.do_move(pv[0], *st++);
- while ( (tte = TT.retrieve(pos.get_key())) != NULL
+ while ( (tte = TT.probe(pos.get_key())) != NULL
&& tte->move() != MOVE_NONE
&& pos.move_is_legal(tte->move())
&& ply < PLY_MAX
do {
k = pos.get_key();
- tte = TT.retrieve(k);
+ tte = TT.probe(k);
// Don't overwrite existing correct entries
if (!tte || tte->move() != pv[ply])
return s.str();
}
+ // Specializations for MovePickerExt in case of Root node
+ MovePickerExt<false, true>::MovePickerExt(const Position& p, Move ttm, Depth d,
+ const History& h, SearchStack* ss, Value b)
+ : MovePicker(p, ttm, d, h, ss, b), firstCall(true) {
+ Move move;
+ Value score = VALUE_ZERO;
+
+ // Score root moves using standard ordering used in main search, the moves
+ // are scored according to the order in which they are returned by MovePicker.
+ // This is the second order score that is used to compare the moves when
+ // the first orders pv_score of both moves are equal.
+ while ((move = MovePicker::get_next_move()) != MOVE_NONE)
+ for (rm = Rml.begin(); rm != Rml.end(); ++rm)
+ if (rm->pv[0] == move)
+ {
+ rm->non_pv_score = score--;
+ break;
+ }
+
+ Rml.sort();
+ rm = Rml.begin();
+ }
+
+ Move MovePickerExt<false, true>::get_next_move() {
+
+ if (!firstCall)
+ ++rm;
+ else
+ firstCall = false;
+
+ return rm != Rml.end() ? rm->pv[0] : MOVE_NONE;
+ }
+
} // namespace