made some optimizations in PopBit and other parts
This commit is contained in:
1 parent
119a87f227
commit
986b49b503
8 files changed
+32
-37
No files matched your search
@@ -17,7 +17,7 @@ OBJS := $(addprefix $(OBJ_DIR)/, $(notdir $(SRCS:.cpp=.o)))
|
||||
|
||||
# Target executable
|
||||
Eleanor$(EXE_EXT): $(OBJS)
|
||||
g++ -o $@ $^
|
||||
g++ -o $@ $^
|
||||
# Rule to compile source files to object files
|
||||
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(SRC_DIR)/types.h | $(OBJ_DIR)
|
||||
g++ -c -o $@ $<
|
||||
|
||||
@@ -36,9 +36,7 @@ void Bitboard::SetBit(int square) {
|
||||
}
|
||||
|
||||
void Bitboard::PopBit(int square) {
|
||||
if (Bitboard(m_board).IsSet(square)) {
|
||||
m_board &= (~Bitboard::GetSquare(square)).GetBoard();
|
||||
}
|
||||
m_board &= ~(1ULL << square);
|
||||
}
|
||||
|
||||
int Bitboard::PopCount() const {
|
||||
@@ -115,4 +113,4 @@ Bitboard Bitboard::operator>>(int other) {
|
||||
|
||||
Bitboard Bitboard::operator~() {
|
||||
return ~(m_board);
|
||||
}
|
||||
}
|
||||
+2
-6
@@ -11,7 +11,7 @@ public:
|
||||
|
||||
Bitboard(const U64& board)
|
||||
: m_board(board) {}
|
||||
|
||||
|
||||
U64 GetBoard() const;
|
||||
bool IsSet(int square) const;
|
||||
void SetBit(int square);
|
||||
@@ -22,10 +22,6 @@ public:
|
||||
int getLS1BIndex() const;
|
||||
static Bitboard getOccupancy(int index, Bitboard attackMask);
|
||||
|
||||
static Bitboard GetSquare(int square) {
|
||||
return (1ULL << square);
|
||||
};
|
||||
|
||||
Bitboard operator&(const Bitboard& other) const;
|
||||
Bitboard operator|(const Bitboard& other) const;
|
||||
Bitboard operator^(const Bitboard& other) const;
|
||||
@@ -38,4 +34,4 @@ public:
|
||||
Bitboard operator<<(int other);
|
||||
Bitboard operator>>(int other);
|
||||
Bitboard operator~();
|
||||
};
|
||||
};
|
||||
+9
-7
@@ -1,5 +1,4 @@
|
||||
#include "board.h"
|
||||
#include "movegen.h"
|
||||
#include <iostream>
|
||||
|
||||
void Board::Init() {
|
||||
@@ -16,6 +15,8 @@ void Board::Reset() {
|
||||
|
||||
pieces = std::array<Bitboard, 6>();
|
||||
colors = std::array<Bitboard, 2>();
|
||||
|
||||
moveList = std::array<Move, 218>();
|
||||
}
|
||||
|
||||
void Board::SetByFen(const char* fen) {
|
||||
@@ -79,7 +80,7 @@ void Board::SetByFen(const char* fen) {
|
||||
pieceColor = White;
|
||||
break;
|
||||
case '/':
|
||||
currentSquare = currentSquare >> 8 + digits;
|
||||
currentSquare = currentSquare >> (8 + digits);
|
||||
digits = 0;
|
||||
fen++;
|
||||
continue;
|
||||
@@ -236,18 +237,19 @@ void Board::PrintBoard() {
|
||||
}
|
||||
|
||||
void Board::AddMove(Move move) {
|
||||
moveList.push_back(move);
|
||||
moveList[currentMoveIndex] = move;
|
||||
currentMoveIndex++;
|
||||
}
|
||||
|
||||
void Board::ResetMoves() {
|
||||
moveList.clear();
|
||||
currentMoveIndex = 0;
|
||||
}
|
||||
|
||||
void Board::ListMoves() {
|
||||
int count = 1;
|
||||
for (Move move : moveList) {
|
||||
for (int i = 0; i < currentMoveIndex; i++) {
|
||||
std::cout << count << ". ";
|
||||
move.PrintMove();
|
||||
moveList[i].PrintMove();
|
||||
count++;
|
||||
}
|
||||
}
|
||||
@@ -410,4 +412,4 @@ void Board::MakeMove(Move move) {
|
||||
if (halfMoves & 2 == 0) fullMoves++;
|
||||
sideToMove = !attackerColor;
|
||||
enPassantTarget = newEpTarget;
|
||||
}
|
||||
}
|
||||
+3
-2
@@ -17,7 +17,8 @@ public:
|
||||
|
||||
Bitboard occupied;
|
||||
|
||||
std::vector<Move> moveList;
|
||||
std::array<Move, 218> moveList;
|
||||
int currentMoveIndex = 0;
|
||||
|
||||
bool sideToMove = White;
|
||||
int halfMoves = 0;
|
||||
@@ -46,4 +47,4 @@ public:
|
||||
|
||||
void Promote(int square, int pieceType, int color, bool isCapture);
|
||||
void MakeMove(Move move);
|
||||
};
|
||||
};
|
||||
+1
-1
@@ -16,4 +16,4 @@ int main() {
|
||||
Perft(board, 10);
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+6
-6
@@ -18,7 +18,7 @@ static Bitboard notHGfile = notHfile & ~files[G];
|
||||
|
||||
Bitboard maskPawnAttacks(int side, int square) {
|
||||
Bitboard attacks;
|
||||
Bitboard attacker = Bitboard::GetSquare(square);
|
||||
Bitboard attacker = (1ULL << square);
|
||||
|
||||
// White
|
||||
if (!side) {
|
||||
@@ -35,7 +35,7 @@ Bitboard maskPawnAttacks(int side, int square) {
|
||||
|
||||
Bitboard maskKnightAttacks(int square) {
|
||||
Bitboard attacks;
|
||||
Bitboard attacker = Bitboard::GetSquare(square);
|
||||
Bitboard attacker = (1ULL << square);
|
||||
|
||||
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
||||
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
||||
@@ -52,7 +52,7 @@ Bitboard maskKnightAttacks(int square) {
|
||||
|
||||
Bitboard maskKingAttacks(int square) {
|
||||
Bitboard attacks;
|
||||
Bitboard attacker = Bitboard::GetSquare(square);
|
||||
Bitboard attacker = (1ULL << square);
|
||||
|
||||
attacks |= (attacker << north);
|
||||
attacks |= (attacker << south);
|
||||
@@ -369,12 +369,12 @@ static void GenKingMoves(Board &board, bool color) {
|
||||
|
||||
void GenerateMoves(Board &board) {
|
||||
bool side = board.sideToMove;
|
||||
board.moveList.clear();
|
||||
board.ResetMoves();
|
||||
|
||||
GenPawnMoves(board, side);
|
||||
GenKnightMoves(board, side);
|
||||
GenKnightMoves(board, side);
|
||||
GenKingMoves(board, side);
|
||||
GenBishopMoves(board, side);
|
||||
GenRookMoves(board, side);
|
||||
GenQueenMoves(board, side);
|
||||
}
|
||||
}
|
||||
+8
-10
@@ -4,16 +4,15 @@
|
||||
U64 HelperPerft(Board board, int depth) {
|
||||
GenerateMoves(board);
|
||||
|
||||
if (depth == 1) return board.moveList.size();
|
||||
if (depth == 1) return board.currentMoveIndex;
|
||||
if (depth == 0) return 1ULL;
|
||||
|
||||
U64 nodes = 0;
|
||||
|
||||
for (int i = 0; i < board.moveList.size(); i++) {
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
Board copy = board;
|
||||
board.MakeMove(board.moveList[i]);
|
||||
nodes += HelperPerft(board, depth - 1);
|
||||
board = copy;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
nodes += HelperPerft(copy, depth - 1);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
@@ -24,11 +23,10 @@ void Perft(Board &board, int depth) {
|
||||
U64 totalNodes = 0;
|
||||
|
||||
auto start = std::chrono::steady_clock::now();
|
||||
for (int i = 0; i < board.moveList.size(); i++) {
|
||||
for (int i = 0; i < board.currentMoveIndex; i++) {
|
||||
Board copy = board;
|
||||
board.MakeMove(board.moveList[i]);
|
||||
U64 nodeCount = HelperPerft(board, depth - 1);
|
||||
board = copy;
|
||||
copy.MakeMove(board.moveList[i]);
|
||||
U64 nodeCount = HelperPerft(copy, depth - 1);
|
||||
totalNodes += nodeCount;
|
||||
std::cout << squareCoords[board.moveList[i].MoveFrom()]
|
||||
<< squareCoords[board.moveList[i].MoveTo()];
|
||||
@@ -56,4 +54,4 @@ void Perft(Board &board, int depth) {
|
||||
std::cout << "Total nodes: " << totalNodes << '\n';
|
||||
std::cout << "Time took: " << elapsed_seconds << "s (";
|
||||
std::cout << int(totalNodes / elapsed_seconds) << " nodes/sec)\n";
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user