Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .clang-format
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
BasedOnStyle: LLVM
IndentWidth: 4
ContinuationIndentWidth: 4
ColumnLimit: 1000
ColumnLimit: 128
AllowAllArgumentsOnNextLine: false
AllowAllConstructorInitializersOnNextLine: false
BinPackParameters: false
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: clang-format check

uses: jidicula/clang-format-action@v4.16.0
on:
pull_request:
branches: [ "main" ]
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ build/
!.gitignore
CMakePresets.json
CMakeSettings.json
*.s
*.s
.vscode
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ endif()
set(SOURCES
position.cpp
attacks.cpp "zobrist.cpp"
"moves_io.cpp" "printers.cpp" "repetition.h" "fwd_decl.h")
"moves_io.cpp" "printers.cpp" "movegen.cpp")
add_library(chesslib STATIC ${SOURCES})
target_include_directories(chesslib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
# --- Enable CTest integration ---
Expand Down
36 changes: 9 additions & 27 deletions attacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,15 +116,17 @@ _POSSIBLY_CONSTEXPR std::array<uint64_t, 64> BishopMagics = {
};

// clang-format on
template <auto AttackFunc, size_t TableSize, bool IsBishop> _POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, TableSize>> generate_magic_table() {
template <auto AttackFunc, size_t TableSize, bool IsBishop>
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, TableSize>> generate_magic_table() {
std::array<Magic, 64> table{};
std::array<Bitboard, TableSize> attacks{};

size_t offset = 0;

for (Square sq = SQ_A1; sq < SQ_NONE; ++sq) {
Bitboard occ = 0;
Bitboard edges = ((attacks::MASK_RANK[0] | attacks::MASK_RANK[7]) & ~attacks::MASK_RANK[rank_of(sq)]) | ((attacks::MASK_FILE[0] | attacks::MASK_FILE[7]) & ~attacks::MASK_FILE[file_of(sq)]);
Bitboard edges = ((attacks::MASK_RANK[0] | attacks::MASK_RANK[7]) & ~attacks::MASK_RANK[rank_of(sq)]) |
((attacks::MASK_FILE[0] | attacks::MASK_FILE[7]) & ~attacks::MASK_FILE[file_of(sq)]);

Bitboard mask = AttackFunc(static_cast<Square>(sq), 0) & ~edges;
int bits = popcount(mask);
Expand Down Expand Up @@ -156,33 +158,13 @@ template <auto AttackFunc, size_t TableSize, bool IsBishop> _POSSIBLY_CONSTEXPR

return std::pair{ table, attacks };
}
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x1480>> bishopData = generate_magic_table<_chess::_HyperbolaBishopAttacks, 0x1480, true>();
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x1480>> bishopData =
generate_magic_table<_chess::_HyperbolaBishopAttacks, 0x1480, true>();
_POSSIBLY_CONSTEXPR std::array<Magic, 64> BishopTable = bishopData.first;
_POSSIBLY_CONSTEXPR std::array<Bitboard, 0x1480> BishopAttacks = bishopData.second;

_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x19000>> rookData = generate_magic_table<_chess::_HyperbolaRookAttacks, 0x19000, false>();
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x19000>> rookData =
generate_magic_table<_chess::_HyperbolaRookAttacks, 0x19000, false>();
_POSSIBLY_CONSTEXPR std::array<Magic, 64> RookTable = rookData.first;
_POSSIBLY_CONSTEXPR std::array<Bitboard, 0x19000> RookAttacks = rookData.second;
} // namespace chess::attacks

namespace chess::movegen {
inline static _POSSIBLY_CONSTEXPR Bitboard att(PieceType pt, Square sq, Bitboard occ) { return (pt == BISHOP) ? _chess::_HyperbolaBishopAttacks(sq, occ) : _chess ::_HyperbolaRookAttacks(sq, occ); }

inline static _POSSIBLY_CONSTEXPR std::array<std::array<Bitboard, 64>, 64> generate_between() {
std::array<std::array<Bitboard, 64>, 64> squares_between_bb{};

for (int sq1 = 0; sq1 < 64; ++sq1) {
for (PieceType pt : { BISHOP, ROOK }) {
for (int sq2 = 0; sq2 < 64; ++sq2) {
if (att(pt, Square(sq1), 0) & (1ULL << sq2)) {
squares_between_bb[sq1][sq2] = att(pt, Square(sq1), 1ULL << (sq2)) & att(pt, Square(sq2), 1ULL << (sq1));
}
squares_between_bb[sq1][sq2] |= 1ULL << (sq2);
}
}
}

return squares_between_bb;
}
_POSSIBLY_CONSTEXPR std::array<std::array<Bitboard, 64>, 64> SQUARES_BETWEEN_BB = generate_between();
} // namespace chess::movegen
} // namespace chess::attacks
7 changes: 1 addition & 6 deletions attacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,9 +283,4 @@ template <PieceType pt> [[nodiscard]] constexpr Bitboard slider(Square sq, Bitbo
return queen(sq, occupied);
}

} // namespace chess::attacks

namespace chess::movegen {
extern const std::array<std::array<Bitboard, 64>, 64> SQUARES_BETWEEN_BB;
[[nodiscard]] inline Bitboard between(Square sq1, Square sq2) noexcept { return SQUARES_BETWEEN_BB[sq1][sq2]; }
} // namespace chess::movegen
} // namespace chess::attacks
3 changes: 3 additions & 0 deletions fwd_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ enum MoveType : uint16_t;
enum File : int8_t;
enum Rank : int8_t;
class Move;
enum class MoveGenType : uint8_t;
template <typename T, typename> class _Position;
using Bitboard=uint64_t;
using Key = uint64_t;
template <typename T, std::size_t MaxSize> class ValueList;
using Movelist = ValueList<Move, 256>;
// bonus: define the piece enums here
enum class PolyglotPiece : uint8_t;
enum class EnginePiece : uint8_t;
Expand Down
Loading