diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-19 11:48:06 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-19 11:48:06 +0200 |
| commit | 86393cf0e2344f81f59db87ec541c6a7ed55d651 (patch) | |
| tree | d3ad4cbd7f873ad5f9a187666a4c89d58841e4ee | |
| parent | 37f7feaf48fba54dc993a5120995b8ca465000bd (diff) | |
perf(move): making undo move use incremental bitboard aproah instead of generation from scrath each time
| -rw-r--r-- | src/board/board.cpp | 73 | ||||
| -rw-r--r-- | src/board/board.hpp | 1 |
2 files changed, 38 insertions, 36 deletions
diff --git a/src/board/board.cpp b/src/board/board.cpp index 4ca0667..7525fab 100644 --- a/src/board/board.cpp +++ b/src/board/board.cpp @@ -1,4 +1,5 @@ #include <cassert> +#include <chrono> #include <cstddef> #include <cstdint> #include <cstdlib> @@ -114,8 +115,8 @@ bool isRepetionDraw(uint64_t key, Game *g) { return repetions >= 3; }; -static void movePiece(uint8_t fromSquare, Piece fromPiece, uint8_t toSquare, - Piece toPiece, Game *g) { +static void movePiece(const uint8_t &fromSquare, const Piece &fromPiece, + const uint8_t &toSquare, const Piece &toPiece, Game *g) { uint64_t from = 1ULL << fromSquare; uint64_t to = 1ULL << toSquare; @@ -238,6 +239,8 @@ Undo MakeMove(uint16_t move, Game *g) { // promotion is always set and should only be aplied when reached final rank // (default: knight) + // because we use only two bits of memory for promotion we cant represent a + // none type so we always have knight set and only it last rank and its pawn if (piece.type == PAWN && IndexToPosition(toSquare).rank == (piece.color ? 7 : 0)) { piece.type = promotion; @@ -373,13 +376,34 @@ Undo MakeMove(uint16_t move, Game *g) { return undo; }; void UndoMove(Undo undo, Game *g) { - g->pieces[undo.from] = undo.movedPiece; + movePiece(undo.to, undo.movedPiece, undo.from, {.type = NONEPIECE}, g); + + // uint64_t from = 1ULL << undo.from; + uint64_t to = 1ULL << undo.to; g->pieces[undo.to] = undo.capturedPiece; + if (undo.capturedPiece.type != NONEPIECE) { + g->PieceBitboards[undo.capturedPiece.color][undo.capturedPiece.type] ^= to; + if (undo.capturedPiece.color) { + g->WhitePieceBitboard ^= to; + } else { + g->BlackPieceBitboard ^= to; + } + }; if (undo.wasEnPassantCapture) { + const uint64_t capturedSquare = + 1ULL << PositionToIndex(undo.enPassantCapturedSquare); g->pieces[PositionToIndex(undo.enPassantCapturedSquare)] = undo.enPassantCapturedPiece; + g->PieceBitboards[undo.enPassantCapturedPiece.color] + [undo.enPassantCapturedPiece.type] |= capturedSquare; + if (undo.enPassantCapturedPiece.color) { + g->WhitePieceBitboard |= capturedSquare; + + } else { + g->BlackPieceBitboard |= capturedSquare; + } } g->turn = undo.oldTurn; @@ -406,11 +430,11 @@ void UndoMove(Undo undo, Game *g) { Position rookTo = IndexToPosition(undo.from); rookTo.file = 7; - g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)]; - g->pieces[PositionToIndex(rookFrom)] = { - .color = false, - .type = NONEPIECE, - }; + movePiece(static_cast<uint8_t>(PositionToIndex(rookFrom)), + g->pieces[PositionToIndex(rookFrom)], + static_cast<uint8_t>(PositionToIndex(rookTo)), + {.type = NONEPIECE}, g); + } else { // d -> a Position rookFrom = IndexToPosition(undo.from); @@ -419,35 +443,14 @@ void UndoMove(Undo undo, Game *g) { Position rookTo = IndexToPosition(undo.from); rookTo.file = 0; - g->pieces[PositionToIndex(rookTo)] = g->pieces[PositionToIndex(rookFrom)]; - g->pieces[PositionToIndex(rookFrom)] = { - .color = false, - .type = NONEPIECE, - }; - } + movePiece(static_cast<uint8_t>(PositionToIndex(rookFrom)), + g->pieces[PositionToIndex(rookFrom)], + static_cast<uint8_t>(PositionToIndex(rookTo)), + {.type = NONEPIECE}, g); + }; } g->history.pop_back(); - UpdateHelpers(g); -}; -void UpdateHelpers(Game *g) { - g->PieceBitboard = 0; - g->WhitePieceBitboard = 0; - g->BlackPieceBitboard = 0; - std::memset(g->PieceBitboards, 0, sizeof(g->PieceBitboards)); - for (uint8_t i = 0; i < 64; i++) { - Piece piece = g->pieces[i]; - if (piece.type == NONEPIECE) { - continue; - } - g->PieceBitboards[piece.color][piece.type] |= (1ULL << i); - - if (piece.color) { - g->WhitePieceBitboard |= (1ULL << i); - } else { - g->BlackPieceBitboard |= (1ULL << i); - } - } g->PieceBitboard = g->WhitePieceBitboard | g->BlackPieceBitboard; -} +};
\ No newline at end of file diff --git a/src/board/board.hpp b/src/board/board.hpp index a2c4e1f..a64289e 100644 --- a/src/board/board.hpp +++ b/src/board/board.hpp @@ -109,5 +109,4 @@ Undo MakeMove(uint16_t move, Game *g); void UndoMove(Undo undo, Game *g); Position FindKing(const Game &g, bool color); -void UpdateHelpers(Game *g); #endif /* SRC_BOARD_H_ */ |
