HooverChessUtils_PgnReader 0.9.0
Loading...
Searching...
No Matches
Static Public Member Functions | Static Private Member Functions | List of all members
hoover_chess_utils::pgn_reader::Attacks_Portable Class Reference

Portable implementations for attacks. More...

#include <bitboard-attacks-portable.h>

Static Public Member Functions

static SquareSet getPawnAttackMask (Square sq, Color pawnColor) noexcept
 
static SquareSet getPawnAttackerMask (Square sq, Color pawnColor) noexcept
 
template<Color pawnColor, bool captureToRight>
static constexpr SquareSet getPawnAttackersMask (SquareSet capturable) noexcept
 
static SquareSet getKnightAttackMask (Square sq) noexcept
 
static SquareSet getKingAttackMask (Square sq) noexcept
 
static SquareSet getBishopAttackMask (Square sq, SquareSet occupancyMask) noexcept
 See Attacks::getBishopAttackMask() for usage documentation.
 
static SquareSet getRookAttackMask (Square sq, SquareSet occupancyMask) noexcept
 See Attacks::getRookAttackMask() for usage documentation.
 
static SquareSet getHorizRookAttackMask (Square sq, SquareSet occupancyMask) noexcept
 Returns horizontal rook attack mask.
 

Static Private Member Functions

static SquareSet getSliderAttackMaskHyperbola (SquareSet pieceBit, SquareSet occupancyMask, SquareSet rayMaskEx) noexcept
 Calculate vertical, diagonal, or antidiagonal attack mask using the Hyperbola quintessence algorithm.
 

Detailed Description

Portable implementations for attacks.

Member Function Documentation

◆ getBishopAttackMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getBishopAttackMask ( Square  sq,
SquareSet  occupancyMask 
)
inlinestaticnoexcept

See Attacks::getBishopAttackMask() for usage documentation.

Parameters
[in]sqBishop square
[in]occupancyMaskSet of occupied squares
Returns
Set of attacked squares

getSliderAttackMaskHyperbola() is used to implement this function.

◆ getHorizRookAttackMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getHorizRookAttackMask ( Square  sq,
SquareSet  occupancyMask 
)
inlinestaticnoexcept

Returns horizontal rook attack mask.

Parameters
[in]sqRook square
[in]occupancyMaskSet of occupied squares
Returns
Set of horizontally attacked squares

◆ getKingAttackMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getKingAttackMask ( Square  sq)
inlinestaticnoexcept

◆ getKnightAttackMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getKnightAttackMask ( Square  sq)
inlinestaticnoexcept

◆ getPawnAttackerMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getPawnAttackerMask ( Square  sq,
Color  pawnColor 
)
inlinestaticnoexcept

◆ getPawnAttackersMask()

template<Color pawnColor, bool captureToRight>
static constexpr SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getPawnAttackersMask ( SquareSet  capturable)
inlinestaticconstexprnoexcept

◆ getPawnAttackMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getPawnAttackMask ( Square  sq,
Color  pawnColor 
)
inlinestaticnoexcept

◆ getRookAttackMask()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getRookAttackMask ( Square  sq,
SquareSet  occupancyMask 
)
inlinestaticnoexcept

See Attacks::getRookAttackMask() for usage documentation.

Parameters
[in]sqRook square
[in]occupancyMaskSet of occupied squares
Returns
Set of attacked squares

getSliderAttackMaskHyperbola() is used to implement the vertical attack mask resolution.

The horizontal attack mask is resolved as follows:

  1. Determine row and column of the attacking piece sq
  2. Determine shift for translating the row to the first rank (row * 8U)
  3. Shift the piece rank to first rank
  4. Use a precomputed table mapping the first rank occupancy and piece column to attack squares
  5. Shift the attack squares back to piece row

◆ getSliderAttackMaskHyperbola()

static SquareSet hoover_chess_utils::pgn_reader::Attacks_Portable::getSliderAttackMaskHyperbola ( SquareSet  pieceBit,
SquareSet  occupancyMask,
SquareSet  rayMaskEx 
)
inlinestaticprivatenoexcept

Calculate vertical, diagonal, or antidiagonal attack mask using the Hyperbola quintessence algorithm.

Parameters
[in]pieceBitSquare set containing only the attacking piece
[in]occupancyMaskOccupied squares
[in]rayMaskExForward/backward attack ray excluding the attacking piece. For example, the file of the rook not including the rook.
Returns
Attacked squares

Illustration

Input Return
oooooo
ooooooo
o
R
ooooooo
oooo
x
x
x
x
x
o = occupied square
shaded squares = rayMaskEx
R = attacker
Squares marked with x = return

Design

The implementation uses a variation of the "o^(o-2r)" trick. First, the occupancy mask is AND'd with the ray mask.

o
o
o

Then, two copies are taken: one as is (left side) and one mirrored vertically (right side):

o
o
o
o
o
o

The attacking piece is subtracted from the boards. Note that the attacking piece bit is also flipped vertically in the right-side board.

o
oo
oooooooo
oooooo
o
oo
oooooooo
oooooooo
oooooo
o
o

The right-side board is flipped vertically again.

o
oo
oooooooo
oooooo
o
o
o
oooooo
oooooooo
oooooooo
oo

The left-side and right-side boards are then XOR'd together.

oox
ooxooooo
ooxooooo
ooxooooo
oox

and the result is AND'd with the ray mask. This gives the final result.

The XOR operation isolates the bits that change in the subtraction operation. This is the reason why this algorithm works.

See also
https://www.chessprogramming.org/Hyperbola_Quintessence

The documentation for this class was generated from the following file: