Skip to content

Commit

Permalink
Simplify stats update
Browse files Browse the repository at this point in the history
Simplify code by moving countermove and follow-up move
history update into procedure.

No functional change.
  • Loading branch information
hxim authored and mcostalba committed Aug 27, 2016
1 parent 3e739f8 commit 9bd856e
Showing 1 changed file with 20 additions and 38 deletions.
58 changes: 20 additions & 38 deletions src/search.cpp
Expand Up @@ -168,8 +168,8 @@ namespace {
Value value_to_tt(Value v, int ply);
Value value_from_tt(Value v, int ply);
void update_pv(Move* pv, Move move, Move* childPv);
void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus);
void update_stats(const Position& pos, Stack* ss, Move move, Move* quiets, int quietsCnt, Value bonus);
void update_opponent_stats(const Position& pos, Stack* ss, Value bonus);
void check_time();

} // namespace
Expand Down Expand Up @@ -651,7 +651,8 @@ namespace {
if ((ss-1)->moveCount == 1 && !pos.captured_piece_type())
{
Value penalty = Value(d * d + 4 * d + 1);
update_opponent_stats(pos, ss, -penalty);
Square prevSq = to_sq((ss-1)->currentMove);
update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -penalty);
}
}
return ttValue;
Expand Down Expand Up @@ -1141,7 +1142,8 @@ namespace {
if ((ss-1)->moveCount == 1 && !pos.captured_piece_type())
{
Value penalty = Value(d * d + 4 * d + 1);
update_opponent_stats(pos, ss, -penalty);
Square prevSq = to_sq((ss-1)->currentMove);
update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, -penalty);
}
}
// Bonus for prior countermove that caused the fail low
Expand All @@ -1151,7 +1153,8 @@ namespace {
{
int d = depth / ONE_PLY;
Value bonus = Value(d * d + 2 * d - 2);
update_opponent_stats(pos, ss, bonus);
Square prevSq = to_sq((ss-1)->currentMove);
update_cm_stats(ss-1, pos.piece_on(prevSq), prevSq, bonus);
}

tte->save(posKey, value_to_tt(bestValue, ss->ply),
Expand Down Expand Up @@ -1411,25 +1414,22 @@ namespace {
}


// update_opponent_stats() updates countermoves for prior opponent move, i.e.
// (ss-1)->currentMove. Called for both capture and non-capture moves.

void update_opponent_stats(const Position& pos, Stack* ss, Value bonus) {
// update_cm_stats() updates countermove and follow-up move history

Square prevSq = to_sq((ss-1)->currentMove);
void update_cm_stats(Stack* ss, Piece pc, Square s, Value bonus) {

CounterMoveStats* cmh = (ss-2)->counterMoves;
CounterMoveStats* fmh1 = (ss-3)->counterMoves;
CounterMoveStats* fmh2 = (ss-5)->counterMoves;
CounterMoveStats* cmh = (ss-1)->counterMoves;
CounterMoveStats* fmh1 = (ss-2)->counterMoves;
CounterMoveStats* fmh2 = (ss-4)->counterMoves;

if (cmh)
cmh->update(pos.piece_on(prevSq), prevSq, bonus);
cmh->update(pc, s, bonus);

if (fmh1)
fmh1->update(pos.piece_on(prevSq), prevSq, bonus);
fmh1->update(pc, s, bonus);

if (fmh2)
fmh2->update(pos.piece_on(prevSq), prevSq, bonus);
fmh2->update(pc, s, bonus);
}


Expand All @@ -1445,42 +1445,24 @@ namespace {
ss->killers[0] = move;
}

CounterMoveStats* cmh = (ss-1)->counterMoves;
CounterMoveStats* fmh1 = (ss-2)->counterMoves;
CounterMoveStats* fmh2 = (ss-4)->counterMoves;

Color c = pos.side_to_move();
Thread* thisThread = pos.this_thread();
thisThread->history.update(pos.moved_piece(move), to_sq(move), bonus);
thisThread->fromTo.update(c, move, bonus);
thisThread->history.update(pos.moved_piece(move), to_sq(move), bonus);
update_cm_stats(ss, pos.moved_piece(move), to_sq(move), bonus);

if (cmh)
if ((ss-1)->counterMoves)
{
Square prevSq = to_sq((ss-1)->currentMove);
thisThread->counterMoves.update(pos.piece_on(prevSq), prevSq, move);
cmh->update(pos.moved_piece(move), to_sq(move), bonus);
}

if (fmh1)
fmh1->update(pos.moved_piece(move), to_sq(move), bonus);

if (fmh2)
fmh2->update(pos.moved_piece(move), to_sq(move), bonus);

// Decrease all the other played quiet moves
for (int i = 0; i < quietsCnt; ++i)
{
thisThread->history.update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
thisThread->fromTo.update(c, quiets[i], -bonus);

if (cmh)
cmh->update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);

if (fmh1)
fmh1->update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);

if (fmh2)
fmh2->update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
thisThread->history.update(pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
update_cm_stats(ss, pos.moved_piece(quiets[i]), to_sq(quiets[i]), -bonus);
}
}

Expand Down

0 comments on commit 9bd856e

Please sign in to comment.