use crate::bitboard::*;
use crate::board::*;
pub(crate) use crate::util::*;
#[repr(align(8))]
struct Data<'a> {
inner: &'a [u8],
}
impl<'a> Data<'a> {
const fn new(inner: &'a [u8]) -> Self {
Self { inner }
}
fn read(&self, index: usize) -> u64 {
let i = index << 3;
u64::from_le_bytes(self.inner.get(i..i + 8).unwrap().try_into().unwrap())
}
}
static MAGIC: Data = Data::new(include_bytes!(concat!(env!("OUT_DIR"), "/magic.bin")));
static TABLE: Data = Data::new(include_bytes!(concat!(env!("OUT_DIR"), "/table.bin")));
#[inline]
pub(crate) fn bishop(square: Square, blockers: Bitboard) -> Bitboard {
aux(0, BISHOP_SHR, square, blockers)
}
#[inline]
pub(crate) fn rook(square: Square, blockers: Bitboard) -> Bitboard {
aux(1, ROOK_SHR, square, blockers)
}
#[inline]
pub(crate) fn aux(j: usize, shr: u8, square: Square, blockers: Bitboard) -> Bitboard {
let i = square.index() as usize + j * 64;
let premask = Bitboard(MAGIC.read(3 * i));
let factor = MAGIC.read(3 * i + 1);
let offset = MAGIC.read(3 * i + 2) as isize;
let value = TABLE.read(((hash(shr, factor, blockers | premask) as isize) + offset) as usize);
Bitboard(value)
}
#[cfg(feature = "alloc")]
#[test]
fn test_magic_data() {
let (magic, table) = crate::magic::build();
assert_eq!(&magic, &MAGIC.inner);
assert_eq!(&table, &TABLE.inner);
assert_eq!(table.len(), 869848);
}