forked from Gigantua/Chess_Movegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SBAMG_IL.hpp
204 lines (176 loc) · 6.23 KB
/
SBAMG_IL.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
/* 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 : 29.01.2022
// Author : Constexpr Inlined SBAMG Version: Daniel Infuehr
#pragma once
#include <stdint.h>
#include <array>
namespace Chess_Lookup::SBAMGInline
{
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(int square) {
int rank = square >> 3;
int file = square & 7;
if constexpr (dir == 0)
return (0xFFull << (square & 56)) ^ (1ull << square); //HORIZONTAL
else if constexpr (dir == 1)
return (0x0101010101010101ull << (square & 7)) ^ (1ull << square); //VERTICAL
else if constexpr (dir == 2)
{
int d = 8 * (square & 7) - (square & 56);
return ((0x8040201008040201ull >> (d & (-d >> 31))) << (-d & (d >> 31))) ^ (1ull << square);
}
else
{
int d = 56 - 8 * (square & 7) - (square & 56);
return ((0x0102040810204080ull >> (d & (-d >> 31))) << (-d & (d >> 31))) ^ (1ull << square);
}
}
static constexpr uint64_t outersquare(int square) {
return (0x81ull << (square & 56)) &~ (1ull << square) | (square == 0); //Probably can be optimised - 2ull << (square >> 1) === (1ull << (square)) - (square == 0)
}
static constexpr uint64_t outersquare_file(int square) {
return (0x0100000000000001ULL << (square & 7)) &~ (1ull << square); //VERTICAL
}
static std::string _map(uint64_t value)
{
static std::string str(64 + 8, 'o');
for (uint64_t i = 0, c = 0; i < 64; i++)
{
uint64_t bitmask = (1ull) << i;
if ((bitmask & value) != 0) str[c++] = 'X';
else str[c++] = '.';
if ((i + 1) % 8 == 0) str[c++] = '\n';
}
return str;
}
static constexpr uint64_t ThisAndNextSq(int sq)
{
return 3ULL << sq;
};
static constexpr uint64_t PrevSquares(int sq)
{
return ((1ULL << sq) - 1) | (sq == 0);
};
constexpr auto Size = 0;
static constexpr int msb(uint64_t value)
{
return 63 - std::countl_zero(value | 1);
}
static constexpr uint64_t rank_attacks(int sq, uint64_t occ)
{
const auto rankmask = dirMask<0>(sq);
occ = (occ & rankmask) | outersquare(sq);
return ((occ - ThisAndNextSq(msb(occ & PrevSquares(sq)))) ^ occ)
& rankmask;
}
static constexpr uint64_t file_attacks(int sq, uint64_t occ)
{
const auto filemask = dirMask<1>(sq);
occ = (occ & filemask) | outersquare_file(sq);
return ((occ - ThisAndNextSq(msb(occ & PrevSquares(sq)))) ^ occ)
& filemask;
}
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)
{
const auto diagmask = dirMask<2>(sq);
occ &= diagmask;
return ((occ - (1ull << sq)) ^ byteswap(byteswap(occ) - (1ull << (sq^56))))
& diagmask;
}
static constexpr uint64_t adiag_attacks(int sq, uint64_t occ)
{
const auto adiagmask = dirMask<3>(sq);
occ &= adiagmask;
return ((occ - (1ull << sq)) ^ byteswap(byteswap(occ) - (1ull << (sq^56))))
& adiagmask;
}
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)
{
constexpr auto rankmask = dirMask<0>(sq);
occ = (occ & rankmask) | outersquare(sq);
return ((occ - ThisAndNextSq(msb(occ & PrevSquares(sq)))) ^ occ)
& rankmask;
}
template<int sq>
static constexpr uint64_t file_attacks(uint64_t occ)
{
constexpr auto filemask = dirMask<1>(sq);
occ = (occ & filemask) | outersquare_file(sq);
return ((occ - ThisAndNextSq(msb(occ & PrevSquares(sq)))) ^ occ)
& filemask;
}
template<int sq>
static constexpr uint64_t diag_attacks(uint64_t occ)
{
constexpr auto diagmask = dirMask<2>(sq);
occ &= diagmask;
return ((occ - (1ull << sq)) ^ byteswap(byteswap(occ) - (1ull << (sq^56))))
& diagmask;
}
template<int sq>
static constexpr uint64_t adiag_attacks(uint64_t occ)
{
constexpr auto adiagmask = dirMask<3>(sq);
occ &= adiagmask;
return ((occ - (1ull << sq)) ^ byteswap(byteswap(occ) - (1ull << (sq^56))))
& adiagmask;
}
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);
}
}