1
0
Fork 0
mirror of https://github.com/sockspls/badfish synced 2025-04-30 16:53:09 +00:00

Use pointers instead of array indices in MovePicker

This avoids calculating the array entry position
at each access and gives another boost of almost 1%.

No functional change.

Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
Marco Costalba 2009-08-28 08:57:52 +02:00
parent 6cf28d4aa7
commit 9e4befe3f1
7 changed files with 106 additions and 109 deletions

View file

@ -429,11 +429,11 @@ Move Book::get_move(const Position& pos) {
if (!bookMove) if (!bookMove)
return MOVE_NONE; return MOVE_NONE;
MoveStack moves[256]; MoveStack mlist[256];
int n = generate_legal_moves(pos, moves); MoveStack* last = generate_legal_moves(pos, mlist);
for (int j = 0; j < n; j++) for (MoveStack* cur = mlist; cur != last; cur++)
if ((int(moves[j].move) & 07777) == bookMove) if ((int(cur->move) & 07777) == bookMove)
return moves[j].move; return cur->move;
return MOVE_NONE; return MOVE_NONE;
} }

View file

@ -137,36 +137,33 @@ namespace {
/// generate_captures generates() all pseudo-legal captures and queen /// generate_captures generates() all pseudo-legal captures and queen
/// promotions. The return value is the number of moves generated. /// promotions. The return value is the number of moves generated.
int generate_captures(const Position& pos, MoveStack* mlist) { MoveStack* generate_captures(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
Color us = pos.side_to_move(); Color us = pos.side_to_move();
Bitboard target = pos.pieces_of_color(opposite_color(us)); Bitboard target = pos.pieces_of_color(opposite_color(us));
MoveStack* mlist_start = mlist;
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target); mlist = generate_piece_moves<ROOK>(pos, mlist, us, target);
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target); mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us); mlist = generate_piece_moves<PAWN, CAPTURE>(pos, mlist, us);
mlist = generate_piece_moves<KING>(pos, mlist, us, target); return generate_piece_moves<KING>(pos, mlist, us, target);
return int(mlist - mlist_start);
} }
/// generate_noncaptures() generates all pseudo-legal non-captures and /// generate_noncaptures() generates all pseudo-legal non-captures and
/// underpromotions. The return value is the number of moves generated. /// underpromotions. The return value is the number of moves generated.
int generate_noncaptures(const Position& pos, MoveStack* mlist) { MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
Color us = pos.side_to_move(); Color us = pos.side_to_move();
Bitboard target = pos.empty_squares(); Bitboard target = pos.empty_squares();
MoveStack* mlist_start = mlist;
mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us); mlist = generate_piece_moves<PAWN, NON_CAPTURE>(pos, mlist, us);
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target); mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target);
@ -175,22 +172,20 @@ int generate_noncaptures(const Position& pos, MoveStack* mlist) {
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target); mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_piece_moves<KING>(pos, mlist, us, target); mlist = generate_piece_moves<KING>(pos, mlist, us, target);
mlist = generate_castle_moves<KING_SIDE>(pos, mlist); mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
mlist = generate_castle_moves<QUEEN_SIDE>(pos, mlist); return generate_castle_moves<QUEEN_SIDE>(pos, mlist);
return int(mlist - mlist_start);
} }
/// generate_non_capture_checks() generates all pseudo-legal non-capturing, /// generate_non_capture_checks() generates all pseudo-legal non-capturing,
/// non-promoting checks. It returns the number of generated moves. /// non-promoting checks. It returns the number of generated moves.
int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) { MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(!pos.is_check()); assert(!pos.is_check());
Color us = pos.side_to_move(); Color us = pos.side_to_move();
Square ksq = pos.king_square(opposite_color(us)); Square ksq = pos.king_square(opposite_color(us));
MoveStack* mlist_start = mlist;
assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING)); assert(pos.piece_on(ksq) == piece_of_color_and_type(opposite_color(us), KING));
@ -213,7 +208,7 @@ int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard
&& castling_is_check(pos, KING_SIDE)) && castling_is_check(pos, KING_SIDE))
mlist = generate_castle_moves<KING_SIDE>(pos, mlist); mlist = generate_castle_moves<KING_SIDE>(pos, mlist);
return int(mlist - mlist_start); return mlist;
} }
@ -221,7 +216,7 @@ int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard
/// in check. Unlike the other move generation functions, this one generates /// in check. Unlike the other move generation functions, this one generates
/// only legal moves. It returns the number of generated moves. /// only legal moves. It returns the number of generated moves.
int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) { MoveStack* generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
assert(pos.is_ok()); assert(pos.is_ok());
assert(pos.is_check()); assert(pos.is_check());
@ -230,7 +225,6 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
Color us = pos.side_to_move(); Color us = pos.side_to_move();
Color them = opposite_color(us); Color them = opposite_color(us);
Square ksq = pos.king_square(us); Square ksq = pos.king_square(us);
MoveStack* mlist_start = mlist;
assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING)); assert(pos.piece_on(ksq) == piece_of_color_and_type(us, KING));
@ -350,7 +344,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
} }
} }
} }
return int(mlist - mlist_start); return mlist;
} }
@ -360,7 +354,7 @@ int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned) {
/// very hard to write an efficient legal move generator, but for the moment /// very hard to write an efficient legal move generator, but for the moment
/// we don't need it. /// we don't need it.
int generate_legal_moves(const Position& pos, MoveStack* mlist) { MoveStack* generate_legal_moves(const Position& pos, MoveStack* mlist) {
assert(pos.is_ok()); assert(pos.is_ok());
@ -370,15 +364,17 @@ int generate_legal_moves(const Position& pos, MoveStack* mlist) {
return generate_evasions(pos, mlist, pinned); return generate_evasions(pos, mlist, pinned);
// Generate pseudo-legal moves // Generate pseudo-legal moves
int n = generate_captures(pos, mlist); MoveStack* last = generate_captures(pos, mlist);
n += generate_noncaptures(pos, mlist + n); last = generate_noncaptures(pos, last);
// Remove illegal moves from the list // Remove illegal moves from the list
for (int i = 0; i < n; i++) for (MoveStack* cur = mlist; cur != last; cur++)
if (!pos.pl_move_is_legal(mlist[i].move, pinned)) if (!pos.pl_move_is_legal(cur->move, pinned))
mlist[i--].move = mlist[--n].move; {
cur->move = (--last)->move;
return n; cur--;
}
return last;
} }
@ -580,11 +576,12 @@ bool move_is_legal(const Position& pos, const Move m) {
else else
{ {
Position p(pos); Position p(pos);
MoveStack moves[64]; MoveStack mlist[64];
int n = generate_evasions(p, moves, pinned); MoveStack* last = generate_evasions(p, mlist, pinned);
for (int i = 0; i < n; i++) for (MoveStack* cur = mlist; cur != last; cur++)
if (moves[i].move == m) if (cur->move == m)
return true; return true;
return false; return false;
} }
} }

View file

@ -32,11 +32,11 @@
//// Prototypes //// Prototypes
//// ////
extern int generate_captures(const Position& pos, MoveStack* mlist); extern MoveStack* generate_captures(const Position& pos, MoveStack* mlist);
extern int generate_noncaptures(const Position& pos, MoveStack* mlist); extern MoveStack* generate_noncaptures(const Position& pos, MoveStack* mlist);
extern int generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc); extern MoveStack* generate_non_capture_checks(const Position& pos, MoveStack* mlist, Bitboard dc);
extern int generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned); extern MoveStack* generate_evasions(const Position& pos, MoveStack* mlist, Bitboard pinned);
extern int generate_legal_moves(const Position& pos, MoveStack* mlist); extern MoveStack* generate_legal_moves(const Position& pos, MoveStack* mlist);
extern bool move_is_legal(const Position& pos, const Move m, Bitboard pinned); extern bool move_is_legal(const Position& pos, const Move m, Bitboard pinned);
extern bool move_is_legal(const Position& pos, const Move m); extern bool move_is_legal(const Position& pos, const Move m);

