From 072a1ea13c9730c21666dcb6bd04ed3dc68ed104 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Mon, 25 Jul 2022 17:34:11 +0200 Subject: [PATCH] Add '*Assign' operators to 'Bitboard' --- src/board/bitboard/mod.rs | 80 ++++++++++++++++++++++++++++++++++++ src/board/direction.rs | 2 +- src/movegen/wizardry/mask.rs | 8 ++-- 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/src/board/bitboard/mod.rs b/src/board/bitboard/mod.rs index 9ef0348..0c06625 100644 --- a/src/board/bitboard/mod.rs +++ b/src/board/bitboard/mod.rs @@ -115,6 +115,22 @@ impl std::ops::Shr for Bitboard { } } +/// Treat bitboard as a set of squares, shift each square's index left by the amount given. +impl std::ops::ShlAssign for Bitboard { + #[inline(always)] + fn shl_assign(&mut self, rhs: usize) { + *self = *self << rhs; + } +} + +/// Treat bitboard as a set of squares, shift each square's index right by the amount given. +impl std::ops::ShrAssign for Bitboard { + #[inline(always)] + fn shr_assign(&mut self, rhs: usize) { + *self = *self >> rhs; + } +} + /// Treat bitboard as a set of squares, and invert the set. impl std::ops::Not for Bitboard { type Output = Bitboard; @@ -145,6 +161,22 @@ impl std::ops::BitOr for Bitboard { } } +/// Treat each bitboard as a set of squares, keep squares that are in either sets. +impl std::ops::BitOrAssign for Bitboard { + #[inline(always)] + fn bitor_assign(&mut self, rhs: Bitboard) { + *self = *self | rhs; + } +} + +/// Treat the [Square] as a singleton bitboard, and apply the operator. +impl std::ops::BitOrAssign for Bitboard { + #[inline(always)] + fn bitor_assign(&mut self, rhs: Square) { + *self = *self | rhs; + } +} + /// Treat each bitboard as a set of squares, keep squares that are in both sets. impl std::ops::BitAnd for Bitboard { type Output = Bitboard; @@ -165,6 +197,22 @@ impl std::ops::BitAnd for Bitboard { } } +/// Treat each bitboard as a set of squares, keep squares that are in both sets. +impl std::ops::BitAndAssign for Bitboard { + #[inline(always)] + fn bitand_assign(&mut self, rhs: Bitboard) { + *self = *self & rhs; + } +} + +/// Treat the [Square] as a singleton bitboard, and apply the operator. +impl std::ops::BitAndAssign for Bitboard { + #[inline(always)] + fn bitand_assign(&mut self, rhs: Square) { + *self = *self & rhs; + } +} + /// Treat each bitboard as a set of squares, keep squares that are in exactly one of either set. impl std::ops::BitXor for Bitboard { type Output = Bitboard; @@ -185,6 +233,22 @@ impl std::ops::BitXor for Bitboard { } } +/// Treat each bitboard as a set of squares, keep squares that are in exactly one of either set. +impl std::ops::BitXorAssign for Bitboard { + #[inline(always)] + fn bitxor_assign(&mut self, rhs: Bitboard) { + *self = *self ^ rhs; + } +} + +/// Treat the [Square] as a singleton bitboard, and apply the operator. +impl std::ops::BitXorAssign for Bitboard { + #[inline(always)] + fn bitxor_assign(&mut self, rhs: Square) { + *self = *self ^ rhs; + } +} + /// Treat each bitboard as a set of squares, and substract one set from another. impl std::ops::Sub for Bitboard { type Output = Bitboard; @@ -205,6 +269,22 @@ impl std::ops::Sub for Bitboard { } } +/// Treat each bitboard as a set of squares, and substract one set from another. +impl std::ops::SubAssign for Bitboard { + #[inline(always)] + fn sub_assign(&mut self, rhs: Bitboard) { + *self = *self - rhs; + } +} + +/// Treat the [Square] as a singleton bitboard, and apply the operator. +impl std::ops::SubAssign for Bitboard { + #[inline(always)] + fn sub_assign(&mut self, rhs: Square) { + *self = *self - rhs; + } +} + #[cfg(test)] mod test { use std::collections::HashSet; diff --git a/src/board/direction.rs b/src/board/direction.rs index 324f97c..40c8d69 100644 --- a/src/board/direction.rs +++ b/src/board/direction.rs @@ -139,7 +139,7 @@ impl Direction { while !board.is_empty() { board = self.move_board(board); - res = res | board; + res |= board; if !(board & blockers).is_empty() { break; } diff --git a/src/movegen/wizardry/mask.rs b/src/movegen/wizardry/mask.rs index 5a6c56e..865c986 100644 --- a/src/movegen/wizardry/mask.rs +++ b/src/movegen/wizardry/mask.rs @@ -20,16 +20,16 @@ pub fn generate_rook_mask(square: Square) -> Bitboard { let mask = { let mut mask = Bitboard::EMPTY; if square.file() != File::A { - mask = mask | File::A.into_bitboard() + mask |= File::A.into_bitboard() }; if square.file() != File::H { - mask = mask | File::H.into_bitboard() + mask |= File::H.into_bitboard() }; if square.rank() != Rank::First { - mask = mask | Rank::First.into_bitboard() + mask |= Rank::First.into_bitboard() }; if square.rank() != Rank::Eighth { - mask = mask | Rank::Eighth.into_bitboard() + mask |= Rank::Eighth.into_bitboard() }; mask };