Skip to main content

Bitboard

Struct Bitboard 

Source
pub struct Bitboard(pub u64);
Expand description

A set of squares represented as a 64-bit bitboard.

Bit n corresponds to Square with discriminant n. Supports standard bitwise operators (&, |, ^, !, <<, >>) as well as set-wise operations with Square values.

Tuple Fields§

§0: u64

Implementations§

Source§

impl Bitboard

Source

pub const EMPTY: Bitboard

The empty bitboard (no squares set).

Source

pub fn is_empty(self) -> bool

Returns true if no squares are set.

Examples found in repository?
examples/pawn_debug.rs (line 27)
7fn main() {
8    atomic_movegen::attacks::init();
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("Usage: pawn_debug <fen>");
12        return;
13    }
14    let fen = &args[1];
15    let board = Board::from_fen(fen).expect("Invalid FEN");
16
17    println!("Board side to move: {:?}", board.side_to_move());
18
19    // Manual check: iterate all pawns
20    let us = board.side_to_move();
21    let _them = us.flip();
22    let _occupied = board.occupied();
23
24    let pawns = board.pieces_color_pt(us, PieceType::Pawn);
25    println!("Pawns ({:?}): {}", us, pawns.count());
26    let mut p = pawns;
27    while !p.is_empty() {
28        let sq = p.pop_lsb();
29        println!(
30            "  Pawn at {} (idx={}): file={} rank={}",
31            sq_str(sq),
32            sq as u8,
33            file_of(sq) as u8,
34            rank_of(sq) as u8
35        );
36    }
37
38    // Check what generate_legal produces
39    let mut moves = MoveList::new();
40    movegen::generate_legal(&board, &mut moves);
41
42    // Find all h3g2 moves
43    let h3 = Square::from_index(23);
44    let g2 = Square::from_index(14);
45    println!("\nAll h3g2 moves:");
46    for (i, &m) in moves.as_slice().iter().enumerate() {
47        if m.from_sq() == h3 && m.to_sq() == g2 {
48            println!(
49                "  Move #{}: from={} to={} type={:?}",
50                i,
51                sq_str(m.from_sq()),
52                sq_str(m.to_sq()),
53                m.move_type()
54            );
55        }
56    }
57
58    // Also check how many times each move appears
59    use std::collections::HashMap;
60    let mut counts: HashMap<(u16, u16, u16), usize> = HashMap::new();
61    for &m in moves.as_slice() {
62        *counts
63            .entry((m.from_sq() as u16, m.to_sq() as u16, m.move_type() as u16))
64            .or_insert(0) += 1;
65    }
66    for ((from, to, mt), count) in &counts {
67        if *count > 1 {
68            println!(
69                "DUPLICATE: {}{} type={:?} appears {} times",
70                sq_str(Square::from_u8(*from as u8)),
71                sq_str(Square::from_u8(*to as u8)),
72                match mt {
73                    0 => "Normal",
74                    1 => "Promo",
75                    2 => "EP",
76                    3 => "Castle",
77                    _ => "?",
78                },
79                count
80            );
81        }
82    }
83}
Source

pub fn count(self) -> u32

Return the number of squares set in the bitboard.

Examples found in repository?
examples/pawn_debug.rs (line 25)
7fn main() {
8    atomic_movegen::attacks::init();
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("Usage: pawn_debug <fen>");
12        return;
13    }
14    let fen = &args[1];
15    let board = Board::from_fen(fen).expect("Invalid FEN");
16
17    println!("Board side to move: {:?}", board.side_to_move());
18
19    // Manual check: iterate all pawns
20    let us = board.side_to_move();
21    let _them = us.flip();
22    let _occupied = board.occupied();
23
24    let pawns = board.pieces_color_pt(us, PieceType::Pawn);
25    println!("Pawns ({:?}): {}", us, pawns.count());
26    let mut p = pawns;
27    while !p.is_empty() {
28        let sq = p.pop_lsb();
29        println!(
30            "  Pawn at {} (idx={}): file={} rank={}",
31            sq_str(sq),
32            sq as u8,
33            file_of(sq) as u8,
34            rank_of(sq) as u8
35        );
36    }
37
38    // Check what generate_legal produces
39    let mut moves = MoveList::new();
40    movegen::generate_legal(&board, &mut moves);
41
42    // Find all h3g2 moves
43    let h3 = Square::from_index(23);
44    let g2 = Square::from_index(14);
45    println!("\nAll h3g2 moves:");
46    for (i, &m) in moves.as_slice().iter().enumerate() {
47        if m.from_sq() == h3 && m.to_sq() == g2 {
48            println!(
49                "  Move #{}: from={} to={} type={:?}",
50                i,
51                sq_str(m.from_sq()),
52                sq_str(m.to_sq()),
53                m.move_type()
54            );
55        }
56    }
57
58    // Also check how many times each move appears
59    use std::collections::HashMap;
60    let mut counts: HashMap<(u16, u16, u16), usize> = HashMap::new();
61    for &m in moves.as_slice() {
62        *counts
63            .entry((m.from_sq() as u16, m.to_sq() as u16, m.move_type() as u16))
64            .or_insert(0) += 1;
65    }
66    for ((from, to, mt), count) in &counts {
67        if *count > 1 {
68            println!(
69                "DUPLICATE: {}{} type={:?} appears {} times",
70                sq_str(Square::from_u8(*from as u8)),
71                sq_str(Square::from_u8(*to as u8)),
72                match mt {
73                    0 => "Normal",
74                    1 => "Promo",
75                    2 => "EP",
76                    3 => "Castle",
77                    _ => "?",
78                },
79                count
80            );
81        }
82    }
83}
Source

