From 6719e247a3e0c75261665eca166fc5c8c8fdaf27 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Mon, 23 Jan 2023 08:47:23 +0300 Subject: refactor: separately store pawn double pushes --- src/attacks.rs | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) (limited to 'src/attacks.rs') diff --git a/src/attacks.rs b/src/attacks.rs index 5399c96..6762bb3 100644 --- a/src/attacks.rs +++ b/src/attacks.rs @@ -45,6 +45,7 @@ pub struct Attacks { /// TODO: compute pawn attacks set-wise pub pawn: [AttackTable; 2], pub pawn_pushes: [AttackTable; 2], + pub pawn_double_pushes: [AttackTable; 2], pub first_rank_attacks: FirstRankAttacks, /// Should be indexed by Direction @@ -59,13 +60,14 @@ impl Attacks { let first_rank_attacks = Self::precompute_first_rank_attacks(); let ray_attacks = Self::precompute_ray_attacks(); let pawn = Self::precompute_pawn_attacks(); - let pawn_pushes = Self::precompute_pawn_pushes(); + let (pawn_pushes, pawn_double_pushes) = Self::precompute_pawn_pushes(); Self { knight, king, pawn, pawn_pushes, + pawn_double_pushes, first_rank_attacks, ray_attacks, } @@ -81,14 +83,19 @@ impl Attacks { attacks } - fn precompute_pawn_pushes() -> [AttackTable; 2] { + fn precompute_pawn_pushes() -> ([AttackTable; 2], [AttackTable; 2]) { let mut pushes = [[0; 64]; 2]; + let mut double_pushes = [[0; 64]; 2]; + for index in 0..64 { let square = 1u64 << index; - pushes[Color::White as usize][index] = (square << 8) | ((square & RANK_2) << 16); - pushes[Color::Black as usize][index] = (square >> 8) | ((square & RANK_6) >> 16); + pushes [Color::White as usize][index] = (square << 8); + double_pushes[Color::White as usize][index] = ((square & RANK_2) << 16); + + pushes [Color::Black as usize][index] = (square >> 8); + double_pushes[Color::Black as usize][index] = ((square & RANK_6) >> 16); } - pushes + (pushes, double_pushes) } fn precompute_knight_attacks() -> AttackTable { @@ -332,11 +339,16 @@ mod tests { #[test] fn test_pawn_pushes() { - let pushes = Attacks::precompute_pawn_pushes(); + let (pushes, double_pushes) = Attacks::precompute_pawn_pushes(); assert_eq!(pushes[Color::White as usize][Square::E4 as usize], Square::E5.to_bitboard()); - assert_eq!(pushes[Color::White as usize][Square::A2 as usize], Square::A3.to_bitboard() | Square::A4.to_bitboard()); + assert_eq!(pushes[Color::White as usize][Square::A2 as usize], Square::A3.to_bitboard()); assert_eq!(pushes[Color::Black as usize][Square::E4 as usize], Square::E3.to_bitboard()); - assert_eq!(pushes[Color::Black as usize][Square::H6 as usize], Square::H5.to_bitboard() | Square::H4.to_bitboard()); + assert_eq!(pushes[Color::Black as usize][Square::H6 as usize], Square::H5.to_bitboard()); + + assert_eq!(double_pushes[Color::White as usize][Square::E4 as usize], 0); + assert_eq!(double_pushes[Color::White as usize][Square::A2 as usize], Square::A4.to_bitboard()); + assert_eq!(double_pushes[Color::Black as usize][Square::E4 as usize], 0); + assert_eq!(double_pushes[Color::Black as usize][Square::H6 as usize], Square::H4.to_bitboard()); } #[test] -- cgit v1.2.3