Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
official-stockfish
GitHub Repository: official-stockfish/stockfish
Path: blob/master/src/types.h
509 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 TYPES_H_INCLUDED
20
#define TYPES_H_INCLUDED
21
22
// When compiling with provided Makefile (e.g. for Linux and OSX), configuration
23
// is done automatically. To get started type 'make help'.
24
//
25
// When Makefile is not used (e.g. with Microsoft Visual Studio) some switches
26
// need to be set manually:
27
//
28
// -DNDEBUG | Disable debugging mode. Always use this for release.
29
//
30
// -DNO_PREFETCH | Disable use of prefetch asm-instruction. You may need this to
31
// | run on some very old machines.
32
//
33
// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works
34
// | only in 64-bit mode and requires hardware with popcnt support.
35
//
36
// -DUSE_PEXT | Add runtime support for use of pext asm-instruction. Works
37
// | only in 64-bit mode and requires hardware with pext support.
38
39
#include <cassert>
40
#include <cstddef>
41
#include <cstdint>
42
#include <type_traits>
43
#include "misc.h"
44
45
#if defined(_MSC_VER)
46
// Disable some silly and noisy warnings from MSVC compiler
47
#pragma warning(disable: 4127) // Conditional expression is constant
48
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
49
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
50
#endif
51
52
// Predefined macros hell:
53
//
54
// __GNUC__ Compiler is GCC, Clang or ICX
55
// __clang__ Compiler is Clang or ICX
56
// __INTEL_LLVM_COMPILER Compiler is ICX
57
// _MSC_VER Compiler is MSVC
58
// _WIN32 Building on Windows (any)
59
// _WIN64 Building on Windows 64 bit
60
61
// Enforce minimum GCC version
62
#if defined(__GNUC__) && !defined(__clang__) \
63
&& (__GNUC__ < 9 || (__GNUC__ == 9 && __GNUC_MINOR__ < 3))
64
#error "Stockfish requires GCC 9.3 or later for correct compilation"
65
#endif
66
67
// Enforce minimum Clang version
68
#if defined(__clang__) && (__clang_major__ < 10)
69
#error "Stockfish requires Clang 10.0 or later for correct compilation"
70
#endif
71
72
#define ASSERT_ALIGNED(ptr, alignment) assert(reinterpret_cast<uintptr_t>(ptr) % alignment == 0)
73
74
#if defined(_WIN64) && defined(_MSC_VER) // No Makefile used
75
#include <intrin.h> // Microsoft header for _BitScanForward64()
76
#define IS_64BIT
77
#endif
78
79
#if defined(USE_POPCNT) && defined(_MSC_VER)
80
#include <nmmintrin.h> // Microsoft header for _mm_popcnt_u64()
81
#endif
82
83
#if !defined(NO_PREFETCH) && defined(_MSC_VER)
84
#include <xmmintrin.h> // Microsoft header for _mm_prefetch()
85
#endif
86
87
#if defined(USE_PEXT)
88
#include <immintrin.h> // Header for _pext_u64() intrinsic
89
#define pext(b, m) _pext_u64(b, m)
90
#else
91
#define pext(b, m) 0
92
#endif
93
94
namespace Stockfish {
95
96
#ifdef USE_POPCNT
97
constexpr bool HasPopCnt = true;
98
#else
99
constexpr bool HasPopCnt = false;
100
#endif
101
102
#ifdef USE_PEXT
103
constexpr bool HasPext = true;
104
#else
105
constexpr bool HasPext = false;
106
#endif
107
108
#ifdef IS_64BIT
109
constexpr bool Is64Bit = true;
110
#else
111
constexpr bool Is64Bit = false;
112
#endif
113
114
using Key = uint64_t;
115
using Bitboard = uint64_t;
116
117
constexpr int MAX_MOVES = 256;
118
constexpr int MAX_PLY = 246;
119
120
enum Color : uint8_t {
121
WHITE,
122
BLACK,
123
COLOR_NB = 2
124
};
125
126
enum CastlingRights : uint8_t {
127
NO_CASTLING,
128
WHITE_OO,
129
WHITE_OOO = WHITE_OO << 1,
130
BLACK_OO = WHITE_OO << 2,
131
BLACK_OOO = WHITE_OO << 3,
132
133
KING_SIDE = WHITE_OO | BLACK_OO,
134
QUEEN_SIDE = WHITE_OOO | BLACK_OOO,
135
WHITE_CASTLING = WHITE_OO | WHITE_OOO,
136
BLACK_CASTLING = BLACK_OO | BLACK_OOO,
137
ANY_CASTLING = WHITE_CASTLING | BLACK_CASTLING,
138
139
CASTLING_RIGHT_NB = 16
140
};
141
142
enum Bound : uint8_t {
143
BOUND_NONE,
144
BOUND_UPPER,
145
BOUND_LOWER,
146
BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
147
};
148
149
// Value is used as an alias for int, this is done to differentiate between a search
150
// value and any other integer value. The values used in search are always supposed
151
// to be in the range (-VALUE_NONE, VALUE_NONE] and should not exceed this range.
152
using Value = int;
153
154
constexpr Value VALUE_ZERO = 0;
155
constexpr Value VALUE_DRAW = 0;
156
constexpr Value VALUE_NONE = 32002;
157
constexpr Value VALUE_INFINITE = 32001;
158
159
constexpr Value VALUE_MATE = 32000;
160
constexpr Value VALUE_MATE_IN_MAX_PLY = VALUE_MATE - MAX_PLY;
161
constexpr Value VALUE_MATED_IN_MAX_PLY = -VALUE_MATE_IN_MAX_PLY;
162
163
constexpr Value VALUE_TB = VALUE_MATE_IN_MAX_PLY - 1;
164
constexpr Value VALUE_TB_WIN_IN_MAX_PLY = VALUE_TB - MAX_PLY;
165
constexpr Value VALUE_TB_LOSS_IN_MAX_PLY = -VALUE_TB_WIN_IN_MAX_PLY;
166
167
168
constexpr bool is_valid(Value value) { return value != VALUE_NONE; }
169
170
constexpr bool is_win(Value value) {
171
assert(is_valid(value));
172
return value >= VALUE_TB_WIN_IN_MAX_PLY;
173
}
174
175
constexpr bool is_loss(Value value) {
176
assert(is_valid(value));
177
return value <= VALUE_TB_LOSS_IN_MAX_PLY;
178
}
179
180
constexpr bool is_decisive(Value value) { return is_win(value) || is_loss(value); }
181
182
// In the code, we make the assumption that these values
183
// are such that non_pawn_material() can be used to uniquely
184
// identify the material on the board.
185
constexpr Value PawnValue = 208;
186
constexpr Value KnightValue = 781;
187
constexpr Value BishopValue = 825;
188
constexpr Value RookValue = 1276;
189
constexpr Value QueenValue = 2538;
190
191
192
// clang-format off
193
enum PieceType : std::uint8_t {
194
NO_PIECE_TYPE, PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
195
ALL_PIECES = 0,
196
PIECE_TYPE_NB = 8
197
};
198
199
enum Piece : std::uint8_t {
200
NO_PIECE,
201
W_PAWN = PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING,
202
B_PAWN = PAWN + 8, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING,
203
PIECE_NB = 16
204
};
205
// clang-format on
206
207
constexpr Value PieceValue[PIECE_NB] = {
208
VALUE_ZERO, PawnValue, KnightValue, BishopValue, RookValue, QueenValue, VALUE_ZERO, VALUE_ZERO,
209
VALUE_ZERO, PawnValue, KnightValue, BishopValue, RookValue, QueenValue, VALUE_ZERO, VALUE_ZERO};
210
211
using Depth = int;
212
213
// The following DEPTH_ constants are used for transposition table entries
214
// and quiescence search move generation stages. In regular search, the
215
// depth stored in the transposition table is literal: the search depth
216
// (effort) used to make the corresponding transposition table value. In
217
// quiescence search, however, the transposition table entries only store
218
// the current quiescence move generation stage (which should thus compare
219
// lower than any regular search depth).
220
constexpr Depth DEPTH_QS = 0;
221
// For transposition table entries where no searching at all was done
222
// (whether regular or qsearch) we use DEPTH_UNSEARCHED, which should thus
223
// compare lower than any quiescence or regular depth. DEPTH_ENTRY_OFFSET
224
// is used only for the transposition table entry occupancy check (see tt.cpp),
225
// and should thus be lower than DEPTH_UNSEARCHED.
226
constexpr Depth DEPTH_UNSEARCHED = -2;
227
constexpr Depth DEPTH_ENTRY_OFFSET = -3;
228
229
// clang-format off
230
enum Square : uint8_t {
231
SQ_A1, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
232
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
233
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
234
SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
235
SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
236
SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
237
SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
238
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
239
SQ_NONE,
240
241
SQUARE_ZERO = 0,
242
SQUARE_NB = 64
243
};
244
// clang-format on
245
246
enum Direction : int8_t {
247
NORTH = 8,
248
EAST = 1,
249
SOUTH = -NORTH,
250
WEST = -EAST,
251
252
NORTH_EAST = NORTH + EAST,
253
SOUTH_EAST = SOUTH + EAST,
254
SOUTH_WEST = SOUTH + WEST,
255
NORTH_WEST = NORTH + WEST
256
};
257
258
enum File : uint8_t {
259
FILE_A,
260
FILE_B,
261
FILE_C,
262
FILE_D,
263
FILE_E,
264
FILE_F,
265
FILE_G,
266
FILE_H,
267
FILE_NB
268
};
269
270
enum Rank : uint8_t {
271
RANK_1,
272
RANK_2,
273
RANK_3,
274
RANK_4,
275
RANK_5,
276
RANK_6,
277
RANK_7,
278
RANK_8,
279
RANK_NB
280
};
281
282
// Keep track of what a move changes on the board (used by NNUE)
283
struct DirtyPiece {
284
Piece pc; // this is never allowed to be NO_PIECE
285
Square from, to; // to should be SQ_NONE for promotions
286
287
// if {add,remove}_sq is SQ_NONE, {add,remove}_pc is allowed to be
288
// uninitialized
289
// castling uses add_sq and remove_sq to remove and add the rook
290
Square remove_sq, add_sq;
291
Piece remove_pc, add_pc;
292
};
293
294
// Keep track of what threats change on the board (used by NNUE)
295
struct DirtyThreat {
296
static constexpr int PcSqOffset = 0;
297
static constexpr int ThreatenedSqOffset = 8;
298
static constexpr int ThreatenedPcOffset = 16;
299
static constexpr int PcOffset = 20;
300
301
DirtyThreat() { /* don't initialize data */ }
302
DirtyThreat(uint32_t raw) :
303
data(raw) {}
304
DirtyThreat(Piece pc, Piece threatened_pc, Square pc_sq, Square threatened_sq, bool add) {
305
data = (uint32_t(add) << 31) | (pc << PcOffset) | (threatened_pc << ThreatenedPcOffset)
306
| (threatened_sq << ThreatenedSqOffset) | (pc_sq << PcSqOffset);
307
}
308
309
Piece pc() const { return static_cast<Piece>(data >> PcOffset & 0xf); }
310
Piece threatened_pc() const { return static_cast<Piece>(data >> ThreatenedPcOffset & 0xf); }
311
Square threatened_sq() const { return static_cast<Square>(data >> ThreatenedSqOffset & 0xff); }
312
Square pc_sq() const { return static_cast<Square>(data >> PcSqOffset & 0xff); }
313
bool add() const { return data >> 31; }
314
uint32_t raw() const { return data; }
315
316
private:
317
uint32_t data;
318
};
319
320
// A piece can be involved in at most 8 outgoing attacks and 16 incoming attacks.
321
// Moving a piece also can reveal at most 8 discovered attacks.
322
// This implies that a non-castling move can change at most (8 + 16) * 3 + 8 = 80 features.
323
// By similar logic, a castling move can change at most (5 + 1 + 3 + 9) * 2 = 36 features.
324
// Thus, 80 should work as an upper bound. Finally, 16 entries are added to accommodate
325
// unmasked vector stores near the end of the list.
326
327
using DirtyThreatList = ValueList<DirtyThreat, 96>;
328
329
struct DirtyThreats {
330
DirtyThreatList list;
331
Color us;
332
Square prevKsq, ksq;
333
334
Bitboard threatenedSqs, threateningSqs;
335
};
336
337
#define ENABLE_INCR_OPERATORS_ON(T) \
338
constexpr T& operator++(T& d) { return d = T(int(d) + 1); } \
339
constexpr T& operator--(T& d) { return d = T(int(d) - 1); }
340
341
ENABLE_INCR_OPERATORS_ON(PieceType)
342
ENABLE_INCR_OPERATORS_ON(Square)
343
ENABLE_INCR_OPERATORS_ON(File)
344
ENABLE_INCR_OPERATORS_ON(Rank)
345
346
#undef ENABLE_INCR_OPERATORS_ON
347
348
constexpr Direction operator+(Direction d1, Direction d2) { return Direction(int(d1) + int(d2)); }
349
constexpr Direction operator*(int i, Direction d) { return Direction(i * int(d)); }
350
351
// Additional operators to add a Direction to a Square
352
constexpr Square operator+(Square s, Direction d) { return Square(int(s) + int(d)); }
353
constexpr Square operator-(Square s, Direction d) { return Square(int(s) - int(d)); }
354
constexpr Square& operator+=(Square& s, Direction d) { return s = s + d; }
355
constexpr Square& operator-=(Square& s, Direction d) { return s = s - d; }
356
357
// Toggle color
358
constexpr Color operator~(Color c) { return Color(c ^ BLACK); }
359
360
// Swap A1 <-> A8
361
constexpr Square flip_rank(Square s) { return Square(s ^ SQ_A8); }
362
363
// Swap A1 <-> H1
364
constexpr Square flip_file(Square s) { return Square(s ^ SQ_H1); }
365
366
// Swap color of piece B_KNIGHT <-> W_KNIGHT
367
constexpr Piece operator~(Piece pc) { return Piece(pc ^ 8); }
368
369
constexpr CastlingRights operator&(Color c, CastlingRights cr) {
370
return CastlingRights((c == WHITE ? WHITE_CASTLING : BLACK_CASTLING) & cr);
371
}
372
373
constexpr Value mate_in(int ply) { return VALUE_MATE - ply; }
374
375
constexpr Value mated_in(int ply) { return -VALUE_MATE + ply; }
376
377
constexpr Square make_square(File f, Rank r) { return Square((r << 3) + f); }
378
379
constexpr Piece make_piece(Color c, PieceType pt) { return Piece((c << 3) + pt); }
380
381
constexpr PieceType type_of(Piece pc) { return PieceType(pc & 7); }
382
383
constexpr Color color_of(Piece pc) {
384
assert(pc != NO_PIECE);
385
return Color(pc >> 3);
386
}
387
388
constexpr bool is_ok(Square s) { return s >= SQ_A1 && s <= SQ_H8; }
389
390
constexpr File file_of(Square s) { return File(s & 7); }
391
392
constexpr Rank rank_of(Square s) { return Rank(s >> 3); }
393
394
constexpr Square relative_square(Color c, Square s) { return Square(s ^ (c * 56)); }
395
396
constexpr Rank relative_rank(Color c, Rank r) { return Rank(r ^ (c * 7)); }
397
398
constexpr Rank relative_rank(Color c, Square s) { return relative_rank(c, rank_of(s)); }
399
400
constexpr Direction pawn_push(Color c) { return c == WHITE ? NORTH : SOUTH; }
401
402
403
// Based on a congruential pseudo-random number generator
404
constexpr Key make_key(uint64_t seed) {
405
return seed * 6364136223846793005ULL + 1442695040888963407ULL;
406
}
407
408
409
enum MoveType : uint16_t {
410
NORMAL,
411
PROMOTION = 1 << 14,
412
EN_PASSANT = 2 << 14,
413
CASTLING = 3 << 14
414
};
415
416
// A move needs 16 bits to be stored
417
//
418
// bit 0- 5: destination square (from 0 to 63)
419
// bit 6-11: origin square (from 0 to 63)
420
// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2)
421
// bit 14-15: special move flag: promotion (1), en passant (2), castling (3)
422
// NOTE: en passant bit is set only when a pawn can be captured
423
//
424
// Special cases are Move::none() and Move::null(). We can sneak these in because
425
// in any normal move the destination square and origin square are always different,
426
// but Move::none() and Move::null() have the same origin and destination square.
427
428
class Move {
429
public:
430
Move() = default;
431
constexpr explicit Move(std::uint16_t d) :
432
data(d) {}
433
434
constexpr Move(Square from, Square to) :
435
data((from << 6) + to) {}
436
437
template<MoveType T>
438
static constexpr Move make(Square from, Square to, PieceType pt = KNIGHT) {
439
return Move(T + ((pt - KNIGHT) << 12) + (from << 6) + to);
440
}
441
442
constexpr Square from_sq() const {
443
assert(is_ok());
444
return Square((data >> 6) & 0x3F);
445
}
446
447
constexpr Square to_sq() const {
448
assert(is_ok());
449
return Square(data & 0x3F);
450
}
451
452
// Same as to_sq() but without assertion, for branchless code paths
453
// where the result is masked/ignored when move is not ok
454
constexpr Square to_sq_unchecked() const { return Square(data & 0x3F); }
455
456
constexpr MoveType type_of() const { return MoveType(data & (3 << 14)); }
457
458
constexpr PieceType promotion_type() const { return PieceType(((data >> 12) & 3) + KNIGHT); }
459
460
constexpr bool is_ok() const { return none().data != data && null().data != data; }
461
462
static constexpr Move null() { return Move(65); }
463
static constexpr Move none() { return Move(0); }
464
465
constexpr bool operator==(const Move& m) const { return data == m.data; }
466
constexpr bool operator!=(const Move& m) const { return data != m.data; }
467
468
constexpr explicit operator bool() const { return data != 0; }
469
470
constexpr std::uint16_t raw() const { return data; }
471
472
struct MoveHash {
473
std::size_t operator()(const Move& m) const { return make_key(m.data); }
474
};
475
476
protected:
477
std::uint16_t data;
478
};
479
480
template<typename T, typename... Ts>
481
struct is_all_same {
482
static constexpr bool value = (std::is_same_v<T, Ts> && ...);
483
};
484
485
template<typename... Ts>
486
constexpr auto is_all_same_v = is_all_same<Ts...>::value;
487
488
} // namespace Stockfish
489
490
#endif // #ifndef TYPES_H_INCLUDED
491
492
#include "tune.h" // Global visibility to tuning setup
493
494