1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-05-01 09:13:08 +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 /// Variables
CACHE_LINE_ALIGNMENT 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 MainSearchPhaseTable[] = { 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 MainSearchNoNullPhaseTable[] = { 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 LowSearchPhaseTable[] = { 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 EvasionsPhaseTable[] = { PH_EVASIONS, PH_STOP};
const MovegenPhaseT QsearchWithChecksPhaseTable[] = { PH_STOP, PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP}; const MovegenPhaseT QsearchWithChecksPhaseTable[] = { PH_TT_MOVES, PH_QCAPTURES, PH_QCHECKS, PH_STOP};
const MovegenPhaseT QsearchWithoutChecksPhaseTable[] = { PH_STOP, PH_TT_MOVES, PH_QCAPTURES, 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 } else
ttMoves[1] = killers[0] = killers[1] = MOVE_NONE; ttMoves[1] = killers[0] = killers[1] = MOVE_NONE;
movesPicked = numOfMoves = numOfBadCaptures = 0; numOfBadCaptures = 0;
finished = false; finished = false;
Color us = pos.side_to_move();
dc = p.discovered_check_candidates(us);
pinned = p.pinned_pieces(us);
if (p.is_check()) if (p.is_check())
phasePtr = EvasionsPhaseTable; phasePtr = EvasionsPhaseTable;
else if (d >= Depth(3 * OnePly)) else if (d >= Depth(3 * OnePly))
@ -89,118 +94,71 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
else else
phasePtr = QsearchWithoutChecksPhaseTable; phasePtr = QsearchWithoutChecksPhaseTable;
Color us = pos.side_to_move(); phasePtr--;
go_next_phase();
dc = p.discovered_check_candidates(us);
pinned = p.pinned_pieces(us);
} }
/// MovePicker::get_next_move() is the most important method of the MovePicker /// MovePicker::go_next_phase() generates, scores and sorts the next bunch
/// class. It returns a new legal move every time it is called, until there /// of moves when there are no more moves to try for the currrent phase.
/// are no more moves left of the types we are interested in.
Move MovePicker::get_next_move() { void MovePicker::go_next_phase() {
Move move; movesPicked = 0;
phase = *(++phasePtr);
while (true) switch (phase) {
{
// 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) {
case PH_NULL_MOVE: case PH_NULL_MOVE:
return MOVE_NULL;
case PH_TT_MOVES: case PH_TT_MOVES:
movesPicked = 0; // This is used as index to ttMoves[] return;
break;
case PH_GOOD_CAPTURES: case PH_GOOD_CAPTURES:
numOfMoves = generate_captures(pos, moves); numOfMoves = generate_captures(pos, moves);
score_captures(); score_captures();
std::sort(moves, moves + numOfMoves); std::sort(moves, moves + numOfMoves);
movesPicked = 0; return;
break;
case PH_KILLERS: case PH_KILLERS:
movesPicked = 0; // This is used as index to killers[] return;
break;
case PH_NONCAPTURES: case PH_NONCAPTURES:
numOfMoves = generate_noncaptures(pos, moves); numOfMoves = generate_noncaptures(pos, moves);
score_noncaptures(); score_noncaptures();
std::sort(moves, moves + numOfMoves); std::sort(moves, moves + numOfMoves);
movesPicked = 0; return;
break;
case PH_BAD_CAPTURES: case PH_BAD_CAPTURES:
// Bad captures SEE value is already calculated so just sort them // Bad captures SEE value is already calculated so just sort them
// to get SEE move ordering. // to get SEE move ordering.
std::sort(badCaptures, badCaptures + numOfBadCaptures); std::sort(badCaptures, badCaptures + numOfBadCaptures);
movesPicked = 0; return;
break;
case PH_EVASIONS: case PH_EVASIONS:
assert(pos.is_check()); assert(pos.is_check());
numOfMoves = generate_evasions(pos, moves, pinned); numOfMoves = generate_evasions(pos, moves, pinned);
score_evasions(); score_evasions();
std::sort(moves, moves + numOfMoves); std::sort(moves, moves + numOfMoves);
movesPicked = 0; return;
break;
case PH_QCAPTURES: case PH_QCAPTURES:
numOfMoves = generate_captures(pos, moves); numOfMoves = generate_captures(pos, moves);
score_captures(); score_captures();
std::sort(moves, moves + numOfMoves); std::sort(moves, moves + numOfMoves);
movesPicked = 0; return;
break;
case PH_QCHECKS: case PH_QCHECKS:
// Perhaps we should order moves move here? FIXME // Perhaps we should order moves move here? FIXME
numOfMoves = generate_non_capture_checks(pos, moves, dc); numOfMoves = generate_non_capture_checks(pos, moves, dc);
movesPicked = 0; return;
break;
case PH_STOP: case PH_STOP:
return MOVE_NONE; return;
default: default:
assert(false); 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(), /// 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 Move MovePicker::get_next_move() {
/// 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() {
assert(movesPicked >= 0); assert(movesPicked >= 0);
assert(!pos.is_check() || *phasePtr == PH_EVASIONS || *phasePtr == PH_STOP); assert(!pos.is_check() || *phasePtr == PH_EVASIONS || *phasePtr == PH_STOP);
assert( pos.is_check() || *phasePtr != PH_EVASIONS); 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: case PH_TT_MOVES:
while (movesPicked < 2) { while (movesPicked < 2) {
@ -368,8 +332,33 @@ Move MovePicker::pick_move_from_list() {
} }
break; break;
case PH_STOP:
return MOVE_NONE;
default: default:
assert(false);
break; break;
} }
go_next_phase();
}
return MOVE_NONE; 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_captures();
void score_noncaptures(); void score_noncaptures();
void score_evasions(); void score_evasions();
Move pick_move_from_list(); void go_next_phase();
const Position& pos; const Position& pos;
const History& H; const History& H;
Move ttMoves[2], killers[2]; Move ttMoves[2], killers[2];
const MovegenPhaseT* phasePtr; const MovegenPhaseT* phasePtr;
int movesPicked, numOfMoves, numOfBadCaptures; int phase, movesPicked, numOfMoves, numOfBadCaptures;
bool finished; bool finished;
Bitboard dc, pinned; Bitboard dc, pinned;
MoveStack moves[256], badCaptures[64]; MoveStack moves[256], badCaptures[64];