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