Add 'Magic' type
This commit is contained in:
parent
847e18dac1
commit
6b0563b1bd
23
src/movegen/magic/magic.rs
Normal file
23
src/movegen/magic/magic.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use crate::board::Bitboard;
|
||||
|
||||
/// A type representing the magic board indexing a given [Square].
|
||||
#[allow(unused)] // FIXME: remove once used
|
||||
pub struct Magic {
|
||||
/// Magic number.
|
||||
magic: u64,
|
||||
/// Base offset into the magic square table.
|
||||
offset: usize,
|
||||
/// Mask to apply to the blocker board before applying the magic.
|
||||
mask: Bitboard,
|
||||
/// Length of the resulting mask after applying the magic.
|
||||
shift: u8,
|
||||
}
|
||||
|
||||
impl Magic {
|
||||
#[allow(unused)] // FIXME: remove once used
|
||||
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
|
||||
}
|
||||
}
|
2
src/movegen/magic/mod.rs
Normal file
2
src/movegen/magic/mod.rs
Normal file
|
@ -0,0 +1,2 @@
|
|||
pub mod magic;
|
||||
pub use magic::*;
|
|
@ -1,3 +1,7 @@
|
|||
// Magic bitboard
|
||||
pub mod magic;
|
||||
pub use magic::*;
|
||||
|
||||
// Move generation implementation details
|
||||
mod bishop;
|
||||
mod king;
|
||||
|
|
Loading…
Reference in a new issue