diff --git a/src/board/bitboard/mod.rs b/src/board/bitboard/mod.rs index 0aa18f7..8dff351 100644 --- a/src/board/bitboard/mod.rs +++ b/src/board/bitboard/mod.rs @@ -68,6 +68,13 @@ impl Bitboard { self == Self::EMPTY } + /// Return true if there are more than piece in the [Bitboard]. This is faster than testing + /// `board.count() > 1`. + #[inline(always)] + pub fn has_more_than_one(self) -> bool { + (self.0 & (self.0.wrapping_sub(1))) != 0 + } + /// Iterate over the power-set of a given [Bitboard], yielding each possible sub-set of /// [Square] that belong to the [Bitboard]. In other words, generate all set of [Square] that /// contain all, some, or none of the [Square] that are in the given [Bitboard]. @@ -388,6 +395,16 @@ mod test { assert_eq!(Bitboard::FILES[0] - Square::A1, Bitboard(0xff - 1)); } + #[test] + fn more_than_one() { + assert!(!Bitboard::EMPTY.has_more_than_one()); + for square in Square::iter() { + assert!(!square.into_bitboard().has_more_than_one()) + } + assert!((Square::A1 | Square::H8).has_more_than_one()); + assert!(Bitboard::ALL.has_more_than_one()); + } + #[test] fn iter_power_set_empty() { assert_eq!(