From 4d8a7f6a2e1256cd7574560d9f1dc6ff6c380d94 Mon Sep 17 00:00:00 2001 From: rektdie Date: Wed, 15 May 2024 00:54:46 +0200 Subject: [PATCH] added operator overloading to Bitboard class --- source/bitboards.cpp | 37 ++++++++++++++++++----- source/bitboards.h | 11 ++++++- source/board.cpp | 2 +- source/evaluate.cpp | 2 +- source/movegen.cpp | 70 ++++++++++++++++++++++---------------------- 5 files changed, 77 insertions(+), 45 deletions(-) diff --git a/source/bitboards.cpp b/source/bitboards.cpp index c61d55a..e3a24ec 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -23,20 +23,16 @@ void Bitboard::PrintBoard() const { std::cout << " Board: 0x" << std::hex << m_board << '\n'; } -U64 Bitboard::GetBoard() const { - return m_board; -} - bool Bitboard::IsSet(int square) const { - return GetSquare(square).GetBoard() & m_board; + return GetSquare(square) & m_board; } void Bitboard::SetBit(int square) { - m_board |= GetSquare(square).GetBoard(); + m_board |= GetSquare(square); } void Bitboard::PopBit(int square) { - m_board &= ~GetSquare(square).GetBoard(); + m_board &= ~GetSquare(square); } int Bitboard::PopCount() const { @@ -84,6 +80,19 @@ Bitboard Bitboard::operator*(const Bitboard& other) const { return m_board * other.m_board; } +Bitboard Bitboard::operator&(const uint64_t& other) const { + return m_board & other; +} +Bitboard Bitboard::operator|(const uint64_t& other) const { + return m_board | other; +} +Bitboard Bitboard::operator^(const uint64_t& other) const { + return m_board ^ other; +} +Bitboard Bitboard::operator*(const uint64_t& other) const { + return m_board * other; +} + Bitboard Bitboard::operator&=(const Bitboard& other) { return m_board &= other.m_board; } @@ -94,6 +103,16 @@ Bitboard Bitboard::operator^=(const Bitboard& other) { return m_board ^= other.m_board; } +Bitboard Bitboard::operator&=(const uint64_t& other) { + return m_board &= other; +} +Bitboard Bitboard::operator|=(const uint64_t& other) { + return m_board |= other; +} +Bitboard Bitboard::operator^=(const uint64_t& other) { + return m_board ^= other; +} + Bitboard Bitboard::operator<<(int other) { if (other > 0) { return m_board << other; @@ -114,3 +133,7 @@ Bitboard Bitboard::operator>>(int other) { Bitboard Bitboard::operator~() { return ~(m_board); } + +Bitboard::operator uint64_t() { + return m_board; +} diff --git a/source/bitboards.h b/source/bitboards.h index 1e64ae5..718aa00 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -12,7 +12,6 @@ public: Bitboard(const U64& board) : m_board(board) {} - U64 GetBoard() const; bool IsSet(int square) const; void SetBit(int square); void PopBit(int square); @@ -30,12 +29,22 @@ public: Bitboard operator|(const Bitboard& other) const; Bitboard operator^(const Bitboard& other) const; Bitboard operator*(const Bitboard& other) const; + + Bitboard operator&(const uint64_t& other) const; + Bitboard operator|(const uint64_t& other) const; + Bitboard operator^(const uint64_t& other) const; + Bitboard operator*(const uint64_t& other) const; Bitboard operator&=(const Bitboard& other); Bitboard operator|=(const Bitboard& other); Bitboard operator^=(const Bitboard& other); + Bitboard operator&=(const uint64_t& other); + Bitboard operator|=(const uint64_t& other); + Bitboard operator^=(const uint64_t& other); + Bitboard operator<<(int other); Bitboard operator>>(int other); Bitboard operator~(); + operator uint64_t(); }; diff --git a/source/board.cpp b/source/board.cpp index c02edc4..4d74aa4 100644 --- a/source/board.cpp +++ b/source/board.cpp @@ -277,7 +277,7 @@ U64 Board::GetAttackMaps(bool side) { bool Board::InCheck(bool side) { Bitboard myKingSquare = colors[side] & pieces[King]; - return GetAttackMaps(!side) & myKingSquare.GetBoard(); + return GetAttackMaps(!side) & myKingSquare; } void Board::SetPiece(int piece, int square, bool color) { diff --git a/source/evaluate.cpp b/source/evaluate.cpp index a2f63e0..a939849 100644 --- a/source/evaluate.cpp +++ b/source/evaluate.cpp @@ -13,7 +13,7 @@ static int GetPositionalScore(Board &board, int type, int color) { Bitboard pieces = board.pieces[type] & board.colors[color]; - while (pieces.GetBoard()) { + while (pieces) { int square = pieces.getLS1BIndex(); // White diff --git a/source/movegen.cpp b/source/movegen.cpp index ff2cb8b..ed29c9c 100644 --- a/source/movegen.cpp +++ b/source/movegen.cpp @@ -22,13 +22,13 @@ Bitboard maskPawnAttacks(int side, int square) { // White if (!side) { - if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa); - if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe); + if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa); + if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe); } // Black else { - if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa); - if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe); + if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa); + if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe); } return attacks; } @@ -37,15 +37,15 @@ Bitboard maskKnightAttacks(int square) { Bitboard attacks; Bitboard attacker = Bitboard::GetSquare(square); - if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north); - if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north); - if (((attacker << noWe + west) & notHGfile).GetBoard()) attacks |= (attacker << noWe + west); - if (((attacker << noEa + east) & notABfile).GetBoard()) attacks |= (attacker << noEa + east); + if (((attacker << noEa + north) & notAfile)) attacks |= (attacker << noEa + north); + if (((attacker << noWe + north) & notHfile)) attacks |= (attacker << noWe + north); + if (((attacker << noWe + west) & notHGfile)) attacks |= (attacker << noWe + west); + if (((attacker << noEa + east) & notABfile)) attacks |= (attacker << noEa + east); - if (((attacker << soWe + south) & notHfile).GetBoard()) attacks |= (attacker << soWe + south); - if (((attacker << soEa + south) & notAfile).GetBoard()) attacks |= (attacker << soEa + south); - if (((attacker << soEa + east) & notABfile).GetBoard()) attacks |= (attacker << soEa + east); - if (((attacker << soWe + west) & notHGfile).GetBoard()) attacks |= (attacker << soWe + west); + if (((attacker << soWe + south) & notHfile)) attacks |= (attacker << soWe + south); + if (((attacker << soEa + south) & notAfile)) attacks |= (attacker << soEa + south); + if (((attacker << soEa + east) & notABfile)) attacks |= (attacker << soEa + east); + if (((attacker << soWe + west) & notHGfile)) attacks |= (attacker << soWe + west); return attacks; } @@ -57,12 +57,12 @@ Bitboard maskKingAttacks(int square) { attacks |= (attacker << north); attacks |= (attacker << south); - if (((attacker << west) & notHfile).GetBoard()) attacks |= (attacker << west); - if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe); - if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa); - if (((attacker << east) & notAfile).GetBoard()) attacks |= (attacker << east); - if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa); - if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe); + if (((attacker << west) & notHfile)) attacks |= (attacker << west); + if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe); + if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa); + if (((attacker << east) & notAfile)) attacks |= (attacker << east); + if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa); + if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe); return attacks; } @@ -140,25 +140,25 @@ Bitboard bishopAttacksOnTheFly(int square, Bitboard block) { for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // North East for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } //South West for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } //South East for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) { currentSquare = (1ULL << (r * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } return attacks; @@ -179,25 +179,25 @@ Bitboard rookAttacksOnTheFly(int square, Bitboard block) { for (int r = tr + 1; r <= 7; r++) { currentSquare = (1ULL << (r * 8 + tf)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // South for (int r = tr - 1; r >= 0; r--) { currentSquare = (1ULL << (r * 8 + tf)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // East for (int f = tf + 1; f <= 7; f++) { currentSquare = (1ULL << (tr * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } // West for (int f = tf - 1; f >= 0; f--) { currentSquare = (1ULL << (tr * 8 + f)); attacks |= currentSquare; - if ((currentSquare & block).GetBoard()) break; + if ((currentSquare & block)) break; } return attacks; @@ -221,7 +221,7 @@ void initSliderAttacks() { for (int index = 0; index < 512; index++) { Bitboard occupancy = Bitboard::getOccupancy(index, bishopMasks[square]); - int magicIndex = (occupancy.GetBoard() * bishopMagicNumbers[square]) + int magicIndex = (occupancy * bishopMagicNumbers[square]) >> (64 - bishopRelevantBits[square]); bishopAttacks[square][magicIndex] = bishopAttacksOnTheFly(square, occupancy); @@ -230,7 +230,7 @@ void initSliderAttacks() { for (int index = 0; index < 4096; index++) { Bitboard occupancy = Bitboard::getOccupancy(index, rookMasks[square]); - int magicIndex = (occupancy.GetBoard() * rookMagicNumbers[square]) + int magicIndex = (occupancy * rookMagicNumbers[square]) >> (64 - rookRelevantBits[square]); rookAttacks[square][magicIndex] = rookAttacksOnTheFly(square, occupancy); @@ -239,7 +239,7 @@ void initSliderAttacks() { } Bitboard getBishopAttack(int square, U64 occupancy) { - occupancy &= bishopMasks[square].GetBoard(); + occupancy &= bishopMasks[square]; occupancy *= bishopMagicNumbers[square]; occupancy >>= (64 - bishopRelevantBits[square]); @@ -247,7 +247,7 @@ Bitboard getBishopAttack(int square, U64 occupancy) { } Bitboard getRookAttack(int square, U64 occupancy) { - occupancy &= rookMasks[square].GetBoard(); + occupancy &= rookMasks[square]; occupancy *= rookMagicNumbers[square]; occupancy >>= (64 - rookRelevantBits[square]); @@ -258,11 +258,11 @@ Bitboard getQueenAttack(int square, U64 occupancy) { U64 bishopOccupancy = occupancy; U64 rookOccupancy = occupancy; - bishopOccupancy &= bishopMasks[square].GetBoard(); + bishopOccupancy &= bishopMasks[square]; bishopOccupancy *= bishopMagicNumbers[square]; bishopOccupancy >>= (64 - bishopRelevantBits[square]); - rookOccupancy &= rookMasks[square].GetBoard(); + rookOccupancy &= rookMasks[square]; rookOccupancy *= rookMagicNumbers[square]; rookOccupancy >>= (64 - rookRelevantBits[square]); @@ -324,14 +324,14 @@ static void GenPawnMoves(Board &board, bool color) { static void GenKnightMoves(Board &board, bool color) { Bitboard knights = board.colors[color] & board.pieces[Knight]; - while (knights.GetBoard()) { + while (knights) { int square = knights.getLS1BIndex(); Bitboard moves = knightAttacks[square] & ~board.colors[color]; Bitboard captures = moves & board.colors[!color]; Bitboard quietMoves = moves & ~board.colors[!color]; - while (captures.GetBoard()) { + while (captures) { int targetSquare = captures.getLS1BIndex(); board.AddMove(Move(square, targetSquare, capture)); @@ -339,7 +339,7 @@ static void GenKnightMoves(Board &board, bool color) { captures.PopBit(targetSquare); } - while (quietMoves.GetBoard()) { + while (quietMoves) { int targetSquare = quietMoves.getLS1BIndex(); board.AddMove(Move(square, targetSquare, quiet));