1
0
Fork 0

const lookup table

This commit is contained in:
Paul-Nicolas Madelaine 2025-10-22 22:59:53 +02:00
parent 752150107e
commit 0d22c59cc3
8 changed files with 573 additions and 542 deletions

View file

@ -23,6 +23,9 @@ macro_rules! container {
pub fn get_mut(&mut self, k: $a) -> &mut T {
unsafe { self.0.get_unchecked_mut(k as usize) }
}
pub(crate) const fn get_const(&self, k: $a) -> &T {
&self.0[k as usize]
}
}
};
}
@ -133,12 +136,12 @@ impl File {
}
#[inline]
pub(crate) fn bitboard(self) -> Bitboard {
pub(crate) const fn bitboard(self) -> Bitboard {
Bitboard(0x0101010101010101 << (self as u8))
}
#[inline]
pub(crate) unsafe fn transmute(value: u8) -> Self {
pub(crate) const unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 8);
std::mem::transmute(value)
}
@ -210,12 +213,12 @@ impl Rank {
}
#[inline]
pub(crate) fn bitboard(self) -> Bitboard {
pub(crate) const fn bitboard(self) -> Bitboard {
Bitboard(0xFF << ((self as u64) << 3))
}
#[inline]
pub(crate) unsafe fn transmute(value: u8) -> Self {
pub(crate) const unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 8);
std::mem::transmute(value)
}
@ -261,18 +264,26 @@ impl Square {
]
}
pub(crate) const fn from_index(index: u8) -> Option<Self> {
if index < 64 {
Some(unsafe { Self::transmute(index) })
} else {
None
}
}
#[inline]
pub fn new(file: File, rank: Rank) -> Self {
unsafe { Self::transmute(((rank as u8) << 3) | file as u8) }
}
#[inline]
pub fn file(self) -> File {
pub const fn file(self) -> File {
unsafe { File::transmute((self as u8) & 7) }
}
#[inline]
pub fn rank(self) -> Rank {
pub const fn rank(self) -> Rank {
unsafe { Rank::transmute((self as u8) >> 3) }
}
@ -283,7 +294,7 @@ impl Square {
}
#[inline]
pub(crate) fn bitboard(self) -> Bitboard {
pub(crate) const fn bitboard(self) -> Bitboard {
Bitboard(1 << self as u8)
}
@ -317,16 +328,17 @@ impl Square {
}
#[inline]
pub(crate) fn trans(self, direction: Direction) -> Option<Self> {
self.check_trans(direction).then(|| unsafe {
// SAFETY: condition is checked before doing the translation
self.trans_unchecked(direction)
})
pub(crate) const fn trans(self, direction: Direction) -> Option<Self> {
if self.check_trans(direction) {
Some(unsafe { self.trans_unchecked(direction) })
} else {
None
}
}
/// SAFETY: the translation must not move the square outside the board
#[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));
let i = self as u8;
unsafe {
@ -345,21 +357,23 @@ impl Square {
/// Returns `false` if the translation would move the square outside the board
#[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 {
Direction::East => self.file() < File::H,
Direction::NorthEast => self.file() < File::H && self.rank() < Rank::Eighth,
Direction::North => self.rank() < Rank::Eighth,
Direction::NorthWest => self.file() > File::A && self.rank() < Rank::Eighth,
Direction::SouthEast => self.file() < File::H && self.rank() > Rank::First,
Direction::South => self.rank() > Rank::First,
Direction::SouthWest => self.file() > File::A && self.rank() > Rank::First,
Direction::West => self.file() > File::A,
Direction::East => file < 7,
Direction::NorthEast => file < 7 && rank < 7,
Direction::North => rank < 7,
Direction::NorthWest => file > 0 && rank < 7,
Direction::SouthEast => file < 7 && rank > 0,
Direction::South => rank > 0,
Direction::SouthWest => file > 0 && rank > 0,
Direction::West => file > 0,
}
}
#[inline]
pub(crate) unsafe fn transmute(value: u8) -> Self {
pub(crate) const unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 64);
std::mem::transmute(value)
}
@ -591,7 +605,16 @@ impl Direction {
}
#[inline]
unsafe fn transmute(value: u8) -> Self {
pub(crate) const fn from_index(index: u8) -> Option<Self> {
if index < 8 {
Some(unsafe { Self::transmute(index) })
} else {
None
}
}
#[inline]
const unsafe fn transmute(value: u8) -> Self {
debug_assert!(value < 8);
std::mem::transmute(value)
}