-#include "thread.h"
-
-namespace {
-
- enum Stages {
- MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
- EVASION, EVASIONS_S2,
- QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
- QSEARCH_1, CAPTURES_S4,
- PROBCUT, CAPTURES_S5,
- RECAPTURE, CAPTURES_S6,
- STOP
- };
-
- // Our insertion sort, which is guaranteed (and also needed) to be stable
- void insertion_sort(ExtMove* begin, ExtMove* end)
- {
- ExtMove tmp, *p, *q;
-
- for (p = begin + 1; p < end; ++p)
- {
- tmp = *p;
- for (q = p; q != begin && *(q-1) < tmp; --q)
- *q = *(q-1);
- *q = tmp;
- }
- }
-
- // Unary predicate used by std::partition to split positive scores from remaining
- // ones so to sort separately the two sets, and with the second sort delayed.
- inline bool has_positive_score(const ExtMove& ms) { return ms.score > 0; }
-
- // Picks the best move in the range (begin, end) and moves it to the front.
- // It's faster than sorting all the moves in advance when there are few
- // moves e.g. possible captures.
- inline ExtMove* pick_best(ExtMove* begin, ExtMove* end)
- {
- std::swap(*begin, *std::max_element(begin, end));
- return begin;
- }
-}
-
-
-/// Constructors of the MovePicker class. As arguments we pass information
-/// to help it to return the presumably good moves first, to decide which
-/// moves to return (in the quiescence search, for instance, we only want to
-/// search captures, promotions and some checks) and about how important good
-/// move ordering is at the current node.
-
-MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
- Move* cm, Search::Stack* s) : pos(p), history(h), depth(d) {
-
- assert(d > DEPTH_ZERO);
-
- cur = end = moves;
- endBadCaptures = moves + MAX_MOVES - 1;
- countermoves = cm;
- ss = s;
-
- if (p.checkers())
- stage = EVASION;
-
- else
- stage = MAIN_SEARCH;
-
- ttMove = (ttm && pos.pseudo_legal(ttm) ? ttm : MOVE_NONE);
- end += (ttMove != MOVE_NONE);
-}
-
-MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const HistoryStats& h,
- Square sq) : pos(p), history(h), cur(moves), end(moves) {
-
- assert(d <= DEPTH_ZERO);
-
- if (p.checkers())
- stage = EVASION;
-
- else if (d > DEPTH_QS_NO_CHECKS)
- stage = QSEARCH_0;
-
- else if (d > DEPTH_QS_RECAPTURES)
- {
- stage = QSEARCH_1;
-
- // Skip TT move if is not a capture or a promotion. This avoids qsearch
- // tree explosion due to a possible perpetual check or similar rare cases
- // when TT table is full.
- if (ttm && !pos.capture_or_promotion(ttm))
- ttm = MOVE_NONE;
- }
- else
- {
- stage = RECAPTURE;
- recaptureSquare = sq;
- ttm = MOVE_NONE;
- }