Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pub fn init_sliding_piece_magics<'a>(
- piece_type: PieceType,
- attacks: &'a mut [Bitboard],
- magics: &mut [Magic<'a>; Square::NB],
- ) {
- debug_assert!(piece_type == Bishop || piece_type == Rook);
- let mut offsets = [0; Square::NB + 1];
- let mut offset: usize = 0;
- for &square in Square::ALL.iter() {
- let magic = &mut magics[square as usize];
- magic.mask = sliding_attacks(piece_type, square, Bitboard::EMPTY);
- let start = offset;
- // Carry-Rippler trick (https://www.chessprogramming.org/Traversing_Subsets_of_a_Set)
- let mut blockers = Bitboard::EMPTY;
- loop {
- let index = Bitboard::extract_bits(blockers, magic.mask);
- attacks[start + index as usize] = sliding_attacks(piece_type, square, blockers);
- offset += 1;
- blockers = Bitboard((blockers.0.wrapping_sub(magic.mask.0)) & magic.mask.0);
- if blockers.empty() {
- break;
- }
- }
- offsets[square as usize + 1] = offset;
- // magic.attacks = &attacks[start..offset];
- }
- for &square in Square::ALL.iter() {
- magics[square as usize].attacks = &attacks[offsets[square as usize]..offsets[square as usize + 1]];
- }
- }
- static ROOK_MAGICS: LazyLock<SlidingPieceMagics<'static>> = LazyLock::new(|| {
- let mut attacks = Box::leak(Box::new([Bitboard::EMPTY; ROOK_ATTACK_NB]));
- let mut magics = [Magic::default(); Square::NB];
- init_sliding_piece_magics(Rook, attacks, &mut magics);
- SlidingPieceMagics {
- attacks: &attacks[..],
- magics,
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment