Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions src/backend/compile_time/gen_caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ use crate::backend::compile_time::gen_caches_non_sliders::{
};
use crate::backend::compile_time::gen_caches_sliders::{PEXT_TABLE_SIZE, gen_cache_sliders};
use crate::backend::compile_time::generated::caches::{
CACHE_BISHOP_PEXT_INDEX, CACHE_BISHOP_PEXT_MASK, CACHE_CAPTURE_PAWN, CACHE_DOUBLE_PUSH_PAWN,
CACHE_KING, CACHE_KNIGHT, CACHE_PEXT_TABLE, CACHE_QUIET_PAWN, CACHE_ROOK_PEXT_INDEX,
CACHE_ROOK_PEXT_MASK,
CACHE_BISHOP_PEXT_INDEX, CACHE_BISHOP_PEXT_MASK, CACHE_CAPTURE_PAWN, CACHE_KING, CACHE_KNIGHT,
CACHE_PEXT_TABLE, CACHE_ROOK_PEXT_INDEX, CACHE_ROOK_PEXT_MASK,
};
use crate::backend::constants::{SIDES, SQUARES_AMOUNT};
use crate::backend::state::board::bitboard::BitBoard;
Expand Down Expand Up @@ -35,13 +34,9 @@ use std::fs;
pub const KING_MOVES: [BitBoard; SQUARES_AMOUNT] = read_bb_cache(&CACHE_KING);
pub const KNIGHT_MOVES: [BitBoard; SQUARES_AMOUNT] = read_bb_cache(&CACHE_KNIGHT);

// Various pawn caches:
pub const PAWN_QUIET_MOVES: [[BitBoard; SQUARES_AMOUNT]; SIDES] =
read_2d_bb_cache(CACHE_QUIET_PAWN);
// Due for removal, only exists until check_decider gets reworked.
pub const PAWN_CAPTURE_MOVES: [[BitBoard; SQUARES_AMOUNT]; SIDES] =
read_2d_bb_cache(CACHE_CAPTURE_PAWN);
pub const PAWN_DOUBLE_PUSH_MOVES: [[BitBoard; SQUARES_AMOUNT]; SIDES] =
read_2d_bb_cache(CACHE_DOUBLE_PUSH_PAWN);