pub fn lsb(self) -> Square

Return the least-significant (lowest-index) set square.

§Panics

Panics in debug mode if the bitboard is empty.

Source

pub fn pop_lsb(&mut self) -> Square

Extract and remove the least-significant set square.

Examples found in repository?
examples/pawn_debug.rs (line 28)
7fn main() {
8    atomic_movegen::attacks::init();
9    let args: Vec<String> = env::args().collect();
10    if args.len() < 2 {
11        eprintln!("Usage: pawn_debug <fen>");
12        return;
13    }
14    let fen = &args[1];
15    let board = Board::from_fen(fen).expect("Invalid FEN");
16
17    println!("Board side to move: {:?}", board.side_to_move());
18
19    // Manual check: iterate all pawns
20    let us = board.side_to_move();
21    let _them = us.flip();
22    let _occupied = board.occupied();
23
24    let pawns = board.pieces_color_pt(us, PieceType::Pawn);
25    println!("Pawns ({:?}): {}", us, pawns.count());
26    let mut p = pawns;
27    while !p.is_empty() {
28        let sq = p.pop_lsb();
29        println!(
30            "  Pawn at {} (idx={}): file={} rank={}",
31            sq_str(sq),
32            sq as u8,
33            file_of(sq) as u8,
34            rank_of(sq) as u8
35        );
36    }
37
38    // Check what generate_legal produces
39    let mut moves = MoveList::new();
40    movegen::generate_legal(&board, &mut moves);
41
42    // Find all h3g2 moves
43    let h3 = Square::from_index(23);
44    let g2 = Square::from_index(14);
45    println!("\nAll h3g2 moves:");
46    for (i, &m) in moves.as_slice().iter().enumerate() {
47        if m.from_sq() == h3 && m.to_sq() == g2 {
48            println!(
49                "  Move #{}: from={} to={} type={:?}",
50                i,
51                sq_str(m.from_sq()),
52                sq_str(m.to_sq()),
53                m.move_type()
54            );
55        }
56    }
57
58    // Also check how many times each move appears
59    use std::collections::HashMap;
60    let mut counts: HashMap<(u16, u16, u16), usize> = HashMap::new();
61    for &m in moves.as_slice() {
62        *counts
63            .entry((m.from_sq() as u16, m.to_sq() as u16, m.move_type() as u16))
64            .or_insert(0) += 1;
65    }
66    for ((from, to, mt), count) in &counts {
67        if *count > 1 {
68            println!(
69                "DUPLICATE: {}{} type={:?} appears {} times",
70                sq_str(Square::from_u8(*from as u8)),
71                sq_str(Square::from_u8(*to as u8)),
72                match mt {
73                    0 => "Normal",
74                    1 => "Promo",
75                    2 => "EP",
76                    3 => "Castle",
77                    _ => "?",
78                },
79                count
80            );
81        }
82    }
83}
Source

pub fn more_than_one(self) -> bool

Returns true if more than one square is set.

Source

pub fn square_bb(sq: Square) -> Bitboard

Return a bitboard with only the given square set. Returns EMPTY for Square::NONE.

Trait Implementations§

Source§

impl BitAnd for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Bitboard) -> Bitboard

Performs the & operation. Read more
Source§

impl BitAnd<Square> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the & operator.
Source§

fn bitand(self, rhs: Square) -> Bitboard

Performs the & operation. Read more
Source§

impl BitOr for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Bitboard) -> Bitboard

Performs the | operation. Read more
Source§

impl BitOr<Square> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the | operator.
Source§

fn bitor(self, rhs: Square) -> Bitboard

Performs the | operation. Read more
Source§

impl BitXor for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Bitboard) -> Bitboard

Performs the ^ operation. Read more
Source§

impl BitXor<Square> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the ^ operator.
Source§

fn bitxor(self, rhs: Square) -> Bitboard

Performs the ^ operation. Read more
Source§

impl Clone for Bitboard

Source§

fn clone(&self) -> Bitboard

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for Bitboard

Source§

impl Debug for Bitboard

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Bitboard

Source§

impl Not for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the ! operator.
Source§

fn not(self) -> Bitboard

Performs the unary ! operation. Read more
Source§

impl PartialEq for Bitboard

Source§

fn eq(&self, other: &Bitboard) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Shl<usize> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the << operator.
Source§

fn shl(self, rhs: usize) -> Bitboard

Performs the << operation. Read more
Source§

impl Shr<usize> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the >> operator.
Source§

fn shr(self, rhs: usize) -> Bitboard

Performs the >> operation. Read more
Source§

impl StructuralPartialEq for Bitboard

Source§

impl Sub<Square> for Bitboard

Source§

type Output = Bitboard

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Square) -> Bitboard

Performs the - operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.