/*1Stockfish, a UCI chess playing engine derived from Glaurung 2.12Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)34Stockfish is free software: you can redistribute it and/or modify5it under the terms of the GNU General Public License as published by6the Free Software Foundation, either version 3 of the License, or7(at your option) any later version.89Stockfish is distributed in the hope that it will be useful,10but WITHOUT ANY WARRANTY; without even the implied warranty of11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the12GNU General Public License for more details.1314You should have received a copy of the GNU General Public License15along with this program. If not, see <http://www.gnu.org/licenses/>.16*/1718#ifndef HISTORY_H_INCLUDED19#define HISTORY_H_INCLUDED2021#include <algorithm>22#include <array>23#include <cassert>24#include <cmath>25#include <cstdint>26#include <cstdlib>27#include <limits>28#include <type_traits> // IWYU pragma: keep2930#include "misc.h"31#include "position.h"3233namespace Stockfish {3435constexpr int PAWN_HISTORY_SIZE = 512; // has to be a power of 236constexpr int CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 237constexpr int CORRECTION_HISTORY_LIMIT = 1024;38constexpr int LOW_PLY_HISTORY_SIZE = 5;3940static_assert((PAWN_HISTORY_SIZE & (PAWN_HISTORY_SIZE - 1)) == 0,41"PAWN_HISTORY_SIZE has to be a power of 2");4243static_assert((CORRECTION_HISTORY_SIZE & (CORRECTION_HISTORY_SIZE - 1)) == 0,44"CORRECTION_HISTORY_SIZE has to be a power of 2");4546inline int pawn_history_index(const Position& pos) {47return pos.pawn_key() & (PAWN_HISTORY_SIZE - 1);48}4950inline int pawn_correction_history_index(const Position& pos) {51return pos.pawn_key() & (CORRECTION_HISTORY_SIZE - 1);52}5354inline int minor_piece_index(const Position& pos) {55return pos.minor_piece_key() & (CORRECTION_HISTORY_SIZE - 1);56}5758template<Color c>59inline int non_pawn_index(const Position& pos) {60return pos.non_pawn_key(c) & (CORRECTION_HISTORY_SIZE - 1);61}6263// StatsEntry is the container of various numerical statistics. We use a class64// instead of a naked value to directly call history update operator<<() on65// the entry. The first template parameter T is the base type of the array,66// and the second template parameter D limits the range of updates in [-D, D]67// when we update values with the << operator68template<typename T, int D>69class StatsEntry {7071static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");72static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");7374T entry;7576public:77StatsEntry& operator=(const T& v) {78entry = v;79return *this;80}81operator const T&() const { return entry; }8283void operator<<(int bonus) {84// Make sure that bonus is in range [-D, D]85int clampedBonus = std::clamp(bonus, -D, D);86entry += clampedBonus - entry * std::abs(clampedBonus) / D;8788assert(std::abs(entry) <= D);89}90};9192enum StatsType {93NoCaptures,94Captures95};9697template<typename T, int D, std::size_t... Sizes>98using Stats = MultiArray<StatsEntry<T, D>, Sizes...>;99100// ButterflyHistory records how often quiet moves have been successful or unsuccessful101// during the current search, and is used for reduction and move ordering decisions.102// It uses 2 tables (one for each color) indexed by the move's from and to squares,103// see https://www.chessprogramming.org/Butterfly_Boards104using ButterflyHistory = Stats<std::int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;105106// LowPlyHistory is adressed by play and move's from and to squares, used107// to improve move ordering near the root108using LowPlyHistory =109Stats<std::int16_t, 7183, LOW_PLY_HISTORY_SIZE, int(SQUARE_NB) * int(SQUARE_NB)>;110111// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]112using CapturePieceToHistory = Stats<std::int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;113114// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]115using PieceToHistory = Stats<std::int16_t, 30000, PIECE_NB, SQUARE_NB>;116117// ContinuationHistory is the combined history of a given pair of moves, usually118// the current one given a previous one. The nested history table is based on119// PieceToHistory instead of ButterflyBoards.120using ContinuationHistory = MultiArray<PieceToHistory, PIECE_NB, SQUARE_NB>;121122// PawnHistory is addressed by the pawn structure and a move's [piece][to]123using PawnHistory = Stats<std::int16_t, 8192, PAWN_HISTORY_SIZE, PIECE_NB, SQUARE_NB>;124125// Correction histories record differences between the static evaluation of126// positions and their search score. It is used to improve the static evaluation127// used by some search heuristics.128// see https://www.chessprogramming.org/Static_Evaluation_Correction_History129enum CorrHistType {130Pawn, // By color and pawn structure131Minor, // By color and positions of minor pieces (Knight, Bishop)132NonPawn, // By non-pawn material positions and color133PieceTo, // By [piece][to] move134Continuation, // Combined history of move pairs135};136137namespace Detail {138139template<CorrHistType>140struct CorrHistTypedef {141using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, CORRECTION_HISTORY_SIZE, COLOR_NB>;142};143144template<>145struct CorrHistTypedef<PieceTo> {146using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, PIECE_NB, SQUARE_NB>;147};148149template<>150struct CorrHistTypedef<Continuation> {151using type = MultiArray<CorrHistTypedef<PieceTo>::type, PIECE_NB, SQUARE_NB>;152};153154template<>155struct CorrHistTypedef<NonPawn> {156using type =157Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, CORRECTION_HISTORY_SIZE, COLOR_NB, COLOR_NB>;158};159160}161162template<CorrHistType T>163using CorrectionHistory = typename Detail::CorrHistTypedef<T>::type;164165using TTMoveHistory = StatsEntry<std::int16_t, 8192>;166167} // namespace Stockfish168169#endif // #ifndef HISTORY_H_INCLUDED170171172