eschac 0.6.0

computing chess moves
Documentation
use crate::bitboard::*;
use crate::board::*;

pub(crate) use crate::util::*;

static MAGIC: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/magic.bin"));
static TABLE: &[u8] = 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(read(MAGIC, 3 * i));
	let factor = read(MAGIC, 3 * i + 1);
	let offset = read(MAGIC, 3 * i + 2) as isize;
	let value = read(
		TABLE,
		((hash(shr, factor, blockers | premask) as isize) + offset) as usize,
	);
	Bitboard(value)
}

#[inline]
fn read(data: &[u8], index: usize) -> u64 {
	let i = index << 3;
	u64::from_le_bytes(data.get(i..i + 8).unwrap().try_into().unwrap())
}

#[cfg(feature = "alloc")]
#[test]
fn test_magic_data() {
	let (magic, table) = crate::magic::build();
	assert_eq!(magic, MAGIC);
	assert_eq!(table, TABLE);
}