1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 01:03:09 +00:00

Change the flow in wich moves are generated and picked

In MovePicker we get the next move with pick_move_from_list(),
then check if the return value is equal to MOVE_NONE and
in this case we update the state to the new phase.

This patch reorders the flow so that now from pick_move_from_list()
renamed get_next_move() we directly call go_next_phase() to
generate and sort the next bunch of moves when there are no more
move to try. This avoids to always check for pick_move_from_list()
returned value and the flow is more linear and natural.

Also use a local variable instead of a pointer dereferencing in a
time critical switch statement in get_next_move()

With this patch alone we have an incredible speed up of 3.2% !!!

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-08-27 09:12:51 +02:00
parent 129cde008c
commit 6cf28d4aa7
2 changed files with 175 additions and 186 deletions

View file

@ -43,12 +43,12 @@ namespace {
/// Variables
CACHE_LINE_ALIGNMENT
const MovegenPhaseT MainSearchPhaseTable[] = { PH_STOP, PH_NULL_MOVE, PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
const MovegenPhaseT MainSearchNoNullPhaseTable[] = { PH_STOP, PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
const MovegenPhaseT LowSearchPhaseTable[] = { PH_STOP, PH_TT_MOVES, PH_GOOD_CAPTURES, PH_NULL_MOVE, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
const MovegenPhaseT EvasionsPhaseTable[] = { PH_STOP, PH_EVASIONS, PH_STOP};
const MovegenPhaseT QsearchWithChecksPhaseTable[] = { PH_STOP, PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
const MovegenPhaseT QsearchWithoutChecksPhaseTable[] = { PH_STOP, PH_TT_MOVES, PH_QCAPTURES, PH_STOP};
const MovegenPhaseT MainSearchPhaseTable[] = { PH_NULL_MOVE, PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
const MovegenPhaseT MainSearchNoNullPhaseTable[] = { PH_TT_MOVES, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
const MovegenPhaseT LowSearchPhaseTable[] = { PH_TT_MOVES, PH_GOOD_CAPTURES, PH_NULL_MOVE, PH_KILLERS, PH_NONCAPTURES, PH_BAD_CAPTURES, PH_STOP};
const MovegenPhaseT EvasionsPhaseTable[] = { PH_EVASIONS, PH_STOP};
const MovegenPhaseT QsearchWithChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
const MovegenPhaseT QsearchWithoutChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_STOP};
}
@ -75,9 +75,14 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
} else
ttMoves[1] = killers[0] = killers[1] = MOVE_NONE;
movesPicked = numOfMoves = numOfBadCaptures = 0;
numOfBadCaptures = 0;
finished = false;
Color us = pos.side_to_move();
dc = p.discovered_check_candidates(us);
pinned = p.pinned_pieces(us);
if (p.is_check())
phasePtr = EvasionsPhaseTable;
else if (d >= Depth(3 * OnePly))
@ -89,118 +94,71 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
else
phasePtr = QsearchWithoutChecksPhaseTable;
Color us = pos.side_to_move();
dc = p.discovered_check_candidates(us);
pinned = p.pinned_pieces(us);
phasePtr--;
go_next_phase();
}
/// MovePicker::get_next_move() is the most important method of the MovePicker
/// class. It returns a new legal move every time it is called, until there
/// are no more moves left of the types we are interested in.
/// MovePicker::go_next_phase() generates, scores and sorts the next bunch
/// of moves when there are no more moves to try for the currrent phase.
Move MovePicker::get_next_move() {
void MovePicker::go_next_phase() {
Move move;
while (true)
{
// If we already have a list of generated moves, pick the best move from
// the list, and return it.
move = pick_move_from_list();
if (move != MOVE_NONE)
{
assert(move_is_ok(move));
return move;
}
// Next phase
phasePtr++;
switch (*phasePtr) {
movesPicked = 0;
phase = *(++phasePtr);
switch (phase) {
case PH_NULL_MOVE:
return MOVE_NULL;
case PH_TT_MOVES:
movesPicked = 0; // This is used as index to ttMoves[]
break;
return;
case PH_GOOD_CAPTURES:
numOfMoves = generate_captures(pos, moves);
score_captures();
std::sort(moves, moves + numOfMoves);
movesPicked = 0;
break;
return;
case PH_KILLERS:
movesPicked = 0; // This is used as index to killers[]
break;
return;
case PH_NONCAPTURES:
numOfMoves = generate_noncaptures(pos, moves);
score_noncaptures();
std::sort(moves, moves + numOfMoves);
movesPicked = 0;
break;
return;
case PH_BAD_CAPTURES:
// Bad captures SEE value is already calculated so just sort them
// to get SEE move ordering.
std::sort(badCaptures, badCaptures + numOfBadCaptures);
movesPicked = 0;
break;
return;
case PH_EVASIONS:
assert(pos.is_check());
numOfMoves = generate_evasions(pos, moves, pinned);
score_evasions();
std::sort(moves, moves + numOfMoves);
movesPicked = 0;
break;
return;
case PH_QCAPTURES:
numOfMoves = generate_captures(pos, moves);
score_captures();
std::sort(moves, moves + numOfMoves);
movesPicked = 0;
break;
return;
case PH_QCHECKS:
// Perhaps we should order moves move here? FIXME
numOfMoves = generate_non_capture_checks(pos, moves, dc);
movesPicked = 0;
break;
return;
case PH_STOP:
return MOVE_NONE;
return;
default:
assert(false);
return MOVE_NONE;
return;
}
}
}
/// A variant of get_next_move() which takes a lock as a parameter, used to
/// prevent multiple threads from picking the same move at a split point.
Move MovePicker::get_next_move(Lock &lock) {
lock_grab(&lock);
if (finished)
{
lock_release(&lock);
return MOVE_NONE;
}
Move m = get_next_move();
if (m == MOVE_NONE)
finished = true;
lock_release(&lock);
return m;
}
/// MovePicker::score_captures(), MovePicker::score_noncaptures(),
@ -276,19 +234,25 @@ void MovePicker::score_evasions() {
}
}
/// MovePicker::get_next_move() is the most important method of the MovePicker
/// class. It returns a new legal move every time it is called, until there
/// are no more moves left.
/// It picks the move with the biggest score from a list of generated moves taking
/// care not to return the tt move if that has already been serched previously.
/// MovePicker::pick_move_from_list() picks the move with the biggest score
/// from a list of generated moves (moves[] or badCaptures[], depending on
/// the current move generation phase). It takes care not to return the
/// transposition table move if that has already been serched previously.
Move MovePicker::pick_move_from_list() {
Move MovePicker::get_next_move() {
assert(movesPicked >= 0);
assert(!pos.is_check() || *phasePtr == PH_EVASIONS || *phasePtr == PH_STOP);
assert( pos.is_check() || *phasePtr != PH_EVASIONS);
switch (*phasePtr) {
while (true)
{
switch (phase) {
case PH_NULL_MOVE:
go_next_phase();
return MOVE_NULL;
case PH_TT_MOVES:
while (movesPicked < 2) {
@ -368,8 +332,33 @@ Move MovePicker::pick_move_from_list() {
}
break;
case PH_STOP:
return MOVE_NONE;
default:
assert(false);
break;
}
go_next_phase();
}
return MOVE_NONE;
}
/// A variant of get_next_move() which takes a lock as a parameter, used to
/// prevent multiple threads from picking the same move at a split point.
Move MovePicker::get_next_move(Lock &lock) {
lock_grab(&lock);
if (finished)
{
lock_release(&lock);
return MOVE_NONE;
}
Move m = get_next_move();
if (m == MOVE_NONE)
finished = true;
lock_release(&lock);
return m;
}

View file

@ -75,13 +75,13 @@ private:
void score_captures();
void score_noncaptures();
void score_evasions();
Move pick_move_from_list();
void go_next_phase();
const Position& pos;
const History& H;
Move ttMoves[2], killers[2];
const MovegenPhaseT* phasePtr;
int movesPicked, numOfMoves, numOfBadCaptures;
int phase, movesPicked, numOfMoves, numOfBadCaptures;
bool finished;
Bitboard dc, pinned;
MoveStack moves[256], badCaptures[64];