[board] Move all the BitBoard shift code to a new bitboard::shifts module
This commit is contained in:
parent
5a08a4477e
commit
4c9e6a51fc
3 changed files with 115 additions and 105 deletions
|
@ -6,7 +6,7 @@ use std::fmt;
|
|||
use std::ops::{BitAnd, BitOr, Not};
|
||||
|
||||
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
|
||||
pub(crate) struct BitBoard(u64);
|
||||
pub(crate) struct BitBoard(pub(super) u64);
|
||||
|
||||
impl BitBoard {
|
||||
pub fn empty() -> BitBoard {
|
||||
|
@ -38,61 +38,6 @@ impl BitBoard {
|
|||
}
|
||||
}
|
||||
|
||||
impl BitBoard {
|
||||
const NOT_A_FILE: u64 = 0xfefefefefefefefe;
|
||||
const NOT_H_FILE: u64 = 0x7f7f7f7f7f7f7f7f;
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north(&self, n: u8) -> BitBoard {
|
||||
BitBoard(self.0 << (8 * n))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 8)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north_east_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 9 & BitBoard::NOT_A_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_east_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 1 & BitBoard::NOT_A_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south_east_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 7 & BitBoard::NOT_A_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south(&self, n: u8) -> BitBoard {
|
||||
BitBoard(self.0 >> (8 * n))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 8)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south_west_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 9 & BitBoard::NOT_H_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_west_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 1 & BitBoard::NOT_H_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north_west_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 7 & BitBoard::NOT_H_FILE)
|
||||
}
|
||||
}
|
||||
|
||||
impl BitBoard {
|
||||
pub(crate) fn occupied_squares(&self) -> impl Iterator<Item = Square> {
|
||||
BitScanner::new(self.0)
|
||||
|
@ -200,55 +145,6 @@ mod tests {
|
|||
assert!(!bb.has_piece_at(&sq));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn cardinal_direction_shifts() {
|
||||
let bb = BitBoard(0x1000_0000); // e4
|
||||
|
||||
assert_eq!(bb.shift_north_one(), BitBoard(0x0010_0000_0000), "North");
|
||||
assert_eq!(bb.shift_east_one(), BitBoard(0x2000_0000), "East");
|
||||
assert_eq!(bb.shift_south_one(), BitBoard(0x0010_0000), "South");
|
||||
assert_eq!(bb.shift_west_one(), BitBoard(0x0800_0000), "West");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intercardinal_direction_shifts() {
|
||||
let bb = BitBoard(0x1000_0000); // e4
|
||||
|
||||
assert_eq!(
|
||||
bb.shift_north_east_one(),
|
||||
BitBoard(0x0020_0000_0000),
|
||||
"North East"
|
||||
);
|
||||
assert_eq!(
|
||||
bb.shift_south_east_one(),
|
||||
BitBoard(0x0020_0000),
|
||||
"South East"
|
||||
);
|
||||
assert_eq!(
|
||||
bb.shift_south_west_one(),
|
||||
BitBoard(0x0008_0000),
|
||||
"South West"
|
||||
);
|
||||
assert_eq!(
|
||||
bb.shift_north_west_one(),
|
||||
BitBoard(0x0008_0000_0000),
|
||||
"North West"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_n() {
|
||||
assert_eq!(
|
||||
BitBoard(0x0008_0000_0000).shift_north(2),
|
||||
BitBoard(0x0008_0000_0000_0000)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BitBoard(0x0008_0000_0000).shift_south(2),
|
||||
BitBoard(0x0008_0000)
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn pieces() {
|
||||
let bb = BitBoard(0x1001_1010); // e4
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
mod bit_scanner;
|
||||
mod bitboard;
|
||||
mod shifts;
|
||||
|
||||
pub(crate) use self::bit_scanner::BitScanner;
|
||||
pub(crate) use self::bitboard::BitBoard;
|
||||
|
|
113
board/src/bitboard/shifts.rs
Normal file
113
board/src/bitboard/shifts.rs
Normal file
|
@ -0,0 +1,113 @@
|
|||
// Eryn Wells <eryn@erynwells.me>
|
||||
|
||||
use super::BitBoard;
|
||||
|
||||
impl BitBoard {
|
||||
const NOT_A_FILE: u64 = 0xfefefefefefefefe;
|
||||
const NOT_H_FILE: u64 = 0x7f7f7f7f7f7f7f7f;
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north(&self, n: u8) -> BitBoard {
|
||||
BitBoard(self.0 << (8 * n))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 8)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north_east_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 9 & BitBoard::NOT_A_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_east_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 1 & BitBoard::NOT_A_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south_east_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 7 & BitBoard::NOT_A_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south(&self, n: u8) -> BitBoard {
|
||||
BitBoard(self.0 >> (8 * n))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 8)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_south_west_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 9 & BitBoard::NOT_H_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_west_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 >> 1 & BitBoard::NOT_H_FILE)
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn shift_north_west_one(&self) -> BitBoard {
|
||||
BitBoard(self.0 << 7 & BitBoard::NOT_H_FILE)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::Square;
|
||||
|
||||
#[test]
|
||||
fn cardinal_direction_shifts() {
|
||||
let bb = BitBoard(0x1000_0000); // e4
|
||||
|
||||
assert_eq!(bb.shift_north_one(), BitBoard(0x0010_0000_0000), "North");
|
||||
assert_eq!(bb.shift_east_one(), BitBoard(0x2000_0000), "East");
|
||||
assert_eq!(bb.shift_south_one(), BitBoard(0x0010_0000), "South");
|
||||
assert_eq!(bb.shift_west_one(), BitBoard(0x0800_0000), "West");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn intercardinal_direction_shifts() {
|
||||
let bb = BitBoard(0x1000_0000); // e4
|
||||
|
||||
assert_eq!(
|
||||
bb.shift_north_east_one(),
|
||||
BitBoard(0x0020_0000_0000),
|
||||
"North East"
|
||||
);
|
||||
assert_eq!(
|
||||
bb.shift_south_east_one(),
|
||||
BitBoard(0x0020_0000),
|
||||
"South East"
|
||||
);
|
||||
assert_eq!(
|
||||
bb.shift_south_west_one(),
|
||||
BitBoard(0x0008_0000),
|
||||
"South West"
|
||||
);
|
||||
assert_eq!(
|
||||
bb.shift_north_west_one(),
|
||||
BitBoard(0x0008_0000_0000),
|
||||
"North West"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shift_n() {
|
||||
assert_eq!(
|
||||
BitBoard(0x0008_0000_0000).shift_north(2),
|
||||
BitBoard(0x0008_0000_0000_0000)
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
BitBoard(0x0008_0000_0000).shift_south(2),
|
||||
BitBoard(0x0008_0000)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue