Skip to content

Commit f8fff0a

Browse files
committed
Refactor board and add missing documentation
1 parent 96bc478 commit f8fff0a

File tree

19 files changed

+555
-572
lines changed

19 files changed

+555
-572
lines changed

CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ set(TOPPLE_VERSION 0.8.0)
77
# Source files for different targets
88
set(SOURCE_FILES
99
board.h board.cpp
10-
endgame.h endgame.cpp
1110
bb.h bb.cpp types.h
1211
hash.h hash.cpp
1312
movegen.h movegen.cpp
@@ -51,7 +50,7 @@ target_link_libraries(ToppleTexelTune Threads::Threads)
5150

5251
# Set -march for the Topple target
5352
target_compile_options(ToppleTest PUBLIC -march=native -O3)
54-
target_compile_options(Topple PUBLIC -march=native -O3 -DNDEBUG) # NDEBUG to disable asserts
53+
target_compile_options(Topple PUBLIC -march=ivybridge -O3 -DNDEBUG) # NDEBUG to disable asserts
5554
target_compile_options(ToppleTune PUBLIC -DTOPPLE_TUNE -O3 -march=native -DNDEBUG)
5655
target_compile_options(ToppleTexelTune PUBLIC -DTEXEL_TUNE -O3 -march=native -DNDEBUG)
5756

bb.cpp

Lines changed: 18 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66

77
#include "bb.h"
88

9-
/**
10-
* =====================================================================================================================
11-
* MAGIC MOVE BIT BOARD GENERATION
12-
* =====================================================================================================================
9+
/*
10+
* Sliding move generation using 'magic' bitboards.
11+
*
12+
* This code uses kogge-stone shifts in namespace bb_shifts to initialise a lookup table.
13+
* Lookup code is in namespace bb_sliders
1314
*/
1415
namespace bb_sliders {
1516
constexpr U64 outer_files = 0x8181818181818181ull;
@@ -161,6 +162,7 @@ namespace bb_sliders {
161162

162163
U64 attacks[88772];
163164

165+
// Use kogge-stone occluded shifts to (slowly) generate bishop moves for the lookup table
164166
U64 compute_bishop_moves(int sq, U64 occupancy) {
165167
U64 open = ~occupancy;
166168
return bb_shifts::shift<D_NE>(bb_shifts::fill_occluded<D_NE>(single_bit(sq), open))
@@ -169,6 +171,7 @@ namespace bb_sliders {
169171
| bb_shifts::shift<D_SW>(bb_shifts::fill_occluded<D_SW>(single_bit(sq), open));
170172
}
171173

174+
// Use kogge-stone occluded shifts to (slowly) generate rook moves for the lookup table
172175
U64 compute_rook_moves(int sq, U64 occupancy) {
173176
U64 open = ~occupancy;
174177
return bb_shifts::shift<D_N>(bb_shifts::fill_occluded<D_N>(single_bit(sq), open))
@@ -177,17 +180,21 @@ namespace bb_sliders {
177180
| bb_shifts::shift<D_W>(bb_shifts::fill_occluded<D_W>(single_bit(sq), open));
178181
}
179182

183+
// Squares that are relevant when looking up bishop moves
180184
U64 compute_bishop_mask(int sq) {
181185
return compute_bishop_moves(sq, 0) & ~edge;
182186
}
183187

188+
// Squares that are relevant when looking up rook moves
184189
U64 compute_rook_mask(int sq) {
190+
// Can't use (compute_rook_moves(sq, 0) & ~edge) because rooks can be on the edge of the board
185191
return ((bb_shifts::shift<D_E>(bb_shifts::fill_occluded<D_E>(single_bit(sq), ~0ull))
186192
| bb_shifts::shift<D_W>(bb_shifts::fill_occluded<D_W>(single_bit(sq), ~0ull))) & ~outer_files)
187193
| ((bb_shifts::shift<D_N>(bb_shifts::fill_occluded<D_N>(single_bit(sq), ~0ull))
188194
| bb_shifts::shift<D_S>(bb_shifts::fill_occluded<D_S>(single_bit(sq), ~0ull))) & ~outer_ranks);
189195
}
190196

197+
// Initialisation code
191198
void init_sliders() {
192199
for (int sq = 0; sq < 64; sq++) {
193200
sq_entry_t entry = {};
@@ -196,7 +203,7 @@ namespace bb_sliders {
196203
entry = {compute_bishop_mask(sq), bishop_magics[sq].factor, attacks + bishop_magics[sq].position};
197204
bits = pop_count(entry.mask);
198205
for (U64 dense_occ = 0; dense_occ < (1u << bits); dense_occ++) {
199-
U64 occ = bb_intrin::pdep(dense_occ, entry.mask);
206+
U64 occ = bb_intrin::pdep(dense_occ, entry.mask); // pdep an incrementing bitfield for all relevant bitboards
200207
entry.base[(occ * entry.magic) >> (64u - 9u)] = compute_bishop_moves(sq, occ);
201208
}
202209
b_table[sq] = entry;
@@ -212,10 +219,8 @@ namespace bb_sliders {
212219
}
213220
}
214221

215-
/**
216-
* =====================================================================================================================
217-
* UTLITY BITBOARDS
218-
* =====================================================================================================================
222+
/*
223+
* Initialisation of various utility bitboards
219224
*/
220225
namespace bb_util {
221226
U64 between[64][64];
@@ -282,10 +287,12 @@ namespace bb_normal_moves {
282287
U64 pawn_moves_x2[2][64];
283288
U64 pawn_caps[2][64];
284289

290+
// We use this method (and signed integers) to not generate moves off the edge of the board (or which wrap around)
285291
inline bool valid_square(const int &file, const int &rank) {
286292
return file >= 0 && file < 8 && rank >= 0 && rank < 8;
287293
}
288294

295+
// Check if move is valid and add to lookup table if it is
289296
inline void update_array(U64 *arr, const uint8_t &file, const uint8_t &rank,
290297
int file_offset, int rank_offset) {
291298
if (valid_square(file + file_offset, rank + rank_offset)) {
@@ -320,6 +327,7 @@ namespace bb_normal_moves {
320327
update_array(pawn_moves_x1[WHITE], file_from, rank_from, 0, 1);
321328
update_array(pawn_moves_x1[BLACK], file_from, rank_from, 0, -1);
322329

330+
// Pawn double moves from 2nd of 7th rank (our ranks are 0 indexed)
323331
if (rank_from == 1) {
324332
update_array(pawn_moves_x2[WHITE], file_from, rank_from, 0, 2);
325333
} else if (rank_from == 6) {
@@ -336,11 +344,6 @@ namespace bb_normal_moves {
336344
}
337345
}
338346

339-
/**
340-
* =====================================================================================================================
341-
* HEADER IMPLEMENTATION
342-
* =====================================================================================================================
343-
*/
344347
void init_tables() {
345348
bb_sliders::init_sliders();
346349
bb_util::init_util();
@@ -356,137 +359,5 @@ uint8_t to_sq(char file, char rank) {
356359
}
357360

358361
std::string from_sq(uint8_t sq) {
359-
switch (Square(sq)) {
360-
case A1:
361-
return std::string("a1");
362-
case B1:
363-
return std::string("b1");
364-
case C1:
365-
return std::string("c1");
366-
case D1:
367-
return std::string("d1");
368-
case E1:
369-
return std::string("e1");
370-
case F1:
371-
return std::string("f1");
372-
case G1:
373-
return std::string("g1");
374-
case H1:
375-
return std::string("h1");
376-
case A2:
377-
return std::string("a2");
378-
case B2:
379-
return std::string("b2");
380-
case C2:
381-
return std::string("c2");
382-
case D2:
383-
return std::string("d2");
384-
case E2:
385-
return std::string("e2");
386-
case F2:
387-
return std::string("f2");
388-
case G2:
389-
return std::string("g2");
390-
case H2:
391-
return std::string("h2");
392-
case A3:
393-
return std::string("a3");
394-
case B3:
395-
return std::string("b3");
396-
case C3:
397-
return std::string("c3");
398-
case D3:
399-
return std::string("d3");
400-
case E3:
401-
return std::string("e3");
402-
case F3:
403-
return std::string("f3");
404-
case G3:
405-
return std::string("g3");
406-
case H3:
407-
return std::string("h3");
408-
case A4:
409-
return std::string("a4");
410-
case B4:
411-
return std::string("b4");
412-
case C4:
413-
return std::string("c4");
414-
case D4:
415-
return std::string("d4");
416-
case E4:
417-
return std::string("e4");
418-
case F4:
419-
return std::string("f4");
420-
case G4:
421-
return std::string("g4");
422-
case H4:
423-
return std::string("h4");
424-
case A5:
425-
return std::string("a5");
426-
case B5:
427-
return std::string("b5");
428-
case C5:
429-
return std::string("c5");
430-
case D5:
431-
return std::string("d5");
432-
case E5:
433-
return std::string("e5");
434-
case F5:
435-
return std::string("f5");
436-
case G5:
437-
return std::string("g5");
438-
case H5:
439-
return std::string("h5");
440-
case A6:
441-
return std::string("a6");
442-
case B6:
443-
return std::string("b6");
444-
case C6:
445-
return std::string("c6");
446-
case D6:
447-
return std::string("d6");
448-
case E6:
449-
return std::string("e6");
450-
case F6:
451-
return std::string("f6");
452-
case G6:
453-
return std::string("g6");
454-
case H6:
455-
return std::string("h6");
456-
case A7:
457-
return std::string("a7");
458-
case B7:
459-
return std::string("b7");
460-
case C7:
461-
return std::string("c7");
462-
case D7:
463-
return std::string("d7");
464-
case E7:
465-
return std::string("e7");
466-
case F7:
467-
return std::string("f7");
468-
case G7:
469-
return std::string("g7");
470-
case H7:
471-
return std::string("h7");
472-
case A8:
473-
return std::string("a8");
474-
case B8:
475-
return std::string("b8");
476-
case C8:
477-
return std::string("c8");
478-
case D8:
479-
return std::string("d8");
480-
case E8:
481-
return std::string("e8");
482-
case F8:
483-
return std::string("f8");
484-
case G8:
485-
return std::string("g8");
486-
case H8:
487-
return std::string("h8");
488-
}
489-
490-
return std::string(); // Can't happen
362+
return std::string{(char) (file_index(sq) + 'a'), (char) (rank_index(sq) + '1')};
491363
}
492-

0 commit comments

Comments
 (0)