View file

@ -100,55 +100,59 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d,
/// MovePicker::go_next_phase() generates, scores and sorts the next bunch /// 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. /// of moves when there are no more moves to try for the current phase.
void MovePicker::go_next_phase() { void MovePicker::go_next_phase() {
movesPicked = 0; curMove = moves;
phase = *(++phasePtr); phase = *(++phasePtr);
switch (phase) { switch (phase) {
case PH_NULL_MOVE: case PH_NULL_MOVE:
case PH_TT_MOVES: case PH_TT_MOVES:
movesPicked = 0;
return; return;
case PH_GOOD_CAPTURES: case PH_GOOD_CAPTURES:
numOfMoves = generate_captures(pos, moves); lastMove = generate_captures(pos, moves);
score_captures(); score_captures();
std::sort(moves, moves + numOfMoves); std::sort(moves, lastMove);
return; return;
case PH_KILLERS: case PH_KILLERS:
movesPicked = 0;
return; return;
case PH_NONCAPTURES: case PH_NONCAPTURES:
numOfMoves = generate_noncaptures(pos, moves); lastMove = generate_noncaptures(pos, moves);
score_noncaptures(); score_noncaptures();
std::sort(moves, moves + numOfMoves); std::sort(moves, lastMove);
return; return;
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); curMove = badCaptures;
lastMove = badCaptures + numOfBadCaptures;
std::sort(badCaptures, lastMove);
return; return;
case PH_EVASIONS: case PH_EVASIONS:
assert(pos.is_check()); assert(pos.is_check());
numOfMoves = generate_evasions(pos, moves, pinned); lastMove = generate_evasions(pos, moves, pinned);
score_evasions(); score_evasions();
std::sort(moves, moves + numOfMoves); std::sort(moves, lastMove);
return; return;
case PH_QCAPTURES: case PH_QCAPTURES:
numOfMoves = generate_captures(pos, moves); lastMove = generate_captures(pos, moves);
score_captures(); score_captures();
std::sort(moves, moves + numOfMoves); std::sort(moves, lastMove);
return; return;
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); lastMove = generate_non_capture_checks(pos, moves, dc);
return; return;
case PH_STOP: case PH_STOP:
@ -161,10 +165,10 @@ void MovePicker::go_next_phase() {
} }
/// MovePicker::score_captures(), MovePicker::score_noncaptures(), /// MovePicker::score_captures(), MovePicker::score_noncaptures() and
/// MovePicker::score_evasions() and MovePicker::score_qcaptures() assign a /// MovePicker::score_evasions() assign a numerical move ordering score
/// numerical move ordering score to each move in a move list. The moves /// to each move in a move list. The moves with highest scores will be
/// with highest scores will be picked first by pick_move_from_list(). /// picked first by get_next_move().
void MovePicker::score_captures() { void MovePicker::score_captures() {
// Winning and equal captures in the main search are ordered by MVV/LVA. // Winning and equal captures in the main search are ordered by MVV/LVA.
@ -183,13 +187,13 @@ void MovePicker::score_captures() {
Move m; Move m;
// Use MVV/LVA ordering // Use MVV/LVA ordering
for (int i = 0; i < numOfMoves; i++) for (MoveStack* cur = moves; cur != lastMove; cur++)
{ {
m = moves[i].move; m = cur->move;
if (move_is_promotion(m)) if (move_is_promotion(m))
moves[i].score = QueenValueMidgame; cur->score = QueenValueMidgame;
else else
moves[i].score = int(pos.midgame_value_of_piece_on(move_to(m))) cur->score = int(pos.midgame_value_of_piece_on(move_to(m)))
-int(pos.type_of_piece_on(move_from(m))); -int(pos.type_of_piece_on(move_from(m)));
} }
} }
@ -202,10 +206,10 @@ void MovePicker::score_noncaptures() {
Square from, to; Square from, to;
int hs; int hs;
for (int i = 0; i < numOfMoves; i++) for (MoveStack* cur = moves; cur != lastMove; cur++)
{ {
from = move_from(moves[i].move); from = move_from(cur->move);
to = move_to(moves[i].move); to = move_to(cur->move);
piece = pos.piece_on(from); piece = pos.piece_on(from);
hs = H.move_ordering_score(piece, to); hs = H.move_ordering_score(piece, to);
@ -214,23 +218,23 @@ void MovePicker::score_noncaptures() {
hs += 1000; hs += 1000;
// pst based scoring // pst based scoring
moves[i].score = hs + pos.pst_delta<Position::MidGame>(piece, from, to); cur->score = hs + pos.pst_delta<Position::MidGame>(piece, from, to);
} }
} }
void MovePicker::score_evasions() { void MovePicker::score_evasions() {
for (int i = 0; i < numOfMoves; i++) for (MoveStack* cur = moves; cur != lastMove; cur++)
{ {
Move m = moves[i].move; Move m = cur->move;
if (m == ttMoves[0]) if (m == ttMoves[0])
moves[i].score = 2*HistoryMax; cur->score = 2*HistoryMax;
else if (!pos.square_is_empty(move_to(m))) else if (!pos.square_is_empty(move_to(m)))
{ {
int seeScore = pos.see(m); int seeScore = pos.see(m);
moves[i].score = (seeScore >= 0)? seeScore + HistoryMax : seeScore; cur->score = (seeScore >= 0)? seeScore + HistoryMax : seeScore;
} else } else
moves[i].score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m)); cur->score = H.move_ordering_score(pos.piece_on(move_from(m)), move_to(m));
} }
} }
@ -242,7 +246,6 @@ void MovePicker::score_evasions() {
Move MovePicker::get_next_move() { 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 || *phasePtr == PH_STOP);
assert( pos.is_check() || *phasePtr != PH_EVASIONS); assert( pos.is_check() || *phasePtr != PH_EVASIONS);
@ -255,7 +258,8 @@ Move MovePicker::get_next_move() {
return MOVE_NULL; return MOVE_NULL;
case PH_TT_MOVES: case PH_TT_MOVES:
while (movesPicked < 2) { while (movesPicked < 2)
{
Move move = ttMoves[movesPicked++]; Move move = ttMoves[movesPicked++];
if ( move != MOVE_NONE if ( move != MOVE_NONE
&& move_is_legal(pos, move, pinned)) && move_is_legal(pos, move, pinned))
@ -264,9 +268,9 @@ Move MovePicker::get_next_move() {
break; break;
case PH_GOOD_CAPTURES: case PH_GOOD_CAPTURES:
while (movesPicked < numOfMoves) while (curMove != lastMove)
{ {
Move move = moves[movesPicked++].move; Move move = (curMove++)->move;
if ( move != ttMoves[0] if ( move != ttMoves[0]
&& move != ttMoves[1] && move != ttMoves[1]
&& pos.pl_move_is_legal(move, pinned)) && pos.pl_move_is_legal(move, pinned))
@ -286,7 +290,8 @@ Move MovePicker::get_next_move() {
break; break;
case PH_KILLERS: case PH_KILLERS:
while (movesPicked < 2) { while (movesPicked < 2)
{
Move move = killers[movesPicked++]; Move move = killers[movesPicked++];
if ( move != MOVE_NONE if ( move != MOVE_NONE
&& move != ttMoves[0] && move != ttMoves[0]
@ -298,9 +303,9 @@ Move MovePicker::get_next_move() {
break; break;
case PH_NONCAPTURES: case PH_NONCAPTURES:
while (movesPicked < numOfMoves) while (curMove != lastMove)
{ {
Move move = moves[movesPicked++].move; Move move = (curMove++)->move;
if ( move != ttMoves[0] if ( move != ttMoves[0]
&& move != ttMoves[1] && move != ttMoves[1]
&& move != killers[0] && move != killers[0]
@ -311,20 +316,16 @@ Move MovePicker::get_next_move() {
break; break;
case PH_EVASIONS: case PH_EVASIONS:
if (movesPicked < numOfMoves)
return moves[movesPicked++].move;
break;
case PH_BAD_CAPTURES: case PH_BAD_CAPTURES:
if (movesPicked < numOfBadCaptures) if (curMove != lastMove)
return badCaptures[movesPicked++].move; return (curMove++)->move;
break; break;
case PH_QCAPTURES: case PH_QCAPTURES:
case PH_QCHECKS: case PH_QCHECKS:
while (movesPicked < numOfMoves) while (curMove != lastMove)
{ {
Move move = moves[movesPicked++].move; Move move = (curMove++)->move;
// Maybe postpone the legality check until after futility pruning? // Maybe postpone the legality check until after futility pruning?
if ( move != ttMoves[0] if ( move != ttMoves[0]
&& pos.pl_move_is_legal(move, pinned)) && pos.pl_move_is_legal(move, pinned))

View file

@ -81,9 +81,10 @@ private:
const History& H; const History& H;
Move ttMoves[2], killers[2]; Move ttMoves[2], killers[2];
const MovegenPhaseT* phasePtr; const MovegenPhaseT* phasePtr;
int phase, movesPicked, numOfMoves, numOfBadCaptures; int phase, movesPicked, numOfBadCaptures;
bool finished; bool finished;
Bitboard dc, pinned; Bitboard dc, pinned;
MoveStack *curMove, *lastMove;
MoveStack moves[256], badCaptures[64]; MoveStack moves[256], badCaptures[64];
}; };
@ -98,7 +99,7 @@ private:
/// a single reply to check. /// a single reply to check.
inline int MovePicker::number_of_moves() const { inline int MovePicker::number_of_moves() const {
return numOfMoves; return int(lastMove - moves);
} }
/// MovePicker::discovered_check_candidates() returns a bitboard containing /// MovePicker::discovered_check_candidates() returns a bitboard containing

View file

@ -1696,7 +1696,7 @@ bool Position::is_mate() const {
MoveStack moves[256]; MoveStack moves[256];
return is_check() && !generate_evasions(*this, moves, pinned_pieces(sideToMove)); return is_check() && (generate_evasions(*this, moves, pinned_pieces(sideToMove)) == moves);
} }
@ -1716,20 +1716,18 @@ bool Position::has_mate_threat(Color c) {
do_null_move(st1); do_null_move(st1);
MoveStack mlist[120]; MoveStack mlist[120];
int count;
bool result = false; bool result = false;
Bitboard dc = discovered_check_candidates(sideToMove); Bitboard dc = discovered_check_candidates(sideToMove);
Bitboard pinned = pinned_pieces(sideToMove); Bitboard pinned = pinned_pieces(sideToMove);
// Generate pseudo-legal non-capture and capture check moves // Generate pseudo-legal non-capture and capture check moves
count = generate_non_capture_checks(*this, mlist, dc); MoveStack* last = generate_non_capture_checks(*this, mlist, dc);
count += generate_captures(*this, mlist + count); last = generate_captures(*this, last);
// Loop through the moves, and see if one of them is mate // Loop through the moves, and see if one of them is mate
for (int i = 0; i < count; i++) for (MoveStack* cur = mlist; cur != last; cur++)
{ {
Move move = mlist[i].move; Move move = cur->move;
if (!pl_move_is_legal(move, pinned)) if (!pl_move_is_legal(move, pinned))
continue; continue;

View file

@ -1968,15 +1968,15 @@ namespace {
bool includeAllMoves = (searchMoves[0] == MOVE_NONE); bool includeAllMoves = (searchMoves[0] == MOVE_NONE);
// Generate all legal moves // Generate all legal moves
int lm_count = generate_legal_moves(pos, mlist); MoveStack* last = generate_legal_moves(pos, mlist);
// Add each move to the moves[] array // Add each move to the moves[] array
for (int i = 0; i < lm_count; i++) for (MoveStack* cur = mlist; cur != last; cur++)
{ {
bool includeMove = includeAllMoves; bool includeMove = includeAllMoves;
for (int k = 0; !includeMove && searchMoves[k] != MOVE_NONE; k++) for (int k = 0; !includeMove && searchMoves[k] != MOVE_NONE; k++)
includeMove = (searchMoves[k] == mlist[i].move); includeMove = (searchMoves[k] == cur->move);
if (!includeMove) if (!includeMove)
continue; continue;
@ -1985,7 +1985,7 @@ namespace {
StateInfo st; StateInfo st;
SearchStack ss[PLY_MAX_PLUS_2]; SearchStack ss[PLY_MAX_PLUS_2];
moves[count].move = mlist[i].move; moves[count].move = cur->move;
pos.do_move(moves[count].move, st); pos.do_move(moves[count].move, st);
moves[count].score = -qsearch(pos, ss, -VALUE_INFINITE, VALUE_INFINITE, Depth(0), 1, 0); moves[count].score = -qsearch(pos, ss, -VALUE_INFINITE, VALUE_INFINITE, Depth(0), 1, 0);
pos.undo_move(moves[count].move); pos.undo_move(moves[count].move);