forked from Gigantua/Chess_Movegen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Boblookup.hpp
191 lines (153 loc) · 5.65 KB
/
Boblookup.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
#pragma once
// Robert Hyatt's and Michael Sherwin's classical bitboard approach
// generate moves for the sliding pieces.
#include <cstdint>
#include <bit>
#include <array>
namespace Chess_Lookup::BobLU {
struct Rays {
uint64_t rayNW;
uint64_t rayNN;
uint64_t rayNE;
uint64_t rayEE;
uint64_t raySE;
uint64_t raySS;
uint64_t raySW;
uint64_t rayWW;
uint64_t rwsNW;
uint64_t rwsNN;
uint64_t rwsNE;
uint64_t rwsEE;
uint64_t rwsSE;
uint64_t rwsSS;
uint64_t rwsSW;
uint64_t rwsWW;
};
consteval std::array<Rays,64> Initialize() {
enum { FILE1, FILE2, FILE3, FILE4, FILE5, FILE6, FILE7, FILE8 };
enum { RANK1, RANK2, RANK3, RANK4, RANK5, RANK6, RANK7, RANK8 };
int sq, ts, file, rank, c;
std::array<Rays, 64> ray{};
for (sq = 0; sq <= 63; sq++) {
file = sq & 7;
rank = sq >> 3;
// Northwest
ray[sq].rayNW = 0;
for (c = 1, ts = sq + 7; file - c >= FILE1 && rank + c <= RANK8; c++, ts += 7) ray[sq].rayNW |= 1ull << ts;
ray[sq].rwsNW = ray[sq].rayNW | 0x8000000000000000;
// Northeast
ray[sq].rayNE = 0;
for (c = 1, ts = sq + 9; file + c <= FILE8 && rank + c <= RANK8; c++, ts += 9) ray[sq].rayNE |= 1ull << ts;
ray[sq].rwsNE = ray[sq].rayNE | 0x8000000000000000;
// Southeast
ray[sq].raySE = 0;
for (c = 1, ts = sq - 7; file + c <= FILE8 && rank - c >= RANK1; c++, ts -= 7) ray[sq].raySE |= 1ull << ts;
ray[sq].rwsSE = ray[sq].raySE | 0x0000000000000001;
// Southwest
ray[sq].raySW = 0;
for (c = 1, ts = sq - 9; file - c >= FILE1 && rank - c >= RANK1; c++, ts -= 9) ray[sq].raySW |= 1ull << ts;
ray[sq].rwsSW = ray[sq].raySW | 0x0000000000000001;
// North
ray[sq].rayNN = 0;
for (c = 1, ts = sq + 8; rank + c <= RANK8; c++, ts += 8) ray[sq].rayNN |= 1ull << ts;
ray[sq].rwsNN = ray[sq].rayNN | 0x8000000000000000;
// East
ray[sq].rayEE = 0;
for (c = 1, ts = sq + 1; file + c <= FILE8; c++, ts += 1) ray[sq].rayEE |= 1ull << ts;
ray[sq].rwsEE = ray[sq].rayEE | 0x8000000000000000;
// South
ray[sq].raySS = 0;
for (c = 1, ts = sq - 8; rank - c >= RANK1; c++, ts -= 8) ray[sq].raySS |= 1ull << ts;
ray[sq].rwsSS = ray[sq].raySS | 0x0000000000000001;
// West
ray[sq].rayWW = 0;
for (c = 1, ts = sq - 1; file - c >= FILE1; c++, ts -= 1) ray[sq].rayWW |= 1ull << ts;
ray[sq].rwsWW = ray[sq].rayWW | 0x0000000000000001;
}
return ray;
}
constexpr std::array<Rays, 64> ray = Initialize();
constexpr std::array<uint64_t, 64> queens = []()
{
std::array<uint64_t, 64> q = {};
for (uint64_t i = 0; i < 64; i++)
{
Rays r = ray[i];
q[i] = r.rayNN |
r.rayEE |
r.raySS |
r.rayWW |
r.rayNW |
r.rayNE |
r.raySE |
r.raySW;
}
return q;
}();
constexpr auto Size = sizeof(ray);
static constexpr uint64_t Bishop(int sq, uint64_t occ) {
uint64_t r2 = 0;
occ |= 0x8000000000000001;
r2 |= ray[sq].rayNW ^ ray[std::countr_zero(ray[sq].rwsNW & occ)].rayNW;
r2 |= ray[sq].rayNE ^ ray[std::countr_zero(ray[sq].rwsNE & occ)].rayNE;
r2 |= ray[sq].raySE ^ ray[63 - std::countl_zero(ray[sq].rwsSE & occ)].raySE;
r2 |= ray[sq].raySW ^ ray[63 - std::countl_zero(ray[sq].rwsSW & occ)].raySW;
return r2;
}
static constexpr uint64_t Rook(int sq, uint64_t occ) {
uint64_t r1 = 0;
occ |= 0x8000000000000001;
r1 |= ray[sq].rayNN ^ ray[std::countr_zero(ray[sq].rwsNN & occ)].rayNN;
r1 |= ray[sq].rayEE ^ ray[std::countr_zero(ray[sq].rwsEE & occ)].rayEE;
r1 |= ray[sq].raySS ^ ray[63 - std::countl_zero(ray[sq].rwsSS & occ)].raySS;
r1 |= ray[sq].rayWW ^ ray[63 - std::countl_zero(ray[sq].rwsWW & occ)].rayWW;
return r1;
}
template<int sq>
static constexpr uint64_t Bishop(uint64_t occ) {
uint64_t r2 = 0;
occ |= 0x8000000000000001;
r2 |= ray[sq].rayNW ^ ray[std::countr_zero(ray[sq].rwsNW & occ)].rayNW;
r2 |= ray[sq].rayNE ^ ray[std::countr_zero(ray[sq].rwsNE & occ)].rayNE;
r2 |= ray[sq].raySE ^ ray[63 - std::countl_zero(ray[sq].rwsSE & occ)].raySE;
r2 |= ray[sq].raySW ^ ray[63 - std::countl_zero(ray[sq].rwsSW & occ)].raySW;
return r2;
}
template<int sq>
static constexpr uint64_t Rook(uint64_t occ) {
uint64_t r1 = 0;
occ |= 0x8000000000000001;
r1 |= ray[sq].rayNN ^ ray[std::countr_zero(ray[sq].rwsNN & occ)].rayNN;
r1 |= ray[sq].rayEE ^ ray[std::countr_zero(ray[sq].rwsEE & occ)].rayEE;
r1 |= ray[sq].raySS ^ ray[63 - std::countl_zero(ray[sq].rwsSS & occ)].raySS;
r1 |= ray[sq].rayWW ^ ray[63 - std::countl_zero(ray[sq].rwsWW & occ)].rayWW;
return r1;
}
static constexpr uint64_t Queen(int sq, uint64_t occ) {
occ |= 0x8000000000000001;
uint64_t bb = (ray[std::countr_zero(ray[sq].rwsNW & occ)].rayNW
| ray[std::countr_zero(ray[sq].rwsNN & occ)].rayNN
| ray[std::countr_zero(ray[sq].rwsNE & occ)].rayNE
| ray[std::countr_zero(ray[sq].rwsEE & occ)].rayEE
| ray[63 - std::countl_zero(ray[sq].rwsSE & occ)].raySE
| ray[63 - std::countl_zero(ray[sq].rwsSS & occ)].raySS
| ray[63 - std::countl_zero(ray[sq].rwsSW & occ)].raySW
| ray[63 - std::countl_zero(ray[sq].rwsWW & occ)].rayWW)
^ queens[sq];
return bb;// Rook(sq, occ) | Bishop(sq, occ);
}
template<int sq>
static constexpr uint64_t Queen(uint64_t occ) {
occ |= 0x8000000000000001;
uint64_t bb = (ray[std::countr_zero(ray[sq].rwsNW & occ)].rayNW
| ray[std::countr_zero(ray[sq].rwsNN & occ)].rayNN
| ray[std::countr_zero(ray[sq].rwsNE & occ)].rayNE
| ray[std::countr_zero(ray[sq].rwsEE & occ)].rayEE
| ray[63 - std::countl_zero(ray[sq].rwsSE & occ)].raySE
| ray[63 - std::countl_zero(ray[sq].rwsSS & occ)].raySS
| ray[63 - std::countl_zero(ray[sq].rwsSW & occ)].raySW
| ray[63 - std::countl_zero(ray[sq].rwsWW & occ)].rayWW)
^ queens[sq];
return bb;//Rook(sq, occ) | Bishop(sq, occ);
}
}