From 986b49b50379600a31e32a4eb1813749dffd8a8e Mon Sep 17 00:00:00 2001 From: rektdie Date: Tue, 14 May 2024 23:14:27 +0200 Subject: [PATCH] made some optimizations in PopBit and other parts --- Makefile | 2 +- source/bitboards.cpp | 6 ++---- source/bitboards.h | 8 ++------ source/board.cpp | 16 +++++++++------- source/board.h | 5 +++-- source/main.cpp | 2 +- source/movegen.cpp | 12 ++++++------ source/perft.cpp | 18 ++++++++---------- 8 files changed, 32 insertions(+), 37 deletions(-) diff --git a/Makefile b/Makefile index 29dab75..883e5c7 100644 --- a/Makefile +++ b/Makefile @@ -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 $@ $< diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 10c7539..58f3314 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -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); -} \ No newline at end of file +} diff --git a/source/bitboards.h b/source/bitboards.h index 8f0038b..d767a28 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -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~(); -}; \ No newline at end of file +}; diff --git a/source/board.cpp b/source/board.cpp index 87a800b..c02edc4 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -1,5 +1,4 @@ #include "board.h" -#include "movegen.h" #include void Board::Init() { @@ -16,6 +15,8 @@ void Board::Reset() { pieces = std::array(); colors = std::array(); + + moveList = std::array(); } 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; -} \ No newline at end of file +} diff --git a/source/board.h b/source/board.h index 12af5c9..f324801 100644 --- a/source/board.h +++ b/source/board.h @@ -17,7 +17,8 @@ public: Bitboard occupied; - std::vector moveList; + std::array 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); -}; \ No newline at end of file +}; diff --git a/source/main.cpp b/source/main.cpp index 2ff7512..040e453 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -16,4 +16,4 @@ int main() { Perft(board, 10); return 0; -} \ No newline at end of file +} diff --git a/source/movegen.cpp b/source/movegen.cpp index 4758cae..1c2459c 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -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); -} \ No newline at end of file +} diff --git a/source/perft.cpp b/source/perft.cpp index cd0ee55..3273ccc 100644 --- a/source/perft.cpp +++ b/source/perft.cpp @@ -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"; -} \ No newline at end of file +}