From 7c4f05af3eacf6090e262b2b8f037e79438d4cea Mon Sep 17 00:00:00 2001 From: Simon Gardling Date: Sat, 22 Feb 2025 19:34:08 -0500 Subject: [PATCH] bitboard refactoring --- src/repr/bitboard.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/repr/bitboard.rs b/src/repr/bitboard.rs index f19f620..eedad1c 100644 --- a/src/repr/bitboard.rs +++ b/src/repr/bitboard.rs @@ -39,14 +39,23 @@ impl BitBoard { Self(0) } - #[cfg(not(feature = "bitvec"))] + #[const_fn(cfg(not(feature = "bitvec")))] pub const fn get(&self, row: usize, col: usize) -> bool { - ((self.0 >> Self::get_index(row, col)) & 0b1) != 0b0 + self.get_by_index(Self::get_index(row, col)) } #[cfg(not(feature = "bitvec"))] + const fn get_by_index(&self, index: usize) -> bool { + ((self.0 >> index) & 0b1) != 0b0 + } + + #[const_fn(cfg(not(feature = "bitvec")))] pub const fn set(&mut self, row: usize, col: usize, value: bool) { - let index = Self::get_index(row, col); + self.set_by_index(Self::get_index(row, col), value); + } + + #[cfg(not(feature = "bitvec"))] + const fn set_by_index(&mut self, index: usize, value: bool) { // PERF! branchless setting of bit (~+3% perf bump) self.0 &= !(0b1 << index); // clear bit self.0 |= (value as BitBoardInner) << index; // set bit (if needed) @@ -57,13 +66,13 @@ impl BitBoard { } #[cfg(feature = "bitvec")] - pub fn get(&self, row: usize, col: usize) -> bool { - self.0[Self::get_index(row, col)] + pub fn get_by_index(&self, index: usize) -> bool { + self.0[index] } #[cfg(feature = "bitvec")] - pub fn set(&mut self, row: usize, col: usize, value: bool) { - self.0.set(Self::get_index(row, col), value); + pub fn set_by_index(&mut self, index: usize, value: bool) { + self.0.set(index, value); } // works on both `bitvec` and native (const on native)