From 6b0563b1bd1d8c1afd8fb629fed0e790f7b61ce4 Mon Sep 17 00:00:00 2001 From: Bruno BELANYI Date: Fri, 22 Jul 2022 10:14:46 +0200 Subject: [PATCH] Add 'Magic' type --- src/movegen/magic/magic.rs | 23 +++++++++++++++++++++++ src/movegen/magic/mod.rs | 2 ++ src/movegen/mod.rs | 4 ++++ 3 files changed, 29 insertions(+) create mode 100644 src/movegen/magic/magic.rs create mode 100644 src/movegen/magic/mod.rs diff --git a/src/movegen/magic/magic.rs b/src/movegen/magic/magic.rs new file mode 100644 index 0000000..0f328d5 --- /dev/null +++ b/src/movegen/magic/magic.rs @@ -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 + } +} diff --git a/src/movegen/magic/mod.rs b/src/movegen/magic/mod.rs new file mode 100644 index 0000000..068c6e7 --- /dev/null +++ b/src/movegen/magic/mod.rs @@ -0,0 +1,2 @@ +pub mod magic; +pub use magic::*; diff --git a/src/movegen/mod.rs b/src/movegen/mod.rs index 746011d..d379194 100644 --- a/src/movegen/mod.rs +++ b/src/movegen/mod.rs @@ -1,3 +1,7 @@ +// Magic bitboard +pub mod magic; +pub use magic::*; + // Move generation implementation details mod bishop; mod king;