wip: const lookup
This commit is contained in:
parent
bbe65b74f4
commit
7c776ff874
9 changed files with 471 additions and 379 deletions
|
|
@ -7,24 +7,25 @@ use std::iter::FusedIterator;
|
||||||
|
|
||||||
/// A set of squares.
|
/// A set of squares.
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||||
|
#[repr(transparent)]
|
||||||
pub struct Bitboard(pub(crate) u64);
|
pub struct Bitboard(pub(crate) u64);
|
||||||
|
|
||||||
impl Bitboard {
|
impl Bitboard {
|
||||||
/// Returns an empty bitboard.
|
/// Returns an empty bitboard.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new() -> Self {
|
pub const fn new() -> Self {
|
||||||
Self(0)
|
Self(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if the bitboard is empty.
|
/// Returns `true` if the bitboard is empty.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_empty(&self) -> bool {
|
pub const fn is_empty(&self) -> bool {
|
||||||
self.0 == 0
|
self.0 == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the square in the bitboard with the smallest index.
|
/// Returns the square in the bitboard with the smallest index.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn first(&self) -> Option<Square> {
|
pub const fn first(&self) -> Option<Square> {
|
||||||
let mask = self.0;
|
let mask = self.0;
|
||||||
match mask {
|
match mask {
|
||||||
0 => None,
|
0 => None,
|
||||||
|
|
@ -32,10 +33,21 @@ impl Bitboard {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(crate) const fn last(&self) -> Option<Square> {
|
||||||
|
let mask = self.0;
|
||||||
|
match mask {
|
||||||
|
0 => None,
|
||||||
|
_ => Some(unsafe {
|
||||||
|
Square::new_unchecked(63_u8.unchecked_sub(mask.leading_zeros() as u8))
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Removes the square in the bitboard with the smallest index and returns it, or `None` if the
|
/// Removes the square in the bitboard with the smallest index and returns it, or `None` if the
|
||||||
/// bitboard is empty.
|
/// bitboard is empty.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn pop(&mut self) -> Option<Square> {
|
pub const fn pop(&mut self) -> Option<Square> {
|
||||||
let Self(ref mut mask) = self;
|
let Self(ref mut mask) = self;
|
||||||
let square = match mask {
|
let square = match mask {
|
||||||
0 => None,
|
0 => None,
|
||||||
|
|
@ -47,13 +59,13 @@ impl Bitboard {
|
||||||
|
|
||||||
/// Inserts a square in the bitboard.
|
/// Inserts a square in the bitboard.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn insert(&mut self, square: Square) {
|
pub const 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.
|
/// Returns `true` if the bitboard contains the given square.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn contains(&self, square: Square) -> bool {
|
pub const fn contains(&self, square: Square) -> bool {
|
||||||
self.0 & (1 << square as u8) != 0
|
self.0 & (1 << square as u8) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
76
src/board.rs
76
src/board.rs
|
|
@ -5,6 +5,7 @@ use crate::bitboard::*;
|
||||||
macro_rules! container {
|
macro_rules! container {
|
||||||
($v: vis, $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)]
|
||||||
|
#[repr(transparent)]
|
||||||
$v 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> {
|
||||||
|
|
@ -116,7 +117,7 @@ impl File {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new_unchecked(index: u8) -> Self {
|
pub const unsafe fn new_unchecked(index: u8) -> Self {
|
||||||
debug_assert!(index < 8);
|
debug_assert!(index < 8);
|
||||||
std::mem::transmute(index)
|
std::mem::transmute(index)
|
||||||
}
|
}
|
||||||
|
|
@ -147,7 +148,7 @@ impl File {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bitboard(self) -> Bitboard {
|
pub const fn bitboard(self) -> Bitboard {
|
||||||
Bitboard(0x0101010101010101 << (self as u8))
|
Bitboard(0x0101010101010101 << (self as u8))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -196,7 +197,7 @@ impl Rank {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new_unchecked(index: u8) -> Self {
|
pub const unsafe fn new_unchecked(index: u8) -> Self {
|
||||||
debug_assert!(index < 8);
|
debug_assert!(index < 8);
|
||||||
std::mem::transmute(index)
|
std::mem::transmute(index)
|
||||||
}
|
}
|
||||||
|
|
@ -232,7 +233,7 @@ impl Rank {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bitboard(self) -> Bitboard {
|
pub const fn bitboard(self) -> Bitboard {
|
||||||
Bitboard(0xFF << ((self as u8) << 3))
|
Bitboard(0xFF << ((self as u8) << 3))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -278,12 +279,16 @@ impl Square {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new(index: u8) -> Option<Self> {
|
pub const fn new(index: u8) -> Option<Self> {
|
||||||
(index < 64).then(|| unsafe { Self::new_unchecked(index) })
|
if index < 64 {
|
||||||
|
Some(unsafe { Self::new_unchecked(index) })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub unsafe fn new_unchecked(index: u8) -> Self {
|
pub const unsafe fn new_unchecked(index: u8) -> Self {
|
||||||
debug_assert!(index < 64);
|
debug_assert!(index < 64);
|
||||||
std::mem::transmute(index)
|
std::mem::transmute(index)
|
||||||
}
|
}
|
||||||
|
|
@ -294,12 +299,12 @@ impl Square {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn file(self) -> File {
|
pub const fn file(self) -> File {
|
||||||
unsafe { File::new_unchecked((self as u8) & 7) }
|
unsafe { File::new_unchecked((self as u8) & 7) }
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn rank(self) -> Rank {
|
pub const fn rank(self) -> Rank {
|
||||||
unsafe { Rank::new_unchecked((self as u8) >> 3) }
|
unsafe { Rank::new_unchecked((self as u8) >> 3) }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -310,7 +315,7 @@ impl Square {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn bitboard(self) -> Bitboard {
|
pub const fn bitboard(self) -> Bitboard {
|
||||||
Bitboard(1 << self as u8)
|
Bitboard(1 << self as u8)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -347,16 +352,17 @@ impl Square {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn trans(self, direction: Direction) -> Option<Self> {
|
pub(crate) const fn trans(self, direction: Direction) -> Option<Self> {
|
||||||
self.check_trans(direction).then(|| unsafe {
|
if self.check_trans(direction) {
|
||||||
// SAFETY: condition is checked before doing the translation
|
Some(unsafe { self.trans_unchecked(direction) })
|
||||||
self.trans_unchecked(direction)
|
} else {
|
||||||
})
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// SAFETY: the translation must not move the square outside the board
|
/// SAFETY: the translation must not move the square outside the board
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) unsafe fn trans_unchecked(self, direction: Direction) -> Self {
|
pub(crate) const unsafe fn trans_unchecked(self, direction: Direction) -> Self {
|
||||||
debug_assert!(self.check_trans(direction));
|
debug_assert!(self.check_trans(direction));
|
||||||
let i = self as u8;
|
let i = self as u8;
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
@ -375,16 +381,18 @@ impl Square {
|
||||||
|
|
||||||
/// Returns `false` if the translation would move the square outside the board
|
/// Returns `false` if the translation would move the square outside the board
|
||||||
#[inline]
|
#[inline]
|
||||||
fn check_trans(self, direction: Direction) -> bool {
|
const fn check_trans(self, direction: Direction) -> bool {
|
||||||
|
let file = self.file() as u8;
|
||||||
|
let rank = self.rank() as u8;
|
||||||
match direction {
|
match direction {
|
||||||
Direction::East => self.file() < File::H,
|
Direction::East => file < 7,
|
||||||
Direction::NorthEast => self.file() < File::H && self.rank() < Rank::Eighth,
|
Direction::NorthEast => file < 7 && rank < 7,
|
||||||
Direction::North => self.rank() < Rank::Eighth,
|
Direction::North => rank < 7,
|
||||||
Direction::NorthWest => self.file() > File::A && self.rank() < Rank::Eighth,
|
Direction::NorthWest => file > 0 && rank < 7,
|
||||||
Direction::SouthEast => self.file() < File::H && self.rank() > Rank::First,
|
Direction::SouthEast => file < 7 && rank > 0,
|
||||||
Direction::South => self.rank() > Rank::First,
|
Direction::South => rank > 0,
|
||||||
Direction::SouthWest => self.file() > File::A && self.rank() > Rank::First,
|
Direction::SouthWest => file > 0 && rank > 0,
|
||||||
Direction::West => self.file() > File::A,
|
Direction::West => file > 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -525,6 +533,7 @@ impl Role {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
|
||||||
|
#[repr(transparent)]
|
||||||
pub 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> {
|
||||||
|
|
@ -615,9 +624,18 @@ impl Direction {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn transmute(value: u8) -> Self {
|
pub const fn new(index: u8) -> Option<Self> {
|
||||||
debug_assert!(value < 8);
|
if index < 8 {
|
||||||
std::mem::transmute(value)
|
Some(unsafe { Self::new_unchecked(index) })
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub const unsafe fn new_unchecked(index: u8) -> Self {
|
||||||
|
debug_assert!(index < 8);
|
||||||
|
std::mem::transmute(index)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -625,7 +643,7 @@ impl std::ops::Not for Direction {
|
||||||
type Output = Self;
|
type Output = Self;
|
||||||
#[inline]
|
#[inline]
|
||||||
fn not(self) -> Self::Output {
|
fn not(self) -> Self::Output {
|
||||||
unsafe { Self::transmute(self as u8 ^ 0b111) }
|
unsafe { Self::new_unchecked(self as u8 ^ 0b111) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
11
src/lib.rs
11
src/lib.rs
|
|
@ -73,12 +73,11 @@
|
||||||
//! - etc.
|
//! - etc.
|
||||||
|
|
||||||
pub(crate) mod array_vec;
|
pub(crate) mod array_vec;
|
||||||
|
pub(crate) mod lookup;
|
||||||
pub(crate) mod magics;
|
pub(crate) mod magics;
|
||||||
pub(crate) mod rays;
|
|
||||||
|
|
||||||
pub mod bitboard;
|
pub mod bitboard;
|
||||||
pub mod board;
|
pub mod board;
|
||||||
pub mod lookup;
|
|
||||||
pub mod position;
|
pub mod position;
|
||||||
pub mod san;
|
pub mod san;
|
||||||
pub mod setup;
|
pub mod setup;
|
||||||
|
|
@ -88,3 +87,11 @@ 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}");
|
||||||
|
}
|
||||||
|
|
|
||||||
420
src/lookup.rs
420
src/lookup.rs
|
|
@ -1,20 +1,146 @@
|
||||||
//! Lookup tables initialisation.
|
|
||||||
//!
|
|
||||||
//! Move generation in eschac requires about 1MB of precomputed lookup tables.
|
|
||||||
|
|
||||||
use crate::bitboard::*;
|
use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
use crate::magics::*;
|
use crate::magics::*;
|
||||||
use crate::rays::*;
|
|
||||||
|
|
||||||
pub(crate) use init::InitialisedLookup;
|
#[allow(long_running_const_eval)]
|
||||||
|
pub(crate) static LOOKUP: Lookup = Lookup::compute();
|
||||||
|
|
||||||
/// Forces the initialisation of the lookup tables.
|
macro_rules! loop_bishop_directions {
|
||||||
///
|
($d: ident, $body: expr) => {{
|
||||||
/// It is not necessary to call this function, as lookup tables are initialised lazily, but it can
|
{
|
||||||
/// be used to ensure that they are initialised before a given time.
|
let $d = Direction::NorthEast;
|
||||||
pub fn init() {
|
$body
|
||||||
InitialisedLookup::init();
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::NorthWest;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::SouthWest;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::SouthEast;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! loop_rook_directions {
|
||||||
|
($d: ident, $body: expr) => {{
|
||||||
|
{
|
||||||
|
let $d = Direction::East;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::North;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::West;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::South;
|
||||||
|
$body
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! loop_all_directions {
|
||||||
|
($d: ident, $body: expr) => {{
|
||||||
|
loop_bishop_directions!($d, $body);
|
||||||
|
loop_rook_directions!($d, $body);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! by_color {
|
||||||
|
($c: ident, $body: expr) => {{
|
||||||
|
ByColor([
|
||||||
|
{
|
||||||
|
let $c = Color::White;
|
||||||
|
$body
|
||||||
|
},
|
||||||
|
{
|
||||||
|
let $c = Color::Black;
|
||||||
|
$body
|
||||||
|
},
|
||||||
|
])
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! by_square {
|
||||||
|
($sq: ident, $init: expr, $body: expr) => {{
|
||||||
|
let mut res = [$init; 64];
|
||||||
|
let mut $sq: u8 = 0;
|
||||||
|
while $sq < 64 {
|
||||||
|
res[$sq as usize] = {
|
||||||
|
let $sq = Square::new($sq).unwrap();
|
||||||
|
$body
|
||||||
|
};
|
||||||
|
$sq += 1;
|
||||||
|
}
|
||||||
|
BySquare(res)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! by_direction {
|
||||||
|
($d: ident, $body: expr) => {{
|
||||||
|
let mut res = [Bitboard(0); 8];
|
||||||
|
let mut $d: u8 = 0;
|
||||||
|
while $d < 8 {
|
||||||
|
res[$d as usize] = {
|
||||||
|
let $d = Direction::new($d).unwrap();
|
||||||
|
$body
|
||||||
|
};
|
||||||
|
$d += 1;
|
||||||
|
}
|
||||||
|
ByDirection(res)
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) struct Rays(BySquare<ByDirection<Bitboard>>);
|
||||||
|
|
||||||
|
impl Rays {
|
||||||
|
pub(crate) const fn new() -> Self {
|
||||||
|
Self(by_square!(
|
||||||
|
square,
|
||||||
|
ByDirection([Bitboard(0); 8]),
|
||||||
|
by_direction!(direction, {
|
||||||
|
let mut square = square;
|
||||||
|
let mut res = 0;
|
||||||
|
while let Some(x) = square.trans(direction) {
|
||||||
|
square = x;
|
||||||
|
res |= square.bitboard().0;
|
||||||
|
}
|
||||||
|
Bitboard(res)
|
||||||
|
})
|
||||||
|
))
|
||||||
|
}
|
||||||
|
|
||||||
|
#[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 {
|
pub(crate) struct Lookup {
|
||||||
|
|
@ -81,75 +207,97 @@ impl Lookup {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn compute() -> Self {
|
const fn compute() -> Self {
|
||||||
let rays = Rays::new();
|
let rays = Rays::new();
|
||||||
|
|
||||||
let lines = BySquare::new(|a| {
|
let lines = by_square!(a, BySquare([Bitboard(0); 64]), {
|
||||||
BySquare::new(|b| {
|
by_square!(b, Bitboard(0), {
|
||||||
for d in Direction::all() {
|
let mut res = Bitboard(0);
|
||||||
|
loop_all_directions!(d, {
|
||||||
let r = rays.ray(a, d);
|
let r = rays.ray(a, d);
|
||||||
if r.contains(b) {
|
if r.contains(b) {
|
||||||
return r;
|
res = r;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
Bitboard::new()
|
res
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let segments = BySquare::new(|a| {
|
let segments: BySquare<BySquare<Bitboard>> = by_square!(a, BySquare([Bitboard(0); 64]), {
|
||||||
BySquare::new(|b| {
|
by_square!(b, Bitboard(0), {
|
||||||
for d in Direction::all() {
|
let mut res = 0;
|
||||||
|
loop_all_directions!(d, {
|
||||||
let r = rays.ray(a, d);
|
let r = rays.ray(a, d);
|
||||||
if r.contains(b) {
|
if r.contains(b) {
|
||||||
return r & !rays.ray(b, d);
|
res = r.0 & !rays.ray(b, d).0;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
b.bitboard()
|
Bitboard(res)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let pawn_attacks = ByColor::new(|color| {
|
let pawn_attacks = by_color!(color, {
|
||||||
let direction = match color {
|
let direction = match color {
|
||||||
Color::White => Direction::North,
|
Color::White => Direction::North,
|
||||||
Color::Black => Direction::South,
|
Color::Black => Direction::South,
|
||||||
};
|
};
|
||||||
BySquare::new(|square| {
|
by_square!(square, Bitboard(0), {
|
||||||
let mut res = Bitboard::new();
|
let mut res = Bitboard(0);
|
||||||
if let Some(square) = square.trans(direction) {
|
if let Some(square) = square.trans(direction) {
|
||||||
square.trans(Direction::East).map(|s| res.insert(s));
|
if let Some(s) = square.trans(Direction::East) {
|
||||||
square.trans(Direction::West).map(|s| res.insert(s));
|
res.insert(s)
|
||||||
|
};
|
||||||
|
if let Some(s) = square.trans(Direction::West) {
|
||||||
|
res.insert(s)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
let king_moves = BySquare::new(|square| {
|
let king_moves = by_square!(sq, Bitboard(0), {
|
||||||
let mut res = Bitboard::new();
|
let mut res = 0;
|
||||||
for direction in Direction::all() {
|
loop_all_directions!(d, {
|
||||||
if let Some(x) = square.trans(direction) {
|
if let Some(x) = sq.trans(d) {
|
||||||
res |= x.bitboard();
|
res |= x.bitboard().0;
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
res
|
Bitboard(res)
|
||||||
});
|
});
|
||||||
|
|
||||||
let knight_moves = BySquare::new(|s| {
|
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) {
|
||||||
s.trans(Direction::NorthEast).map(|s| res.insert(s));
|
if let Some(s) = s.trans(Direction::NorthEast) {
|
||||||
s.trans(Direction::NorthWest).map(|s| res.insert(s));
|
res.insert(s);
|
||||||
|
}
|
||||||
|
if let Some(s) = s.trans(Direction::NorthWest) {
|
||||||
|
res.insert(s)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if let Some(s) = s.trans(Direction::West) {
|
if let Some(s) = s.trans(Direction::West) {
|
||||||
s.trans(Direction::NorthWest).map(|s| res.insert(s));
|
if let Some(s) = s.trans(Direction::NorthWest) {
|
||||||
s.trans(Direction::SouthWest).map(|s| res.insert(s));
|
res.insert(s)
|
||||||
|
};
|
||||||
|
if let Some(s) = s.trans(Direction::SouthWest) {
|
||||||
|
res.insert(s)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if let Some(s) = s.trans(Direction::South) {
|
if let Some(s) = s.trans(Direction::South) {
|
||||||
s.trans(Direction::SouthWest).map(|s| res.insert(s));
|
if let Some(s) = s.trans(Direction::SouthWest) {
|
||||||
s.trans(Direction::SouthEast).map(|s| res.insert(s));
|
res.insert(s)
|
||||||
|
};
|
||||||
|
if let Some(s) = s.trans(Direction::SouthEast) {
|
||||||
|
res.insert(s)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
if let Some(s) = s.trans(Direction::East) {
|
if let Some(s) = s.trans(Direction::East) {
|
||||||
s.trans(Direction::SouthEast).map(|s| res.insert(s));
|
if let Some(s) = s.trans(Direction::SouthEast) {
|
||||||
s.trans(Direction::NorthEast).map(|s| res.insert(s));
|
res.insert(s)
|
||||||
|
};
|
||||||
|
if let Some(s) = s.trans(Direction::NorthEast) {
|
||||||
|
res.insert(s)
|
||||||
|
};
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
});
|
});
|
||||||
|
|
@ -168,35 +316,175 @@ impl Lookup {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod init {
|
// -------- Magics --------
|
||||||
use std::{mem::MaybeUninit, sync::LazyLock};
|
|
||||||
|
|
||||||
use super::Lookup;
|
const BISHOP_SHR: u8 = 55;
|
||||||
|
const ROOK_SHR: u8 = 52;
|
||||||
|
|
||||||
static mut LOOKUP: MaybeUninit<Lookup> = MaybeUninit::uninit();
|
const TABLE_SIZE: usize = 267_293;
|
||||||
|
|
||||||
#[allow(static_mut_refs)]
|
pub(crate) struct Magics {
|
||||||
static INIT: LazyLock<()> = LazyLock::new(|| unsafe {
|
bishop: BySquare<Magic>,
|
||||||
LOOKUP.write(Lookup::compute());
|
rook: BySquare<Magic>,
|
||||||
|
table: [Bitboard; TABLE_SIZE],
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy)]
|
||||||
|
struct Magic {
|
||||||
|
premask: Bitboard,
|
||||||
|
factor: u64,
|
||||||
|
offset: isize,
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! loop_subsets {
|
||||||
|
($premask: ident, $subset: ident, $body: expr) => {{
|
||||||
|
let mut $subset: u64 = 0;
|
||||||
|
loop {
|
||||||
|
$subset = $subset.wrapping_sub($premask) & $premask;
|
||||||
|
$body
|
||||||
|
if $subset == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Magics {
|
||||||
|
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;
|
||||||
|
loop_bishop_directions!(direction, {
|
||||||
|
premask |= rays.ray(square, direction).0;
|
||||||
|
});
|
||||||
|
premask &= !Rank::First.bitboard().0;
|
||||||
|
premask &= !Rank::Eighth.bitboard().0;
|
||||||
|
premask &= !File::A.bitboard().0;
|
||||||
|
premask &= !File::H.bitboard().0;
|
||||||
|
|
||||||
|
let factor = bishop_factors(square);
|
||||||
|
|
||||||
|
let mut a = usize::MAX;
|
||||||
|
loop_subsets!(premask, blockers, {
|
||||||
|
let cur = hash(BISHOP_SHR, factor, Bitboard(blockers | !premask));
|
||||||
|
if cur < a {
|
||||||
|
a = cur;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let offset = len as isize - a as isize;
|
||||||
|
|
||||||
|
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);
|
||||||
});
|
});
|
||||||
|
|
||||||
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
Magic {
|
||||||
pub(crate) struct InitialisedLookup(());
|
premask: Bitboard(!premask),
|
||||||
|
factor,
|
||||||
|
offset,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
impl InitialisedLookup {
|
let rook = by_square!(square, null, {
|
||||||
#[inline]
|
let mut premask = 0;
|
||||||
pub(crate) fn init() -> Self {
|
premask |= rays.ray(square, Direction::East).0 & !File::H.bitboard().0;
|
||||||
LazyLock::force(&INIT);
|
premask |= rays.ray(square, Direction::North).0 & !Rank::Eighth.bitboard().0;
|
||||||
Self(())
|
premask |= rays.ray(square, Direction::West).0 & !File::A.bitboard().0;
|
||||||
|
premask |= rays.ray(square, Direction::South).0 & !Rank::First.bitboard().0;
|
||||||
|
|
||||||
|
let factor = rook_factors(square);
|
||||||
|
|
||||||
|
let mut a = usize::MAX;
|
||||||
|
loop_subsets!(premask, blockers, {
|
||||||
|
let cur = hash(ROOK_SHR, factor, Bitboard(blockers | !premask));
|
||||||
|
if cur < a {
|
||||||
|
a = cur;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
let offset = len as isize - a as isize;
|
||||||
|
|
||||||
|
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 {
|
||||||
|
premask: Bitboard(!premask),
|
||||||
|
factor,
|
||||||
|
offset,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// if len != TABLE_SIZE {
|
||||||
|
// panic!()
|
||||||
|
// }
|
||||||
|
|
||||||
|
Self {
|
||||||
|
bishop,
|
||||||
|
rook,
|
||||||
|
table,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::ops::Deref for InitialisedLookup {
|
|
||||||
type Target = Lookup;
|
|
||||||
#[allow(static_mut_refs)]
|
|
||||||
#[inline]
|
#[inline]
|
||||||
fn deref(&self) -> &Self::Target {
|
pub(crate) fn bishop(&self, square: Square, blockers: Bitboard) -> Bitboard {
|
||||||
unsafe { LOOKUP.assume_init_ref() }
|
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 {
|
||||||
|
premask,
|
||||||
|
factor,
|
||||||
|
offset,
|
||||||
|
} = magic;
|
||||||
|
debug_assert!(self
|
||||||
|
.table
|
||||||
|
.get(
|
||||||
|
(hash(shr, factor, blockers | premask) as isize)
|
||||||
|
.checked_add(offset)
|
||||||
|
.unwrap() as usize
|
||||||
|
)
|
||||||
|
.is_some());
|
||||||
|
*self.table.get_unchecked(
|
||||||
|
((hash(shr, factor, blockers | premask) as isize).unchecked_add(offset)) as usize,
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
189
src/magics.rs
189
src/magics.rs
|
|
@ -1,193 +1,16 @@
|
||||||
use crate::bitboard::*;
|
use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
use crate::rays::Rays;
|
|
||||||
|
|
||||||
const BISHOP_SHR: u8 = 55;
|
pub(crate) const BISHOP_SHR: u8 = 55;
|
||||||
const ROOK_SHR: u8 = 52;
|
pub(crate) const ROOK_SHR: u8 = 52;
|
||||||
|
|
||||||
pub(crate) struct Magics {
|
pub(crate) const TABLE_SIZE: usize = 0;
|
||||||
bishop: BySquare<Magic>,
|
|
||||||
rook: BySquare<Magic>,
|
|
||||||
table: Box<[Bitboard]>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Copy)]
|
pub(crate) const fn hash(shr: u8, factor: u64, x: Bitboard) -> usize {
|
||||||
struct Magic {
|
|
||||||
premask: Bitboard,
|
|
||||||
factor: u64,
|
|
||||||
offset: isize,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl Magics {
|
|
||||||
pub(crate) fn compute(rays: &Rays) -> Self {
|
|
||||||
let mut data = Vec::new();
|
|
||||||
|
|
||||||
let mut aux =
|
|
||||||
|shr,
|
|
||||||
factors: fn(Square) -> u64,
|
|
||||||
make_table: fn(&Rays, Square) -> (Bitboard, Vec<(Bitboard, Bitboard)>)| {
|
|
||||||
BySquare::new(|square| {
|
|
||||||
let (premask, table) = make_table(rays, square);
|
|
||||||
let factor = factors(square);
|
|
||||||
let offset = fill_table(&mut data, shr, factor, table);
|
|
||||||
Magic {
|
|
||||||
premask,
|
|
||||||
factor,
|
|
||||||
offset,
|
|
||||||
}
|
|
||||||
})
|
|
||||||
};
|
|
||||||
|
|
||||||
let bishop = aux(BISHOP_SHR, bishop_factors, make_bishop_table);
|
|
||||||
let rook = aux(ROOK_SHR, rook_factors, make_rook_table);
|
|
||||||
|
|
||||||
let mut table = Box::new_uninit_slice(data.len());
|
|
||||||
for (i, entry) in data.into_iter().enumerate() {
|
|
||||||
table[i].write(entry);
|
|
||||||
}
|
|
||||||
|
|
||||||
Self {
|
|
||||||
bishop,
|
|
||||||
rook,
|
|
||||||
table: unsafe { table.assume_init() },
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn bishop(&self, square: Square, 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 {
|
|
||||||
premask,
|
|
||||||
factor,
|
|
||||||
offset,
|
|
||||||
} = magic;
|
|
||||||
*self.table.get_unchecked(
|
|
||||||
((hash(shr, factor, blockers | premask) as isize).unchecked_add(offset)) as usize,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn fill_table(
|
|
||||||
data: &mut Vec<Bitboard>,
|
|
||||||
shr: u8,
|
|
||||||
factor: u64,
|
|
||||||
table: Vec<(Bitboard, Bitboard)>,
|
|
||||||
) -> isize {
|
|
||||||
let offset = data.len() as isize
|
|
||||||
- table
|
|
||||||
.iter()
|
|
||||||
.map(|(x, _)| hash(shr, factor, *x) as isize)
|
|
||||||
.min()
|
|
||||||
.unwrap();
|
|
||||||
for (x, y) in &table {
|
|
||||||
let i = (hash(shr, factor, *x) as isize + offset) as usize;
|
|
||||||
while data.len() <= i {
|
|
||||||
data.push(Bitboard::new());
|
|
||||||
}
|
|
||||||
if data[i] != Bitboard::new() && data[i] != *y {
|
|
||||||
panic!();
|
|
||||||
}
|
|
||||||
data[i] = *y;
|
|
||||||
}
|
|
||||||
offset
|
|
||||||
}
|
|
||||||
|
|
||||||
fn make_bishop_table(rays: &Rays, square: Square) -> (Bitboard, Vec<(Bitboard, Bitboard)>) {
|
|
||||||
let mut premask = Bitboard::new();
|
|
||||||
for direction in [
|
|
||||||
Direction::NorthWest,
|
|
||||||
Direction::SouthWest,
|
|
||||||
Direction::SouthEast,
|
|
||||||
Direction::NorthEast,
|
|
||||||
] {
|
|
||||||
premask |= rays.ray(square, direction);
|
|
||||||
}
|
|
||||||
premask &= !Rank::First.bitboard();
|
|
||||||
premask &= !Rank::Eighth.bitboard();
|
|
||||||
premask &= !File::A.bitboard();
|
|
||||||
premask &= !File::H.bitboard();
|
|
||||||
|
|
||||||
let mut table = make_table(premask, |blockers| {
|
|
||||||
let mut res = Bitboard::new();
|
|
||||||
for direction in [
|
|
||||||
Direction::NorthWest,
|
|
||||||
Direction::SouthWest,
|
|
||||||
Direction::SouthEast,
|
|
||||||
Direction::NorthEast,
|
|
||||||
] {
|
|
||||||
res |= rays.blocked(square, direction, blockers);
|
|
||||||
}
|
|
||||||
res
|
|
||||||
});
|
|
||||||
|
|
||||||
premask = !premask;
|
|
||||||
for (x, _) in &mut table {
|
|
||||||
*x |= premask;
|
|
||||||
}
|
|
||||||
|
|
||||||
(premask, table)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn make_rook_table(rays: &Rays, square: Square) -> (Bitboard, Vec<(Bitboard, Bitboard)>) {
|
|
||||||
let mut premask = Bitboard::new();
|
|
||||||
premask |= rays.ray(square, Direction::North) & !Rank::Eighth.bitboard();
|
|
||||||
premask |= rays.ray(square, Direction::West) & !File::A.bitboard();
|
|
||||||
premask |= rays.ray(square, Direction::South) & !Rank::First.bitboard();
|
|
||||||
premask |= rays.ray(square, Direction::East) & !File::H.bitboard();
|
|
||||||
|
|
||||||
let mut table = make_table(premask, |blockers| {
|
|
||||||
let mut res = Bitboard::new();
|
|
||||||
for direction in [
|
|
||||||
Direction::North,
|
|
||||||
Direction::West,
|
|
||||||
Direction::South,
|
|
||||||
Direction::East,
|
|
||||||
] {
|
|
||||||
res |= rays.blocked(square, direction, blockers);
|
|
||||||
}
|
|
||||||
res
|
|
||||||
});
|
|
||||||
|
|
||||||
premask = !premask;
|
|
||||||
for (x, _) in &mut table {
|
|
||||||
*x |= premask;
|
|
||||||
}
|
|
||||||
|
|
||||||
(premask, table)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn make_table<T, F>(premask: Bitboard, f: F) -> Vec<(Bitboard, T)>
|
|
||||||
where
|
|
||||||
F: Fn(Bitboard) -> T,
|
|
||||||
{
|
|
||||||
let mut res = Vec::new();
|
|
||||||
let mut subset: u64 = 0;
|
|
||||||
loop {
|
|
||||||
subset = subset.wrapping_sub(premask.0) & premask.0;
|
|
||||||
let x = Bitboard(subset);
|
|
||||||
let y = f(x);
|
|
||||||
res.push((x, y));
|
|
||||||
if subset == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res
|
|
||||||
}
|
|
||||||
|
|
||||||
fn hash(shr: u8, factor: u64, x: Bitboard) -> usize {
|
|
||||||
(x.0.wrapping_mul(factor) >> shr) as usize
|
(x.0.wrapping_mul(factor) >> shr) as usize
|
||||||
}
|
}
|
||||||
|
|
||||||
fn bishop_factors(square: Square) -> u64 {
|
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,
|
||||||
|
|
@ -256,7 +79,7 @@ fn bishop_factors(square: Square) -> u64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn rook_factors(square: Square) -> u64 {
|
pub(crate) const fn rook_factors(square: Square) -> u64 {
|
||||||
match square {
|
match square {
|
||||||
Square::A1 => 0x002000A28110000C,
|
Square::A1 => 0x002000A28110000C,
|
||||||
Square::B1 => 0x0018000C01060001,
|
Square::B1 => 0x0018000C01060001,
|
||||||
|
|
|
||||||
3
src/main.rs
Normal file
3
src/main.rs
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
eschac::test();
|
||||||
|
}
|
||||||
|
|
@ -3,7 +3,6 @@
|
||||||
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::*;
|
||||||
|
|
@ -55,10 +54,7 @@ use std::iter::FusedIterator;
|
||||||
/// requirement is to be efficient while respecting the [`Ord`] trait protocol. It should not be
|
/// requirement is to be efficient while respecting the [`Ord`] trait protocol. It should not be
|
||||||
/// considered stable.
|
/// considered stable.
|
||||||
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||||
pub struct Position {
|
pub struct Position(Setup);
|
||||||
setup: Setup,
|
|
||||||
lookup: InitialisedLookup,
|
|
||||||
}
|
|
||||||
|
|
||||||
const MAX_LEGAL_MOVES: usize = 218;
|
const MAX_LEGAL_MOVES: usize = 218;
|
||||||
|
|
||||||
|
|
@ -76,8 +72,7 @@ impl Position {
|
||||||
/// i.e. `rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -`
|
/// i.e. `rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq -`
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self(Setup {
|
||||||
setup: Setup {
|
|
||||||
w: Bitboard(0x000000000000FFFF),
|
w: Bitboard(0x000000000000FFFF),
|
||||||
p_b_q: Bitboard(0x2CFF00000000FF2C),
|
p_b_q: Bitboard(0x2CFF00000000FF2C),
|
||||||
n_b_k: Bitboard(0x7600000000000076),
|
n_b_k: Bitboard(0x7600000000000076),
|
||||||
|
|
@ -85,9 +80,7 @@ impl Position {
|
||||||
turn: Color::White,
|
turn: Color::White,
|
||||||
castling_rights: CastlingRights::full(),
|
castling_rights: CastlingRights::full(),
|
||||||
en_passant: OptionSquare::None,
|
en_passant: OptionSquare::None,
|
||||||
},
|
})
|
||||||
lookup: InitialisedLookup::init(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to read a valid position from a text record.
|
/// Tries to read a valid position from a text record.
|
||||||
|
|
@ -182,25 +175,25 @@ impl Position {
|
||||||
/// responsibility to rule out the legality of en passant before calling this function.
|
/// responsibility to rule out the legality of en passant before calling this function.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove_en_passant_target_square(&mut self) {
|
pub fn remove_en_passant_target_square(&mut self) {
|
||||||
self.setup.en_passant = OptionSquare::None;
|
self.0.en_passant = OptionSquare::None;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the occupancy of a square.
|
/// Returns the occupancy of a square.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn get(&self, square: Square) -> Option<Piece> {
|
pub fn get(&self, square: Square) -> Option<Piece> {
|
||||||
self.setup.get(square)
|
self.0.get(square)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the color whose turn it is to play.
|
/// Returns the color whose turn it is to play.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn turn(&self) -> Color {
|
pub fn turn(&self) -> Color {
|
||||||
self.setup.turn()
|
self.0.turn()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns `true` if castling is available for the given color and side.
|
/// Returns `true` if castling is available for the given color and side.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn castling_rights(&self, color: Color, side: CastlingSide) -> bool {
|
pub fn castling_rights(&self, color: Color, side: CastlingSide) -> bool {
|
||||||
self.setup.castling_rights(color, side)
|
self.0.castling_rights(color, side)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the en passant target square if it exists.
|
/// Returns the en passant target square if it exists.
|
||||||
|
|
@ -209,26 +202,26 @@ impl Position {
|
||||||
/// legal or even pseudo-legal.
|
/// legal or even pseudo-legal.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn en_passant_target_square(&self) -> Option<Square> {
|
pub fn en_passant_target_square(&self) -> Option<Square> {
|
||||||
self.setup.en_passant_target_square()
|
self.0.en_passant_target_square()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Discards the castling rights for the given color and side.
|
/// Discards the castling rights for the given color and side.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn remove_castling_rights(&mut self, color: Color, side: CastlingSide) {
|
pub fn remove_castling_rights(&mut self, color: Color, side: CastlingSide) {
|
||||||
self.setup.set_castling_rights(color, side, false);
|
self.0.set_castling_rights(color, side, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Borrows the position as a [`Setup`].
|
/// Borrows the position as a [`Setup`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn as_setup(&self) -> &Setup {
|
pub fn as_setup(&self) -> &Setup {
|
||||||
&self.setup
|
&self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a position to a [`Setup`], allowing to edit the position without enforcing its
|
/// Converts a position to a [`Setup`], allowing to edit the position without enforcing its
|
||||||
/// legality.
|
/// legality.
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn into_setup(self) -> Setup {
|
pub fn into_setup(self) -> Setup {
|
||||||
self.setup
|
self.0
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Tries to pass the turn to the other color, failing if it would leave the king in check.
|
/// Tries to pass the turn to the other color, failing if it would leave the king in check.
|
||||||
|
|
@ -236,8 +229,8 @@ 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 = self.lookup;
|
let d = &crate::lookup::LOOKUP;
|
||||||
let setup = &self.setup;
|
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;
|
||||||
let q = setup.p_b_q & setup.r_q_k;
|
let q = setup.p_b_q & setup.r_q_k;
|
||||||
|
|
@ -255,23 +248,19 @@ impl Position {
|
||||||
| d.knight(king_square) & n
|
| d.knight(king_square) & n
|
||||||
| d.bishop(king_square, blockers) & (q | b)
|
| d.bishop(king_square, blockers) & (q | b)
|
||||||
| d.rook(king_square, blockers) & (q | r));
|
| d.rook(king_square, blockers) & (q | r));
|
||||||
checkers.is_empty().then(|| Self {
|
checkers.is_empty().then(|| {
|
||||||
setup: Setup {
|
Self(Setup {
|
||||||
turn: !setup.turn,
|
turn: !setup.turn,
|
||||||
en_passant: OptionSquare::None,
|
en_passant: OptionSquare::None,
|
||||||
..setup.clone()
|
..setup.clone()
|
||||||
},
|
})
|
||||||
lookup: d,
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the mirror image of the position (see [`Setup::mirror`]).
|
/// Returns the mirror image of the position (see [`Setup::mirror`]).
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn mirror(&self) -> Self {
|
pub fn mirror(&self) -> Self {
|
||||||
Self {
|
Self(self.0.mirror())
|
||||||
setup: self.setup.mirror(),
|
|
||||||
lookup: self.lookup,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns the number of possible chess games for a given number of moves.
|
/// Returns the number of possible chess games for a given number of moves.
|
||||||
|
|
@ -351,7 +340,7 @@ impl Position {
|
||||||
to,
|
to,
|
||||||
promotion,
|
promotion,
|
||||||
} = uci;
|
} = uci;
|
||||||
let role = self.setup.get_role(from).ok_or(InvalidUciMove::Illegal)?;
|
let role = self.0.get_role(from).ok_or(InvalidUciMove::Illegal)?;
|
||||||
#[inline]
|
#[inline]
|
||||||
fn aux<'l, const ROLE: u8>(
|
fn aux<'l, const ROLE: u8>(
|
||||||
position: &'l Position,
|
position: &'l Position,
|
||||||
|
|
@ -550,9 +539,7 @@ impl<'l> Move<'l> {
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn is_capture(self) -> bool {
|
pub fn is_capture(self) -> bool {
|
||||||
self.raw.kind == MoveType::EnPassant
|
self.raw.kind == MoveType::EnPassant
|
||||||
|| !((self.position.setup.p_b_q
|
|| !((self.position.0.p_b_q | self.position.0.n_b_k | self.position.0.r_q_k)
|
||||||
| self.position.setup.n_b_k
|
|
||||||
| self.position.setup.r_q_k)
|
|
||||||
& self.to().bitboard())
|
& self.to().bitboard())
|
||||||
.is_empty()
|
.is_empty()
|
||||||
}
|
}
|
||||||
|
|
@ -562,7 +549,7 @@ impl<'l> Move<'l> {
|
||||||
pub fn captured(self) -> Option<Role> {
|
pub fn captured(self) -> Option<Role> {
|
||||||
match self.raw.kind {
|
match self.raw.kind {
|
||||||
MoveType::EnPassant => Some(Role::Pawn),
|
MoveType::EnPassant => Some(Role::Pawn),
|
||||||
_ => self.position.setup.get_role(self.raw.to),
|
_ => self.position.0.get_role(self.raw.to),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -932,10 +919,7 @@ impl<'l> Visitor for Moves<'l> {
|
||||||
impl Position {
|
impl Position {
|
||||||
/// SAFETY: The position must be valid.
|
/// SAFETY: The position must be valid.
|
||||||
pub(crate) unsafe fn from_setup(setup: Setup) -> Self {
|
pub(crate) unsafe fn from_setup(setup: Setup) -> Self {
|
||||||
Self {
|
Self(setup)
|
||||||
setup,
|
|
||||||
lookup: InitialisedLookup::init(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn generate_moves<T>(&self, visitor: &mut T)
|
fn generate_moves<T>(&self, visitor: &mut T)
|
||||||
|
|
@ -953,8 +937,8 @@ impl Position {
|
||||||
turn,
|
turn,
|
||||||
en_passant,
|
en_passant,
|
||||||
castling_rights,
|
castling_rights,
|
||||||
} = self.setup;
|
} = self.0;
|
||||||
let d = self.lookup;
|
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 {
|
||||||
|
|
@ -1267,7 +1251,7 @@ impl Position {
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
unsafe fn play_unchecked(&mut self, m: RawMove) {
|
unsafe fn play_unchecked(&mut self, m: RawMove) {
|
||||||
let Self { setup, .. } = self;
|
let Self(setup) = self;
|
||||||
|
|
||||||
setup.en_passant = OptionSquare::None;
|
setup.en_passant = OptionSquare::None;
|
||||||
|
|
||||||
|
|
|
||||||
42
src/rays.rs
42
src/rays.rs
|
|
@ -1,44 +1,2 @@
|
||||||
use crate::bitboard::*;
|
use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
|
|
||||||
pub(crate) struct Rays(BySquare<ByDirection<Bitboard>>);
|
|
||||||
|
|
||||||
impl Rays {
|
|
||||||
pub(crate) fn new() -> Self {
|
|
||||||
Self(BySquare::new(|square| {
|
|
||||||
ByDirection::new(|direction| {
|
|
||||||
let mut square = square;
|
|
||||||
let mut res = Bitboard::new();
|
|
||||||
while let Some(x) = square.trans(direction) {
|
|
||||||
square = x;
|
|
||||||
res |= square.bitboard();
|
|
||||||
}
|
|
||||||
res
|
|
||||||
})
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn ray(&self, square: Square, direction: Direction) -> Bitboard {
|
|
||||||
*self.0.get(square).get(direction)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn blocked(
|
|
||||||
&self,
|
|
||||||
square: Square,
|
|
||||||
direction: Direction,
|
|
||||||
blockers: Bitboard,
|
|
||||||
) -> Bitboard {
|
|
||||||
let blockers = blockers & *self.0.get(square).get(direction);
|
|
||||||
let square2 = if (direction as u8) < 4 {
|
|
||||||
blockers.first()
|
|
||||||
} else {
|
|
||||||
blockers.last()
|
|
||||||
};
|
|
||||||
*self.0.get(square).get(direction)
|
|
||||||
& !match square2 {
|
|
||||||
Some(square2) => *self.0.get(square2).get(direction),
|
|
||||||
None => Bitboard::new(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
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.**
|
||||||
|
|
@ -347,7 +346,7 @@ 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 = InitialisedLookup::init();
|
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();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue