-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodern.cpp
More file actions
443 lines (395 loc) · 14 KB
/
Copy pathmodern.cpp
File metadata and controls
443 lines (395 loc) · 14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
enum BoundType {
BOUND_EXACT = 0,
BOUND_LOWER = 1,
BOUND_UPPER = 2,
};
struct TtEntry {
std::uint64_t key = 0;
double score = 0.0;
int depth = -1;
int flag = BOUND_EXACT;
Move best_move{};
bool has_move = false;
};
std::uint64_t splitmix64(std::uint64_t x) {
x += 0x9e3779b97f4a7c15ULL;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
bool same_mv(const Move &lhs, const Move &rhs) {
return lhs.from == rhs.from &&
lhs.to == rhs.to &&
lhs.promotion == rhs.promotion &&
lhs.is_en_passant == rhs.is_en_passant &&
lhs.is_castling == rhs.is_castling;
}
std::array<std::array<std::uint64_t, 64>, 12> g_piece_keys{};
std::array<std::uint64_t, 16> g_castle_keys{};
std::array<std::uint64_t, 9> g_ep_keys{};
std::uint64_t g_side_key = 0;
bool g_hash_ready = false;
int piece_key_idx(int piece) {
int type = piece_type(piece) - 1;
int color_offset = is_white_piece(piece) ? 0 : 6;
return color_offset + type;
}
int ep_key_idx(int ep_square) {
return ep_square == -1 ? 8 : file_of(ep_square);
}
void init_hash_keys_once() {
if (g_hash_ready) {
return;
}
// Not crypto, just fast and stable enough for TT keys.
std::uint64_t seed = 0x9e3779b97f4a7c15ULL;
for (auto &piece_table : g_piece_keys) {
for (auto &slot : piece_table) {
seed = splitmix64(seed);
slot = seed;
}
}
for (auto &slot : g_castle_keys) {
seed = splitmix64(seed);
slot = seed;
}
for (auto &slot : g_ep_keys) {
seed = splitmix64(seed);
slot = seed;
}
seed = splitmix64(seed);
g_side_key = seed;
g_hash_ready = true;
}
int ep_square_after(const Board &b, const Move &move, int moved_piece) {
if (piece_type(moved_piece) == PAWN && std::abs(rank_of(move.to) - rank_of(move.from)) == 2) {
return b.white_to_move ? (move.from + 8) : (move.from - 8);
}
return -1;
}
int castle_rights_after(const Board &b, const Move &move, int moved_piece, int captured_piece) {
int rights = b.castling_rights;
auto clear_if_on = [&](int square) {
if (square == square_from_string("a1")) {
rights &= ~WHITE_QUEENSIDE;
} else if (square == square_from_string("h1")) {
rights &= ~WHITE_KINGSIDE;
} else if (square == square_from_string("a8")) {
rights &= ~BLACK_QUEENSIDE;
} else if (square == square_from_string("h8")) {
rights &= ~BLACK_KINGSIDE;
}
};
if (piece_type(moved_piece) == KING) {
if (is_white_piece(moved_piece)) {
rights &= ~(WHITE_KINGSIDE | WHITE_QUEENSIDE);
} else {
rights &= ~(BLACK_KINGSIDE | BLACK_QUEENSIDE);
}
}
if (piece_type(moved_piece) == ROOK) {
clear_if_on(move.from);
}
if (captured_piece != EMPTY) {
clear_if_on(move.to);
}
return rights;
}
std::uint64_t modern_hash_position(const Board &board) {
init_hash_keys_once();
std::uint64_t key = 0;
for (int sq = 0; sq < 64; ++sq) {
int piece = board.squares[sq];
if (piece == EMPTY) {
continue;
}
key ^= g_piece_keys[piece_key_idx(piece)][sq];
}
key ^= g_castle_keys[board.castling_rights & 15];
key ^= g_ep_keys[ep_key_idx(board.ep_square)];
if (!board.white_to_move) {
key ^= g_side_key;
}
return key;
}
std::uint64_t modern_hash_after_move(const Board &board, const Move &move) {
init_hash_keys_once();
std::uint64_t key = board.hash_key;
int moved_piece = board.squares[move.from];
int captured_piece = EMPTY;
int captured_square = move.to;
if (move.is_en_passant) {
captured_square = board.white_to_move ? (move.to - 8) : (move.to + 8);
captured_piece = board.squares[captured_square];
} else {
captured_piece = board.squares[move.to];
}
key ^= g_side_key;
key ^= g_castle_keys[board.castling_rights & 15];
key ^= g_ep_keys[ep_key_idx(board.ep_square)];
key ^= g_piece_keys[piece_key_idx(moved_piece)][move.from];
if (captured_piece != EMPTY) {
key ^= g_piece_keys[piece_key_idx(captured_piece)][captured_square];
}
if (move.is_castling) {
int rook_piece = board.white_to_move ? ROOK : -ROOK;
int rook_from = -1;
int rook_to = -1;
if (move.to == square_from_string("g1")) {
rook_from = square_from_string("h1");
rook_to = square_from_string("f1");
} else if (move.to == square_from_string("c1")) {
rook_from = square_from_string("a1");
rook_to = square_from_string("d1");
} else if (move.to == square_from_string("g8")) {
rook_from = square_from_string("h8");
rook_to = square_from_string("f8");
} else if (move.to == square_from_string("c8")) {
rook_from = square_from_string("a8");
rook_to = square_from_string("d8");
}
if (rook_from != -1) {
key ^= g_piece_keys[piece_key_idx(rook_piece)][rook_from];
key ^= g_piece_keys[piece_key_idx(rook_piece)][rook_to];
}
}
int placed_piece = moved_piece;
if (move.promotion != EMPTY) {
placed_piece = make_piece(board.white_to_move, move.promotion);
}
key ^= g_piece_keys[piece_key_idx(placed_piece)][move.to];
key ^= g_castle_keys[castle_rights_after(board, move, moved_piece, captured_piece) & 15];
key ^= g_ep_keys[ep_key_idx(ep_square_after(board, move, moved_piece))];
return key;
}
class ModernSearchState {
public:
static constexpr int kMaxPly = 128;
ModernSearchState() {
init_zobrist();
resize_tt(16);
clear_ordering();
}
void resize_tt(int hash_mb) {
hash_mb = std::max(1, hash_mb);
std::size_t bytes = static_cast<std::size_t>(hash_mb) * 1024ULL * 1024ULL;
std::size_t count = bytes / sizeof(TtEntry);
if (count < 1) {
count = 1;
}
tt.assign(count, TtEntry{});
}
void clear_tt() {
for (auto &entry : tt) {
entry = TtEntry{};
}
}
void clear_ordering() {
for (auto &slot : killer_valid) {
slot = {false, false};
}
for (auto &side : history) {
for (auto &from : side) {
from.fill(0);
}
}
}
std::uint64_t position_key(const Board &b) const {
std::uint64_t key = 0;
for (int sq = 0; sq < 64; ++sq) {
int piece = b.squares[sq];
if (piece == EMPTY) {
continue;
}
key ^= zobrist_piece[piece_index(piece)][sq];
}
key ^= zobrist_castle[b.castling_rights & 15];
key ^= zobrist_ep[ep_index(b.ep_square)];
if (!b.white_to_move) {
key ^= zobrist_side;
}
return key;
}
std::uint64_t next_key(const Board &b, const Move &move, std::uint64_t key_before) const {
std::uint64_t key = key_before;
int moved_piece = b.squares[move.from];
int captured_piece = EMPTY;
int captured_square = move.to;
if (move.is_en_passant) {
captured_square = b.white_to_move ? (move.to - 8) : (move.to + 8);
captured_piece = b.squares[captured_square];
} else {
captured_piece = b.squares[move.to];
}
key ^= zobrist_side;
key ^= zobrist_castle[b.castling_rights & 15];
key ^= zobrist_ep[ep_index(b.ep_square)];
key ^= zobrist_piece[piece_index(moved_piece)][move.from];
if (captured_piece != EMPTY) {
key ^= zobrist_piece[piece_index(captured_piece)][captured_square];
}
if (move.is_castling) {
int rook_piece = b.white_to_move ? ROOK : -ROOK;
int rook_from = -1;
int rook_to = -1;
if (move.to == square_from_string("g1")) {
rook_from = square_from_string("h1");
rook_to = square_from_string("f1");
} else if (move.to == square_from_string("c1")) {
rook_from = square_from_string("a1");
rook_to = square_from_string("d1");
} else if (move.to == square_from_string("g8")) {
rook_from = square_from_string("h8");
rook_to = square_from_string("f8");
} else if (move.to == square_from_string("c8")) {
rook_from = square_from_string("a8");
rook_to = square_from_string("d8");
}
if (rook_from != -1) {
key ^= zobrist_piece[piece_index(rook_piece)][rook_from];
key ^= zobrist_piece[piece_index(rook_piece)][rook_to];
}
}
int placed_piece = moved_piece;
if (move.promotion != EMPTY) {
placed_piece = make_piece(b.white_to_move, move.promotion);
}
key ^= zobrist_piece[piece_index(placed_piece)][move.to];
key ^= zobrist_castle[updated_castling_rights(b, move, moved_piece, captured_piece) & 15];
key ^= zobrist_ep[ep_index(next_ep_square(b, move, moved_piece))];
return key;
}
const TtEntry *probe(std::uint64_t key) const {
if (tt.empty()) {
return nullptr;
}
const auto &entry = tt[key % tt.size()];
if (entry.key != key) {
return nullptr;
}
return &entry;
}
void store(std::uint64_t key, int depth, double score, int flag, const Move *best_move) {
if (tt.empty()) {
return;
}
auto &entry = tt[key % tt.size()];
if (entry.depth > depth && entry.key == key) {
return;
}
entry.key = key;
entry.depth = depth;
entry.score = score;
entry.flag = flag;
entry.has_move = best_move != nullptr;
entry.best_move = best_move ? *best_move : Move{};
}
void promote_tt_move(std::vector<Move> &moves, const Move &tt_move) const {
auto it = std::find_if(moves.begin(), moves.end(), [&](const Move &mv) {
return same_mv(mv, tt_move);
});
if (it != moves.end() && it != moves.begin()) {
std::rotate(moves.begin(), it, it + 1);
}
}
int killer_bonus(int ply, const Move &move) const {
if (ply < 0 || ply >= kMaxPly) {
return 0;
}
if (killer_valid[ply][0] && same_mv(killer_moves[ply][0], move)) {
return 9000;
}
if (killer_valid[ply][1] && same_mv(killer_moves[ply][1], move)) {
return 7000;
}
return 0;
}
int history_bonus(bool white, const Move &move) const {
return history[white ? 0 : 1][move.from][move.to];
}
void note_cutoff(bool white, int ply, const Move &move, bool is_capture, int depth) {
if (is_capture) {
return;
}
if (ply >= 0 && ply < kMaxPly) {
if (!killer_valid[ply][0] || !same_mv(killer_moves[ply][0], move)) {
killer_moves[ply][1] = killer_moves[ply][0];
killer_valid[ply][1] = killer_valid[ply][0];
killer_moves[ply][0] = move;
killer_valid[ply][0] = true;
}
}
int &hist = history[white ? 0 : 1][move.from][move.to];
hist = std::min(hist + depth * depth * 32, 2000000);
}
private:
std::array<std::array<std::uint64_t, 64>, 12> zobrist_piece{};
std::array<std::uint64_t, 16> zobrist_castle{};
std::array<std::uint64_t, 9> zobrist_ep{};
std::uint64_t zobrist_side = 0;
std::vector<TtEntry> tt;
std::array<std::array<Move, 2>, kMaxPly> killer_moves{};
std::array<std::array<bool, 2>, kMaxPly> killer_valid{};
std::array<std::array<std::array<int, 64>, 64>, 2> history{};
void init_zobrist() {
std::uint64_t seed = 0x9e3779b97f4a7c15ULL;
for (auto &piece_table : zobrist_piece) {
for (auto &slot : piece_table) {
seed = splitmix64(seed);
slot = seed;
}
}
for (auto &slot : zobrist_castle) {
seed = splitmix64(seed);
slot = seed;
}
for (auto &slot : zobrist_ep) {
seed = splitmix64(seed);
slot = seed;
}
seed = splitmix64(seed);
zobrist_side = seed;
}
int piece_index(int piece) const {
int type = piece_type(piece) - 1;
int color_offset = is_white_piece(piece) ? 0 : 6;
return color_offset + type;
}
int ep_index(int ep_square) const {
return ep_square == -1 ? 8 : file_of(ep_square);
}
int next_ep_square(const Board &b, const Move &move, int moved_piece) const {
if (piece_type(moved_piece) == PAWN && std::abs(rank_of(move.to) - rank_of(move.from)) == 2) {
return b.white_to_move ? (move.from + 8) : (move.from - 8);
}
return -1;
}
int updated_castling_rights(const Board &b, const Move &move, int moved_piece, int captured_piece) const {
int rights = b.castling_rights;
auto clear_if_on = [&](int square) {
if (square == square_from_string("a1")) {
rights &= ~WHITE_QUEENSIDE;
} else if (square == square_from_string("h1")) {
rights &= ~WHITE_KINGSIDE;
} else if (square == square_from_string("a8")) {
rights &= ~BLACK_QUEENSIDE;
} else if (square == square_from_string("h8")) {
rights &= ~BLACK_KINGSIDE;
}
};
if (piece_type(moved_piece) == KING) {
if (is_white_piece(moved_piece)) {
rights &= ~(WHITE_KINGSIDE | WHITE_QUEENSIDE);
} else {
rights &= ~(BLACK_KINGSIDE | BLACK_QUEENSIDE);
}
}
if (piece_type(moved_piece) == ROOK) {
clear_if_on(move.from);
}
if (captured_piece != EMPTY) {
clear_if_on(move.to);
}
return rights;
}
};