Skip to main content

hermes_simd/
attacks.rs

1use hermes_simd_core::bitboard::BitBoardKernel;
2use hermes_simd_intrinsics::Magic;
3
4// SAFETY (shared by all three wrappers): the `Magic` backend's attack methods
5// perform only computed bit math and bounds-checked table indexing — they carry
6// no ISA/target-feature precondition and cannot cause undefined behavior for any
7// input (an out-of-range `square` trips an array bounds check and panics). The
8// safe wrappers below therefore uphold the `unsafe` trait contract for the
9// `Magic` impl without an unverified precondition.
10
11/// Generate Rook attacks for a square given occupancy using the Magic bitboard backend.
12///
13/// # Panics
14/// Panics if `square >= 64` (the magic tables are indexed by `square`).
15#[inline(always)]
16pub fn rook_attacks(square: u8, occupancy: u64) -> u64 {
17    unsafe { <Magic as BitBoardKernel>::rook_attacks(square, occupancy) }
18}
19
20/// Generate Bishop attacks for a square given occupancy using the Magic bitboard backend.
21///
22/// # Panics
23/// Panics if `square >= 64` (the magic tables are indexed by `square`).
24#[inline(always)]
25pub fn bishop_attacks(square: u8, occupancy: u64) -> u64 {
26    unsafe { <Magic as BitBoardKernel>::bishop_attacks(square, occupancy) }
27}
28
29/// Generate Queen attacks for a square given occupancy using the Magic bitboard backend.
30///
31/// # Panics
32/// Panics if `square >= 64` (the magic tables are indexed by `square`).
33#[inline(always)]
34pub fn queen_attacks(square: u8, occupancy: u64) -> u64 {
35    unsafe { <Magic as BitBoardKernel>::queen_attacks(square, occupancy) }
36}