-
Notifications
You must be signed in to change notification settings - Fork 5
/
SBAMG.hpp
278 lines (248 loc) · 10.1 KB
/
SBAMG.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
/* M42 - A small library for Bitboard attack mask generation, by Syed Fahad
*
* Usage: Just include this file as a normal header, and m42.cpp into your
* source code, and then call M42::init() at your program startup.
* THIS IS IMPORTANT, you've to call M42::init() in the main() function of
* your program, to use this library.
* Read the documentation to know what every function does.
*
* Developer contact: sydfhd at gmail dot com
*
* License: MIT License (see https://mit-license.org/)
*/
// Datum : 28.01.2022
// Author : Constexpr SBAMG Version: Daniel Infuehr
#pragma once
#include <stdint.h>
#include <array>
namespace Chess_Lookup::SBAMG
{
template<uint64_t bb>
static constexpr uint64_t mask_shift(int ranks) {
return ranks > 0 ? bb >> (ranks << 3) : bb << -(ranks << 3);
}
template<int dir>
static constexpr uint64_t dirMask(unsigned int square) {
int rank = square >> 3;
int file = square & 7;
if (dir == 0)
return 0xFFull << (square & 56); //HORIZONTAL
else if (dir == 1)
return 0x0101010101010101ull << (square & 7); //VERTICAL
else if (dir == 2)
return mask_shift<0x8040201008040201ull>(file - rank); //Diagonal
else {
return mask_shift<0x0102040810204080ull>(7 - file - rank); //Antidiagonal
}
}
static constexpr uint64_t OuterSquares_Ranks[64] = {
0x0000000000000081ULL, 0x0000000000000081ULL, 0x0000000000000081ULL,
0x0000000000000081ULL, 0x0000000000000081ULL, 0x0000000000000081ULL,
0x0000000000000081ULL, 0x0000000000000001ULL, 0x0000000000008000ULL,
0x0000000000008100ULL, 0x0000000000008100ULL, 0x0000000000008100ULL,
0x0000000000008100ULL, 0x0000000000008100ULL, 0x0000000000008100ULL,
0x0000000000000100ULL, 0x0000000000800000ULL, 0x0000000000810000ULL,
0x0000000000810000ULL, 0x0000000000810000ULL, 0x0000000000810000ULL,
0x0000000000810000ULL, 0x0000000000810000ULL, 0x0000000000010000ULL,
0x0000000080000000ULL, 0x0000000081000000ULL, 0x0000000081000000ULL,
0x0000000081000000ULL, 0x0000000081000000ULL, 0x0000000081000000ULL,
0x0000000081000000ULL, 0x0000000001000000ULL, 0x0000008000000000ULL,
0x0000008100000000ULL, 0x0000008100000000ULL, 0x0000008100000000ULL,
0x0000008100000000ULL, 0x0000008100000000ULL, 0x0000008100000000ULL,
0x0000000100000000ULL, 0x0000800000000000ULL, 0x0000810000000000ULL,
0x0000810000000000ULL, 0x0000810000000000ULL, 0x0000810000000000ULL,
0x0000810000000000ULL, 0x0000810000000000ULL, 0x0000010000000000ULL,
0x0080000000000000ULL, 0x0081000000000000ULL, 0x0081000000000000ULL,
0x0081000000000000ULL, 0x0081000000000000ULL, 0x0081000000000000ULL,
0x0081000000000000ULL, 0x0001000000000000ULL, 0x8000000000000000ULL,
0x8100000000000000ULL, 0x8100000000000000ULL, 0x8100000000000000ULL,
0x8100000000000000ULL, 0x8100000000000000ULL, 0x8100000000000000ULL,
0x0100000000000000ULL
};
static constexpr uint64_t OuterSquares_Files[64] = {
0x0100000000000000ULL, 0x0200000000000000ULL, 0x0400000000000000ULL,
0x0800000000000000ULL, 0x1000000000000000ULL, 0x2000000000000000ULL,
0x4000000000000000ULL, 0x8000000000000000ULL, 0x0100000000000001ULL,
0x0200000000000002ULL, 0x0400000000000004ULL, 0x0800000000000008ULL,
0x1000000000000010ULL, 0x2000000000000020ULL, 0x4000000000000040ULL,
0x8000000000000080ULL, 0x0100000000000001ULL, 0x0200000000000002ULL,
0x0400000000000004ULL, 0x0800000000000008ULL, 0x1000000000000010ULL,
0x2000000000000020ULL, 0x4000000000000040ULL, 0x8000000000000080ULL,
0x0100000000000001ULL, 0x0200000000000002ULL, 0x0400000000000004ULL,
0x0800000000000008ULL, 0x1000000000000010ULL, 0x2000000000000020ULL,
0x4000000000000040ULL, 0x8000000000000080ULL, 0x0100000000000001ULL,
0x0200000000000002ULL, 0x0400000000000004ULL, 0x0800000000000008ULL,
0x1000000000000010ULL, 0x2000000000000020ULL, 0x4000000000000040ULL,
0x8000000000000080ULL, 0x0100000000000001ULL, 0x0200000000000002ULL,
0x0400000000000004ULL, 0x0800000000000008ULL, 0x1000000000000010ULL,
0x2000000000000020ULL, 0x4000000000000040ULL, 0x8000000000000080ULL,
0x0100000000000001ULL, 0x0200000000000002ULL, 0x0400000000000004ULL,
0x0800000000000008ULL, 0x1000000000000010ULL, 0x2000000000000020ULL,
0x4000000000000040ULL, 0x8000000000000080ULL, 0x0000000000000001ULL,
0x0000000000000002ULL, 0x0000000000000004ULL, 0x0000000000000008ULL,
0x0000000000000010ULL, 0x0000000000000020ULL, 0x0000000000000040ULL,
0x0000000000000080ULL
};
static constexpr std::array<uint64_t, 64> SquareMask = []()
{
std::array<uint64_t, 64> ret_SquareMask = {};
for (uint64_t sq = 0; sq < 64; sq++)
{
ret_SquareMask[sq] = 1ULL << sq;
}
return ret_SquareMask;
}();
static constexpr std::array<uint64_t, 64> ThisAndNextSq = []()
{
std::array<uint64_t, 64> ret_ThisAndNextSq = {};
for (uint64_t sq = 0; sq < 64; sq++)
{
ret_ThisAndNextSq[sq] = 3ULL << sq;
}
return ret_ThisAndNextSq;
}();
static constexpr std::array<uint64_t, 64> PrevSquares = []()
{
std::array<uint64_t, 64> ret_PrevSquares = {};
for (int sq = 0; sq < 64; sq++)
{
ret_PrevSquares[sq] = ((1ULL << sq) - 1) + (sq == 0);
}
return ret_PrevSquares;
}();
static constexpr std::array<uint64_t, 64> RankMaskEx = []()
{
std::array<uint64_t, 64> ret_RankMaskEx = {};
for (int sq = 0; sq < 64; sq++)
{
ret_RankMaskEx[sq] = dirMask<0>(sq) & ~SquareMask[sq];
}
return ret_RankMaskEx;
}();
static constexpr std::array<uint64_t, 64> FileMaskEx = []()
{
std::array<uint64_t, 64> ret_FileMaskEx = {};
for (int sq = 0; sq < 64; sq++)
{
ret_FileMaskEx[sq] = dirMask<1>(sq) & ~SquareMask[sq];
}
return ret_FileMaskEx;
}();
static constexpr std::array<uint64_t, 64> DiagMaskEx = []()
{
std::array<uint64_t, 64> ret_DiagMaskEx = {};
for (int sq = 0; sq < 64; sq++)
{
ret_DiagMaskEx[sq] = dirMask<2>(sq) & ~SquareMask[sq];
}
return ret_DiagMaskEx;
}();
static constexpr std::array<uint64_t, 64> ADiagMaskEx = []()
{
std::array<uint64_t, 64> ret_ADiagMaskEx = {};
for (int sq = 0; sq < 64; sq++)
{
ret_ADiagMaskEx[sq] = dirMask<3>(sq) & ~SquareMask[sq];
}
return ret_ADiagMaskEx;
}();
constexpr auto Size = 9 * 64 * sizeof(uint64_t);
static constexpr uint64_t msb(uint64_t value)
{
return 63ull - std::countl_zero(value | 1);
}
static constexpr uint64_t rank_attacks(int sq, uint64_t occ)
{
occ = (occ & RankMaskEx[sq]) | OuterSquares_Ranks[sq];
return ((occ - ThisAndNextSq[msb(occ & PrevSquares[sq])]) ^ occ)
& RankMaskEx[sq];
}
static constexpr uint64_t file_attacks(int sq, uint64_t occ)
{
occ = (occ & FileMaskEx[sq]) | OuterSquares_Files[sq];
return ((occ - ThisAndNextSq[msb(occ & PrevSquares[sq])]) ^ occ)
& FileMaskEx[sq];
}
static constexpr uint64_t byteswap_constexpr(uint64_t b) {
b = ((b >> 8) & 0x00FF00FF00FF00FFULL) | ((b & 0x00FF00FF00FF00FFULL) << 8);
b = ((b >> 16) & 0x0000FFFF0000FFFFULL) | ((b & 0x0000FFFF0000FFFFULL) << 16);
return (b >> 32) | (b << 32);
return b;
}
static constexpr uint64_t byteswap(uint64_t b) {
if (std::is_constant_evaluated()) { return byteswap_constexpr(b); }
#if defined(_MSC_VER)
return _byteswap_uint64(b);
#elif defined(__GNUC__)
return __builtin_bswap64(b);
#else
return byteswap_constexpr(b);
#endif
}
// NORMAL VERSION
static constexpr uint64_t diag_attacks(int sq, uint64_t occ)
{
occ &= DiagMaskEx[sq];
return ((occ - SquareMask[sq]) ^ byteswap(byteswap(occ) - SquareMask[sq ^ 56]))
& DiagMaskEx[sq];
}
static constexpr uint64_t adiag_attacks(int sq, uint64_t occ)
{
occ &= ADiagMaskEx[sq];
return ((occ - SquareMask[sq]) ^ byteswap(byteswap(occ) - SquareMask[sq ^ 56]))
& ADiagMaskEx[sq];
}
static constexpr uint64_t Bishop(int sq, uint64_t occ)
{
return diag_attacks(sq, occ) | adiag_attacks(sq, occ);
}
static constexpr uint64_t Rook(int sq, uint64_t occ)
{
return file_attacks(sq, occ) | rank_attacks(sq, occ);
}
static constexpr uint64_t Queen(int sq, uint64_t occ)
{
return Bishop(sq, occ) | Rook(sq, occ);
}
// TEMPLATE VERSION
template<int sq>
static constexpr uint64_t rank_attacks(uint64_t occ)
{
occ = (occ & RankMaskEx[sq]) | OuterSquares_Ranks[sq];
return ((occ - ThisAndNextSq[msb(occ & PrevSquares[sq])]) ^ occ)
& RankMaskEx[sq];
}
template<int sq>
static constexpr uint64_t file_attacks(uint64_t occ)
{
occ = (occ & FileMaskEx[sq]) | OuterSquares_Files[sq];
return ((occ - ThisAndNextSq[msb(occ & PrevSquares[sq])]) ^ occ)
& FileMaskEx[sq];
}
template<int sq>
static constexpr uint64_t diag_attacks(uint64_t occ)
{
occ &= DiagMaskEx[sq];
return ((occ - SquareMask[sq]) ^ byteswap(byteswap(occ) - SquareMask[sq ^ 56]))
& DiagMaskEx[sq];
}
template<int sq>
static constexpr uint64_t adiag_attacks(uint64_t occ)
{
occ &= ADiagMaskEx[sq];
return ((occ - SquareMask[sq]) ^ byteswap(byteswap(occ) - SquareMask[sq ^ 56]))
& ADiagMaskEx[sq];
}
template<int sq>
static constexpr uint64_t Rook(uint64_t occ) {
return file_attacks<sq>(occ) | rank_attacks<sq>(occ);
}
template<int sq>
static constexpr uint64_t Bishop(uint64_t occ) {
return diag_attacks<sq>(occ) | adiag_attacks<sq>(occ);
}
template<int sq>
static constexpr uint64_t Queen(uint64_t occ) {
return Rook<sq>(occ) | Bishop<sq>(occ);
}
}