Skip to content

Commit

Permalink
Fix null move issue
Browse files Browse the repository at this point in the history
Fix altering for stats landing on B1 Square after a null move and fix considering counter-moves on A1 for root node.

fixes #4333 by preventing calls to from_sq and to_sq functions over null-moves and none-moves.

closes #4448

bench: 4980082
  • Loading branch information
peregrineshahin authored and vondele committed Mar 19, 2023
1 parent f0556dc commit 515b66f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
19 changes: 12 additions & 7 deletions src/search.cpp
Expand Up @@ -605,7 +605,7 @@ namespace {
(ss+2)->killers[0] = (ss+2)->killers[1] = MOVE_NONE;
(ss+2)->cutoffCnt = 0;
ss->doubleExtensions = (ss-1)->doubleExtensions;
Square prevSq = to_sq((ss-1)->currentMove);
Square prevSq = is_ok((ss-1)->currentMove) ? to_sq((ss-1)->currentMove) : SQ_NONE;

// Initialize statScore to zero for the grandchildren of the current position.
// So statScore is shared between all grandchildren and only the first grandchild
Expand Down Expand Up @@ -647,7 +647,7 @@ namespace {
update_quiet_stats(pos, ss, ttMove, stat_bonus(depth));

// Extra penalty for early quiet moves of the previous ply (~0 Elo on STC, ~2 Elo on LTC)
if ((ss-1)->moveCount <= 2 && !priorCapture)
if (prevSq != SQ_NONE && (ss-1)->moveCount <= 2 && !priorCapture)
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -stat_bonus(depth + 1));
}
// Penalty for a quiet ttMove that fails low (~1 Elo)
Expand Down Expand Up @@ -935,7 +935,7 @@ namespace {
nullptr , (ss-4)->continuationHistory,
nullptr , (ss-6)->continuationHistory };

Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq];
Move countermove = prevSq != SQ_NONE ? thisThread->counterMoves[pos.piece_on(prevSq)][prevSq] : MOVE_NONE;

MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory,
&captureHistory,
Expand Down Expand Up @@ -1383,7 +1383,7 @@ namespace {
quietsSearched, quietCount, capturesSearched, captureCount, depth);

// Bonus for prior countermove that caused the fail low
else if (!priorCapture)
else if (!priorCapture && prevSq != SQ_NONE)
{
int bonus = (depth > 5) + (PvNode || cutNode) + (bestValue < alpha - 97 * depth) + ((ss-1)->moveCount > 10);
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, stat_bonus(depth) * bonus);
Expand Down Expand Up @@ -1525,7 +1525,7 @@ namespace {
// to search the moves. Because the depth is <= 0 here, only captures,
// queen promotions, and other checks (only if depth >= DEPTH_QS_CHECKS)
// will be generated.
Square prevSq = to_sq((ss-1)->currentMove);
Square prevSq = (ss-1)->currentMove != MOVE_NULL ? to_sq((ss-1)->currentMove) : SQ_NONE;
MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory,
&thisThread->captureHistory,
contHist,
Expand Down Expand Up @@ -1714,7 +1714,8 @@ namespace {
Thread* thisThread = pos.this_thread();
CapturePieceToHistory& captureHistory = thisThread->captureHistory;
Piece moved_piece = pos.moved_piece(bestMove);
PieceType captured = type_of(pos.piece_on(to_sq(bestMove)));
PieceType captured;

int bonus1 = stat_bonus(depth + 1);

if (!pos.capture_stage(bestMove))
Expand All @@ -1733,12 +1734,16 @@ namespace {
}
}
else
{
// Increase stats for the best move in case it was a capture move
captured = type_of(pos.piece_on(to_sq(bestMove)));
captureHistory[moved_piece][to_sq(bestMove)][captured] << bonus1;
}

// Extra penalty for a quiet early move that was not a TT move or
// main killer move in previous ply when it gets refuted.
if ( ((ss-1)->moveCount == 1 + (ss-1)->ttHit || ((ss-1)->currentMove == (ss-1)->killers[0]))
if ( prevSq != SQ_NONE
&& ((ss-1)->moveCount == 1 + (ss-1)->ttHit || ((ss-1)->currentMove == (ss-1)->killers[0]))
&& !pos.captured_piece())
update_continuation_histories(ss-1, pos.piece_on(prevSq), prevSq, -bonus1);

Expand Down
10 changes: 6 additions & 4 deletions src/types.h
Expand Up @@ -416,6 +416,10 @@ inline Color color_of(Piece pc) {
return Color(pc >> 3);
}

constexpr bool is_ok(Move m) {
return m != MOVE_NONE && m != MOVE_NULL;
}

constexpr bool is_ok(Square s) {
return s >= SQ_A1 && s <= SQ_H8;
}
Expand Down Expand Up @@ -445,10 +449,12 @@ constexpr Direction pawn_push(Color c) {
}

constexpr Square from_sq(Move m) {
assert(is_ok(m));
return Square((m >> 6) & 0x3F);
}

constexpr Square to_sq(Move m) {
assert(is_ok(m));
return Square(m & 0x3F);
}

Expand All @@ -473,10 +479,6 @@ constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
}

constexpr bool is_ok(Move m) {
return from_sq(m) != to_sq(m); // Catch MOVE_NULL and MOVE_NONE
}

/// Based on a congruential pseudo random number generator
constexpr Key make_key(uint64_t seed) {
return seed * 6364136223846793005ULL + 1442695040888963407ULL;
Expand Down
6 changes: 3 additions & 3 deletions src/uci.cpp
Expand Up @@ -358,15 +358,15 @@ std::string UCI::square(Square s) {

string UCI::move(Move m, bool chess960) {

Square from = from_sq(m);
Square to = to_sq(m);

if (m == MOVE_NONE)
return "(none)";

if (m == MOVE_NULL)
return "0000";

Square from = from_sq(m);
Square to = to_sq(m);

if (type_of(m) == CASTLING && !chess960)
to = make_square(to > from ? FILE_G : FILE_C, rank_of(from));

Expand Down

0 comments on commit 515b66f

Please sign in to comment.