Skip to content

Commit 66278aa

Browse files
authored
Modernizes movegen (#28)
1 parent 0c59751 commit 66278aa

14 files changed

Lines changed: 614 additions & 427 deletions

.clang-format

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
BasedOnStyle: LLVM
22
IndentWidth: 4
33
ContinuationIndentWidth: 4
4-
ColumnLimit: 1000
4+
ColumnLimit: 128
55
AllowAllArgumentsOnNextLine: false
66
AllowAllConstructorInitializersOnNextLine: false
77
BinPackParameters: false

.github/workflows/clang-format.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
name: clang-format check
2+
3+
uses: jidicula/clang-format-action@v4.16.0
4+
on:
5+
pull_request:
6+
branches: [ "main" ]

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ build/
99
!.gitignore
1010
CMakePresets.json
1111
CMakeSettings.json
12-
*.s
12+
*.s
13+
.vscode

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ endif()
4040
set(SOURCES
4141
position.cpp
4242
attacks.cpp "zobrist.cpp"
43-
"moves_io.cpp" "printers.cpp" "repetition.h" "fwd_decl.h")
43+
"moves_io.cpp" "printers.cpp" "movegen.cpp")
4444
add_library(chesslib STATIC ${SOURCES})
4545
target_include_directories(chesslib PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
4646
# --- Enable CTest integration ---

attacks.cpp

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -116,15 +116,17 @@ _POSSIBLY_CONSTEXPR std::array<uint64_t, 64> BishopMagics = {
116116
};
117117

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

123124
size_t offset = 0;
124125

125126
for (Square sq = SQ_A1; sq < SQ_NONE; ++sq) {
126127
Bitboard occ = 0;
127-
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)]);
128+
Bitboard edges = ((attacks::MASK_RANK[0] | attacks::MASK_RANK[7]) & ~attacks::MASK_RANK[rank_of(sq)]) |
129+
((attacks::MASK_FILE[0] | attacks::MASK_FILE[7]) & ~attacks::MASK_FILE[file_of(sq)]);
128130

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

157159
return std::pair{ table, attacks };
158160
}
159-
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x1480>> bishopData = generate_magic_table<_chess::_HyperbolaBishopAttacks, 0x1480, true>();
161+
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x1480>> bishopData =
162+
generate_magic_table<_chess::_HyperbolaBishopAttacks, 0x1480, true>();
160163
_POSSIBLY_CONSTEXPR std::array<Magic, 64> BishopTable = bishopData.first;
161164
_POSSIBLY_CONSTEXPR std::array<Bitboard, 0x1480> BishopAttacks = bishopData.second;
162165

163-
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x19000>> rookData = generate_magic_table<_chess::_HyperbolaRookAttacks, 0x19000, false>();
166+
_POSSIBLY_CONSTEXPR std::pair<std::array<Magic, 64>, std::array<Bitboard, 0x19000>> rookData =
167+
generate_magic_table<_chess::_HyperbolaRookAttacks, 0x19000, false>();
164168
_POSSIBLY_CONSTEXPR std::array<Magic, 64> RookTable = rookData.first;
165169
_POSSIBLY_CONSTEXPR std::array<Bitboard, 0x19000> RookAttacks = rookData.second;
166-
} // namespace chess::attacks
167-
168-
namespace chess::movegen {
169-
inline static _POSSIBLY_CONSTEXPR Bitboard att(PieceType pt, Square sq, Bitboard occ) { return (pt == BISHOP) ? _chess::_HyperbolaBishopAttacks(sq, occ) : _chess ::_HyperbolaRookAttacks(sq, occ); }
170-
171-
inline static _POSSIBLY_CONSTEXPR std::array<std::array<Bitboard, 64>, 64> generate_between() {
172-
std::array<std::array<Bitboard, 64>, 64> squares_between_bb{};
173-
174-
for (int sq1 = 0; sq1 < 64; ++sq1) {
175-
for (PieceType pt : { BISHOP, ROOK }) {
176-
for (int sq2 = 0; sq2 < 64; ++sq2) {
177-
if (att(pt, Square(sq1), 0) & (1ULL << sq2)) {
178-
squares_between_bb[sq1][sq2] = att(pt, Square(sq1), 1ULL << (sq2)) & att(pt, Square(sq2), 1ULL << (sq1));
179-
}
180-
squares_between_bb[sq1][sq2] |= 1ULL << (sq2);
181-
}
182-
}
183-
}
184-
185-
return squares_between_bb;
186-
}
187-
_POSSIBLY_CONSTEXPR std::array<std::array<Bitboard, 64>, 64> SQUARES_BETWEEN_BB = generate_between();
188-
} // namespace chess::movegen
170+
} // namespace chess::attacks

attacks.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,4 @@ template <PieceType pt> [[nodiscard]] constexpr Bitboard slider(Square sq, Bitbo
283283
return queen(sq, occupied);
284284
}
285285

286-
} // namespace chess::attacks
287-
288-
namespace chess::movegen {
289-
extern const std::array<std::array<Bitboard, 64>, 64> SQUARES_BETWEEN_BB;
290-
[[nodiscard]] inline Bitboard between(Square sq1, Square sq2) noexcept { return SQUARES_BETWEEN_BB[sq1][sq2]; }
291-
} // namespace chess::movegen
286+
} // namespace chess::attacks

fwd_decl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@ enum MoveType : uint16_t;
1515
enum File : int8_t;
1616
enum Rank : int8_t;
1717
class Move;
18+
enum class MoveGenType : uint8_t;
1819
template <typename T, typename> class _Position;
1920
using Bitboard=uint64_t;
2021
using Key = uint64_t;
22+
template <typename T, std::size_t MaxSize> class ValueList;
23+
using Movelist = ValueList<Move, 256>;
2124
// bonus: define the piece enums here
2225
enum class PolyglotPiece : uint8_t;
2326
enum class EnginePiece : uint8_t;

0 commit comments

Comments
 (0)