Skip to content

Commit

Permalink
Movegen speedup via magic bitboards (#640)
Browse files Browse the repository at this point in the history
* Movegen speedup via magic bitboards

* build system support for PEXT instruction

* Removed dependency from legacy rook/bishop attack tables in initialization code

* Optimization: use pointer to square attacks table instead of offset to save some instructions

* Consistently use term 'attack line' everywhere

* Add PEXT support path

* Comment improvement

* More comment improvements

* Revert accidental board_test changes

This reverts commit 7f1374d.

* More comment improvements

* Address review comments

* Initialize magic bitboards in board_test

* Cleanup of unused code

* Initialize magic bitboards in tests

* Rename bitboard operators +,* to |,&

* Added inline qualifiers
  • Loading branch information
ddobbelaere authored and mooskagh committed Jan 19, 2019
1 parent c5180c3 commit 9a6c0a9
Show file tree
Hide file tree
Showing 13 changed files with 356 additions and 124 deletions.
4 changes: 4 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,10 @@ if not get_option('popcnt')
add_project_arguments('-DNO_POPCNT', language : 'cpp')
endif

if not get_option('pext')
add_project_arguments('-DNO_PEXT', language : 'cpp')
endif

executable('lc0', 'src/main.cc',
files, include_directories: includes, dependencies: deps, install: true)

Expand Down
5 changes: 5 additions & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ option('popcnt',
value: true,
description: 'Use the popcnt instruction')

option('pext',
type: 'boolean',
value: false,
description: 'Use the pext instruction')

option('gtest',
type: 'boolean',
value: true,
Expand Down
18 changes: 11 additions & 7 deletions src/chess/bitboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ class BitBoard {
return board_ == other.board_;
}

bool operator!=(const BitBoard& other) const {
return board_ != other.board_;
}

BitIterator<BoardSquare> begin() const { return board_; }
BitIterator<BoardSquare> end() const { return 0; }

Expand All @@ -195,7 +199,7 @@ class BitBoard {
}

// Applies a mask to the bitboard (intersects).
BitBoard& operator*=(const BitBoard& a) {
BitBoard& operator&=(const BitBoard& a) {
board_ &= a.board_;
return *this;
}
Expand All @@ -206,10 +210,15 @@ class BitBoard {
}

// Returns union (bitwise OR) of two boards.
friend BitBoard operator+(const BitBoard& a, const BitBoard& b) {
friend BitBoard operator|(const BitBoard& a, const BitBoard& b) {
return {a.board_ | b.board_};
}

// Returns intersection (bitwise AND) of two boards.
friend BitBoard operator&(const BitBoard& a, const BitBoard& b) {
return {a.board_ & b.board_};
}

// Returns bitboard with one bit reset.
friend BitBoard operator-(const BitBoard& a, const BoardSquare& b) {
return {a.board_ & ~(1ULL << b.as_int())};
Expand All @@ -220,11 +229,6 @@ class BitBoard {
return {a.board_ & ~b.board_};
}

// Returns intersection (bitwise AND) of two boards.
friend BitBoard operator*(const BitBoard& a, const BitBoard& b) {
return {a.board_ & b.board_};
}

private:
std::uint64_t board_ = 0;
};
Expand Down
Loading

0 comments on commit 9a6c0a9

Please sign in to comment.