-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmagic_table.rs
More file actions
225 lines (197 loc) Β· 6.11 KB
/
magic_table.rs
File metadata and controls
225 lines (197 loc) Β· 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
//! Magic bitboard implementation for efficient sliding piece move generation.
//!
//! **Performance optimizations:**
//! - `#[inline]` on `get_rook_targets` and `get_bishop_targets`: 0.9% improvement
//! - `#[inline(always)]` on `magic_index` for guaranteed inlining in hot paths
use common::bitboard::{
bitboard::Bitboard,
square::{Square, ORDERED_SQUARES},
};
#[cfg(feature = "instrumentation")]
use tracing::instrument;
include!(concat!(env!("OUT_DIR"), "/magic_table.rs"));
pub struct MagicEntry {
mask: u64,
magic: u64,
shift: u8,
offset: u32,
}
#[derive(Clone)]
pub struct MagicTable {
rook_table: Vec<Bitboard>,
bishop_table: Vec<Bitboard>,
}
impl Default for MagicTable {
fn default() -> Self {
let rook_table = make_table(
ROOK_TABLE_SIZE,
&[(1, 0), (0, -1), (-1, 0), (0, 1)],
ROOK_MAGICS,
);
let bishop_table = make_table(
BISHOP_TABLE_SIZE,
&[(1, 1), (1, -1), (-1, -1), (-1, 1)],
BISHOP_MAGICS,
);
Self {
rook_table,
bishop_table,
}
}
}
impl MagicTable {
pub fn new() -> Self {
Default::default()
}
#[inline]
#[cfg_attr(feature = "instrumentation", instrument(skip_all))]
pub fn get_rook_targets(&self, square: Square, blockers: Bitboard) -> Bitboard {
let magic = &ROOK_MAGICS[square.index() as usize];
self.rook_table[magic_index(magic, blockers)]
}
#[inline]
#[cfg_attr(feature = "instrumentation", instrument(skip_all))]
pub fn get_bishop_targets(&self, square: Square, blockers: Bitboard) -> Bitboard {
let magic = &BISHOP_MAGICS[square.index() as usize];
self.bishop_table[magic_index(magic, blockers)]
}
}
fn make_table(
table_size: usize,
slider_deltas: &[(i8, i8)],
magics: &[MagicEntry; 64],
) -> Vec<Bitboard> {
let mut table = vec![Bitboard::EMPTY; table_size];
for &square in &ORDERED_SQUARES {
let square_bitboard = Bitboard(1 << square.index());
let magic_entry = &magics[square.index() as usize];
let mask = Bitboard(magic_entry.mask);
let mut blockers = Bitboard::EMPTY;
loop {
let moves = slider_moves(slider_deltas, square_bitboard, blockers);
table[magic_index(magic_entry, blockers)] = moves;
// Carry-Rippler trick that enumerates all subsets of the mask, getting us all blockers.
// https://www.chessprogramming.org/Traversing_Subsets_of_a_Set#All_Subsets_of_any_Set
blockers.0 = blockers.0.wrapping_sub(mask.0) & mask.0;
if blockers.is_empty() {
break;
}
}
}
table
}
fn slider_moves(slider_deltas: &[(i8, i8)], square: Bitboard, blockers: Bitboard) -> Bitboard {
let mut moves = Bitboard::EMPTY;
for &(d_rank, d_file) in slider_deltas {
let mut ray = square;
while !blockers.overlaps(ray) {
if let Some(shifted) = try_offset(ray, d_rank, d_file) {
ray = shifted;
moves |= ray;
} else {
break;
}
}
}
moves
}
fn try_offset(square: Bitboard, d_rank: i8, d_file: i8) -> Option<Bitboard> {
let sq = square.to_square();
let rank = sq.rank() as i8;
let file = sq.file() as i8;
let new_rank = rank.wrapping_add(d_rank);
let new_file = file.wrapping_add(d_file);
if !(0..8).contains(&new_rank) || !(0..8).contains(&new_file) {
None
} else {
Some(Square::from_rank_file(new_rank as u8, new_file as u8).to_bitboard())
}
}
#[inline(always)]
fn magic_index(entry: &MagicEntry, blockers: Bitboard) -> usize {
let blockers = blockers.0 & entry.mask;
let hash = blockers.wrapping_mul(entry.magic);
let index = (hash >> entry.shift) as usize;
entry.offset as usize + index
}
#[cfg(test)]
mod tests {
use common::bitboard::*;
use crate::{
board::{color::Color, piece::Piece, Board},
chess_position,
};
use super::*;
#[test]
fn test_get_rook_targets() {
let magic_table = MagicTable::new();
let board = chess_position! {
........
..P.....
........
........
........
P.R..p.p
..K.....
........
};
let targets = magic_table.get_rook_targets(C3, board.occupied());
// Targets should assume any piece in the way can be taken
let expected_targets = A3 | B3 | D3 | E3 | F3 | C2 | C4 | C5 | C6 | C7;
assert_eq!(targets, expected_targets);
}
#[test]
fn test_get_bishop_targets() {
let magic_table = MagicTable::new();
let board = chess_position! {
........
.......p
........
....p...
........
..B.....
.b......
........
};
let targets = magic_table.get_bishop_targets(C3, board.occupied());
// Targets should assume any piece in the way can be taken
let expected_targets = D4 | E5 | B2 | D2 | E1 | B4 | A5;
assert_eq!(targets, expected_targets);
}
#[test]
fn test_get_queen_targets() {
let magic_table = MagicTable::new();
let board = chess_position! {
........
........
........
Qp......
P.......
........
........
.R.....k
};
let rook_targets = magic_table.get_rook_targets(A5, board.occupied());
let bishop_targets = magic_table.get_bishop_targets(A5, board.occupied());
let targets = rook_targets | bishop_targets;
let expected_targets = Bitboard::EMPTY
// queen - north
| A6
| A7
| A8
// queen - northeast
| B6
| C7
| D8
// queen - east
| B5
// queen - southeast
| B4
| C3
| D2
| E1
// queen - south
| A4;
assert_eq!(targets, expected_targets);
}
}