// Various slider caches:
pub const ROOK_PEXT_MASK: [BitBoard; SQUARES_AMOUNT] = read_bb_cache(&CACHE_ROOK_PEXT_MASK);
Expand Down Expand Up @@ -95,12 +90,8 @@ pub fn write_caches() {
let knight_moves = calculate_potential_moves_cache(Piece::Knight);
let knight_moves = knight_moves.map(|b| b.value);

let quiet_pawn_moves = generate_pawn_moves(PawnMoveType::Quiet);
let quiet_pawn_moves = quiet_pawn_moves.map(|a| a.map(|b| b.value));
let capture_pawn_moves = generate_pawn_moves(PawnMoveType::Capture);
let capture_pawn_moves = capture_pawn_moves.map(|a| a.map(|b| b.value));
let double_push_pawn_moves = generate_pawn_moves(PawnMoveType::DoublePush);
let double_push_pawn_moves = double_push_pawn_moves.map(|a| a.map(|b| b.value));

let pext_data = gen_cache_sliders();
let rook_pext_mask = pext_data.rook_pext_mask;
Expand All @@ -115,18 +106,10 @@ pub fn write_caches() {
let cache_strings = [
format!("pub const CACHE_KING: [u64; 64] = {:?};", king_moves),
format!("pub const CACHE_KNIGHT: [u64; 64] = {:?};", knight_moves),
format!(
"pub const CACHE_QUIET_PAWN: [[u64; 64]; 2] = {:?};",
quiet_pawn_moves
),
format!(
"pub const CACHE_CAPTURE_PAWN: [[u64; 64]; 2] = {:?};",
capture_pawn_moves
),
format!(
"pub const CACHE_DOUBLE_PUSH_PAWN: [[u64; 64]; 2] = {:?};",
double_push_pawn_moves
),
format!(
"pub const CACHE_ROOK_PEXT_MASK: [u64; 64] = {:?};",
rook_pext_mask
Expand Down
6 changes: 0 additions & 6 deletions src/backend/compile_time/generated/caches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,9 @@ pub const CACHE_KING: [u64; 64] = [770, 1797, 3594, 7188, 14376, 28752, 57504, 4
#[rustfmt::skip]
pub const CACHE_KNIGHT: [u64; 64] = [132096, 329728, 659712, 1319424, 2638848, 5277696, 10489856, 4202496, 33816580, 84410376, 168886289, 337772578, 675545156, 1351090312, 2685403152, 1075839008, 8657044482, 21609056261, 43234889994, 86469779988, 172939559976, 345879119952, 687463207072, 275414786112, 2216203387392, 5531918402816, 11068131838464, 22136263676928, 44272527353856, 88545054707712, 175990581010432, 70506185244672, 567348067172352, 1416171111120896, 2833441750646784, 5666883501293568, 11333767002587136, 22667534005174272, 45053588738670592, 18049583422636032, 145241105196122112, 362539804446949376, 725361088165576704, 1450722176331153408, 2901444352662306816, 5802888705324613632, 11533718717099671552, 4620693356194824192, 288234782788157440, 576469569871282176, 1224997833292120064, 2449995666584240128, 4899991333168480256, 9799982666336960512, 1152939783987658752, 2305878468463689728, 1128098930098176, 2257297371824128, 4796069720358912, 9592139440717824, 19184278881435648, 38368557762871296, 4679521487814656, 9077567998918656];

#[rustfmt::skip]
pub const CACHE_QUIET_PAWN: [[u64; 64]; 2] = [[256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968, 72057594037927936, 144115188075855872, 288230376151711744, 576460752303423488, 1152921504606846976, 2305843009213693952, 4611686018427387904, 9223372036854775808, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072, 262144, 524288, 1048576, 2097152, 4194304, 8388608, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 1099511627776, 2199023255552, 4398046511104, 8796093022208, 17592186044416, 35184372088832, 70368744177664, 140737488355328, 281474976710656, 562949953421312, 1125899906842624, 2251799813685248, 4503599627370496, 9007199254740992, 18014398509481984, 36028797018963968]];

#[rustfmt::skip]
pub const CACHE_CAPTURE_PAWN: [[u64; 64]; 2] = [[512, 1280, 2560, 5120, 10240, 20480, 40960, 16384, 131072, 327680, 655360, 1310720, 2621440, 5242880, 10485760, 4194304, 33554432, 83886080, 167772160, 335544320, 671088640, 1342177280, 2684354560, 1073741824, 8589934592, 21474836480, 42949672960, 85899345920, 171798691840, 343597383680, 687194767360, 274877906944, 2199023255552, 5497558138880, 10995116277760, 21990232555520, 43980465111040, 87960930222080, 175921860444160, 70368744177664, 562949953421312, 1407374883553280, 2814749767106560, 5629499534213120, 11258999068426240, 22517998136852480, 45035996273704960, 18014398509481984, 144115188075855872, 360287970189639680, 720575940379279360, 1441151880758558720, 2882303761517117440, 5764607523034234880, 11529215046068469760, 4611686018427387904, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 2, 5, 10, 20, 40, 80, 160, 64, 512, 1280, 2560, 5120, 10240, 20480, 40960, 16384, 131072, 327680, 655360, 1310720, 2621440, 5242880, 10485760, 4194304, 33554432, 83886080, 167772160, 335544320, 671088640, 1342177280, 2684354560, 1073741824, 8589934592, 21474836480, 42949672960, 85899345920, 171798691840, 343597383680, 687194767360, 274877906944, 2199023255552, 5497558138880, 10995116277760, 21990232555520, 43980465111040, 87960930222080, 175921860444160, 70368744177664, 562949953421312, 1407374883553280, 2814749767106560, 5629499534213120, 11258999068426240, 22517998136852480, 45035996273704960, 18014398509481984]];

#[rustfmt::skip]
pub const CACHE_DOUBLE_PUSH_PAWN: [[u64; 64]; 2] = [[0, 0, 0, 0, 0, 0, 0, 0, 16777216, 33554432, 67108864, 134217728, 268435456, 536870912, 1073741824, 2147483648, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4294967296, 8589934592, 17179869184, 34359738368, 68719476736, 137438953472, 274877906944, 549755813888, 0, 0, 0, 0, 0, 0, 0, 0]];

#[rustfmt::skip]
pub const CACHE_ROOK_PEXT_MASK: [u64; 64] = [282578800148862, 565157600297596, 1130315200595066, 2260630401190006, 4521260802379886, 9042521604759646, 18085043209519166, 36170086419038334, 282578800180736, 565157600328704, 1130315200625152, 2260630401218048, 4521260802403840, 9042521604775424, 18085043209518592, 36170086419037696, 282578808340736, 565157608292864, 1130315208328192, 2260630408398848, 4521260808540160, 9042521608822784, 18085043209388032, 36170086418907136, 282580897300736, 565159647117824, 1130317180306432, 2260632246683648, 4521262379438080, 9042522644946944, 18085043175964672, 36170086385483776, 283115671060736, 565681586307584, 1130822006735872, 2261102847592448, 4521664529305600, 9042787892731904, 18085034619584512, 36170077829103616, 420017753620736, 699298018886144, 1260057572672512, 2381576680245248, 4624614895390720, 9110691325681664, 18082844186263552, 36167887395782656, 35466950888980736, 34905104758997504, 34344362452452352, 33222877839362048, 30979908613181440, 26493970160820224, 17522093256097792, 35607136465616896, 9079539427579068672, 8935706818303361536, 8792156787827803136, 8505056726876686336, 7930856604974452736, 6782456361169985536, 4485655873561051136, 9115426935197958144];

Expand Down
233 changes: 142 additions & 91 deletions src/backend/movegen/move_gen_pawn_util.rs
Original file line number Diff line number Diff line change
@@ -1,123 +1,174 @@
use crate::backend::compile_time::gen_caches::{
PAWN_CAPTURE_MOVES, PAWN_DOUBLE_PUSH_MOVES, PAWN_QUIET_MOVES,
};
use crate::backend::movegen::moove::Moove;
use crate::backend::movegen::move_gen::iterate_over_bitboard_for_non_slider;
use crate::backend::state::board::bb_manager::BBManager;
use crate::backend::state::board::bitboard::BitBoard;
use crate::backend::state::game::state::State;
use crate::backend::state::piece::Piece::{Pawn, Queen};
use crate::backend::state::piece::Piece::Pawn;
use crate::backend::state::piece::{PROMOTABLE_PIECES, Side};
use crate::backend::state::square::Square;

const BLACK_PROMOTION_RANK_BB: BitBoard = BitBoard::new_from_rank(0);
const WHITE_PROMOTION_RANK_BB: BitBoard = BitBoard::new_from_rank(7);
const WHITE_PAWN_START_RANK_BB: BitBoard = BitBoard::new_from_rank(1);
const BLACK_PAWN_START_RANK_BB: BitBoard = BitBoard::new_from_rank(6);
const PROMOTION_RANKS_BB: BitBoard = BitBoard {
value: (BLACK_PROMOTION_RANK_BB.value | WHITE_PROMOTION_RANK_BB.value),
};
const LEFT_SIDE_BB: BitBoard = BitBoard::new_from_file(0);
const RIGHT_SIDE_BB: BitBoard = BitBoard::new_from_file(7);

pub fn gen_pawn_moves(
moves: &mut Vec<Moove>,
game_state: &State,
state: &State,
friendly_pieces_bb: BitBoard,
enemy_pieces_bb: BitBoard,
active_color: Side,
) {
let mut pawn_moves = Vec::new();
// Quiet pawn moves
iterate_over_bitboard_for_non_slider(
&mut pawn_moves,
PAWN_QUIET_MOVES[active_color as usize],
game_state
.bb_manager
.get_colored_piece_bb(Pawn, active_color),
friendly_pieces_bb | enemy_pieces_bb,
);
let occupancy_bb = friendly_pieces_bb | enemy_pieces_bb;
let pawn_bb = state.bb_manager.get_colored_piece_bb(Pawn, active_color);

let rank_offset = match active_color {
Side::White => -1,
Side::Black => 1,
};

// Capture pawn moves
iterate_over_bitboard_for_non_slider(
&mut pawn_moves,
PAWN_CAPTURE_MOVES[active_color as usize],
game_state
.bb_manager
.get_colored_piece_bb(Pawn, active_color),
create_pawn_capture_mask(game_state, enemy_pieces_bb),
// single push
single_push(moves, active_color, occupancy_bb, pawn_bb, rank_offset);

// double push
double_push(moves, active_color, occupancy_bb, pawn_bb, rank_offset);

let mut possible_captures_bb = enemy_pieces_bb;
match state.irreversible_data.en_passant_square {
None => {}
Some(square) => {
possible_captures_bb.fill_square(square);
}
}
// left captures
let shift = match active_color {
Side::White => 7,
Side::Black => -9,
};
one_dir_capture(
moves,
possible_captures_bb,
pawn_bb,
rank_offset,
shift,
LEFT_SIDE_BB,
1,
);
promotion_logic(&mut pawn_moves);
moves.append(&mut pawn_moves);

// Double pawn push moves
let mut pawn_moves = get_double_pawn_push_moves(
&game_state.bb_manager,
active_color,
friendly_pieces_bb | enemy_pieces_bb,

// right captures
let shift = match active_color {
Side::White => 9,
Side::Black => -7,
};
one_dir_capture(
moves,
possible_captures_bb,
pawn_bb,
rank_offset,
shift,
RIGHT_SIDE_BB,
-1,
);
moves.append(&mut pawn_moves);
}
pub fn get_double_pawn_push_moves(
bitboard_manager: &BBManager,
active_color: Side,
all_pieces_bb: BitBoard,
) -> Vec<Moove> {
let mut moves: Vec<Moove> = Vec::new();

let mut pawn_bitboard = bitboard_manager.get_colored_piece_bb(Pawn, active_color);

let starting_bitboard = match active_color {
Side::White => BitBoard::new_from_rank(1),
Side::Black => BitBoard::new_from_rank(6),
fn single_push(
moves: &mut Vec<Moove>,
active_color: Side,
occupancy_bb: BitBoard,
pawn_bb: BitBoard,
rank_offset: i8,
) {
let mut push_pawn_bb = match active_color {
Side::White => pawn_bb << 8,
Side::Black => pawn_bb >> 8,
};
// cant go there if something is there
push_pawn_bb &= !occupancy_bb;

pawn_bitboard &= starting_bitboard;
let no_promotion_push_pawn_bb = push_pawn_bb & !PROMOTION_RANKS_BB;
pawn_bb_to_moves_no_promotion(moves, no_promotion_push_pawn_bb, 0, rank_offset);

for square in pawn_bitboard {
let mut single_push_bb = PAWN_QUIET_MOVES[active_color as usize][square.square_to_index()];
single_push_bb &= all_pieces_bb;
if !single_push_bb.is_empty() {
continue;
}
let promotion_push_pawn_bb = push_pawn_bb & PROMOTION_RANKS_BB;
pawn_bb_to_moves_promotion(moves, promotion_push_pawn_bb, 0, rank_offset);
}

let mut double_push_bb =
PAWN_DOUBLE_PUSH_MOVES[active_color as usize][square.square_to_index()];
double_push_bb &= all_pieces_bb;
if !double_push_bb.is_empty() {
continue;
fn double_push(
moves: &mut Vec<Moove>,
active_color: Side,
occupancy_bb: BitBoard,
pawn_bb: BitBoard,
rank_offset: i8,
) {
let double_push_bb = match active_color {
Side::White => {
(((pawn_bb & WHITE_PAWN_START_RANK_BB) << 8) & !occupancy_bb) << 8 & !occupancy_bb
}
Side::Black => {
(((pawn_bb & BLACK_PAWN_START_RANK_BB) >> 8) & !occupancy_bb) >> 8 & !occupancy_bb
}
};
pawn_bb_to_moves_no_promotion(moves, double_push_bb, 0, 2 * rank_offset);
}

let moove = Moove::new(
square,
square
.forward_by_one(active_color)
.forward_by_one(active_color),
);
moves.push(moove);
fn one_dir_capture(
moves: &mut Vec<Moove>,
enemy_pieces_bb: BitBoard,
mut pawn_bb: BitBoard,
rank_offset: i8,
shift: i32,
mask: BitBoard,
file_offset: i8,
) {
pawn_bb &= !mask;

if shift.is_negative() {
pawn_bb >>= shift.unsigned_abs();
} else {
pawn_bb <<= shift;
}
let capture_bb = pawn_bb & enemy_pieces_bb;

let capture_no_promotion = capture_bb & !PROMOTION_RANKS_BB;
pawn_bb_to_moves_no_promotion(moves, capture_no_promotion, file_offset, rank_offset);

moves
let captures_promotion = capture_bb & PROMOTION_RANKS_BB;
pawn_bb_to_moves_promotion(moves, captures_promotion, file_offset, rank_offset);
}

pub fn create_pawn_capture_mask(game_state: &State, enemy_pieces_bitboard: BitBoard) -> BitBoard {
// Capture pawn moves
let mut pawn_capture_mask = enemy_pieces_bitboard;
match game_state.irreversible_data.en_passant_square {
None => {}
Some(ep_square) => {
pawn_capture_mask.fill_square(ep_square);
}
fn pawn_bb_to_moves_no_promotion(
moves: &mut Vec<Moove>,
pawn_bb: BitBoard,
file_offset: i8,
rank_offset: i8,
) {
for square in pawn_bb {
let from_square = Square {
file: square.file + file_offset,
rank: square.rank + rank_offset,
};
let moove = Moove::new(from_square, square);
moves.push(moove);
}
pawn_capture_mask = !pawn_capture_mask;
pawn_capture_mask
}

pub fn promotion_logic(moves: &mut Vec<Moove>) {
if moves.is_empty() {
return;
}
for index in (0..moves.len()).rev() {
let moove = moves[index];
if moove.to.is_on_promotion_rank() {
for piece_type in PROMOTABLE_PIECES {
if piece_type == Queen {
moves[index].promotion_type = Some(Queen);
continue;
}
let mut moove = moove;
moove.promotion_type = Some(piece_type);
moves.push(moove);
}
fn pawn_bb_to_moves_promotion(
moves: &mut Vec<Moove>,
pawn_bb: BitBoard,
file_offset: i8,
rank_offset: i8,
) {
for square in pawn_bb {
let from_square = Square {
file: square.file + file_offset,
rank: square.rank + rank_offset,
};
for piece_type in PROMOTABLE_PIECES {
let mut moove = Moove::new(from_square, square);
moove.promotion_type = Some(piece_type);
moves.push(moove);
}
}
}
Loading