bitboard refactoring

This commit is contained in:
Simon Gardling 2025-02-22 19:34:08 -05:00
parent 6d9be3dbe6
commit 7c4f05af3e
Signed by: titaniumtown
GPG Key ID: 9AB28AC10ECE533D

View File

@ -39,14 +39,23 @@ impl BitBoard {
Self(0) Self(0)
} }
#[cfg(not(feature = "bitvec"))] #[const_fn(cfg(not(feature = "bitvec")))]
pub const fn get(&self, row: usize, col: usize) -> bool { 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"))] #[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) { 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) // PERF! branchless setting of bit (~+3% perf bump)
self.0 &= !(0b1 << index); // clear bit self.0 &= !(0b1 << index); // clear bit
self.0 |= (value as BitBoardInner) << index; // set bit (if needed) self.0 |= (value as BitBoardInner) << index; // set bit (if needed)
@ -57,13 +66,13 @@ impl BitBoard {
} }
#[cfg(feature = "bitvec")] #[cfg(feature = "bitvec")]
pub fn get(&self, row: usize, col: usize) -> bool { pub fn get_by_index(&self, index: usize) -> bool {
self.0[Self::get_index(row, col)] self.0[index]
} }
#[cfg(feature = "bitvec")] #[cfg(feature = "bitvec")]
pub fn set(&mut self, row: usize, col: usize, value: bool) { pub fn set_by_index(&mut self, index: usize, value: bool) {
self.0.set(Self::get_index(row, col), value); self.0.set(index, value);
} }
// works on both `bitvec` and native (const on native) // works on both `bitvec` and native (const on native)