HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
bitboard-attacks-portable.h
Go to the documentation of this file.
1// Hoover Chess Utilities / PGN reader
2// Copyright (C) 2023-2025 Sami Kiminki
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <https://www.gnu.org/licenses/>.
16
17#ifndef HOOVER_CHESS_UTILS__PGN_READER__BITBOARD_ATTACKS_PORTABLE_H_INCLUDED
18#define HOOVER_CHESS_UTILS__PGN_READER__BITBOARD_ATTACKS_PORTABLE_H_INCLUDED
19
20#include "chessboard-types.h"
22
23#include "bitboard-intercepts.h"
24#include "lookup-utils.h"
25
26#include <array>
27#include <cstdint>
28
30{
31
35{
36private:
64 // </td>
207 SquareSet pieceBit, SquareSet occupancyMask, SquareSet rayMaskEx) noexcept;
208
209public:
210
211 static inline SquareSet getPawnAttackMask(Square sq, Color pawnColor) noexcept
212 {
213 return SquareSet {
216 static_cast<std::size_t>(sq), pawnColor) };
217 }
218
219 static inline SquareSet getPawnAttackerMask(Square sq, Color pawnColor) noexcept
220 {
221 static_assert(static_cast<std::uint64_t>(Color::WHITE) == 0U);
222 static_assert(static_cast<std::uint64_t>(Color::BLACK) == 8U);
223
224 return SquareSet {
227 static_cast<std::size_t>(sq), oppositeColor(pawnColor)) };
228 }
229
230 template <Color pawnColor, bool captureToRight>
231 static constexpr inline SquareSet getPawnAttackersMask(SquareSet capturable) noexcept
232 {
233 if constexpr (pawnColor == Color::WHITE)
234 {
235 if constexpr (captureToRight)
236 {
237 SquareSet destMask { 0xFE'FE'FE'FE'FE'FE'FE'00 };
238 return (capturable & destMask) >> 9U;
239 }
240 else
241 {
242 SquareSet destMask { 0x7F'7F'7F'7F'7F'7F'7F'00 };
243 return (capturable & destMask) >> 7U;
244 }
245 }
246 else
247 {
248 if constexpr (captureToRight)
249 {
250 SquareSet destMask { 0x00'FE'FE'FE'FE'FE'FE'FE };
251 return (capturable & destMask) << 7U;
252 }
253 else
254 {
255 SquareSet destMask { 0x00'7F'7F'7F'7F'7F'7F'7F };
256 return (capturable & destMask) << 9U;
257 }
258 }
259 }
260
261 static inline SquareSet getKnightAttackMask(Square sq) noexcept
262 {
264 }
265
266 static inline SquareSet getKingAttackMask(Square sq) noexcept
267 {
269 }
270
278 static inline SquareSet getBishopAttackMask(Square sq, SquareSet occupancyMask) noexcept;
279
296 static inline SquareSet getRookAttackMask(Square sq, SquareSet occupancyMask) noexcept;
297
303 static inline SquareSet getHorizRookAttackMask(Square sq, SquareSet occupancyMask) noexcept
304 {
305 // horizontal attacks
306 const SquareUnderlyingType rankShift = static_cast<SquareUnderlyingType>(sq) & 56U;
307 const SquareUnderlyingType sqColumn = static_cast<SquareUnderlyingType>(sq) & 7U;
308 const std::uint64_t occupancyShifted = static_cast<std::uint64_t>(occupancyMask >> rankShift) & 0xFFU;
309
310 return SquareSet { ctBitBoardTables.rookHorizAttackMasks[occupancyShifted][sqColumn] } << rankShift;
311 }
312};
313
314#if (BITBOARD_TABLES_HAVE_HYPERBOLA)
316 SquareSet pieceBit, SquareSet occupancyMask, SquareSet rayMaskEx) noexcept
317{
318 std::uint64_t forward { static_cast<std::uint64_t>(occupancyMask & rayMaskEx) };
319 std::uint64_t reverse { static_cast<std::uint64_t>(SquareSet { forward }.flipVert()) };
320
321 forward = forward - static_cast<std::uint64_t>(pieceBit);
322 reverse = reverse - static_cast<std::uint64_t>(pieceBit.flipVert());
323
324 return (SquareSet { forward } ^ SquareSet { reverse }.flipVert()) & rayMaskEx;
325}
326
327SquareSet Attacks_Portable::getBishopAttackMask(Square sq, SquareSet occupancyMask) noexcept
328{
329 const SquareSet pieceBit { ctBitBoardTables.hyperbolaAttackMasks[getIndexOfSquare(sq)].sqBit };
330
331 return
332 getSliderAttackMaskHyperbola(
333 pieceBit, occupancyMask, SquareSet { ctBitBoardTables.hyperbolaAttackMasks[getIndexOfSquare(sq)].diagBLTREx }) |
334 getSliderAttackMaskHyperbola(
335 pieceBit, occupancyMask, SquareSet { ctBitBoardTables.hyperbolaAttackMasks[getIndexOfSquare(sq)].diagBRTLEx });
336}
337
338SquareSet Attacks_Portable::getRookAttackMask(Square sq, SquareSet occupancyMask) noexcept
339{
340 // vertical attacks
341 const SquareSet pieceBit { ctBitBoardTables.hyperbolaAttackMasks[getIndexOfSquare(sq)].sqBit };
342 const SquareSet vertAttacks {
343 getSliderAttackMaskHyperbola(
344 pieceBit, occupancyMask, SquareSet { ctBitBoardTables.hyperbolaAttackMasks[getIndexOfSquare(sq)].vertMaskEx }) };
345
346 // horizontal attacks
347 const SquareUnderlyingType rankShift = static_cast<SquareUnderlyingType>(sq) & 56U;
348 const SquareUnderlyingType sqColumn = static_cast<SquareUnderlyingType>(sq) & 7U;
349 const std::uint64_t occupancyShifted = static_cast<std::uint64_t>(occupancyMask >> rankShift) & 0xFFU;
350
351 return vertAttacks | (SquareSet { ctBitBoardTables.rookHorizAttackMasks[occupancyShifted][sqColumn] } << rankShift);
352}
353#endif
354
355}
356
357#endif
Portable implementations for attacks.
Definition bitboard-attacks-portable.h:35
static SquareSet getBishopAttackMask(Square sq, SquareSet occupancyMask) noexcept
See Attacks::getBishopAttackMask() for usage documentation.
static SquareSet getHorizRookAttackMask(Square sq, SquareSet occupancyMask) noexcept
Returns horizontal rook attack mask.
Definition bitboard-attacks-portable.h:303
static SquareSet getPawnAttackMask(Square sq, Color pawnColor) noexcept
Definition bitboard-attacks-portable.h:211
static SquareSet getSliderAttackMaskHyperbola(SquareSet pieceBit, SquareSet occupancyMask, SquareSet rayMaskEx) noexcept
Calculate vertical, diagonal, or antidiagonal attack mask using the Hyperbola quintessence algorithm.
static SquareSet getKingAttackMask(Square sq) noexcept
Definition bitboard-attacks-portable.h:266
static SquareSet getKnightAttackMask(Square sq) noexcept
Definition bitboard-attacks-portable.h:261
static SquareSet getPawnAttackerMask(Square sq, Color pawnColor) noexcept
Definition bitboard-attacks-portable.h:219
static SquareSet getRookAttackMask(Square sq, SquareSet occupancyMask) noexcept
See Attacks::getRookAttackMask() for usage documentation.
static constexpr SquareSet getPawnAttackersMask(SquareSet capturable) noexcept
Definition bitboard-attacks-portable.h:231
Set of squares. Implemented using a bit-mask.
Definition chessboard-types-squareset.h:35
constexpr Color oppositeColor(Color c) noexcept
Flips the color.
Definition chessboard-types.h:409
std::uint_fast8_t SquareUnderlyingType
Underlying type of Square
Definition chessboard-types.h:38
constexpr std::size_t getIndexOfSquare(Square sq) noexcept
Returns an index for a square.
Definition chessboard-types.h:372
Color
Color of a piece or side to move.
Definition chessboard-types.h:194
Square
Named square.
Definition chessboard-types.h:122
@ BLACK
Black piece or black side to move.
@ WHITE
White piece or white side to move.
const U64Type & turnSpecificArrayLookup(const std::array< std::array< U64Type, 2U >, N > &array, std::size_t i, Color turn) noexcept
Optimized lookup for arrays indexed by general index and turn (side-to-move).
Definition lookup-utils.h:92
Definition chessboard-types-squareset.h:30
const BitBoardTables ctBitBoardTables
Various bitboard attack and other tables.
std::array< std::array< std::uint64_t, 2U >, 64U > pawnAttackMasks
Pawn attack masks.
Definition bitboard-tables.h:45
std::array< std::array< std::uint8_t, 8U >, 256U > rookHorizAttackMasks
Horizontal rook attacks: column to squares on the column.
Definition bitboard-tables.h:75
std::array< std::uint64_t, 64U > kingAttackMasks
King attack masks.
Definition bitboard-tables.h:57
std::array< std::uint64_t, 64U > knightAttackMasks
Knight attack masks.
Definition bitboard-tables.h:51