1
0
Fork 0

make bitboards public

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-19 15:13:51 +02:00
parent 5f1bc22797
commit 04dfbb23aa
4 changed files with 49 additions and 37 deletions

View file

@ -1,22 +1,28 @@
//! Sets of squares.
use crate::board::*; use crate::board::*;
use std::iter::ExactSizeIterator; use std::iter::ExactSizeIterator;
use std::iter::FusedIterator; use std::iter::FusedIterator;
/// A set of squares.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Bitboard(pub(crate) u64); pub struct Bitboard(pub(crate) u64);
impl Bitboard { impl Bitboard {
/// Returns an empty bitboard.
#[inline] #[inline]
pub fn new() -> Self { pub fn new() -> Self {
Self(0) Self(0)
} }
/// Returns `true` if the bitboard is empty.
#[inline] #[inline]
pub fn is_empty(&self) -> bool { pub fn is_empty(&self) -> bool {
self.0 == 0 self.0 == 0
} }
/// Returns the square in the bitboard with the smallest index.
#[inline] #[inline]
pub fn first(&self) -> Option<Square> { pub fn first(&self) -> Option<Square> {
let mask = self.0; let mask = self.0;
@ -26,6 +32,8 @@ impl Bitboard {
} }
} }
/// Removes the square in the bitboard with the smallest index and returns it, or `None` if the
/// bitboard is empty.
#[inline] #[inline]
pub fn pop(&mut self) -> Option<Square> { pub fn pop(&mut self) -> Option<Square> {
let Self(ref mut mask) = self; let Self(ref mut mask) = self;
@ -37,13 +45,27 @@ impl Bitboard {
square square
} }
/// Inserts a square in the bitboard.
#[inline] #[inline]
pub fn insert(&mut self, square: Square) { pub fn insert(&mut self, square: Square) {
self.0 |= 1 << square as u8; self.0 |= 1 << square as u8;
} }
/// Returns `true` if the bitboard contains the given square.
#[inline] #[inline]
pub fn trans(&self, direction: Direction) -> Self { pub fn contains(&self, square: Square) -> bool {
self.0 & (1 << square as u8) != 0
}
/// Returns the mirror of the bitboard (see [`Setup::mirror`]).
#[inline]
pub fn mirror(self) -> Bitboard {
let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes();
Self(u64::from_le_bytes([h, g, f, e, d, c, b, a]))
}
#[inline]
pub(crate) fn trans(&self, direction: Direction) -> Self {
match direction { match direction {
Direction::North => Self(self.0 << 8), Direction::North => Self(self.0 << 8),
Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(), Direction::NorthEast => Self(self.0 << 9) & !File::A.bitboard(),
@ -55,17 +77,6 @@ impl Bitboard {
Direction::NorthWest => Self(self.0 << 7) & !File::H.bitboard(), Direction::NorthWest => Self(self.0 << 7) & !File::H.bitboard(),
} }
} }
#[inline]
pub fn contains(&self, square: Square) -> bool {
self.0 & (1 << square as u8) != 0
}
#[inline]
pub fn mirror(self) -> Bitboard {
let [a, b, c, d, e, f, g, h] = self.0.to_le_bytes();
Self(u64::from_le_bytes([h, g, f, e, d, c, b, a]))
}
} }
impl std::ops::BitOr for Bitboard { impl std::ops::BitOr for Bitboard {

View file

@ -3,9 +3,9 @@
use crate::bitboard::*; use crate::bitboard::*;
macro_rules! container { macro_rules! container {
($a:ident, $b:ident, $n:literal) => { ($v: vis, $a:ident, $b:ident, $n:literal) => {
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub(crate) struct $b<T>(pub(crate) [T; $n]); $v struct $b<T>(pub(crate) [T; $n]);
#[allow(unused)] #[allow(unused)]
impl<T> $b<T> { impl<T> $b<T> {
#[inline] #[inline]
@ -35,7 +35,7 @@ pub enum Color {
Black, Black,
} }
container!(Color, ByColor, 2); container!(pub, Color, ByColor, 2);
impl Color { impl Color {
#[inline] #[inline]
@ -93,7 +93,7 @@ pub enum File {
H, H,
} }
container!(File, ByFile, 8); container!(pub(crate), File, ByFile, 8);
impl File { impl File {
#[inline] #[inline]
@ -173,7 +173,7 @@ pub enum Rank {
Eighth, Eighth,
} }
container!(Rank, ByRank, 8); container!(pub(crate), Rank, ByRank, 8);
impl Rank { impl Rank {
#[inline] #[inline]
@ -259,7 +259,7 @@ pub enum Square{
A8, B8, C8, D8, E8, F8, G8, H8, A8, B8, C8, D8, E8, F8, G8, H8,
} }
container!(Square, BySquare, 64); container!(pub, Square, BySquare, 64);
impl Square { impl Square {
#[inline] #[inline]
@ -310,7 +310,7 @@ impl Square {
} }
#[inline] #[inline]
pub(crate) fn bitboard(self) -> Bitboard { pub fn bitboard(self) -> Bitboard {
Bitboard(1 << self as u8) Bitboard(1 << self as u8)
} }
@ -525,7 +525,7 @@ impl Role {
} }
#[derive(Clone, Copy, PartialEq, Eq, Hash)] #[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub(crate) struct ByRole<T>(pub(crate) [T; 6]); pub struct ByRole<T>(pub(crate) [T; 6]);
#[allow(unused)] #[allow(unused)]
impl<T> ByRole<T> { impl<T> ByRole<T> {
#[inline] #[inline]
@ -597,7 +597,7 @@ pub(crate) enum Direction {
West, West,
} }
container!(Direction, ByDirection, 8); container!(pub(crate), Direction, ByDirection, 8);
impl Direction { impl Direction {
#[inline] #[inline]
@ -639,7 +639,7 @@ pub enum CastlingSide {
Long, Long,
} }
container!(CastlingSide, ByCastlingSide, 2); container!(pub(crate), CastlingSide, ByCastlingSide, 2);
impl CastlingSide { impl CastlingSide {
#[inline] #[inline]

View file

@ -73,10 +73,10 @@
//! - etc. //! - etc.
pub(crate) mod array_vec; pub(crate) mod array_vec;
pub(crate) mod bitboard;
pub(crate) mod magics; pub(crate) mod magics;
pub(crate) mod rays; pub(crate) mod rays;
pub mod bitboard;
pub mod board; pub mod board;
pub mod lookup; pub mod lookup;
pub mod position; pub mod position;

View file

@ -261,7 +261,7 @@ impl Setup {
let d = InitialisedLookup::init(); let d = InitialisedLookup::init();
let blockers = self.p_b_q | self.n_b_k | self.r_q_k; let blockers = self.p_b_q | self.n_b_k | self.r_q_k;
let pieces = self.bitboards(); let pieces = self.pieces();
if Color::all() if Color::all()
.into_iter() .into_iter()
@ -379,20 +379,9 @@ impl Setup {
} }
} }
/// Returns the bitboard of each kind of piece.
#[inline] #[inline]
pub(crate) fn get_role(&self, square: Square) -> Option<Role> { pub fn pieces(&self) -> ByColor<ByRole<Bitboard>> {
let mask = square.bitboard();
let bit0 = (self.p_b_q & mask).0 >> square as u8;
let bit1 = (self.n_b_k & mask).0 >> square as u8;
let bit2 = (self.r_q_k & mask).0 >> square as u8;
match bit0 | bit1 << 1 | bit2 << 2 {
0 => None,
i => Some(unsafe { Role::transmute(i as u8) }),
}
}
#[inline]
pub(crate) fn bitboards(&self) -> ByColor<ByRole<Bitboard>> {
let Self { let Self {
w, w,
p_b_q, p_b_q,
@ -423,6 +412,18 @@ impl Setup {
}) })
}) })
} }
#[inline]
pub(crate) fn get_role(&self, square: Square) -> Option<Role> {
let mask = square.bitboard();
let bit0 = (self.p_b_q & mask).0 >> square as u8;
let bit1 = (self.n_b_k & mask).0 >> square as u8;
let bit2 = (self.r_q_k & mask).0 >> square as u8;
match bit0 | bit1 << 1 | bit2 << 2 {
0 => None,
i => Some(unsafe { Role::transmute(i as u8) }),
}
}
} }
impl std::fmt::Debug for Setup { impl std::fmt::Debug for Setup {