2022-07-24 13:40:01 +02:00
|
|
|
pub(crate) mod generation;
|
2022-07-22 10:37:56 +02:00
|
|
|
mod mask;
|
2024-03-29 02:43:27 +01:00
|
|
|
|
|
|
|
use crate::board::Bitboard;
|
|
|
|
|
|
|
|
/// A type representing the magic board indexing a given [crate::board::Square].
|
2024-03-29 03:54:24 +01:00
|
|
|
#[derive(Clone, Debug)]
|
2024-03-29 03:55:03 +01:00
|
|
|
pub(crate) struct Magic {
|
2024-03-29 02:43:27 +01:00
|
|
|
/// Magic number.
|
|
|
|
pub(crate) magic: u64,
|
|
|
|
/// Base offset into the magic square table.
|
|
|
|
pub(crate) offset: usize,
|
|
|
|
/// Mask to apply to the blocker board before applying the magic.
|
|
|
|
pub(crate) mask: Bitboard,
|
|
|
|
/// Length of the resulting mask after applying the magic.
|
|
|
|
pub(crate) shift: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Magic {
|
|
|
|
/// Compute the index into the magics database for this set of `blockers`.
|
|
|
|
pub fn get_index(&self, blockers: Bitboard) -> usize {
|
|
|
|
let relevant_occupancy = (blockers & self.mask).0;
|
|
|
|
let base_index = ((relevant_occupancy.wrapping_mul(self.magic)) >> self.shift) as usize;
|
|
|
|
base_index + self.offset
|
|
|
|
}
|
|
|
|
}
|