forked from Gigantua/Chess_Movegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Kindergarten.hpp
424 lines (416 loc) · 15.1 KB
/
Kindergarten.hpp
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
#pragma once
// Datum : 2022-01-25
// Author : Tsoj
// Content: [Old C++ implementation](https://gitlab.com/tsoj/Googleplex_Starthinker/-/blob/master/src/bitboard.hpp)
// of a sliding piece algorithm that today is used by
// [Nalwald](https://gitlab.com/tsoj/Nalwald/-/blob/master/bitboard.nim).
#include <stdint.h>
#include <bit>
#include <array>
#include <stddef.h>
namespace Chess_Lookup::Kindergarten
{
constexpr std::array<uint64_t, 64> ranks_64 = []()
{
uint64_t ranks[8] = {};
for (uint64_t i = 0; i < 8; i++)
{
ranks[i] = (uint64_t)(0b11111111) << (i * 8);
}
std::array<uint64_t, 64> ranks_64_ret = {};
for (uint64_t i = 0; i < 64; i++)
{
ranks_64_ret[i] = ranks[i / 8];
}
return ranks_64_ret;
}();
constexpr std::array<uint64_t, 64> files_64 = []()
{
uint64_t files[8] = {};
for (uint64_t i = 0; i < 8; i++)
{
files[i] = (uint64_t)(0b0000000100000001000000010000000100000001000000010000000100000001) << i;
}
std::array<uint64_t, 64> files_64_ret = {};
for (uint64_t i = 0; i < 64; i++)
{
files_64_ret[i] = files[i % 8];
}
return files_64_ret;
}();
constexpr uint64_t upper_left_side_zero = 0b1000000011000000111000001111000011111000111111001111111011111111;
constexpr uint64_t lower_right_side_zero = 0b1111111101111111001111110001111100001111000001110000001100000001;
constexpr uint64_t lower_left_side_zero = 0b1111111011111100111110001111000011100000110000001000000000000000;
constexpr uint64_t upper_right_side_zero = 0b0000000000000001000000110000011100001111000111110011111101111111;
constexpr uint64_t main_diagonal = 0b1000000001000000001000000001000000001000000001000000001000000001; //A1 to H8
constexpr uint64_t anti_diagonal = 0b0000000100000010000001000000100000010000001000000100000010000000; //H1 to A8
constexpr std::array<uint64_t, 64> diagonals_64 = []()
{
std::array<uint64_t, 64> diagonals_64_ret = {};
for (uint64_t i = 0; i < 8; i++)
{
uint64_t current_diagonal = (main_diagonal << i) & upper_left_side_zero;
uint64_t tmp = current_diagonal;
while (tmp != 0)
{
uint64_t index = std::countr_zero(tmp);
diagonals_64_ret[index] = current_diagonal;
tmp = tmp & (~(1ull << index));
}
}
for (uint64_t i = 1; i < 8; i++)
{
uint64_t current_diagonal = (main_diagonal >> i) & lower_right_side_zero;
uint64_t tmp = current_diagonal;
while (tmp != 0)
{
uint64_t index = std::countr_zero(tmp);
diagonals_64_ret[index] = current_diagonal;
tmp = tmp & ~(1ull << index);
}
}
return diagonals_64_ret;
}();
constexpr std::array<uint64_t, 64> anti_diagonals_64 = []()
{
std::array<uint64_t, 64> anti_diagonals_64_ret = {};
for (int i = 1; i < 8; i++)
{
uint64_t current_anti_diagonal = (anti_diagonal << i) & lower_left_side_zero;
uint64_t tmp = current_anti_diagonal;
while (tmp != 0)
{
uint64_t index = std::countr_zero(tmp);
anti_diagonals_64_ret[index] = current_anti_diagonal;
tmp = tmp & ~(1ull << index);
}
}
{
uint64_t current_anti_diagonal = anti_diagonal;
uint64_t tmp = current_anti_diagonal;
while (tmp != 0)
{
uint64_t index = std::countr_zero(tmp);
anti_diagonals_64_ret[index] = current_anti_diagonal;
tmp = tmp & ~(1ull << index);
}
}
for (int i = 1; i < 8; i++)
{
uint64_t current_anti_diagonal = (anti_diagonal >> i) & upper_right_side_zero;
uint64_t tmp = current_anti_diagonal;
while (tmp != 0)
{
uint64_t index = std::countr_zero(tmp);
anti_diagonals_64_ret[index] = current_anti_diagonal;
tmp = tmp & ~(1ull << index);
}
}
return anti_diagonals_64_ret;
}();
constexpr inline uint8_t hashkeyRank(const int& square, const uint64_t& occupancy)
{
return ((occupancy >> ((square / 8) * 8)) >> 1) & 0b111111U;
}
constexpr inline uint8_t hashkeyFile(const int& square, const uint64_t& occupancy)
{
return (((((occupancy >> (square % 8)) & files_64[0]) * main_diagonal) >> 56) >> 1) & 0b111111U;
}
constexpr inline uint8_t hashkeyDiagonal(const int& square, const uint64_t& occupancy)
{
return ((((occupancy & diagonals_64[square]) * files_64[0]) >> 56) >> 1) & 0b111111U;
}
constexpr inline uint8_t hashkeyAntiDiagonal(const int& square, const uint64_t& occupancy)
{
return ((((occupancy & anti_diagonals_64[square]) * files_64[0]) >> 56) >> 1) & 0b111111U;
}
constexpr uint64_t north = 8;
constexpr uint64_t south = -8;
constexpr uint64_t east = 1;
constexpr uint64_t west = -1;
constexpr uint64_t north_east = 9;
constexpr uint64_t north_west = 7;
constexpr uint64_t south_west = -9;
constexpr uint64_t south_east = -7;
constexpr std::array<uint64_t, 64> possible_ranks = []()
{
std::array<uint64_t, 64> possible_ranks_ret = {};
for (uint64_t i = 0; i < 64; i++)
{
uint64_t tmp = (uint64_t)(0b10000001) | (((uint64_t)(i)) << 1);
for (int j = 0; j < 8; j++)
{
possible_ranks_ret[i] |= tmp << (j * 8);
}
}
return possible_ranks_ret;
}();
constexpr std::array<uint64_t, 64> possible_files = []()
{
std::array<uint64_t, 64> possible_files_ret = {};
for (uint64_t i = 0; i < 64; i++)
{
auto rankToFile = [&](uint64_t rank)
{
return (((rank & 0b11111111U) * main_diagonal) & files_64[7]) >> 7;
};
uint64_t tmp = rankToFile((uint64_t)(0b10000001) | (((uint64_t)(i)) << 1));
for (int j = 0; j < 8; j++)
{
possible_files_ret[i] |= tmp << j;
}
}
return possible_files_ret;
}();
constexpr std::array<std::array<uint64_t, 64>, 64> rank_attack_table = []()
{
std::array<std::array<uint64_t, 64>, 64> rank_attack_table_ret = {};
for (int index = 0; index < 64; index++)
{
for (uint64_t possible_ranks_index = 0; possible_ranks_index < 64; possible_ranks_index++)
{
uint64_t tmp_attack_mask = 0;
uint64_t occupancy = possible_ranks[possible_ranks_index];
uint64_t i = index;
while (true)
{
i += east;
if (i >= 64)
{
break;
}
if (index % 8 == 7)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
i = index;
while (true)
{
i += west;
if (i >= 64) //equals i < 0)
{
break;
}
if (index % 8 == 0)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
rank_attack_table_ret[index][hashkeyRank(index, occupancy)] = tmp_attack_mask;
}
}
return rank_attack_table_ret;
}();
constexpr std::array<std::array<uint64_t, 64>, 64> file_attack_table = []()
{
std::array<std::array<uint64_t, 64>, 64> file_attack_table_ret = {};
for (int index = 0; index < 64; index++)
{
for (uint64_t possible_files_index = 0; possible_files_index < 64; possible_files_index++)
{
uint64_t tmp_attack_mask = 0;
uint64_t occupancy = possible_files[possible_files_index];
uint64_t i = index;
while (true)
{
i += north;
if (i >= 64)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
i = index;
while (true)
{
i += south;
if (i >= 64/*equals i < 0*/)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
file_attack_table_ret[index][hashkeyFile(index, occupancy)] = tmp_attack_mask;
}
}
return file_attack_table_ret;
}();
constexpr std::array<std::array<uint64_t, 64>, 64> diagonal_attack_table = []()
{
std::array<std::array<uint64_t, 64>, 64> diagonal_attack_table_ret = {};
for (int index = 0; index < 64; index++)
{
for (uint64_t possible_ranks_index = 0; possible_ranks_index < 64; possible_ranks_index++)
{
uint64_t tmp_attack_mask = 0;
uint64_t occupancy = possible_ranks[possible_ranks_index];
uint64_t i = index;
while (true)
{
i += north_east;
if (i >= 64)
{
break;
}
if (index % 8 == 7)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
i = index;
while (true)
{
i += south_west;
if (i >= 64/*equals i < 0*/)
{
break;
}
if (index % 8 == 0)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
diagonal_attack_table_ret[index][hashkeyDiagonal(index, occupancy)] = tmp_attack_mask;
}
}
return diagonal_attack_table_ret;
}();
constexpr std::array<std::array<uint64_t, 64>, 64> anti_diagonal_attack_table = []()
{
std::array<std::array<uint64_t, 64>, 64> anti_diagonal_attack_table_ret = {};
for (int index = 0; index < 64; index++)
{
for (uint64_t possible_ranks_index = 0; possible_ranks_index < 64; possible_ranks_index++)
{
uint64_t tmp_attack_mask = 0;
uint64_t occupancy = possible_ranks[possible_ranks_index];
uint64_t i = index;
while (true)
{
i += north_west;
if (i >= 64)
{
break;
}
if (index % 8 == 0)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
i = index;
while (true)
{
i += south_east;
if (i >= 64/*equals i < 0*/)
{
break;
}
if (index % 8 == 7)
{
break;
}
if ((occupancy & (1ull << i)) != 0)
{
tmp_attack_mask |= (1ull << i);
break;
}
else
{
tmp_attack_mask |= (1ull << i);
}
}
anti_diagonal_attack_table_ret[index][hashkeyAntiDiagonal(index, occupancy)] = tmp_attack_mask;
}
}
return anti_diagonal_attack_table_ret;
}();
static constexpr auto Size =
sizeof(anti_diagonal_attack_table) +
sizeof(diagonal_attack_table) +
sizeof(file_attack_table) +
sizeof(rank_attack_table) +
sizeof(anti_diagonals_64) +
sizeof(diagonals_64) +
sizeof(files_64) +
sizeof(ranks_64);
//return the uint64 with the rook targets
static constexpr uint64_t Rook(int square, uint64_t occupation)
{
return rank_attack_table[square][hashkeyRank(square, occupation)] |
file_attack_table[square][hashkeyFile(square, occupation)];
}
//return the uint64 with the bishops targets
static constexpr uint64_t Bishop(int square, uint64_t occupation)
{
return anti_diagonal_attack_table[square][hashkeyAntiDiagonal(square, occupation)] |
diagonal_attack_table[square][hashkeyDiagonal(square, occupation)];
}
static uint64_t Queen(int square, uint64_t occupation) {
return anti_diagonal_attack_table[square][hashkeyAntiDiagonal(square, occupation)] |
diagonal_attack_table[square][hashkeyDiagonal(square, occupation)] |
rank_attack_table[square][hashkeyRank(square, occupation)] |
file_attack_table[square][hashkeyFile(square, occupation)];
}
}