[board] Implement a BitBoard MoveLibrary

This struct computes and stores piece moves per square to speed up computation
of move lists.

Initialize a `static mut` instance once via std::sync::Once, and return a static
reference to it. So far, it computes king and knight moves.

Make BitBoard::empty() and MoveLibrary::new() const functions, enabling static
construction of the MOVE_LIBRARY instance without needing to wrap it in an
Option.
This commit is contained in:
Eryn Wells 2024-01-01 09:25:31 -08:00
parent 4a54d8b877
commit 06bfc4ac57
2 changed files with 93 additions and 2 deletions

View file

@ -1,6 +1,6 @@
// Eryn Wells <eryn@erynwells.me>
use super::library::{FILES, RANKS};
use super::library::{library, FILES, RANKS};
use super::BitScanner;
use crate::Square;
use std::fmt;
@ -10,7 +10,7 @@ use std::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, Not};
pub(crate) struct BitBoard(pub(super) u64);
impl BitBoard {
pub fn empty() -> BitBoard {
pub const fn empty() -> BitBoard {
BitBoard(0)
}
@ -28,6 +28,10 @@ impl BitBoard {
FILES[file]
}
pub fn knight_moves(sq: Square) -> BitBoard {
library().knight_moves(sq)
}
pub fn from_square(sq: Square) -> BitBoard {
BitBoard(1 << sq.index())
}