BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Masks.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
24
25#pragma once
26
27#include <cstddef> // IWYU pragma: keep - for size_t
33#include <utility>
34
39
40using pieces::Color;
41using std::size_t;
42
43using namespace board::literals; // NOLINT(build/namespaces_literals)
44
47
49inline constexpr auto ALL = 0XFFFFFFFFFFFFFFFF_bb;
50
52inline constexpr auto NONE = 0X0_bb;
53
55inline constexpr auto DARK_SQUARES = 0xAA55AA55AA55AA55_bb;
56
58inline constexpr auto LIGHT_SQUARES = 0x55AA55AA55AA55AA_bb;
59
61inline constexpr auto MAIN_DIAGONAL = 0x8040201008040201_bb;
62
64inline constexpr auto MAIN_ANTIDIAGONAL = 0x0102040810204080_bb;
65
67inline constexpr auto CENTER = 0X1818000000_bb;
68
70inline constexpr auto PERIMETER = 0XFF818181818181FF_bb;
71
73[[nodiscard, gnu::const]] constexpr auto diagonal(const Square& square) noexcept -> Bitboard
74{
75 const auto diag = static_cast<int>(square.file) - static_cast<int>(square.rank);
76
77 if (std::cmp_greater_equal(diag, 0))
78 return MAIN_DIAGONAL >> static_cast<size_t>(diag) * 8uz;
79
80 return MAIN_DIAGONAL << static_cast<size_t>(-diag) * 8uz;
81}
82
84[[nodiscard, gnu::const]] constexpr auto antidiagonal(const Square& square) noexcept -> Bitboard
85{
86 const auto diag = 7 - static_cast<int>(square.file) - static_cast<int>(square.rank);
87
88 if (std::cmp_greater_equal(diag, 0))
89 return MAIN_ANTIDIAGONAL >> static_cast<size_t>(diag) * 8uz;
90
92}
93
100[[nodiscard, gnu::const]] constexpr auto queenside_castle_rook_pos_mask(
101 const Color side) noexcept -> Bitboard
102{
103 const auto rank = back_rank_for(side);
104
105 Bitboard mask;
106
107 mask.set(Square { .file = File::A, .rank = rank });
108 mask.set(Square { .file = File::D, .rank = rank });
109
110 return mask;
111}
112
119[[nodiscard, gnu::const]] constexpr auto kingside_castle_rook_pos_mask(
120 const Color side) noexcept -> Bitboard
121{
122 const auto rank = back_rank_for(side);
123
124 Bitboard mask;
125
126 mask.set(Square { .file = File::H, .rank = rank });
127 mask.set(Square { .file = File::F, .rank = rank });
128
129 return mask;
130}
131
133
137namespace files {
138
141
142 // NOLINTBEGIN(readability-identifier-length)
143
145 inline constexpr auto A = 0x0101010101010101_bb;
146
148 inline constexpr auto B = 0X202020202020202_bb;
149
151 inline constexpr auto C = 0X404040404040404_bb;
152
154 inline constexpr auto D = 0X808080808080808_bb;
155
157 inline constexpr auto E = 0X1010101010101010_bb;
158
160 inline constexpr auto F = 0X2020202020202020_bb;
161
163 inline constexpr auto G = 0X4040404040404040_bb;
164
166 inline constexpr auto H = 0x8080808080808080_bb;
167
168 // NOLINTEND(readability-identifier-length)
169
171 [[nodiscard, gnu::const]] constexpr auto get(const File file) noexcept -> Bitboard
172 {
173 return A << static_cast<size_t>(file);
174 }
175
177
178} // namespace files
179
183namespace ranks {
184
187
189 inline constexpr auto ONE = 0x00000000000000FF_bb;
190
192 inline constexpr auto TWO = 0XFF00_bb;
193
195 inline constexpr auto THREE = 0XFF0000_bb;
196
198 inline constexpr auto FOUR = 0XFF000000_bb;
199
201 inline constexpr auto FIVE = 0XFF00000000_bb;
202
204 inline constexpr auto SIX = 0XFF0000000000_bb;
205
207 inline constexpr auto SEVEN = 0XFF000000000000_bb;
208
210 inline constexpr auto EIGHT = 0xFF00000000000000_bb;
211
213 [[nodiscard, gnu::const]] constexpr auto get(const Rank rank) noexcept -> Bitboard
214 {
215 return ONE << (8uz * std::to_underlying(rank));
216 }
217
219
220} // namespace ranks
221
227namespace starting {
228
235 namespace white {
236
239
241 inline constexpr auto PAWNS = ranks::TWO;
242
244 inline constexpr auto ROOKS = 0X81_bb;
245
247 inline constexpr auto KNIGHTS = 0X42_bb;
248
250 inline constexpr auto BISHOPS = 0X24_bb;
251
253 inline constexpr auto QUEEN = 0X8_bb;
254
256 inline constexpr auto KING = 0X10_bb;
257
259
260 } // namespace white
261
268 namespace black {
269
272
274 inline constexpr auto PAWNS = ranks::SEVEN;
275
277 inline constexpr auto ROOKS = 0X8100000000000000_bb;
278
280 inline constexpr auto KNIGHTS = 0X4200000000000000_bb;
281
283 inline constexpr auto BISHOPS = 0X2400000000000000_bb;
284
286 inline constexpr auto QUEEN = 0X800000000000000_bb;
287
289 inline constexpr auto KING = 0X1000000000000000_bb;
290
292
293 } // namespace black
294
297
299 [[nodiscard, gnu::const]] constexpr auto pawns(const Color color) noexcept -> Bitboard
300 {
301 if (color == Color::White)
302 return white::PAWNS;
303
304 return black::PAWNS;
305 }
306
308 [[nodiscard, gnu::const]] constexpr auto rooks(const Color color) noexcept -> Bitboard
309 {
310 if (color == Color::White)
311 return white::ROOKS;
312
313 return black::ROOKS;
314 }
315
317 [[nodiscard, gnu::const]] constexpr auto knights(const Color color) noexcept -> Bitboard
318 {
319 if (color == Color::White)
320 return white::KNIGHTS;
321
322 return black::KNIGHTS;
323 }
324
326 [[nodiscard, gnu::const]] constexpr auto bishops(const Color color) noexcept -> Bitboard
327 {
328 if (color == Color::White)
329 return white::BISHOPS;
330
331 return black::BISHOPS;
332 }
333
335 [[nodiscard, gnu::const]] constexpr auto queen(const Color color) noexcept -> Bitboard
336 {
337 if (color == Color::White)
338 return white::QUEEN;
339
340 return black::QUEEN;
341 }
342
344 [[nodiscard, gnu::const]] constexpr auto king(const Color color) noexcept -> Bitboard
345 {
346 if (color == Color::White)
347 return white::KING;
348
349 return black::KING;
350 }
351
353
354} // namespace starting
355
356} // namespace chess::board::masks
constexpr auto MAIN_DIAGONAL
Definition Masks.hpp:61
constexpr auto DARK_SQUARES
Definition Masks.hpp:55
constexpr auto EIGHT
Definition Masks.hpp:210
constexpr auto queenside_castle_rook_pos_mask(const Color side) noexcept -> Bitboard
Definition Masks.hpp:100
constexpr auto knights(const Color color) noexcept -> Bitboard
Definition Masks.hpp:317
constexpr auto PERIMETER
Definition Masks.hpp:70
constexpr auto CENTER
Definition Masks.hpp:67
constexpr auto ALL
Definition Masks.hpp:49
constexpr auto SEVEN
Definition Masks.hpp:207
constexpr auto FIVE
Definition Masks.hpp:201
constexpr auto bishops(const Color color) noexcept -> Bitboard
Definition Masks.hpp:326
constexpr auto FOUR
Definition Masks.hpp:198
constexpr auto NONE
Definition Masks.hpp:52
constexpr auto get(const File file) noexcept -> Bitboard
Definition Masks.hpp:171
constexpr auto antidiagonal(const Square &square) noexcept -> Bitboard
Definition Masks.hpp:84
constexpr auto THREE
Definition Masks.hpp:195
constexpr auto pawns(const Color color) noexcept -> Bitboard
Definition Masks.hpp:299
constexpr auto king(const Color color) noexcept -> Bitboard
Definition Masks.hpp:344
constexpr auto queen(const Color color) noexcept -> Bitboard
Definition Masks.hpp:335
constexpr auto MAIN_ANTIDIAGONAL
Definition Masks.hpp:64
constexpr auto diagonal(const Square &square) noexcept -> Bitboard
Definition Masks.hpp:73
constexpr auto get(const Rank rank) noexcept -> Bitboard
Definition Masks.hpp:213
constexpr auto LIGHT_SQUARES
Definition Masks.hpp:58
constexpr auto rooks(const Color color) noexcept -> Bitboard
Definition Masks.hpp:308
constexpr auto kingside_castle_rook_pos_mask(const Color side) noexcept -> Bitboard
Definition Masks.hpp:119
constexpr auto back_rank_for(Color color) noexcept -> Rank
Definition Rank.hpp:132
@ A
The A file.
Definition File.hpp:37
@ F
The F file.
Definition File.hpp:42
@ H
The H file.
Definition File.hpp:44
@ D
The D file. This is the file that the queens start on.
Definition File.hpp:40
constexpr void set(const Square square) noexcept
Definition Bitboard.hpp:101