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
30 changes: 27 additions & 3 deletions .github/workflows/clang-format.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,30 @@
name: clang-format check
name: clang-format

uses: jidicula/clang-format-action@v4.16.0
on:
pull_request:
branches: [ "main" ]
branches: [ "main" ]

jobs:
format:
runs-on: ubuntu-latest
permissions:
contents: write # required to push back into PR

steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Run clang-format
run: |
clang-format -i $(git ls-files '*.cpp' '*.hpp' '*.h' '*.c')

- name: Commit and push changes
run: |
if ! git diff --quiet; then
git config user.email "actions@github.com"
git.config user.name "GitHub Actions"
git add .
git commit -m "Apply clang-format"
git push
fi
4 changes: 2 additions & 2 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ jobs:
shell: bash
run: |
if [[ "${{ matrix.os }}" == "windows-latest" ]]; then
ctest --build-config ${{ matrix.build_type }} --verbose
ctest --build-config ${{ matrix.build_type }} --verbose -j 4
else
ctest --verbose
ctest --verbose -j 4
fi
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ FetchContent_MakeAvailable(doctest)
add_executable(test_chess
tests.cpp
)
add_executable(NonImportantTests
non_core_tests.cpp
)
target_link_libraries(test_chess PRIVATE chesslib)
target_include_directories(test_chess PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${doctest_SOURCE_DIR})
add_test(NAME tests COMMAND test_chess)
target_link_libraries(NonImportantTests PRIVATE chesslib)
target_include_directories(NonImportantTests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR} ${doctest_SOURCE_DIR})
add_test(NAME test_core COMMAND test_chess)
add_test(NAME api_tests COMMAND NonImportantTests)
28 changes: 27 additions & 1 deletion attacks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,30 @@ _POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x1900
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::attacks
namespace chess::movegen {
inline static Bitboard att(PieceType pt, Square sq, Bitboard occ) {
return (pt == BISHOP) ? chess::_chess::_HyperbolaBishopAttacks(sq, occ) : chess::_chess::_HyperbolaRookAttacks(sq, occ);
}

inline static std::array<std::array<Bitboard, 64>, 64> generate_between() {
assert(att(BISHOP, SQ_A1, 0) == 9241421688590303744ULL &&
"Well... this is a debug message, but this is a landmine that the magic bitboards are NOT initialized before this, "
"wontwork\n");
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;
}
std::array<std::array<Bitboard, 64>, 64> SQUARES_BETWEEN_BB = generate_between();
} // namespace chess::movegen
11 changes: 8 additions & 3 deletions attacks.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ extern const std::array<Bitboard, 0x1480> BishopAttacks;
* @return
*/
template <Direction direction> [[nodiscard]] static constexpr Bitboard shift(const Bitboard b) {
ASSUME(direction == NORTH || direction == EAST || direction == SOUTH || direction == WEST || direction == NORTH_EAST || direction == SOUTH_EAST || direction == SOUTH_WEST || direction == NORTH_WEST);
ASSUME(direction == NORTH || direction == EAST || direction == SOUTH || direction == WEST || direction == NORTH_EAST ||
direction == SOUTH_EAST || direction == SOUTH_WEST || direction == NORTH_WEST);
switch (direction) {
case Direction::NORTH:
return b << 8;
Expand Down Expand Up @@ -240,15 +241,19 @@ template <Color c> [[nodiscard]] constexpr Bitboard pawn(const Bitboard pawns) {
* @param occupied
* @return
*/
[[nodiscard]] constexpr Bitboard bishop(Square sq, Bitboard occupied) { return BishopAttacks[BishopTable[(int)sq].index + BishopTable[(int)sq](occupied)]; }
[[nodiscard]] constexpr Bitboard bishop(Square sq, Bitboard occupied) {
return BishopAttacks[BishopTable[(int)sq].index + BishopTable[(int)sq](occupied)];
}

/**
* @brief Returns the rook attacks for a given square
* @param sq
* @param occupied
* @return
*/
[[nodiscard]] constexpr Bitboard rook(Square sq, Bitboard occupied) { return RookAttacks[RookTable[(int)sq].index + RookTable[(int)sq](occupied)]; }
[[nodiscard]] constexpr Bitboard rook(Square sq, Bitboard occupied) {
return RookAttacks[RookTable[(int)sq].index + RookTable[(int)sq](occupied)];
}

/**
* @brief Returns the queen attacks for a given square
Expand Down
4 changes: 2 additions & 2 deletions fwd_decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ enum MoveType : uint16_t;
enum File : int8_t;
enum Rank : int8_t;
class Move;
enum class MoveGenType : uint8_t;
enum class MoveGenType : uint16_t;
template <typename T, typename> class _Position;
using Bitboard=uint64_t;
using Bitboard = uint64_t;
using Key = uint64_t;
template <typename T, std::size_t MaxSize> class ValueList;
using Movelist = ValueList<Move, 256>;
Expand Down
55 changes: 23 additions & 32 deletions movegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Move *splat_moves(Move *moveList, Square from, Bitboard to_bb) {
return moveList;
}

template <int offset> Move *splat_pawn_moves(Move *moveList, Bitboard to_bb) {
template <Direction offset> Move *splat_pawn_moves(Move *moveList, Bitboard to_bb) {
const auto *table = reinterpret_cast<const __m512i *>(SPLAT_PAWN_TABLE<offset>.data.data());
moveList = write_moves(moveList, static_cast<uint32_t>(to_bb >> 0), _mm512_load_si512(table + 0));
moveList = write_moves(moveList, static_cast<uint32_t>(to_bb >> 32), _mm512_load_si512(table + 1));
Expand Down Expand Up @@ -435,37 +435,28 @@ template Move *chess::_chess::splat_pawn_moves<SOUTH_WEST>(Move *, Bitboard);
Movelist &, \
Bitboard, \
Bitboard, \
Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::WHITE, true>(const _Position<PieceC, void>&, Movelist&, Bitboard, Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::BLACK, true>(const _Position<PieceC, void>&, Movelist&, Bitboard, Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::WHITE, false>(const _Position<PieceC, void>&, Movelist&, Bitboard, Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::BLACK, false>(const _Position<PieceC, void>&, Movelist&, Bitboard, Bitboard); \
template void chess::movegen::genKingMoves<PieceC, Color::WHITE, true>(const _Position<PieceC, void>&, Movelist&); \
template void chess::movegen::genKingMoves<PieceC, Color::BLACK, true>(const _Position<PieceC, void>&, Movelist&); \
template void chess::movegen::genKingMoves<PieceC, Color::WHITE, false>(const _Position<PieceC, void>&, Movelist&); \
template void chess::movegen::genKingMoves<PieceC, Color::BLACK, false>(const _Position<PieceC, void>&, Movelist&);
Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::WHITE, true>(const _Position<PieceC, void> &, \
Movelist &, \
Bitboard, \
Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::BLACK, true>(const _Position<PieceC, void> &, \
Movelist &, \
Bitboard, \
Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::WHITE, false>(const _Position<PieceC, void> &, \
Movelist &, \
Bitboard, \
Bitboard); \
template void chess::movegen::genKnightMoves<PieceC, Color::BLACK, false>(const _Position<PieceC, void> &, \
Movelist &, \
Bitboard, \
Bitboard); \
template void chess::movegen::genKingMoves<PieceC, Color::WHITE, true>(const _Position<PieceC, void> &, Movelist &); \
template void chess::movegen::genKingMoves<PieceC, Color::BLACK, true>(const _Position<PieceC, void> &, Movelist &); \
template void chess::movegen::genKingMoves<PieceC, Color::WHITE, false>(const _Position<PieceC, void> &, Movelist &); \
template void chess::movegen::genKingMoves<PieceC, Color::BLACK, false>(const _Position<PieceC, void> &, Movelist &);
INSTANTIATE(EnginePiece)
INSTANTIATE(PolyglotPiece)
INSTANTIATE(ContiguousMappingPiece)
inline static Bitboard att(PieceType pt, Square sq, Bitboard occ) {
return (pt == BISHOP) ? attacks::bishop(sq, occ) : attacks::rook(sq, occ);
}

inline static 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;
}
std::array<std::array<Bitboard, 64>, 64> movegen::SQUARES_BETWEEN_BB = generate_between();
} // namespace chess
} // namespace chess
29 changes: 16 additions & 13 deletions movegen.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
#pragma once
#include "fwd_decl.h"
#include <array>
namespace chess::_chess{
template <Direction offset> extern Move *splat_pawn_moves(Move *moveList, Bitboard to_bb);
extern Move *splat_moves(Move *moveList, Square from, Bitboard to_bb);
}
namespace chess::_chess {
template <Direction offset> extern Move *splat_pawn_moves(Move *moveList, Bitboard to_bb);
extern Move *splat_moves(Move *moveList, Square from, Bitboard to_bb);
} // namespace chess::_chess
namespace chess::movegen {

template <typename T, Color c> void genEP(const _Position<T, void>&, Movelist&);
template <typename T, Color c> void genPawnDoubleMoves(const _Position<T, void>&, Movelist &, Bitboard, Bitboard);
template <typename T, Color c, bool capturesOnly = false> void genPawnSingleMoves(const _Position<T, void>&, Movelist &, Bitboard, Bitboard, Bitboard);
template <typename T, Color c, bool capturesOnly = false> void genKnightMoves(const _Position<T, void>&, Movelist &, Bitboard, Bitboard);
template <typename T, Color c, bool capturesOnly = false> void genKingMoves(const _Position<T, void>&, Movelist &);
template <typename T, Color c, PieceType pt, bool capturesOnly = false> void genSlidingMoves(const _Position<T, void>&, Movelist &, Bitboard, Bitboard, Bitboard);
extern 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]; }

template <typename T, Color c> void genEP(const _Position<T, void> &, Movelist &);
template <typename T, Color c> void genPawnDoubleMoves(const _Position<T, void> &, Movelist &, Bitboard, Bitboard);
template <typename T, Color c, bool capturesOnly = false>
void genPawnSingleMoves(const _Position<T, void> &, Movelist &, Bitboard, Bitboard, Bitboard);
template <typename T, Color c, bool capturesOnly = false>
void genKnightMoves(const _Position<T, void> &, Movelist &, Bitboard, Bitboard);
template <typename T, Color c, bool capturesOnly = false> void genKingMoves(const _Position<T, void> &, Movelist &);
template <typename T, Color c, PieceType pt, bool capturesOnly = false>
void genSlidingMoves(const _Position<T, void> &, Movelist &, Bitboard, Bitboard, Bitboard);
extern 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
Loading