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

Always have counterMoves associated

Simplifies away all associated checks, leading to a ~0.5% speedup.
The code now explicitly checks if moves are OK, rather than using nullptr checks.

Verified for no regression:

LLR: 2.96 (-2.94,2.94) [-3.00,1.00]
Total: 32218 W: 5762 L: 5660 D: 20796

No functional change

Closes #1021
This commit is contained in:
Joost VandeVondele 2017-03-08 18:35:23 -08:00 committed by Joona Kiiski
parent cc76524c2e
commit d490bb9973
3 changed files with 26 additions and 29 deletions

View file

@ -149,9 +149,9 @@ void MovePicker::score<QUIETS>() {
Color c = pos.side_to_move(); Color c = pos.side_to_move();
for (auto& m : *this) for (auto& m : *this)
m.value = (cmh ? (*cmh)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO) m.value = (*cmh)[pos.moved_piece(m)][to_sq(m)]
+ (fmh ? (*fmh)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO) + (*fmh)[pos.moved_piece(m)][to_sq(m)]
+ (fmh2 ? (*fmh2)[pos.moved_piece(m)][to_sq(m)] : VALUE_ZERO) + (*fmh2)[pos.moved_piece(m)][to_sq(m)]
+ history.get(c, m); + history.get(c, m);
} }

View file

