misc lookup
This commit is contained in:
parent
e547d7596f
commit
09458cfc36
1 changed files with 98 additions and 97 deletions
193
src/lookup.rs
193
src/lookup.rs
|
|
@ -2,78 +2,42 @@ use crate::bitboard::*;
|
||||||
use crate::board::*;
|
use crate::board::*;
|
||||||
use crate::magics::*;
|
use crate::magics::*;
|
||||||
|
|
||||||
macro_rules! loop_bishop_directions {
|
macro_rules! loop_subsets {
|
||||||
($d: ident, $body: expr) => {{
|
($premask: ident, $subset: ident, $e: expr) => {{
|
||||||
{
|
let mut $subset: u64 = 0;
|
||||||
let $d = Direction::NorthEast;
|
loop {
|
||||||
$body
|
$subset = $subset.wrapping_sub($premask) & $premask;
|
||||||
|
$e
|
||||||
|
if $subset == 0 {
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
{
|
|
||||||
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 {
|
macro_rules! by_color {
|
||||||
($c: ident, $body: expr) => {{
|
($c: ident, $e: expr) => {{
|
||||||
ByColor([
|
ByColor([
|
||||||
{
|
{
|
||||||
let $c = Color::White;
|
let $c = Color::White;
|
||||||
$body
|
$e
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
let $c = Color::Black;
|
let $c = Color::Black;
|
||||||
$body
|
$e
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! by_square {
|
macro_rules! by_square {
|
||||||
($sq: ident, $init: expr, $body: expr) => {{
|
($sq: ident, $init: expr, $e: expr) => {{
|
||||||
let mut res = [$init; 64];
|
let mut res = [$init; 64];
|
||||||
let mut $sq: u8 = 0;
|
let mut $sq: u8 = 0;
|
||||||
while $sq < 64 {
|
while $sq < 64 {
|
||||||
res[$sq as usize] = {
|
res[$sq as usize] = {
|
||||||
let $sq = Square::new($sq).unwrap();
|
let $sq = Square::new($sq).unwrap();
|
||||||
$body
|
$e
|
||||||
};
|
};
|
||||||
$sq += 1;
|
$sq += 1;
|
||||||
}
|
}
|
||||||
|
|
@ -81,14 +45,63 @@ macro_rules! by_square {
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! loop_bishop_directions {
|
||||||
|
($d: ident, $e: expr) => {{
|
||||||
|
{
|
||||||
|
let $d = Direction::NorthEast;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::NorthWest;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::SouthWest;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::SouthEast;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! loop_rook_directions {
|
||||||
|
($d: ident, $e: expr) => {{
|
||||||
|
{
|
||||||
|
let $d = Direction::East;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::North;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::West;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
{
|
||||||
|
let $d = Direction::South;
|
||||||
|
$e
|
||||||
|
}
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! loop_all_directions {
|
||||||
|
($d: ident, $e: expr) => {{
|
||||||
|
loop_bishop_directions!($d, $e);
|
||||||
|
loop_rook_directions!($d, $e);
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
macro_rules! by_direction {
|
macro_rules! by_direction {
|
||||||
($d: ident, $body: expr) => {{
|
($d: ident, $e: expr) => {{
|
||||||
let mut res = [Bitboard(0); 8];
|
let mut res = [Bitboard(0); 8];
|
||||||
let mut $d: u8 = 0;
|
let mut $d: u8 = 0;
|
||||||
while $d < 8 {
|
while $d < 8 {
|
||||||
res[$d as usize] = {
|
res[$d as usize] = {
|
||||||
let $d = Direction::new($d).unwrap();
|
let $d = Direction::new($d).unwrap();
|
||||||
$body
|
$e
|
||||||
};
|
};
|
||||||
$d += 1;
|
$d += 1;
|
||||||
}
|
}
|
||||||
|
|
@ -109,6 +122,7 @@ const RAYS: BySquare<ByDirection<Bitboard>> = by_square!(
|
||||||
Bitboard(res)
|
Bitboard(res)
|
||||||
})
|
})
|
||||||
);
|
);
|
||||||
|
|
||||||
const LINES: BySquare<BySquare<Bitboard>> = by_square!(a, BySquare([Bitboard(0); 64]), {
|
const LINES: BySquare<BySquare<Bitboard>> = 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);
|
||||||
|
|
@ -134,6 +148,7 @@ const SEGMENTS: BySquare<BySquare<Bitboard>> = by_square!(a, BySquare([Bitboard(
|
||||||
Bitboard(res | b.bitboard().0)
|
Bitboard(res | b.bitboard().0)
|
||||||
})
|
})
|
||||||
});
|
});
|
||||||
|
|
||||||
const KING_MOVES: BySquare<Bitboard> = by_square!(sq, Bitboard(0), {
|
const KING_MOVES: BySquare<Bitboard> = by_square!(sq, Bitboard(0), {
|
||||||
let mut res = 0;
|
let mut res = 0;
|
||||||
loop_all_directions!(d, {
|
loop_all_directions!(d, {
|
||||||
|
|
@ -143,8 +158,9 @@ const KING_MOVES: BySquare<Bitboard> = by_square!(sq, Bitboard(0), {
|
||||||
});
|
});
|
||||||
Bitboard(res)
|
Bitboard(res)
|
||||||
});
|
});
|
||||||
|
|
||||||
const KNIGHT_MOVES: BySquare<Bitboard> = by_square!(s, Bitboard(0), {
|
const KNIGHT_MOVES: BySquare<Bitboard> = by_square!(s, Bitboard(0), {
|
||||||
let mut res = Bitboard::new();
|
let mut res = Bitboard(0);
|
||||||
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) {
|
||||||
res.insert(s);
|
res.insert(s);
|
||||||
|
|
@ -179,6 +195,7 @@ const KNIGHT_MOVES: BySquare<Bitboard> = by_square!(s, Bitboard(0), {
|
||||||
}
|
}
|
||||||
res
|
res
|
||||||
});
|
});
|
||||||
|
|
||||||
const PAWN_ATTACKS: ByColor<BySquare<Bitboard>> = {
|
const PAWN_ATTACKS: ByColor<BySquare<Bitboard>> = {
|
||||||
by_color!(color, {
|
by_color!(color, {
|
||||||
let direction = match color {
|
let direction = match color {
|
||||||
|
|
@ -239,32 +256,6 @@ pub(crate) fn pawn_attack(color: Color, square: Square) -> Bitboard {
|
||||||
*PAWN_ATTACKS.get(color).get(square)
|
*PAWN_ATTACKS.get(color).get(square)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `role != Pawn`
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn targets(role: Role, from: Square, blockers: Bitboard) -> Bitboard {
|
|
||||||
match role {
|
|
||||||
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 {
|
|
||||||
($premask: ident, $subset: ident, $body: expr) => {{
|
|
||||||
let mut $subset: u64 = 0;
|
|
||||||
loop {
|
|
||||||
$subset = $subset.wrapping_sub($premask) & $premask;
|
|
||||||
$body
|
|
||||||
if $subset == 0 {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
const fn bishop_premask(square: Square) -> Bitboard {
|
const fn bishop_premask(square: Square) -> Bitboard {
|
||||||
let mut premask = 0;
|
let mut premask = 0;
|
||||||
loop_bishop_directions!(direction, {
|
loop_bishop_directions!(direction, {
|
||||||
|
|
@ -300,20 +291,19 @@ const MAGICS: (BySquare<Magic>, BySquare<Magic>, usize) = {
|
||||||
{
|
{
|
||||||
let premask = bishop_premask(square).0;
|
let premask = bishop_premask(square).0;
|
||||||
let factor = bishop_factor(square);
|
let factor = bishop_factor(square);
|
||||||
let mut a = usize::MAX;
|
let mut i = usize::MAX;
|
||||||
let mut b = 0;
|
let mut j = 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 < i {
|
||||||
a = cur;
|
i = cur;
|
||||||
}
|
}
|
||||||
if cur > b {
|
if cur > j {
|
||||||
b = cur;
|
j = cur;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
assert!(b > a);
|
let offset = len as isize - i as isize;
|
||||||
let offset = len as isize - a as isize;
|
len += j - i + 1;
|
||||||
len += b - a + 1;
|
|
||||||
Magic {
|
Magic {
|
||||||
premask: Bitboard(!premask),
|
premask: Bitboard(!premask),
|
||||||
factor,
|
factor,
|
||||||
|
|
@ -331,20 +321,19 @@ const MAGICS: (BySquare<Magic>, BySquare<Magic>, usize) = {
|
||||||
{
|
{
|
||||||
let premask = rook_premask(square).0;
|
let premask = rook_premask(square).0;
|
||||||
let factor = rook_factor(square);
|
let factor = rook_factor(square);
|
||||||
let mut a = usize::MAX;
|
let mut i = usize::MAX;
|
||||||
let mut b = 0;
|
let mut j = 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 < i {
|
||||||
a = cur;
|
i = cur;
|
||||||
}
|
}
|
||||||
if cur > b {
|
if cur > j {
|
||||||
b = cur;
|
j = cur;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
assert!(b > a);
|
let offset = len as isize - i as isize;
|
||||||
let offset = len as isize - a as isize;
|
len += j - i + 1;
|
||||||
len += b - a + 1;
|
|
||||||
Magic {
|
Magic {
|
||||||
premask: Bitboard(!premask),
|
premask: Bitboard(!premask),
|
||||||
factor,
|
factor,
|
||||||
|
|
@ -438,3 +427,15 @@ unsafe fn magic_aux(shr: u8, magic: Magic, blockers: Bitboard) -> Bitboard {
|
||||||
const fn hash(shr: u8, factor: u64, x: Bitboard) -> usize {
|
const 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
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub(crate) fn targets(role: Role, from: Square, blockers: Bitboard) -> Bitboard {
|
||||||
|
match role {
|
||||||
|
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),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue