mirror of
https://github.com/sockspls/badfish
synced 2025-04-30 08:43:09 +00:00
Compute checkers from scratch
This micro-optimization only complicates the code and provides no benefit. Removing it is even a speedup on my machine (i7-3770k, linux, gcc 4.9.1): stat test master diff mean 2,403,118 2,390,904 12,214 stdev 12,043 10,620 3,677 speedup 0.51% P(speedup>0) 100.0% No functional change.
This commit is contained in:
parent
901bfb1f55
commit
dc13004283
4 changed files with 17 additions and 41 deletions
|
@ -687,10 +687,10 @@ bool Position::gives_check(Move m, const CheckInfo& ci) const {
|
||||||
void Position::do_move(Move m, StateInfo& newSt) {
|
void Position::do_move(Move m, StateInfo& newSt) {
|
||||||
|
|
||||||
CheckInfo ci(*this);
|
CheckInfo ci(*this);
|
||||||
do_move(m, newSt, ci, gives_check(m, ci));
|
do_move(m, newSt, gives_check(m, ci));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool givesCheck) {
|
void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) {
|
||||||
|
|
||||||
assert(is_ok(m));
|
assert(is_ok(m));
|
||||||
assert(&newSt != st);
|
assert(&newSt != st);
|
||||||
|
@ -848,32 +848,8 @@ void Position::do_move(Move m, StateInfo& newSt, const CheckInfo& ci, bool gives
|
||||||
// Update the key with the final value
|
// Update the key with the final value
|
||||||
st->key = k;
|
st->key = k;
|
||||||
|
|
||||||
// Update checkers bitboard: piece must be already moved due to attacks_from()
|
// Calculate checkers bitboard (if move is check)
|
||||||
st->checkersBB = 0;
|
st->checkersBB = givesCheck ? attackers_to(king_square(them)) & pieces(us) : 0;
|
||||||
|
|
||||||
if (givesCheck)
|
|
||||||
{
|
|
||||||
if (type_of(m) != NORMAL)
|
|
||||||
st->checkersBB = attackers_to(king_square(them)) & pieces(us);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
// Direct checks
|
|
||||||
if (ci.checkSq[pt] & to)
|
|
||||||
st->checkersBB |= to;
|
|
||||||
|
|
||||||
// Discovered checks
|
|
||||||
if (ci.dcCandidates && (ci.dcCandidates & from))
|
|
||||||
{
|
|
||||||
assert(pt != QUEEN);
|
|
||||||
|
|
||||||
if (pt != ROOK)
|
|
||||||
st->checkersBB |= attacks_from<ROOK>(king_square(them)) & pieces(us, QUEEN, ROOK);
|
|
||||||
|
|
||||||
if (pt != BISHOP)
|
|
||||||
st->checkersBB |= attacks_from<BISHOP>(king_square(them)) & pieces(us, QUEEN, BISHOP);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sideToMove = ~sideToMove;
|
sideToMove = ~sideToMove;
|
||||||
|
|
||||||
|
|
|
@ -139,7 +139,7 @@ public:
|
||||||
|
|
||||||
// Doing and undoing moves
|
// Doing and undoing moves
|
||||||
void do_move(Move m, StateInfo& st);
|
void do_move(Move m, StateInfo& st);
|
||||||
void do_move(Move m, StateInfo& st, const CheckInfo& ci, bool givesCheck);
|
void do_move(Move m, StateInfo& st, bool givesCheck);
|
||||||
void undo_move(Move m);
|
void undo_move(Move m);
|
||||||
void do_null_move(StateInfo& st);
|
void do_null_move(StateInfo& st);
|
||||||
void undo_null_move();
|
void undo_null_move();
|
||||||
|
|
|
@ -158,7 +158,7 @@ uint64_t Search::perft(Position& pos, Depth depth) {
|
||||||
cnt = 1, nodes++;
|
cnt = 1, nodes++;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
pos.do_move(m, st, ci, pos.gives_check(m, ci));
|
pos.do_move(m, st, pos.gives_check(m, ci));
|
||||||
cnt = leaf ? MoveList<LEGAL>(pos).size() : perft<false>(pos, depth - ONE_PLY);
|
cnt = leaf ? MoveList<LEGAL>(pos).size() : perft<false>(pos, depth - ONE_PLY);
|
||||||
nodes += cnt;
|
nodes += cnt;
|
||||||
pos.undo_move(m);
|
pos.undo_move(m);
|
||||||
|
@ -694,7 +694,7 @@ namespace {
|
||||||
if (pos.legal(move, ci.pinned))
|
if (pos.legal(move, ci.pinned))
|
||||||
{
|
{
|
||||||
ss->currentMove = move;
|
ss->currentMove = move;
|
||||||
pos.do_move(move, st, ci, pos.gives_check(move, ci));
|
pos.do_move(move, st, pos.gives_check(move, ci));
|
||||||
value = -search<NonPV, false>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode);
|
value = -search<NonPV, false>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode);
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
if (value >= rbeta)
|
if (value >= rbeta)
|
||||||
|
@ -886,7 +886,7 @@ moves_loop: // When in check and at SpNode search starts from here
|
||||||
quietsSearched[quietCount++] = move;
|
quietsSearched[quietCount++] = move;
|
||||||
|
|
||||||
// Step 14. Make the move
|
// Step 14. Make the move
|
||||||
pos.do_move(move, st, ci, givesCheck);
|
pos.do_move(move, st, givesCheck);
|
||||||
|
|
||||||
// Step 15. Reduced depth search (LMR). If the move fails high it will be
|
// Step 15. Reduced depth search (LMR). If the move fails high it will be
|
||||||
// re-searched at full depth.
|
// re-searched at full depth.
|
||||||
|
@ -1247,7 +1247,7 @@ moves_loop: // When in check and at SpNode search starts from here
|
||||||
ss->currentMove = move;
|
ss->currentMove = move;
|
||||||
|
|
||||||
// Make and search the move
|
// Make and search the move
|
||||||
pos.do_move(move, st, ci, givesCheck);
|
pos.do_move(move, st, givesCheck);
|
||||||
value = givesCheck ? -qsearch<NT, true>(pos, ss+1, -beta, -alpha, depth - ONE_PLY)
|
value = givesCheck ? -qsearch<NT, true>(pos, ss+1, -beta, -alpha, depth - ONE_PLY)
|
||||||
: -qsearch<NT, false>(pos, ss+1, -beta, -alpha, depth - ONE_PLY);
|
: -qsearch<NT, false>(pos, ss+1, -beta, -alpha, depth - ONE_PLY);
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
|
|
|
@ -369,7 +369,7 @@ static int probe_ab(Position& pos, int alpha, int beta, int *success)
|
||||||
if (!pos.capture(capture) || type_of(capture) == ENPASSANT
|
if (!pos.capture(capture) || type_of(capture) == ENPASSANT
|
||||||
|| !pos.legal(capture, ci.pinned))
|
|| !pos.legal(capture, ci.pinned))
|
||||||
continue;
|
continue;
|
||||||
pos.do_move(capture, st, ci, pos.gives_check(capture, ci));
|
pos.do_move(capture, st, pos.gives_check(capture, ci));
|
||||||
v = -probe_ab(pos, -beta, -alpha, success);
|
v = -probe_ab(pos, -beta, -alpha, success);
|
||||||
pos.undo_move(capture);
|
pos.undo_move(capture);
|
||||||
if (*success == 0) return 0;
|
if (*success == 0) return 0;
|
||||||
|
@ -432,7 +432,7 @@ int Tablebases::probe_wdl(Position& pos, int *success)
|
||||||
if (type_of(capture) != ENPASSANT
|
if (type_of(capture) != ENPASSANT
|
||||||
|| !pos.legal(capture, ci.pinned))
|
|| !pos.legal(capture, ci.pinned))
|
||||||
continue;
|
continue;
|
||||||
pos.do_move(capture, st, ci, pos.gives_check(capture, ci));
|
pos.do_move(capture, st, pos.gives_check(capture, ci));
|
||||||
int v0 = -probe_ab(pos, -2, 2, success);
|
int v0 = -probe_ab(pos, -2, 2, success);
|
||||||
pos.undo_move(capture);
|
pos.undo_move(capture);
|
||||||
if (*success == 0) return 0;
|
if (*success == 0) return 0;
|
||||||
|
@ -495,7 +495,7 @@ static int probe_dtz_no_ep(Position& pos, int *success)
|
||||||
if (type_of(pos.moved_piece(move)) != PAWN || pos.capture(move)
|
if (type_of(pos.moved_piece(move)) != PAWN || pos.capture(move)
|
||||||
|| !pos.legal(move, ci.pinned))
|
|| !pos.legal(move, ci.pinned))
|
||||||
continue;
|
continue;
|
||||||
pos.do_move(move, st, ci, pos.gives_check(move, ci));
|
pos.do_move(move, st, pos.gives_check(move, ci));
|
||||||
int v = -probe_ab(pos, -2, -wdl + 1, success);
|
int v = -probe_ab(pos, -2, -wdl + 1, success);
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
if (*success == 0) return 0;
|
if (*success == 0) return 0;
|
||||||
|
@ -517,7 +517,7 @@ static int probe_dtz_no_ep(Position& pos, int *success)
|
||||||
if (pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN
|
if (pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN
|
||||||
|| !pos.legal(move, ci.pinned))
|
|| !pos.legal(move, ci.pinned))
|
||||||
continue;
|
continue;
|
||||||
pos.do_move(move, st, ci, pos.gives_check(move, ci));
|
pos.do_move(move, st, pos.gives_check(move, ci));
|
||||||
int v = -Tablebases::probe_dtz(pos, success);
|
int v = -Tablebases::probe_dtz(pos, success);
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
if (*success == 0) return 0;
|
if (*success == 0) return 0;
|
||||||
|
@ -536,7 +536,7 @@ static int probe_dtz_no_ep(Position& pos, int *success)
|
||||||
Move move = moves->move;
|
Move move = moves->move;
|
||||||
if (!pos.legal(move, ci.pinned))
|
if (!pos.legal(move, ci.pinned))
|
||||||
continue;
|
continue;
|
||||||
pos.do_move(move, st, ci, pos.gives_check(move, ci));
|
pos.do_move(move, st, pos.gives_check(move, ci));
|
||||||
if (st.rule50 == 0) {
|
if (st.rule50 == 0) {
|
||||||
if (wdl == -2) v = -1;
|
if (wdl == -2) v = -1;
|
||||||
else {
|
else {
|
||||||
|
@ -612,7 +612,7 @@ int Tablebases::probe_dtz(Position& pos, int *success)
|
||||||
if (type_of(capture) != ENPASSANT
|
if (type_of(capture) != ENPASSANT
|
||||||
|| !pos.legal(capture, ci.pinned))
|
|| !pos.legal(capture, ci.pinned))
|
||||||
continue;
|
continue;
|
||||||
pos.do_move(capture, st, ci, pos.gives_check(capture, ci));
|
pos.do_move(capture, st, pos.gives_check(capture, ci));
|
||||||
int v0 = -probe_ab(pos, -2, 2, success);
|
int v0 = -probe_ab(pos, -2, 2, success);
|
||||||
pos.undo_move(capture);
|
pos.undo_move(capture);
|
||||||
if (*success == 0) return 0;
|
if (*success == 0) return 0;
|
||||||
|
@ -702,7 +702,7 @@ bool Tablebases::root_probe(Position& pos, Search::RootMoveVector& rootMoves, Va
|
||||||
// Probe each move.
|
// Probe each move.
|
||||||
for (size_t i = 0; i < rootMoves.size(); i++) {
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
Move move = rootMoves[i].pv[0];
|
Move move = rootMoves[i].pv[0];
|
||||||
pos.do_move(move, st, ci, pos.gives_check(move, ci));
|
pos.do_move(move, st, pos.gives_check(move, ci));
|
||||||
int v = 0;
|
int v = 0;
|
||||||
if (pos.checkers() && dtz > 0) {
|
if (pos.checkers() && dtz > 0) {
|
||||||
ExtMove s[192];
|
ExtMove s[192];
|
||||||
|
@ -812,7 +812,7 @@ bool Tablebases::root_probe_wdl(Position& pos, Search::RootMoveVector& rootMoves
|
||||||
// Probe each move.
|
// Probe each move.
|
||||||
for (size_t i = 0; i < rootMoves.size(); i++) {
|
for (size_t i = 0; i < rootMoves.size(); i++) {
|
||||||
Move move = rootMoves[i].pv[0];
|
Move move = rootMoves[i].pv[0];
|
||||||
pos.do_move(move, st, ci, pos.gives_check(move, ci));
|
pos.do_move(move, st, pos.gives_check(move, ci));
|
||||||
int v = -Tablebases::probe_wdl(pos, &success);
|
int v = -Tablebases::probe_wdl(pos, &success);
|
||||||
pos.undo_move(move);
|
pos.undo_move(move);
|
||||||
if (!success) return false;
|
if (!success) return false;
|
||||||
|
|
Loading…
Add table
Reference in a new issue