From 9f8bf52436d3778bbc4f7caf5561f7961c8c1e08 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 16 Aug 2023 02:34:03 +0300 Subject: feat: implement bitboard setwise fills --- src/bitboard.rs | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) (limited to 'src/bitboard.rs') diff --git a/src/bitboard.rs b/src/bitboard.rs index 4fb5b30..056822b 100644 --- a/src/bitboard.rs +++ b/src/bitboard.rs @@ -29,6 +29,21 @@ pub trait BitboardFns { /// Convert bitboard into the list of squares fn serialize(self) -> Vec; + + /// Return bitboard shifted nort, no wrap occurs + fn nort_one(self) -> Self; + /// Return bitboard shifted sout, no wrap occurs + fn sout_one(self) -> Self; + /// Return bitboard shifted east, no wrap occurs + fn east_one(self) -> Self; + /// Return bitboard shifted west, no wrap occurs + fn west_one(self) -> Self; + + /// Return bitboard with a nort fill + fn nort_fill(self) -> Self; + /// Return bitboard with a sout fill + fn sout_fill(self) -> Self; + } const DE_BRUJIN_SEQUENCE: [u8; 64] = [ @@ -42,6 +57,9 @@ const DE_BRUJIN_SEQUENCE: [u8; 64] = [ 25, 14, 19, 9, 13, 8, 7, 6 ]; +static NOT_A_FILE: Bitboard = 0xFEFEFEFEFEFEFEFE; +static NOT_H_FILE: Bitboard = 0x7F7F7F7F7F7F7F7F; + impl BitboardFns for Bitboard { fn print(self, title: &str) { println!("\n {}", title); @@ -98,6 +116,38 @@ impl BitboardFns for Bitboard { } serialized } + + fn nort_one(self) -> Self { + self << 8 + } + + fn sout_one(self) -> Self { + self >> 8 + } + + fn east_one(self) -> Self { + (self << 1) & NOT_A_FILE + } + + fn west_one(self) -> Self { + (self >> 1) & NOT_H_FILE + } + + fn nort_fill(self) -> Self { + let mut fill = self; + fill |= fill << 8; + fill |= fill << 16; + fill |= fill << 32; + fill + } + + fn sout_fill(self) -> Self { + let mut fill = self; + fill |= fill >> 8; + fill |= fill >> 16; + fill |= fill >> 32; + fill + } } #[cfg(test)] @@ -139,4 +189,36 @@ mod tests { assert_eq!(serialized[1], Square::from(15)); assert_eq!(serialized[2], Square::from(60)); } + + #[test] + fn shifts() { + let bb = Square::E4.to_bitboard(); + assert_eq!(bb.nort_one(), Square::E5.to_bitboard()); + assert_eq!(bb.sout_one(), Square::E3.to_bitboard()); + assert_eq!(bb.west_one(), Square::D4.to_bitboard()); + assert_eq!(bb.east_one(), Square::F4.to_bitboard()); + } + + #[test] + fn shifts_wraps() { + assert_eq!(Square::A1.to_bitboard().sout_one(), 0); + assert_eq!(Square::A1.to_bitboard().west_one(), 0); + assert_eq!(Square::H8.to_bitboard().nort_one(), 0); + assert_eq!(Square::H8.to_bitboard().east_one(), 0); + } + + #[test] + fn fills() { + let bb = Square::A4.to_bitboard() | Square::E4.to_bitboard(); + let nort = bb.nort_fill(); + let sout = bb.sout_fill(); + + assert_eq!(nort.pop_count(), 10); + assert!(nort & Square::A8.to_bitboard() != 0); + assert!(nort & Square::E8.to_bitboard() != 0); + + assert_eq!(sout.pop_count(), 8); + assert!(sout & Square::A1.to_bitboard() != 0); + assert!(sout & Square::E1.to_bitboard() != 0); + } } -- cgit v1.2.3