From ed604600042a4f2c0023ac9a189215e50fc7aa0f Mon Sep 17 00:00:00 2001 From: Michael Chaly Date: Fri, 15 Mar 2024 18:55:40 +0300 Subject: [PATCH] Clamp history bonus to stats range Before, one always had to keep track of the bonus one assigns to a history to stop the stats from overflowing. This is a quality of life improvement. Since this would often go unnoticed during benching. Passed non-regression bounds: https://tests.stockfishchess.org/tests/view/65ef2af40ec64f0526c44cbc LLR: 2.94 (-2.94,2.94) <-1.75,0.25> Total: 179232 W: 46513 L: 46450 D: 86269 Ptnml(0-2): 716, 20323, 47452, 20432, 693 closes https://github.com/official-stockfish/Stockfish/pull/5116 No functional change --- src/movepick.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/movepick.h b/src/movepick.h index 357918a9..a16fdcd2 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -19,17 +19,17 @@ #ifndef MOVEPICK_H_INCLUDED #define MOVEPICK_H_INCLUDED +#include #include #include -#include #include #include #include #include // IWYU pragma: keep #include "movegen.h" -#include "types.h" #include "position.h" +#include "types.h" namespace Stockfish { @@ -69,10 +69,11 @@ class StatsEntry { operator const T&() const { return entry; } void operator<<(int bonus) { - assert(std::abs(bonus) <= D); // Ensure range is [-D, D] static_assert(D <= std::numeric_limits::max(), "D overflows T"); - entry += bonus - entry * std::abs(bonus) / D; + // Make sure that bonus is in range [-D, D] + int clampedBonus = std::clamp(bonus, -D, D); + entry += clampedBonus - entry * std::abs(clampedBonus) / D; assert(std::abs(entry) <= D); }