Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/Stockfish
Path: blob/master/src/history.h
634 views
1
/*
2
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3
Copyright (C) 2004-2026 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 <atomic>
25
#include <cassert>
26
#include <cmath>
27
#include <cstdint>
28
#include <cstdlib>
29
#include <limits>
30
#include <type_traits> // IWYU pragma: keep
31
32
#include "memory.h"
33
#include "misc.h"
34
#include "position.h"
35
36
namespace Stockfish {
37
38
constexpr int PAWN_HISTORY_BASE_SIZE = 8192; // has to be a power of 2
39
constexpr int UINT_16_HISTORY_SIZE = std::numeric_limits<uint16_t>::max() + 1;
40
constexpr int CORRHIST_BASE_SIZE = UINT_16_HISTORY_SIZE;
41
constexpr int CORRECTION_HISTORY_LIMIT = 1024;
42
constexpr int LOW_PLY_HISTORY_SIZE = 5;
43
44
static_assert((PAWN_HISTORY_BASE_SIZE & (PAWN_HISTORY_BASE_SIZE - 1)) == 0,
45
"PAWN_HISTORY_BASE_SIZE has to be a power of 2");
46
47
static_assert((CORRHIST_BASE_SIZE & (CORRHIST_BASE_SIZE - 1)) == 0,
48
"CORRHIST_BASE_SIZE has to be a power of 2");
49
50
// StatsEntry is the container of various numerical statistics. We use a class
51
// instead of a naked value to directly call history update operator<<() on
52
// the entry. The first template parameter T is the base type of the array,
53
// and the second template parameter D limits the range of updates in [-D, D]
54
// when we update values with the << operator
55
template<typename T, int D, bool Atomic = false>
56
struct StatsEntry {
57
static_assert(std::is_arithmetic_v<T>, "Not an arithmetic type");
58
59
private:
60
std::conditional_t<Atomic, std::atomic<T>, T> entry;
61
62
public:
63
void operator=(const T& v) {
64
if constexpr (Atomic)
65
entry.store(v, std::memory_order_relaxed);
66
else
67
entry = v;
68
}
69
70
operator T() const {
71
if constexpr (Atomic)
72
return entry.load(std::memory_order_relaxed);
73
else
74
return entry;
75
}
76
77
void operator<<(int bonus) {
78
// Make sure that bonus is in range [-D, D]
79
int clampedBonus = std::clamp(bonus, -D, D);
80
T val = *this;
81
*this = val + clampedBonus - val * std::abs(clampedBonus) / D;
82
83
assert(std::abs(T(*this)) <= D);
84
}
85
};
86
87
enum StatsType {
88
NoCaptures,
89
Captures
90
};
91
92
template<typename T, int D, std::size_t... Sizes>
93
using Stats = MultiArray<StatsEntry<T, D>, Sizes...>;
94
95
template<typename T, int D, std::size_t... Sizes>
96
using AtomicStats = MultiArray<StatsEntry<T, D, true>, Sizes...>;
97
98
// DynStats is a dynamically sized array of Stats, used for thread-shared histories
99
// which should scale with the total number of threads. The SizeMultiplier gives
100
// the per-thread allocation count of T.
101
template<typename T, int SizeMultiplier>
102
struct DynStats {
103
explicit DynStats(size_t s) {
104
size = s * SizeMultiplier;
105
data = make_unique_large_page<T[]>(size);
106
}
107
// Sets all values in the range to 0
108
void clear_range(int value, size_t threadIdx, size_t numaTotal) {
109
size_t start = uint64_t(threadIdx) * size / numaTotal;
110
assert(start < size);
111
size_t end = threadIdx + 1 == numaTotal ? size : uint64_t(threadIdx + 1) * size / numaTotal;
112
113
while (start < end)
114
data[start++].fill(value);
115
}
116
size_t get_size() const { return size; }
117
T& operator[](size_t index) {
118
assert(index < size);
119
return data.get()[index];
120
}
121
const T& operator[](size_t index) const {
122
assert(index < size);
123
return data.get()[index];
124
}
125
126
private:
127
size_t size;
128
LargePagePtr<T[]> data;
129
};
130
131
// ButterflyHistory records how often quiet moves have been successful or unsuccessful
132
// during the current search, and is used for reduction and move ordering decisions.
133
// It uses 2 tables (one for each color) indexed by the move's from and to squares,
134
// see https://www.chessprogramming.org/Butterfly_Boards
135
using ButterflyHistory = Stats<std::int16_t, 7183, COLOR_NB, UINT_16_HISTORY_SIZE>;
136
137
// LowPlyHistory is addressed by ply and move's from and to squares, used
138
// to improve move ordering near the root
139
using LowPlyHistory = Stats<std::int16_t, 7183, LOW_PLY_HISTORY_SIZE, UINT_16_HISTORY_SIZE>;
140
141
// CapturePieceToHistory is addressed by a move's [piece][to][captured piece type]
142
using CapturePieceToHistory = Stats<std::int16_t, 10692, PIECE_NB, SQUARE_NB, PIECE_TYPE_NB>;
143
144
// PieceToHistory is like ButterflyHistory but is addressed by a move's [piece][to]
145
using PieceToHistory = Stats<std::int16_t, 30000, PIECE_NB, SQUARE_NB>;
146
147
// ContinuationHistory is the combined history of a given pair of moves, usually
148
// the current one given a previous one. The nested history table is based on
149
// PieceToHistory instead of ButterflyBoards.
150
using ContinuationHistory = MultiArray<PieceToHistory, PIECE_NB, SQUARE_NB>;
151
152
// PawnHistory is addressed by the pawn structure and a move's [piece][to]
153
using PawnHistory =
154
DynStats<AtomicStats<std::int16_t, 8192, PIECE_NB, SQUARE_NB>, PAWN_HISTORY_BASE_SIZE>;
155
156
// Correction histories record differences between the static evaluation of
157
// positions and their search score. It is used to improve the static evaluation
158
// used by some search heuristics.
159
// see https://www.chessprogramming.org/Static_Evaluation_Correction_History
160
enum CorrHistType {
161
Pawn, // By color and pawn structure
162
Minor, // By color and positions of minor pieces (Knight, Bishop)
163
NonPawn, // By non-pawn material positions and color
164
PieceTo, // By [piece][to] move
165
Continuation, // Combined history of move pairs
166
};
167
168
template<typename T, int D>
169
struct CorrectionBundle {
170
StatsEntry<T, D, true> pawn;
171
StatsEntry<T, D, true> minor;
172
StatsEntry<T, D, true> nonPawnWhite;
173
StatsEntry<T, D, true> nonPawnBlack;
174
175
void operator=(T val) {
176
pawn = val;
177
minor = val;
178
nonPawnWhite = val;
179
nonPawnBlack = val;
180
}
181
};
182
183
namespace Detail {
184
185
template<CorrHistType>
186
struct CorrHistTypedef {
187
using type =
188
DynStats<Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB>, CORRHIST_BASE_SIZE>;
189
};
190
191
template<>
192
struct CorrHistTypedef<PieceTo> {
193
using type = Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, PIECE_NB, SQUARE_NB>;
194
};
195
196
template<>
197
struct CorrHistTypedef<Continuation> {
198
using type = MultiArray<CorrHistTypedef<PieceTo>::type, PIECE_NB, SQUARE_NB>;
199
};
200
201
template<>
202
struct CorrHistTypedef<NonPawn> {
203
using type = DynStats<Stats<std::int16_t, CORRECTION_HISTORY_LIMIT, COLOR_NB, COLOR_NB>,
204
CORRHIST_BASE_SIZE>;
205
};
206
207
}
208
209
using UnifiedCorrectionHistory =
210
DynStats<MultiArray<CorrectionBundle<std::int16_t, CORRECTION_HISTORY_LIMIT>, COLOR_NB>,
211
CORRHIST_BASE_SIZE>;
212
213
template<CorrHistType T>
214
using CorrectionHistory = typename Detail::CorrHistTypedef<T>::type;
215
216
using TTMoveHistory = StatsEntry<std::int16_t, 8192>;
217
218
// Set of histories shared between groups of threads. To avoid excessive
219
// cross-node data transfer, histories are shared only between threads
220
// on a given NUMA node. The passed size must be a power of two to make
221
// the indexing more efficient.
222
struct SharedHistories {
223
SharedHistories(size_t threadCount) :
224
correctionHistory(threadCount),
225
pawnHistory(threadCount) {
226
assert((threadCount & (threadCount - 1)) == 0 && threadCount != 0);
227
sizeMinus1 = correctionHistory.get_size() - 1;
228
pawnHistSizeMinus1 = pawnHistory.get_size() - 1;
229
}
230
231
size_t get_size() const { return sizeMinus1 + 1; }
232
233
auto& pawn_entry(const Position& pos) {
234
return pawnHistory[pos.pawn_key() & pawnHistSizeMinus1];
235
}
236
const auto& pawn_entry(const Position& pos) const {
237
return pawnHistory[pos.pawn_key() & pawnHistSizeMinus1];
238
}
239
240
auto& pawn_correction_entry(const Position& pos) {
241
return correctionHistory[pos.pawn_key() & sizeMinus1];
242
}
243
const auto& pawn_correction_entry(const Position& pos) const {
244
return correctionHistory[pos.pawn_key() & sizeMinus1];
245
}
246
247
auto& minor_piece_correction_entry(const Position& pos) {
248
return correctionHistory[pos.minor_piece_key() & sizeMinus1];
249
}
250
const auto& minor_piece_correction_entry(const Position& pos) const {
251
return correctionHistory[pos.minor_piece_key() & sizeMinus1];
252
}
253
254
template<Color c>
255
auto& nonpawn_correction_entry(const Position& pos) {
256
return correctionHistory[pos.non_pawn_key(c) & sizeMinus1];
257
}
258
template<Color c>
259
const auto& nonpawn_correction_entry(const Position& pos) const {
260
return correctionHistory[pos.non_pawn_key(c) & sizeMinus1];
261
}
262
263
UnifiedCorrectionHistory correctionHistory;
264
PawnHistory pawnHistory;
265
266
267
private:
268
size_t sizeMinus1, pawnHistSizeMinus1;
269
};
270
271
} // namespace Stockfish
272
273
#endif // #ifndef HISTORY_H_INCLUDED
274
275