Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/history.h
376 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2025 The Stockfish developers (see AUTHORS file)
4
5
Stockfish is free software: you can redistribute it and/or modify
6
it under the terms of the GNU General Public License as published by
7
the Free Software Foundation, either version 3 of the License, or
8
(at your option) any later version.
9
10
Stockfish is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
GNU General Public License for more details.
14
15
You should have received a copy of the GNU General Public License
16
along with this program. If not, see <http://www.gnu.org/licenses/>.
17
*/
18
19
#ifndef HISTORY_H_INCLUDED
20
#define HISTORY_H_INCLUDED
21
22
#include <algorithm>
23
#include <array>
24
#include <cassert>
25
#include <cmath>
26
#include <cstdint>
27
#include <cstdlib>
28
#include <limits>
29
#include <type_traits> // IWYU pragma: keep
30
31
#include "misc.h"
32
#include "position.h"
33
34
namespace Stockfish {
35
36
constexpr int PAWN_HISTORY_SIZE = 512; // has to be a power of 2
37
constexpr int CORRECTION_HISTORY_SIZE = 32768; // has to be a power of 2
38
constexpr int CORRECTION_HISTORY_LIMIT = 1024;
39
constexpr int LOW_PLY_HISTORY_SIZE = 5;
40
41
static_assert((PAWN_HISTORY_SIZE & (PAWN_HISTORY_SIZE - 1)) == 0,
42
"PAWN_HISTORY_SIZE has to be a power of 2");
43
44
static_assert((CORRECTION_HISTORY_SIZE & (CORRECTION_HISTORY_SIZE - 1)) == 0,
45
"CORRECTION_HISTORY_SIZE has to be a power of 2");
46
47
inline int pawn_history_index(const Position& pos) {
48
return pos.pawn_key() & (PAWN_HISTORY_SIZE - 1);
49
}
50
51
inline int pawn_correction_history_index(const Position& pos) {
52
return pos.pawn_key() & (CORRECTION_HISTORY_SIZE - 1);
53
}
54
55
inline int minor_piece_index(const Position& pos) {
56
return pos.minor_piece_key() & (CORRECTION_HISTORY_SIZE - 1);
57
}
58
59
template<Color c>
60
inline int non_pawn_index(const Position& pos) {
61
return pos.non_pawn_key(c) & (CORRECTION_HISTORY_SIZE - 1);
62
}
63
64
// StatsEntry is the container of various numerical statistics. We use a class
65
// instead of a naked value to directly call history update operator<<() on
66
// the entry. The first template parameter T is the base type of the array,
67
// and the second template parameter D limits the range of updates in [-D, D]
68
// when we update values with the << operator
69
template<typename T, int D>
70
class StatsEntry {
71
72
static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");
73
static_assert(D <= std::numeric_limits<T>::max(), "D overflows T");
74
75
T entry;
76
77
public:
78
StatsEntry& operator=(const T& v) {
79
entry = v;
80
return *this;
81
}
82
operator const T&() const { return entry; }
83
84
void operator<<(int bonus) {
85
// Make sure that bonus is in range [-D, D]
86
int clampedBonus = std::clamp(bonus, -D, D);
87
entry += clampedBonus - entry * std::abs(clampedBonus) / D;
88
89
assert(std::abs(entry) <= D);
90
}
91
};
92
93
enum StatsType {
94
NoCaptures,
95
Captures
96
};
97
98
template<typename T, int D, std::size_t... Sizes>
99
using Stats = MultiArray<StatsEntry<T, D>, Sizes...>;
100
101
// ButterflyHistory records how often quiet moves have been successful or unsuccessful
102
// during the current search, and is used for reduction and move ordering decisions.
103
// It uses 2 tables (one for each color) indexed by the move's from and to squares,
104
// see https://www.chessprogramming.org/Butterfly_Boards
105
using ButterflyHistory = Stats<std::int16_t, 7183, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)>;
106
107
// LowPlyHistory is adressed by play and move's from and to squares, used
108
// to improve move ordering near the root
109
using LowPlyHistory =
110
Stats<std::int16_t, 7183, LOW_PLY_HISTORY_SIZE, int(SQUARE_NB) * int(SQUARE_NB)>;
111
112
// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
113
using CapturePieceToHistory = Stats<std::int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;
114
115
// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
116
using PieceToHistory = Stats<std::int16_t, 30000, PIECE_NB, SQUARE_NB>;
117
118
// ContinuationHistory is the combined history of a given pair of moves, usually
119
// the current one given a previous one. The nested history table is based on
120
// PieceToHistory instead of ButterflyBoards.
121
using ContinuationHistory = MultiArray<PieceToHistory, PIECE_NB, SQUARE_NB>;
122
123
// PawnHistory is addressed by the pawn structure and a move's [piece][to]
124
using PawnHistory = Stats<std::int16_t, 8192, PAWN_HISTORY_SIZE, PIECE_NB, SQUARE_NB>;
125
126
// Correction histories record differences between the static evaluation of
127
// positions and their search score. It is used to improve the static evaluation
128
// used by some search heuristics.
129
// see https://www.chessprogramming.org/Static_Evaluation_Correction_History
130
enum CorrHistType {
131
Pawn, // By color and pawn structure
132
Minor, // By color and positions of minor pieces (Knight, Bishop)
133
NonPawn, // By non-pawn material positions and color
134
PieceTo, // By [piece][to] move
135
Continuation, // Combined history of move pairs
136
};
137
138
namespace Detail {
139
140
template<CorrHistType>
141
struct CorrHistTypedef {
142
using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, CORRECTION_HISTORY_SIZE, COLOR_NB>;
143
};
144
145
template<>
146
struct CorrHistTypedef<PieceTo> {
147
using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, PIECE_NB, SQUARE_NB>;
148
};
149
150
template<>
151
struct CorrHistTypedef<Continuation> {
152
using type = MultiArray<CorrHistTypedef<PieceTo>::type, PIECE_NB, SQUARE_NB>;
153
};
154
155
template<>
156
struct CorrHistTypedef<NonPawn> {
157
using type =
158
Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, CORRECTION_HISTORY_SIZE, COLOR_NB, COLOR_NB>;
159
};
160
161
}
162
163
template<CorrHistType T>
164
using CorrectionHistory = typename Detail::CorrHistTypedef<T>::type;
165
166
using TTMoveHistory = StatsEntry<std::int16_t, 8192>;
167
168
} // namespace Stockfish
169
170
#endif // #ifndef HISTORY_H_INCLUDED
171
172