From e0fb783c30f86d9ff01328b14fefded21492677e Mon Sep 17 00:00:00 2001 From: Joost VandeVondele Date: Wed, 31 Dec 2025 15:55:00 +0100 Subject: [PATCH] Fix incorrect initialization Fixes https://github.com/official-stockfish/Stockfish/issues/6505 Missing initialization seemingly resulting in side effects, as discussed in the issue. Credit to Sopel for spotting the bug. PR used as a testcase for CoPilot, doing the right thing https://github.com/official-stockfish/Stockfish/pull/6478#discussion_r2655467218 closes https://github.com/official-stockfish/Stockfish/pull/6511 No functional change --- src/history.h | 6 +++--- src/search.cpp | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/history.h b/src/history.h index 9811d0397..3aa9ef623 100644 --- a/src/history.h +++ b/src/history.h @@ -105,10 +105,10 @@ struct DynStats { data = make_unique_large_page(size); } // Sets all values in the range to 0 - void clear_range(int value, size_t threadIdx) { - size_t start = threadIdx * SizeMultiplier; + void clear_range(int value, size_t threadIdx, size_t numaTotal) { + size_t start = uint64_t(threadIdx) * size / numaTotal; assert(start < size); - size_t end = std::min(start + SizeMultiplier, size); + size_t end = threadIdx + 1 == numaTotal ? size : uint64_t(threadIdx + 1) * size / numaTotal; while (start < end) data[start++].fill(value); diff --git a/src/search.cpp b/src/search.cpp index 87a96cab8..c1a7d5880 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -582,8 +582,8 @@ void Search::Worker::clear() { captureHistory.fill(-689); // Each thread is responsible for clearing their part of shared history - sharedHistory.correctionHistory.clear_range(0, numaThreadIdx); - sharedHistory.pawnHistory.clear_range(-1238, numaThreadIdx); + sharedHistory.correctionHistory.clear_range(0, numaThreadIdx, numaTotal); + sharedHistory.pawnHistory.clear_range(-1238, numaThreadIdx, numaTotal); ttMoveHistory = 0;