const lookup
This commit is contained in:
parent
71d0d5e0d8
commit
028549542b
8 changed files with 361 additions and 422 deletions
|
|
@ -65,7 +65,7 @@ impl Bitboard {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub const fn remove(&mut self, square: Square) {
|
pub const fn remove(&mut self, square: Square) {
|
||||||
todo!()
|
self.0 &= !(1 << square as u8);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the bitboard contains the given square.
|
/// Returns `true` if the bitboard contains the given square.
|
||||||
|
|
|
||||||
|
|
@ -24,6 +24,9 @@ macro_rules! container {
|
||||||
pub fn get_mut(&mut self, k: $a) -> &mut T {
|
pub fn get_mut(&mut self, k: $a) -> &mut T {
|
||||||
unsafe { self.0.get_unchecked_mut(k as usize) }
|
unsafe { self.0.get_unchecked_mut(k as usize) }
|
||||||
}
|
}
|
||||||
|
pub(crate) const fn get_const(&self, k: $a) -> &T {
|
||||||
|
&self.0[k as usize]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -87,11 +87,3 @@ pub mod uci;
|
||||||
pub mod prelude {
|
pub mod prelude {
|
||||||
pub use crate::{position::Position, san::San, setup::Setup, uci::UciMove};
|
pub use crate::{position::Position, san::San, setup::Setup, uci::UciMove};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test() {
|
|
||||||
use crate::bitboard::*;
|
|
||||||
use crate::board::*;
|
|
||||||
let d = &crate::lookup::LOOKUP;
|
|
||||||
let Bitboard(x) = d.pawn_attack(Color::White, Square::F7);
|
|
||||||
println!("{x:016x}");
|
|
||||||
}
|
|
||||||
|
|
|
||||||
446
src/lookup.rs
446
src/lookup.rs
|
|
@ -2,9 +2,6 @@ use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
use crate::magics::*;
|
use crate::magics::*;
|
||||||
|
|
||||||
#[allow(long_running_const_eval)]
|
|
||||||
pub(crate) static LOOKUP: Lookup = Lookup::compute();
|
|
||||||
|
|
||||||
macro_rules! loop_bishop_directions {
|
macro_rules! loop_bishop_directions {
|
||||||
($d: ident, $body: expr) => {{
|
($d: ident, $body: expr) => {{
|
||||||
{
|
{
|
||||||
|
|
@ -99,11 +96,7 @@ macro_rules! by_direction {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct Rays(BySquare<ByDirection<Bitboard>>);
|
const RAYS: BySquare<ByDirection<Bitboard>> = by_square!(
|
||||||
|
|
||||||
impl Rays {
|
|
||||||
pub(crate) const fn new() -> Self {
|
|
||||||
Self(by_square!(
|
|
||||||
square,
|
square,
|
||||||
ByDirection([Bitboard(0); 8]),
|
ByDirection([Bitboard(0); 8]),
|
||||||
by_direction!(direction, {
|
by_direction!(direction, {
|
||||||
|
|
@ -115,147 +108,33 @@ impl Rays {
|
||||||
}
|
}
|
||||||
Bitboard(res)
|
Bitboard(res)
|
||||||
})
|
})
|
||||||
))
|
);
|
||||||
}
|
const LINES: BySquare<BySquare<Bitboard>> = by_square!(a, BySquare([Bitboard(0); 64]), {
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) const fn ray(&self, square: Square, direction: Direction) -> Bitboard {
|
|
||||||
(self.0).0[square as u8 as usize].0[direction as u8 as usize]
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) const fn blocked(
|
|
||||||
&self,
|
|
||||||
square: Square,
|
|
||||||
direction: Direction,
|
|
||||||
blockers: Bitboard,
|
|
||||||
) -> Bitboard {
|
|
||||||
let ray = self.ray(square, direction);
|
|
||||||
let blockers = Bitboard(blockers.0 & ray.0);
|
|
||||||
let square = if (direction as u8) < 4 {
|
|
||||||
Bitboard::first(&blockers)
|
|
||||||
} else {
|
|
||||||
Bitboard::last(&blockers)
|
|
||||||
};
|
|
||||||
match square {
|
|
||||||
None => ray,
|
|
||||||
Some(square) => Bitboard(ray.0 & !self.ray(square, direction).0),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) struct Lookup {
|
|
||||||
rays: Rays,
|
|
||||||
lines: BySquare<BySquare<Bitboard>>,
|
|
||||||
segments: BySquare<BySquare<Bitboard>>,
|
|
||||||
pawn_attacks: ByColor<BySquare<Bitboard>>,
|
|
||||||
king_moves: BySquare<Bitboard>,
|
|
||||||
knight_moves: BySquare<Bitboard>,
|
|
||||||
pub(crate) magics: Magics,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Lookup {
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn line(&self, a: Square, b: Square) -> Bitboard {
|
|
||||||
*self.lines.get(a).get(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn segment(&self, a: Square, b: Square) -> Bitboard {
|
|
||||||
*self.segments.get(a).get(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn ray(&self, square: Square, direction: Direction) -> Bitboard {
|
|
||||||
self.rays.ray(square, direction)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn king(&self, square: Square) -> Bitboard {
|
|
||||||
*self.king_moves.get(square)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn knight(&self, square: Square) -> Bitboard {
|
|
||||||
*self.knight_moves.get(square)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn pawn_attack(&self, color: Color, square: Square) -> Bitboard {
|
|
||||||
*self.pawn_attacks.get(color).get(square)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn bishop(&self, square: Square, blockers: Bitboard) -> Bitboard {
|
|
||||||
self.magics.bishop(square, blockers)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn rook(&self, square: Square, blockers: Bitboard) -> Bitboard {
|
|
||||||
self.magics.rook(square, blockers)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// `role != Pawn`
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn targets(&self, role: Role, from: Square, blockers: Bitboard) -> Bitboard {
|
|
||||||
match role {
|
|
||||||
Role::Pawn => unreachable!(),
|
|
||||||
Role::Knight => self.knight(from),
|
|
||||||
Role::Bishop => self.bishop(from, blockers),
|
|
||||||
Role::Rook => self.rook(from, blockers),
|
|
||||||
Role::Queen => self.bishop(from, blockers) | self.rook(from, blockers),
|
|
||||||
Role::King => self.king(from),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const fn compute() -> Self {
|
|
||||||
let rays = Rays::new();
|
|
||||||
|
|
||||||
let lines = by_square!(a, BySquare([Bitboard(0); 64]), {
|
|
||||||
by_square!(b, Bitboard(0), {
|
by_square!(b, Bitboard(0), {
|
||||||
let mut res = Bitboard(0);
|
let mut res = Bitboard(0);
|
||||||
loop_all_directions!(d, {
|
loop_all_directions!(d, {
|
||||||
let r = rays.ray(a, d);
|
let r = *RAYS.get_const(a).get_const(d);
|
||||||
if r.contains(b) {
|
if r.contains(b) {
|
||||||
res = r;
|
res = r;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
res
|
res
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let segments: BySquare<BySquare<Bitboard>> = by_square!(a, BySquare([Bitboard(0); 64]), {
|
const SEGMENTS: BySquare<BySquare<Bitboard>> = by_square!(a, BySquare([Bitboard(0); 64]), {
|
||||||
by_square!(b, Bitboard(0), {
|
by_square!(b, Bitboard(0), {
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
loop_all_directions!(d, {
|
loop_all_directions!(d, {
|
||||||
let r = rays.ray(a, d);
|
let r = *RAYS.get_const(a).get_const(d);
|
||||||
if r.contains(b) {
|
if r.contains(b) {
|
||||||
res = r.0 & !rays.ray(b, d).0;
|
res = r.0 & !RAYS.get_const(b).get_const(d).0;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Bitboard(res | b.bitboard().0)
|
Bitboard(res | b.bitboard().0)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
const KING_MOVES: BySquare<Bitboard> = by_square!(sq, Bitboard(0), {
|
||||||
let pawn_attacks = by_color!(color, {
|
|
||||||
let direction = match color {
|
|
||||||
Color::White => Direction::North,
|
|
||||||
Color::Black => Direction::South,
|
|
||||||
};
|
|
||||||
by_square!(square, Bitboard(0), {
|
|
||||||
let mut res = Bitboard(0);
|
|
||||||
if let Some(square) = square.trans(direction) {
|
|
||||||
if let Some(s) = square.trans(Direction::East) {
|
|
||||||
res.insert(s)
|
|
||||||
};
|
|
||||||
if let Some(s) = square.trans(Direction::West) {
|
|
||||||
res.insert(s)
|
|
||||||
};
|
|
||||||
}
|
|
||||||
res
|
|
||||||
})
|
|
||||||
});
|
|
||||||
|
|
||||||
let king_moves = by_square!(sq, Bitboard(0), {
|
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
loop_all_directions!(d, {
|
loop_all_directions!(d, {
|
||||||
if let Some(x) = sq.trans(d) {
|
if let Some(x) = sq.trans(d) {
|
||||||
|
|
@ -263,9 +142,8 @@ impl Lookup {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Bitboard(res)
|
Bitboard(res)
|
||||||
});
|
});
|
||||||
|
const KNIGHT_MOVES: BySquare<Bitboard> = by_square!(s, Bitboard(0), {
|
||||||
let knight_moves = by_square!(s, Bitboard(0), {
|
|
||||||
let mut res = Bitboard::new();
|
let mut res = Bitboard::new();
|
||||||
if let Some(s) = s.trans(Direction::North) {
|
if let Some(s) = s.trans(Direction::North) {
|
||||||
if let Some(s) = s.trans(Direction::NorthEast) {
|
if let Some(s) = s.trans(Direction::NorthEast) {
|
||||||
|
|
@ -300,40 +178,78 @@ impl Lookup {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
});
|
});
|
||||||
|
const PAWN_ATTACKS: ByColor<BySquare<Bitboard>> = {
|
||||||
let magics = Magics::compute(&rays);
|
by_color!(color, {
|
||||||
|
let direction = match color {
|
||||||
Self {
|
Color::White => Direction::North,
|
||||||
rays,
|
Color::Black => Direction::South,
|
||||||
lines,
|
};
|
||||||
segments,
|
by_square!(square, Bitboard(0), {
|
||||||
pawn_attacks,
|
let mut res = Bitboard(0);
|
||||||
king_moves,
|
if let Some(square) = square.trans(direction) {
|
||||||
knight_moves,
|
if let Some(s) = square.trans(Direction::East) {
|
||||||
magics,
|
res.insert(s)
|
||||||
|
};
|
||||||
|
if let Some(s) = square.trans(Direction::West) {
|
||||||
|
res.insert(s)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
res
|
||||||
|
})
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
|
const fn blocked_ray(square: Square, direction: Direction, blockers: Bitboard) -> Bitboard {
|
||||||
|
let ray = *RAYS.get_const(square).get_const(direction);
|
||||||
|
let blockers = Bitboard(blockers.0 & ray.0);
|
||||||
|
let square = if (direction as u8) < 4 {
|
||||||
|
Bitboard::first(&blockers)
|
||||||
|
} else {
|
||||||
|
Bitboard::last(&blockers)
|
||||||
|
};
|
||||||
|
match square {
|
||||||
|
None => ray,
|
||||||
|
Some(square) => Bitboard(ray.0 & !RAYS.get_const(square).get_const(direction).0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// -------- Magics --------
|
#[inline]
|
||||||
|
pub(crate) fn ray(square: Square, direction: Direction) -> Bitboard {
|
||||||
const BISHOP_SHR: u8 = 55;
|
*RAYS.get(square).get(direction)
|
||||||
const ROOK_SHR: u8 = 52;
|
}
|
||||||
|
#[inline]
|
||||||
const TABLE_SIZE: usize = 267_293;
|
pub(crate) fn line(a: Square, b: Square) -> Bitboard {
|
||||||
|
*LINES.get(a).get(b)
|
||||||
pub(crate) struct Magics {
|
}
|
||||||
bishop: BySquare<Magic>,
|
#[inline]
|
||||||
rook: BySquare<Magic>,
|
pub(crate) fn segment(a: Square, b: Square) -> Bitboard {
|
||||||
table: [Bitboard; TABLE_SIZE],
|
*SEGMENTS.get(a).get(b)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn king(square: Square) -> Bitboard {
|
||||||
|
*KING_MOVES.get(square)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn knight(square: Square) -> Bitboard {
|
||||||
|
*KNIGHT_MOVES.get(square)
|
||||||
|
}
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn pawn_attack(color: Color, square: Square) -> Bitboard {
|
||||||
|
*PAWN_ATTACKS.get(color).get(square)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
/// `role != Pawn`
|
||||||
struct Magic {
|
#[inline]
|
||||||
premask: Bitboard,
|
pub(crate) fn targets(role: Role, from: Square, blockers: Bitboard) -> Bitboard {
|
||||||
factor: u64,
|
match role {
|
||||||
offset: isize,
|
Role::Pawn => unreachable!(),
|
||||||
|
Role::Knight => knight(from),
|
||||||
|
Role::Bishop => bishop(from, blockers),
|
||||||
|
Role::Rook => rook(from, blockers),
|
||||||
|
Role::Queen => bishop(from, blockers) | rook(from, blockers),
|
||||||
|
Role::King => king(from),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! loop_subsets {
|
macro_rules! loop_subsets {
|
||||||
|
|
@ -349,142 +265,176 @@ macro_rules! loop_subsets {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Magics {
|
const fn bishop_premask(square: Square) -> Bitboard {
|
||||||
pub(crate) const fn compute(rays: &Rays) -> Self {
|
|
||||||
let null = Magic {
|
|
||||||
premask: Bitboard(0),
|
|
||||||
factor: 0,
|
|
||||||
offset: 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut table = [Bitboard(0); TABLE_SIZE];
|
|
||||||
let mut len: usize = 0;
|
|
||||||
|
|
||||||
let bishop = by_square!(square, null, {
|
|
||||||
let mut premask = 0;
|
let mut premask = 0;
|
||||||
loop_bishop_directions!(direction, {
|
loop_bishop_directions!(direction, {
|
||||||
premask |= rays.ray(square, direction).0;
|
premask |= RAYS.get_const(square).get_const(direction).0;
|
||||||
});
|
});
|
||||||
premask &= !Rank::First.bitboard().0;
|
premask &= !Rank::First.bitboard().0;
|
||||||
premask &= !Rank::Eighth.bitboard().0;
|
premask &= !Rank::Eighth.bitboard().0;
|
||||||
premask &= !File::A.bitboard().0;
|
premask &= !File::A.bitboard().0;
|
||||||
premask &= !File::H.bitboard().0;
|
premask &= !File::H.bitboard().0;
|
||||||
|
Bitboard(premask)
|
||||||
|
}
|
||||||
|
|
||||||
let factor = bishop_factors(square);
|
const fn rook_premask(square: Square) -> Bitboard {
|
||||||
|
let rays = RAYS.get_const(square);
|
||||||
|
let mut premask = 0;
|
||||||
|
premask |= rays.get_const(Direction::East).0 & !File::H.bitboard().0;
|
||||||
|
premask |= rays.get_const(Direction::North).0 & !Rank::Eighth.bitboard().0;
|
||||||
|
premask |= rays.get_const(Direction::West).0 & !File::A.bitboard().0;
|
||||||
|
premask |= rays.get_const(Direction::South).0 & !Rank::First.bitboard().0;
|
||||||
|
Bitboard(premask)
|
||||||
|
}
|
||||||
|
|
||||||
|
const MAGICS: (BySquare<Magic>, BySquare<Magic>, usize) = {
|
||||||
|
let mut len: usize = 0;
|
||||||
|
(
|
||||||
|
by_square!(
|
||||||
|
square,
|
||||||
|
Magic {
|
||||||
|
premask: Bitboard(0),
|
||||||
|
factor: 0,
|
||||||
|
offset: 0
|
||||||
|
},
|
||||||
|
{
|
||||||
|
let premask = bishop_premask(square).0;
|
||||||
|
let factor = bishop_factor(square);
|
||||||
let mut a = usize::MAX;
|
let mut a = usize::MAX;
|
||||||
|
let mut b = 0;
|
||||||
loop_subsets!(premask, blockers, {
|
loop_subsets!(premask, blockers, {
|
||||||
let cur = hash(BISHOP_SHR, factor, Bitboard(blockers | !premask));
|
let cur = hash(BISHOP_SHR, factor, Bitboard(blockers | !premask));
|
||||||
if cur < a {
|
if cur < a {
|
||||||
a = cur;
|
a = cur;
|
||||||
}
|
}
|
||||||
|
if cur > b {
|
||||||
|
b = cur;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
assert!(b > a);
|
||||||
let offset = len as isize - a as isize;
|
let offset = len as isize - a as isize;
|
||||||
|
len += b - a + 1;
|
||||||
loop_subsets!(premask, blockers, {
|
|
||||||
let index = (offset
|
|
||||||
+ hash(BISHOP_SHR, factor, Bitboard(blockers | !premask)) as isize)
|
|
||||||
as usize;
|
|
||||||
let mut res = 0;
|
|
||||||
loop_bishop_directions!(direction, {
|
|
||||||
res |= rays.blocked(square, direction, Bitboard(blockers)).0;
|
|
||||||
});
|
|
||||||
if table[index].0 != 0 && table[index].0 != res {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
if index >= len {
|
|
||||||
len = index + 1;
|
|
||||||
}
|
|
||||||
table[index] = Bitboard(res);
|
|
||||||
});
|
|
||||||
|
|
||||||
Magic {
|
Magic {
|
||||||
premask: Bitboard(!premask),
|
premask: Bitboard(!premask),
|
||||||
factor,
|
factor,
|
||||||
offset,
|
offset,
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
),
|
||||||
let rook = by_square!(square, null, {
|
by_square!(
|
||||||
let mut premask = 0;
|
square,
|
||||||
premask |= rays.ray(square, Direction::East).0 & !File::H.bitboard().0;
|
Magic {
|
||||||
premask |= rays.ray(square, Direction::North).0 & !Rank::Eighth.bitboard().0;
|
premask: Bitboard(0),
|
||||||
premask |= rays.ray(square, Direction::West).0 & !File::A.bitboard().0;
|
factor: 0,
|
||||||
premask |= rays.ray(square, Direction::South).0 & !Rank::First.bitboard().0;
|
offset: 0
|
||||||
|
},
|
||||||
let factor = rook_factors(square);
|
{
|
||||||
|
let premask = rook_premask(square).0;
|
||||||
|
let factor = rook_factor(square);
|
||||||
let mut a = usize::MAX;
|
let mut a = usize::MAX;
|
||||||
|
let mut b = 0;
|
||||||
loop_subsets!(premask, blockers, {
|
loop_subsets!(premask, blockers, {
|
||||||
let cur = hash(ROOK_SHR, factor, Bitboard(blockers | !premask));
|
let cur = hash(ROOK_SHR, factor, Bitboard(blockers | !premask));
|
||||||
if cur < a {
|
if cur < a {
|
||||||
a = cur;
|
a = cur;
|
||||||
}
|
}
|
||||||
|
if cur > b {
|
||||||
|
b = cur;
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
assert!(b > a);
|
||||||
let offset = len as isize - a as isize;
|
let offset = len as isize - a as isize;
|
||||||
|
len += b - a + 1;
|
||||||
loop_subsets!(premask, blockers, {
|
|
||||||
let index = (offset
|
|
||||||
+ hash(ROOK_SHR, factor, Bitboard(blockers | !premask)) as isize)
|
|
||||||
as usize;
|
|
||||||
let mut res = 0;
|
|
||||||
loop_rook_directions!(direction, {
|
|
||||||
res |= rays.blocked(square, direction, Bitboard(blockers)).0;
|
|
||||||
});
|
|
||||||
if table[index].0 != 0 && table[index].0 != res {
|
|
||||||
panic!()
|
|
||||||
}
|
|
||||||
if index >= len {
|
|
||||||
len = index + 1;
|
|
||||||
}
|
|
||||||
table[index] = Bitboard(res);
|
|
||||||
});
|
|
||||||
|
|
||||||
Magic {
|
Magic {
|
||||||
premask: Bitboard(!premask),
|
premask: Bitboard(!premask),
|
||||||
factor,
|
factor,
|
||||||
offset,
|
offset,
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
),
|
||||||
|
len,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct Magic {
|
||||||
|
premask: Bitboard,
|
||||||
|
factor: u64,
|
||||||
|
offset: isize,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(long_running_const_eval)]
|
||||||
|
const MAGIC_TABLE: [Bitboard; MAGICS.2] = {
|
||||||
|
let mut table = [Bitboard(0); MAGICS.2];
|
||||||
|
by_square!(square, (), {
|
||||||
|
let Magic {
|
||||||
|
premask,
|
||||||
|
factor,
|
||||||
|
offset,
|
||||||
|
} = *MAGICS.0.get_const(square);
|
||||||
|
let premask = !premask.0;
|
||||||
|
loop_subsets!(premask, blockers, {
|
||||||
|
let index = (hash(BISHOP_SHR, factor, Bitboard(blockers | !premask)) as isize + offset)
|
||||||
|
as usize;
|
||||||
|
let mut res = 0;
|
||||||
|
loop_bishop_directions!(direction, {
|
||||||
|
res |= blocked_ray(square, direction, Bitboard(blockers)).0;
|
||||||
});
|
});
|
||||||
|
assert!(table[index].0 == 0 || table[index].0 == res);
|
||||||
|
table[index] = Bitboard(res);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
by_square!(square, (), {
|
||||||
|
let Magic {
|
||||||
|
premask,
|
||||||
|
factor,
|
||||||
|
offset,
|
||||||
|
} = *MAGICS.1.get_const(square);
|
||||||
|
let premask = !premask.0;
|
||||||
|
loop_subsets!(premask, blockers, {
|
||||||
|
let index =
|
||||||
|
(offset + hash(ROOK_SHR, factor, Bitboard(blockers | !premask)) as isize) as usize;
|
||||||
|
let mut res = 0;
|
||||||
|
loop_rook_directions!(direction, {
|
||||||
|
res |= blocked_ray(square, direction, Bitboard(blockers)).0;
|
||||||
|
});
|
||||||
|
assert!(table[index].0 == 0 || table[index].0 == res);
|
||||||
|
table[index] = Bitboard(res);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
table
|
||||||
|
};
|
||||||
|
|
||||||
// if len != TABLE_SIZE {
|
#[inline]
|
||||||
// panic!()
|
pub(crate) fn bishop(square: Square, blockers: Bitboard) -> Bitboard {
|
||||||
// }
|
unsafe { magic_aux(BISHOP_SHR, *(MAGICS.0).get(square), blockers) }
|
||||||
|
}
|
||||||
|
|
||||||
Self {
|
#[inline]
|
||||||
bishop,
|
pub(crate) fn rook(square: Square, blockers: Bitboard) -> Bitboard {
|
||||||
rook,
|
unsafe { magic_aux(ROOK_SHR, *(MAGICS.1).get(square), blockers) }
|
||||||
table,
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn bishop(&self, square: Square, blockers: Bitboard) -> Bitboard {
|
unsafe fn magic_aux(shr: u8, magic: Magic, blockers: Bitboard) -> Bitboard {
|
||||||
unsafe { self.get_unchecked(BISHOP_SHR, *self.bishop.get(square), blockers) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn rook(&self, square: Square, blockers: Bitboard) -> Bitboard {
|
|
||||||
unsafe { self.get_unchecked(ROOK_SHR, *self.rook.get(square), blockers) }
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
unsafe fn get_unchecked(&self, shr: u8, magic: Magic, blockers: Bitboard) -> Bitboard {
|
|
||||||
let Magic {
|
let Magic {
|
||||||
premask,
|
premask,
|
||||||
factor,
|
factor,
|
||||||
offset,
|
offset,
|
||||||
} = magic;
|
} = magic;
|
||||||
debug_assert!(self
|
debug_assert!(MAGIC_TABLE
|
||||||
.table
|
|
||||||
.get(
|
.get(
|
||||||
(hash(shr, factor, blockers | premask) as isize)
|
(hash(shr, factor, blockers | premask) as isize)
|
||||||
.checked_add(offset)
|
.checked_add(offset)
|
||||||
.unwrap() as usize
|
.unwrap() as usize
|
||||||
)
|
)
|
||||||
.is_some());
|
.is_some());
|
||||||
*self.table.get_unchecked(
|
*MAGIC_TABLE.get_unchecked(
|
||||||
((hash(shr, factor, blockers | premask) as isize).unchecked_add(offset)) as usize,
|
((hash(shr, factor, blockers | premask) as isize).unchecked_add(offset)) as usize,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
const fn hash(shr: u8, factor: u64, x: Bitboard) -> usize {
|
||||||
|
(x.0.wrapping_mul(factor) >> shr) as usize
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,7 @@
|
||||||
use crate::bitboard::*;
|
use crate::board::Square;
|
||||||
use crate::board::*;
|
|
||||||
|
|
||||||
pub(crate) const BISHOP_SHR: u8 = 55;
|
pub(crate) const BISHOP_SHR: u8 = 55;
|
||||||
pub(crate) const ROOK_SHR: u8 = 52;
|
pub(crate) const fn bishop_factor(square: Square) -> u64 {
|
||||||
|
|
||||||
pub(crate) const TABLE_SIZE: usize = 0;
|
|
||||||
|
|
||||||
pub(crate) const fn hash(shr: u8, factor: u64, x: Bitboard) -> usize {
|
|
||||||
(x.0.wrapping_mul(factor) >> shr) as usize
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) const fn bishop_factors(square: Square) -> u64 {
|
|
||||||
match square {
|
match square {
|
||||||
Square::A1 => 0x0000404040404040,
|
Square::A1 => 0x0000404040404040,
|
||||||
Square::B1 => 0x0040C100081000E8,
|
Square::B1 => 0x0040C100081000E8,
|
||||||
|
|
@ -79,7 +70,8 @@ pub(crate) const fn bishop_factors(square: Square) -> u64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) const fn rook_factors(square: Square) -> u64 {
|
pub(crate) const ROOK_SHR: u8 = 52;
|
||||||
|
pub(crate) const fn rook_factor(square: Square) -> u64 {
|
||||||
match square {
|
match square {
|
||||||
Square::A1 => 0x002000A28110000C,
|
Square::A1 => 0x002000A28110000C,
|
||||||
Square::B1 => 0x0018000C01060001,
|
Square::B1 => 0x0018000C01060001,
|
||||||
|
|
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
fn main() {
|
|
||||||
eschac::test();
|
|
||||||
}
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
use crate::array_vec::*;
|
use crate::array_vec::*;
|
||||||
use crate::bitboard::*;
|
use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
|
use crate::lookup;
|
||||||
use crate::san::*;
|
use crate::san::*;
|
||||||
use crate::setup::*;
|
use crate::setup::*;
|
||||||
use crate::uci::*;
|
use crate::uci::*;
|
||||||
|
|
@ -229,7 +230,6 @@ impl Position {
|
||||||
/// When possible, this inverts the color to play and removes the en passant square if it
|
/// When possible, this inverts the color to play and removes the en passant square if it
|
||||||
/// exists.
|
/// exists.
|
||||||
pub fn pass(&self) -> Option<Self> {
|
pub fn pass(&self) -> Option<Self> {
|
||||||
let d = &crate::lookup::LOOKUP;
|
|
||||||
let setup = &self.0;
|
let setup = &self.0;
|
||||||
let blockers = setup.p_b_q | setup.n_b_k | setup.r_q_k;
|
let blockers = setup.p_b_q | setup.n_b_k | setup.r_q_k;
|
||||||
let k = setup.n_b_k & setup.r_q_k;
|
let k = setup.n_b_k & setup.r_q_k;
|
||||||
|
|
@ -244,10 +244,10 @@ impl Position {
|
||||||
};
|
};
|
||||||
let king_square = (us & k).next().unwrap();
|
let king_square = (us & k).next().unwrap();
|
||||||
let checkers = them
|
let checkers = them
|
||||||
& (d.pawn_attack(setup.turn, king_square) & p
|
& (lookup::pawn_attack(setup.turn, king_square) & p
|
||||||
| d.knight(king_square) & n
|
| lookup::knight(king_square) & n
|
||||||
| d.bishop(king_square, blockers) & (q | b)
|
| lookup::bishop(king_square, blockers) & (q | b)
|
||||||
| d.rook(king_square, blockers) & (q | r));
|
| lookup::rook(king_square, blockers) & (q | r));
|
||||||
checkers.is_empty().then(|| {
|
checkers.is_empty().then(|| {
|
||||||
Self(Setup {
|
Self(Setup {
|
||||||
turn: !setup.turn,
|
turn: !setup.turn,
|
||||||
|
|
@ -938,7 +938,6 @@ impl Position {
|
||||||
en_passant,
|
en_passant,
|
||||||
castling_rights,
|
castling_rights,
|
||||||
} = self.0;
|
} = self.0;
|
||||||
let d = &crate::lookup::LOOKUP;
|
|
||||||
|
|
||||||
let blockers = p_b_q | n_b_k | r_q_k;
|
let blockers = p_b_q | n_b_k | r_q_k;
|
||||||
let (us, them) = match turn {
|
let (us, them) = match turn {
|
||||||
|
|
@ -968,10 +967,10 @@ impl Position {
|
||||||
|
|
||||||
let forward = turn.forward();
|
let forward = turn.forward();
|
||||||
|
|
||||||
let x = d.bishop(king_square, blockers);
|
let x = lookup::bishop(king_square, blockers);
|
||||||
let y = d.rook(king_square, blockers);
|
let y = lookup::rook(king_square, blockers);
|
||||||
let checkers = d.pawn_attack(turn, king_square) & theirs.pawn()
|
let checkers = lookup::pawn_attack(turn, king_square) & theirs.pawn()
|
||||||
| d.knight(king_square) & theirs.knight()
|
| lookup::knight(king_square) & theirs.knight()
|
||||||
| x & theirs.bishop()
|
| x & theirs.bishop()
|
||||||
| y & theirs.rook();
|
| y & theirs.rook();
|
||||||
|
|
||||||
|
|
@ -980,10 +979,10 @@ impl Position {
|
||||||
let blockers = blockers ^ ours.king();
|
let blockers = blockers ^ ours.king();
|
||||||
theirs
|
theirs
|
||||||
.king()
|
.king()
|
||||||
.map(|sq| d.king(sq))
|
.map(|sq| lookup::king(sq))
|
||||||
.chain(theirs.bishop().map(|sq| d.bishop(sq, blockers)))
|
.chain(theirs.bishop().map(|sq| lookup::bishop(sq, blockers)))
|
||||||
.chain(theirs.rook().map(|sq| d.rook(sq, blockers)))
|
.chain(theirs.rook().map(|sq| lookup::rook(sq, blockers)))
|
||||||
.chain(theirs.knight().map(|sq| d.knight(sq)))
|
.chain(theirs.knight().map(|sq| lookup::knight(sq)))
|
||||||
.chain(std::iter::once(
|
.chain(std::iter::once(
|
||||||
theirs.pawn().trans(!forward).trans(Direction::East),
|
theirs.pawn().trans(!forward).trans(Direction::East),
|
||||||
))
|
))
|
||||||
|
|
@ -994,7 +993,7 @@ impl Position {
|
||||||
};
|
};
|
||||||
// king moves
|
// king moves
|
||||||
visitor.moves(
|
visitor.moves(
|
||||||
(global_mask_to & d.king(king_square) & !us & !attacked).map(|to| RawMove {
|
(global_mask_to & lookup::king(king_square) & !us & !attacked).map(|to| RawMove {
|
||||||
kind: MoveType::KingMove,
|
kind: MoveType::KingMove,
|
||||||
from: king_square,
|
from: king_square,
|
||||||
to,
|
to,
|
||||||
|
|
@ -1052,17 +1051,17 @@ impl Position {
|
||||||
}
|
}
|
||||||
|
|
||||||
let blockers_x_ray = blockers & !(x | y);
|
let blockers_x_ray = blockers & !(x | y);
|
||||||
let pinned_diagonally = (d.bishop(king_square, blockers_x_ray) & theirs.bishop())
|
let pinned_diagonally = (lookup::bishop(king_square, blockers_x_ray) & theirs.bishop())
|
||||||
.map(|sq| d.segment(king_square, sq))
|
.map(|sq| lookup::segment(king_square, sq))
|
||||||
.reduce_or();
|
.reduce_or();
|
||||||
let pinned_horizontally = (d.rook(king_square, blockers_x_ray) & theirs.rook())
|
let pinned_horizontally = (lookup::rook(king_square, blockers_x_ray) & theirs.rook())
|
||||||
.map(|sq| d.segment(king_square, sq))
|
.map(|sq| lookup::segment(king_square, sq))
|
||||||
.reduce_or();
|
.reduce_or();
|
||||||
let pinned = pinned_diagonally | pinned_horizontally;
|
let pinned = pinned_diagonally | pinned_horizontally;
|
||||||
|
|
||||||
let checker = checkers.first();
|
let checker = checkers.first();
|
||||||
let block_check = checker
|
let block_check = checker
|
||||||
.map(|checker| d.segment(king_square, checker))
|
.map(|checker| lookup::segment(king_square, checker))
|
||||||
.unwrap_or(Bitboard(!0));
|
.unwrap_or(Bitboard(!0));
|
||||||
let target_mask = global_mask_to & block_check;
|
let target_mask = global_mask_to & block_check;
|
||||||
|
|
||||||
|
|
@ -1105,7 +1104,7 @@ impl Position {
|
||||||
// pawn attacks kingside
|
// pawn attacks kingside
|
||||||
{
|
{
|
||||||
let targets =
|
let targets =
|
||||||
(global_mask_from & ours.pawn() & (!pinned | d.ray(king_square, kside)))
|
(global_mask_from & ours.pawn() & (!pinned | lookup::ray(king_square, kside)))
|
||||||
.trans(kside)
|
.trans(kside)
|
||||||
& them
|
& them
|
||||||
& target_mask;
|
& target_mask;
|
||||||
|
|
@ -1127,7 +1126,7 @@ impl Position {
|
||||||
// pawn attacks queenside
|
// pawn attacks queenside
|
||||||
{
|
{
|
||||||
let targets =
|
let targets =
|
||||||
(global_mask_from & ours.pawn() & (!pinned | d.ray(king_square, qside)))
|
(global_mask_from & ours.pawn() & (!pinned | lookup::ray(king_square, qside)))
|
||||||
.trans(qside)
|
.trans(qside)
|
||||||
& them
|
& them
|
||||||
& target_mask;
|
& target_mask;
|
||||||
|
|
@ -1167,14 +1166,18 @@ impl Position {
|
||||||
if block_check.contains(to)
|
if block_check.contains(to)
|
||||||
|| checker.is_none_or(|checker| checker == capture_square)
|
|| checker.is_none_or(|checker| checker == capture_square)
|
||||||
{
|
{
|
||||||
let candidates = d.pawn_attack(!turn, to) & ours.pawn();
|
let candidates = lookup::pawn_attack(!turn, to) & ours.pawn();
|
||||||
let blockers = blockers ^ capture_square.bitboard();
|
let blockers = blockers ^ capture_square.bitboard();
|
||||||
let pinned = pinned
|
let pinned = pinned
|
||||||
| (d.rook(king_square, blockers & !(d.rook(king_square, blockers)))
|
| (lookup::rook(
|
||||||
& theirs.rook())
|
king_square,
|
||||||
.map(|sq| d.segment(king_square, sq))
|
blockers & !(lookup::rook(king_square, blockers)),
|
||||||
|
) & theirs.rook())
|
||||||
|
.map(|sq| lookup::segment(king_square, sq))
|
||||||
.reduce_or();
|
.reduce_or();
|
||||||
(global_mask_from & candidates & (!pinned | d.segment(king_square, to)))
|
(global_mask_from
|
||||||
|
& candidates
|
||||||
|
& (!pinned | lookup::segment(king_square, to)))
|
||||||
.for_each(|from| {
|
.for_each(|from| {
|
||||||
visitor.en_passant_is_legal();
|
visitor.en_passant_is_legal();
|
||||||
visitor.moves(std::iter::once(RawMove {
|
visitor.moves(std::iter::once(RawMove {
|
||||||
|
|
@ -1194,11 +1197,13 @@ impl Position {
|
||||||
let aux = |visitor: &mut T, role| {
|
let aux = |visitor: &mut T, role| {
|
||||||
for from in global_mask_from & *ours.get(role) & !pinned {
|
for from in global_mask_from & *ours.get(role) & !pinned {
|
||||||
visitor.moves(
|
visitor.moves(
|
||||||
(d.targets(role, from, blockers) & !us & target_mask).map(|to| RawMove {
|
(lookup::targets(role, from, blockers) & !us & target_mask).map(|to| {
|
||||||
|
RawMove {
|
||||||
kind: MoveType::PieceMove,
|
kind: MoveType::PieceMove,
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
role,
|
role,
|
||||||
|
}
|
||||||
}),
|
}),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
@ -1227,14 +1232,14 @@ impl Position {
|
||||||
let aux = |visitor: &mut T, role, pinned_mask| {
|
let aux = |visitor: &mut T, role, pinned_mask| {
|
||||||
for from in global_mask_from & *ours.get(role) & pinned_mask {
|
for from in global_mask_from & *ours.get(role) & pinned_mask {
|
||||||
visitor.moves(
|
visitor.moves(
|
||||||
(global_mask_to & pinned & !us & d.line(king_square, from)).map(|to| {
|
(global_mask_to & pinned & !us & lookup::line(king_square, from)).map(
|
||||||
RawMove {
|
|to| RawMove {
|
||||||
kind: MoveType::PieceMove,
|
kind: MoveType::PieceMove,
|
||||||
from,
|
from,
|
||||||
to,
|
to,
|
||||||
role,
|
role,
|
||||||
}
|
},
|
||||||
}),
|
),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
14
src/setup.rs
14
src/setup.rs
|
|
@ -4,6 +4,7 @@
|
||||||
|
|
||||||
use crate::bitboard::*;
|
use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
|
use crate::lookup;
|
||||||
use crate::position::*;
|
use crate::position::*;
|
||||||
|
|
||||||
/// **A builder type for chess positions.**
|
/// **A builder type for chess positions.**
|
||||||
|
|
@ -346,7 +347,6 @@ impl Setup {
|
||||||
debug_assert!((self.p_b_q & self.n_b_k & self.r_q_k).is_empty());
|
debug_assert!((self.p_b_q & self.n_b_k & self.r_q_k).is_empty());
|
||||||
|
|
||||||
let mut reasons = IllegalPositionReasons::new();
|
let mut reasons = IllegalPositionReasons::new();
|
||||||
let d = &crate::lookup::LOOKUP;
|
|
||||||
|
|
||||||
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.pieces();
|
let pieces = self.pieces();
|
||||||
|
|
@ -367,13 +367,13 @@ impl Setup {
|
||||||
|
|
||||||
if pieces.get(!self.turn).king().any(|enemy_king| {
|
if pieces.get(!self.turn).king().any(|enemy_king| {
|
||||||
let pieces = pieces.get(self.turn);
|
let pieces = pieces.get(self.turn);
|
||||||
!(d.king(enemy_king) & *pieces.get(Role::King)
|
!(lookup::king(enemy_king) & *pieces.get(Role::King)
|
||||||
| d.bishop(enemy_king, blockers)
|
| lookup::bishop(enemy_king, blockers)
|
||||||
& (*pieces.get(Role::Queen) | *pieces.get(Role::Bishop))
|
& (*pieces.get(Role::Queen) | *pieces.get(Role::Bishop))
|
||||||
| d.rook(enemy_king, blockers)
|
| lookup::rook(enemy_king, blockers)
|
||||||
& (*pieces.get(Role::Queen) | *pieces.get(Role::Rook))
|
& (*pieces.get(Role::Queen) | *pieces.get(Role::Rook))
|
||||||
| d.knight(enemy_king) & *pieces.get(Role::Knight)
|
| lookup::knight(enemy_king) & *pieces.get(Role::Knight)
|
||||||
| d.pawn_attack(!self.turn, enemy_king) & *pieces.get(Role::Pawn))
|
| lookup::pawn_attack(!self.turn, enemy_king) & *pieces.get(Role::Pawn))
|
||||||
.is_empty()
|
.is_empty()
|
||||||
}) {
|
}) {
|
||||||
reasons.add(IllegalPositionReason::HangingKing);
|
reasons.add(IllegalPositionReason::HangingKing);
|
||||||
|
|
@ -449,7 +449,7 @@ impl Setup {
|
||||||
.king()
|
.king()
|
||||||
.first()
|
.first()
|
||||||
.is_some_and(|king_square| {
|
.is_some_and(|king_square| {
|
||||||
!(d.bishop(king_square, blockers)
|
!(lookup::bishop(king_square, blockers)
|
||||||
& (pieces.get(!self.turn).queen() | pieces.get(!self.turn).bishop()))
|
& (pieces.get(!self.turn).queen() | pieces.get(!self.turn).bishop()))
|
||||||
.is_empty()
|
.is_empty()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue