[bitboard, core] Make library getters const; parameterize Library attributes

- Add chessfriend_core::Color::NUM
- All the library getters can be const.
- Use the constants from the core library to define the length of the slices in
  the Library struct.
This commit is contained in:
Eryn Wells 2024-04-25 07:56:53 -07:00
parent cad040e454
commit 1b63f56042
2 changed files with 34 additions and 31 deletions

View file

@ -74,7 +74,7 @@ pub(super) fn library() -> &'static MoveLibrary {
macro_rules! library_getter {
($name:ident) => {
pub(super) fn $name(&self, sq: Square) -> BitBoard {
pub(super) const fn $name(&self, sq: Square) -> BitBoard {
self.$name[sq as usize]
}
};
@ -83,29 +83,29 @@ macro_rules! library_getter {
#[derive(Debug)]
pub(super) struct MoveLibrary {
// Rays
rays: [[BitBoard; 8]; Square::NUM],
rays: [[BitBoard; Direction::NUM]; Square::NUM],
// Piecewise move tables
pawn_attacks: [[BitBoard; 64]; 2],
pawn_pushes: [[BitBoard; 64]; 2],
knight_moves: [BitBoard; 64],
bishop_moves: [BitBoard; 64],
rook_moves: [BitBoard; 64],
queen_moves: [BitBoard; 64],
king_moves: [BitBoard; 64],
pawn_attacks: [[BitBoard; Square::NUM]; Color::NUM],
pawn_pushes: [[BitBoard; Square::NUM]; Color::NUM],
knight_moves: [BitBoard; Square::NUM],
bishop_moves: [BitBoard; Square::NUM],
rook_moves: [BitBoard; Square::NUM],
queen_moves: [BitBoard; Square::NUM],
king_moves: [BitBoard; Square::NUM],
}
impl MoveLibrary {
const fn new() -> MoveLibrary {
MoveLibrary {
rays: [[BitBoard::empty(); 8]; Square::NUM],
pawn_attacks: [[BitBoard::empty(); 64]; 2],
pawn_pushes: [[BitBoard::empty(); 64]; 2],
knight_moves: [BitBoard::empty(); 64],
bishop_moves: [BitBoard::empty(); 64],
rook_moves: [BitBoard::empty(); 64],
queen_moves: [BitBoard::empty(); 64],
king_moves: [BitBoard::empty(); 64],
rays: [[BitBoard::empty(); Direction::NUM]; Square::NUM],
pawn_attacks: [[BitBoard::empty(); Square::NUM]; Color::NUM],
pawn_pushes: [[BitBoard::empty(); Square::NUM]; Color::NUM],
knight_moves: [BitBoard::empty(); Square::NUM],
bishop_moves: [BitBoard::empty(); Square::NUM],
rook_moves: [BitBoard::empty(); Square::NUM],
queen_moves: [BitBoard::empty(); Square::NUM],
king_moves: [BitBoard::empty(); Square::NUM],
}
}
@ -125,23 +125,23 @@ impl MoveLibrary {
fn init_orthogonal_rays(&mut self, sq: Square) {
let sq_bb: BitBoard = sq.into();
let rays = &mut self.rays[sq as usize];
rays[Direction::North as usize] = Self::generate_ray(sq_bb, BitBoard::shift_north_one);
rays[Direction::South as usize] = Self::generate_ray(sq_bb, BitBoard::shift_south_one);
rays[Direction::East as usize] = Self::generate_ray(sq_bb, BitBoard::shift_east_one);
rays[Direction::West as usize] = Self::generate_ray(sq_bb, BitBoard::shift_west_one);
rays[Direction::North as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_north_one);
rays[Direction::South as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_south_one);
rays[Direction::East as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_east_one);
rays[Direction::West as usize] = Self::_generate_ray(sq_bb, BitBoard::shift_west_one);
}
fn init_diagonal_rays(&mut self, sq: Square) {
let sq_bb: BitBoard = sq.into();
let rays = &mut self.rays[sq as usize];
rays[Direction::NorthEast as usize] =
Self::generate_ray(sq_bb, BitBoard::shift_north_east_one);
Self::_generate_ray(sq_bb, BitBoard::shift_north_east_one);
rays[Direction::NorthWest as usize] =
Self::generate_ray(sq_bb, BitBoard::shift_north_west_one);
Self::_generate_ray(sq_bb, BitBoard::shift_north_west_one);
rays[Direction::SouthWest as usize] =
Self::generate_ray(sq_bb, BitBoard::shift_south_west_one);
Self::_generate_ray(sq_bb, BitBoard::shift_south_west_one);
rays[Direction::SouthEast as usize] =
Self::generate_ray(sq_bb, BitBoard::shift_south_east_one);
Self::_generate_ray(sq_bb, BitBoard::shift_south_east_one);
}
fn init_king_moves(&mut self, idx: usize) {
@ -225,8 +225,7 @@ impl MoveLibrary {
};
}
#[inline]
fn generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard {
fn _generate_ray(sq: BitBoard, shift: fn(&BitBoard) -> BitBoard) -> BitBoard {
let mut ray = BitBoard::empty();
let mut iter = shift(&sq);
@ -238,15 +237,15 @@ impl MoveLibrary {
ray
}
pub(super) fn ray(&self, sq: Square, dir: Direction) -> &BitBoard {
pub(super) const fn ray(&self, sq: Square, dir: Direction) -> &BitBoard {
&self.rays[sq as usize][dir as usize]
}
pub(super) fn pawn_pushes(&self, sq: Square, color: Color) -> BitBoard {
pub(super) const fn pawn_pushes(&self, sq: Square, color: Color) -> BitBoard {
self.pawn_pushes[color as usize][sq as usize]
}
pub(super) fn pawn_attacks(&self, sq: Square, color: Color) -> BitBoard {
pub(super) const fn pawn_attacks(&self, sq: Square, color: Color) -> BitBoard {
self.pawn_attacks[color as usize][sq as usize]
}