@ -377,7 +377,7 @@ void Position::set_state(StateInfo* si) const {
/// Position::set() is an overload to initialize the position object with /// Position::set() is an overload to initialize the position object with
/// the given endgame code string like "KBPKN". It is manily an helper to /// the given endgame code string like "KBPKN". It is mainly a helper to
/// get the material key out of an endgame code. Position is not playable, /// get the material key out of an endgame code. Position is not playable,
/// indeed is even not guaranteed to be legal. /// indeed is even not guaranteed to be legal.

View file

@ -324,6 +324,8 @@ void Thread::search() {
MainThread* mainThread = (this == Threads.main() ? Threads.main() : nullptr); MainThread* mainThread = (this == Threads.main() ? Threads.main() : nullptr);
std::memset(ss-4, 0, 7 * sizeof(Stack)); std::memset(ss-4, 0, 7 * sizeof(Stack));
for(int i = -4; i < 0; i++)
(ss+i)->counterMoves = &this->counterMoveHistory[NO_PIECE][0]; // use as sentinel.
bestValue = delta = alpha = -VALUE_INFINITE; bestValue = delta = alpha = -VALUE_INFINITE;
beta = VALUE_INFINITE; beta = VALUE_INFINITE;
@ -593,7 +595,7 @@ namespace {
assert(0 <= ss->ply && ss->ply < MAX_PLY); assert(0 <= ss->ply && ss->ply < MAX_PLY);
ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE; ss->currentMove = (ss+1)->excludedMove = bestMove = MOVE_NONE;
ss->counterMoves = nullptr; ss->counterMoves = &thisThread->counterMoveHistory[NO_PIECE][0];
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE; (ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE;
Square prevSq = to_sq((ss-1)->currentMove); Square prevSq = to_sq((ss-1)->currentMove);
@ -729,14 +731,15 @@ namespace {
&& (ss->staticEval >= beta - 35 * (depth / ONE_PLY - 6) || depth >= 13 * ONE_PLY) && (ss->staticEval >= beta - 35 * (depth / ONE_PLY - 6) || depth >= 13 * ONE_PLY)
&& pos.non_pawn_material(pos.side_to_move())) && pos.non_pawn_material(pos.side_to_move()))
{ {
ss->currentMove = MOVE_NULL;
ss->counterMoves = nullptr;
assert(eval - beta >= 0); assert(eval - beta >= 0);
// Null move dynamic reduction based on depth and value // Null move dynamic reduction based on depth and value
Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY; Depth R = ((823 + 67 * depth / ONE_PLY) / 256 + std::min((eval - beta) / PawnValueMg, 3)) * ONE_PLY;
ss->currentMove = MOVE_NULL;
ss->counterMoves = &thisThread->counterMoveHistory[NO_PIECE][0];
pos.do_null_move(st); pos.do_null_move(st);
Value nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -beta+1) Value nullValue = depth-R < ONE_PLY ? -qsearch<NonPV, false>(pos, ss+1, -beta, -beta+1)
: - search<NonPV>(pos, ss+1, -beta, -beta+1, depth-R, !cutNode, true); : - search<NonPV>(pos, ss+1, -beta, -beta+1, depth-R, !cutNode, true);
@ -771,8 +774,7 @@ namespace {
Depth rdepth = depth - 4 * ONE_PLY; Depth rdepth = depth - 4 * ONE_PLY;
assert(rdepth >= ONE_PLY); assert(rdepth >= ONE_PLY);
assert((ss-1)->currentMove != MOVE_NONE); assert(is_ok((ss-1)->currentMove));
assert((ss-1)->currentMove != MOVE_NULL);
MovePicker mp(pos, ttMove, rbeta - ss->staticEval); MovePicker mp(pos, ttMove, rbeta - ss->staticEval);
@ -781,6 +783,7 @@ namespace {
{ {
ss->currentMove = move; ss->currentMove = move;
ss->counterMoves = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)]; ss->counterMoves = &thisThread->counterMoveHistory[pos.moved_piece(move)][to_sq(move)];
pos.do_move(move, st); pos.do_move(move, st);
value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode, false); value = -search<NonPV>(pos, ss+1, -rbeta, -rbeta+1, rdepth, !cutNode, false);
pos.undo_move(move); pos.undo_move(move);
@ -806,6 +809,9 @@ moves_loop: // When in check search starts from here
const CounterMoveStats* cmh = (ss-1)->counterMoves; const CounterMoveStats* cmh = (ss-1)->counterMoves;
const CounterMoveStats* fmh = (ss-2)->counterMoves; const CounterMoveStats* fmh = (ss-2)->counterMoves;
const CounterMoveStats* fmh2 = (ss-4)->counterMoves; const CounterMoveStats* fmh2 = (ss-4)->counterMoves;
const bool cm_ok = is_ok((ss-1)->currentMove);
const bool fm_ok = is_ok((ss-2)->currentMove);
const bool fm2_ok = is_ok((ss-4)->currentMove);
MovePicker mp(pos, ttMove, depth, ss); MovePicker mp(pos, ttMove, depth, ss);
value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc
@ -905,9 +911,9 @@ moves_loop: // When in check search starts from here
// Countermoves based pruning // Countermoves based pruning
if ( lmrDepth < 3 if ( lmrDepth < 3
&& (!cmh || (*cmh )[moved_piece][to_sq(move)] < VALUE_ZERO) && (((*cmh )[moved_piece][to_sq(move)] < VALUE_ZERO) || !cm_ok)
&& (!fmh || (*fmh )[moved_piece][to_sq(move)] < VALUE_ZERO) && (((*fmh )[moved_piece][to_sq(move)] < VALUE_ZERO) || !fm_ok)
&& (!fmh2 || (*fmh2)[moved_piece][to_sq(move)] < VALUE_ZERO || (cmh && fmh))) && (((*fmh2)[moved_piece][to_sq(move)] < VALUE_ZERO) || !fm2_ok || (cm_ok && fm_ok)))
continue; continue;
// Futility pruning: parent node // Futility pruning: parent node
@ -967,9 +973,9 @@ moves_loop: // When in check search starts from here
&& !pos.see_ge(make_move(to_sq(move), from_sq(move)), VALUE_ZERO)) && !pos.see_ge(make_move(to_sq(move), from_sq(move)), VALUE_ZERO))
r -= 2 * ONE_PLY; r -= 2 * ONE_PLY;
ss->history = (cmh ? (*cmh )[moved_piece][to_sq(move)] : VALUE_ZERO) ss->history = (*cmh )[moved_piece][to_sq(move)]
+ (fmh ? (*fmh )[moved_piece][to_sq(move)] : VALUE_ZERO) + (*fmh )[moved_piece][to_sq(move)]
+ (fmh2 ? (*fmh2)[moved_piece][to_sq(move)] : VALUE_ZERO) + (*fmh2)[moved_piece][to_sq(move)]
+ thisThread->history.get(~pos.side_to_move(), move) + thisThread->history.get(~pos.side_to_move(), move)
- 4000; // Correction factor - 4000; // Correction factor
@ -1112,7 +1118,7 @@ moves_loop: // When in check search starts from here
// Bonus for prior countermove that caused the fail low // Bonus for prior countermove that caused the fail low
else if ( depth >= 3 * ONE_PLY else if ( depth >= 3 * ONE_PLY
&& !pos.captured_piece() && !pos.captured_piece()
&& is_ok((ss-1)->currentMove)) && cm_ok)
update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth)); update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth));
tte->save(posKey, value_to_tt(bestValue, ss->ply), tte->save(posKey, value_to_tt(bestValue, ss->ply),
@ -1372,18 +1378,9 @@ moves_loop: // When in check search starts from here
void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus) { void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus) {
CounterMoveStats* cmh = (ss-1)->counterMoves; for (int i : {1, 2, 4})
CounterMoveStats* fmh1 = (ss-2)->counterMoves; if (is_ok((ss-i)->currentMove))
CounterMoveStats* fmh2 = (ss-4)->counterMoves; (ss-i)->counterMoves->update(pc, s, bonus);
if (cmh)
cmh->update(pc, s, bonus);
if (fmh1)
fmh1->update(pc, s, bonus);
if (fmh2)
fmh2->update(pc, s, bonus);
} }
@ -1403,7 +1400,7 @@ moves_loop: // When in check search starts from here
thisThread->history.update(c, move, bonus); thisThread->history.update(c, move, bonus);
update_cm_stats(ss, pos.moved_piece(move), to_sq(move), bonus); update_cm_stats(ss, pos.moved_piece(move), to_sq(move), bonus);
if ((ss-1)->counterMoves) if (is_ok((ss-1)->currentMove))
{ {
Square prevSq = to_sq((ss-1)->currentMove); Square prevSq = to_sq((ss-1)->currentMove);
thisThread->counterMoves.update(pos.piece_on(prevSq), prevSq, move); thisThread->counterMoves.update(pos.piece_on(prevSq), prevSq, move);