Skip to content

Commit

Permalink
Add more debug_asserts
Browse files Browse the repository at this point in the history
  • Loading branch information
Tearth committed Sep 22, 2024
1 parent 397590c commit 6798754
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 10 deletions.
6 changes: 6 additions & 0 deletions common/src/time/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub struct DateTime {
impl DateTime {
/// Constructs a new instance of [DateTime] with `year`, `month`, `day`, `hour`, `minute` and `second`.
pub fn new(year: u16, month: u8, day: u8, hour: u8, minute: u8, second: u8) -> DateTime {
debug_assert!(month <= 12);
debug_assert!(day <= 31);
debug_assert!(hour < 24);
debug_assert!(minute < 60);
debug_assert!(second < 60);

DateTime { year, month, day, hour, minute, second }
}

Expand Down
10 changes: 7 additions & 3 deletions src/cache/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ impl TranspositionTable {
hashtable
}

/// Adds a new entry (storing the key, `score`, `best_move`, `depth`, `ply`, `score_type` and `age`) using `hash` to calculate an index of the bucket.
/// Adds a new entry (storing the key, `score`, `best_move`, `depth`, `ply`, `r#type` and `age`) using `hash` to calculate an index of the bucket.
/// Replacement strategy considers a few elements to optimize memory usage and prioritizes slots to replace as follows:
/// - empty slots or slots with the same key as the new entry
/// - slots with the smallest depth (if there are some old entries, prioritize them)
///
/// This function takes care of converting mate `score` using passed `ply`.
pub fn add(&self, hash: u64, mut score: i16, best_move: Move, depth: i8, ply: u16, score_type: u8, age: u8) {
pub fn add(&self, hash: u64, mut score: i16, best_move: Move, depth: i8, ply: u16, r#type: u8, age: u8) {
debug_assert!(r#type == 1 || r#type == 2 || r#type == 4);

let key = self.get_key(hash);
let index = self.get_index(hash);
let bucket = &self.table[index];
Expand Down Expand Up @@ -107,7 +109,7 @@ impl TranspositionTable {
}
}

bucket.entries[desired_index].set_data(key, score, best_move, depth, score_type, age);
bucket.entries[desired_index].set_data(key, score, best_move, depth, r#type, age);
}

/// Gets a wanted entry using `hash` to calculate an index of the bucket. This function takes care of converting
Expand Down Expand Up @@ -252,6 +254,8 @@ impl TranspositionTableEntry {

/// Converts `key`, `score`, `best_move`, `depth`, `r#type` and `age` into an atomic word, and stores it.
pub fn set_data(&self, key: u16, score: i16, best_move: Move, depth: i8, r#type: u8, age: u8) {
debug_assert!(r#type == 1 || r#type == 2 || r#type == 4);

let key_data = 0
| (key as u64)
| (((score as u16) as u64) << 16)
Expand Down
6 changes: 3 additions & 3 deletions src/engine/see.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl SEEContainer {

/// Recursive function called by `evaluate` to help evaluate a static exchange evaluation result.
fn evaluate_internal(&self, attacking_piece: usize, target_piece: usize, attackers: usize, defenders: usize) -> i8 {
debug_assert!(target_piece <= 7);
debug_assert!(target_piece < 8);

if attackers == 0 {
return 0;
Expand All @@ -70,7 +70,7 @@ impl SEEContainer {
/// - 1 queen (index 6)
/// - 1 king (index 7)
fn get_see_piece_index(&self, piece: usize) -> usize {
debug_assert!(piece <= 6);
debug_assert!(piece < 6);

match piece {
PAWN => 0,
Expand All @@ -85,7 +85,7 @@ impl SEEContainer {

/// Gets a piece value based on `piece_index` saved in SEE format (look `get_see_piece_index`).
fn get_piece_value(&self, piece_index: usize) -> i8 {
debug_assert!(piece_index <= 7);
debug_assert!(piece_index < 8);

match piece_index {
0 => SEE_PAWN_VALUE, // Pawn
Expand Down
2 changes: 0 additions & 2 deletions src/evaluation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@ impl PackedEval {
/// - 0 represents a board without any piece (ending phase)
/// - every value between them represents a board state somewhere in the middle game
pub fn taper_score(&self, game_phase: u8) -> i16 {
debug_assert!(game_phase <= 24);

let opening_score = (self.get_opening() as i32) * (game_phase as i32);
let ending_score = (self.get_ending() as i32) * ((INITIAL_GAME_PHASE as i32) - (game_phase as i32));

Expand Down
4 changes: 4 additions & 0 deletions src/evaluation/pawns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub fn evaluate_without_cache(board: &Board) -> PackedEval {

/// Evaluates pawn structure on the `board` for the specified `color`.
fn evaluate_color(board: &Board, color: usize) -> PackedEval {
debug_assert!(color < 2);

let mut result = PackedEval::default();
let pawns_data = get_pawns_data(board, color);

Expand All @@ -72,6 +74,8 @@ fn evaluate_color(board: &Board, color: usize) -> PackedEval {

/// Gets all pawn features on `board` for `color`.
fn get_pawns_data(board: &Board, color: usize) -> PawnsData {
debug_assert!(color < 2);

let mut doubled_pawns = 0;
let mut isolated_pawns = 0;
let mut chained_pawns = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/evaluation/pst/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub fn recalculate_incremental_values(board: &mut Board) {

/// Gets a PST value for the specified `color`, `piece`, `phase` and `square`.
pub fn get_pst_value(piece: usize, king_square: usize, square: usize) -> PackedEval {
debug_assert!(piece <= 6);
debug_assert!(piece < 6);
debug_assert!(king_square < 64);
debug_assert!(square < 64);

Expand All @@ -93,7 +93,7 @@ pub fn get_pst_value(piece: usize, king_square: usize, square: usize) -> PackedE
/// Similarly, their indices (starting from `index`) are inserted into `indices`.
#[cfg(feature = "dev")]
pub fn get_coefficients(board: &Board, piece: usize, index: &mut u16, coefficients: &mut Vec<TunerCoefficient>, indices: &mut Vec<u16>) {
debug_assert!(piece <= 6);
debug_assert!(piece < 6);

for bucket in 0..KING_BUCKETS_COUNT {
let valid_for_white = bucket == KING_BUCKETS[63 - board.pieces[WHITE][KING].bit_scan()];
Expand Down
25 changes: 25 additions & 0 deletions src/state/movegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ pub struct MagicSquare {
impl MagicContainer {
/// Gets a rook moves for the square specified by `square`, considering `occupancy_bb`.
pub fn get_rook_moves(&self, mut occupancy_bb: u64, square: usize) -> u64 {
debug_assert!(square < 64);

occupancy_bb &= self.rook_squares[square].mask;
occupancy_bb = occupancy_bb.wrapping_mul(self.rook_squares[square].magic);
occupancy_bb >>= 64 - self.rook_squares[square].shift;
Expand All @@ -188,6 +190,8 @@ impl MagicContainer {

/// Gets a bishop moves for the square specified by `square`, considering `occupancy_bb`.
pub fn get_bishop_moves(&self, mut occupancy_bb: u64, square: usize) -> u64 {
debug_assert!(square < 64);

occupancy_bb &= self.bishop_squares[square].mask;
occupancy_bb = occupancy_bb.wrapping_mul(self.bishop_squares[square].magic);
occupancy_bb >>= 64 - self.bishop_squares[square].shift;
Expand All @@ -197,21 +201,26 @@ impl MagicContainer {

/// Gets a queen moves for the square specified by `square`, considering `occupancy_bb`.
pub fn get_queen_moves(&self, occupancy_bb: u64, square: usize) -> u64 {
debug_assert!(square < 64);
self.get_rook_moves(occupancy_bb, square) | self.get_bishop_moves(occupancy_bb, square)
}

/// Gets a knight moves for the square specified by `square`, without considering an occupancy.
pub fn get_knight_moves(&self, square: usize, patterns: &PatternsContainer) -> u64 {
debug_assert!(square < 64);
patterns.get_jumps(square)
}

/// Gets a king moves for the square specified by `square`, without considering an occupancy.
pub fn get_king_moves(&self, square: usize, patterns: &PatternsContainer) -> u64 {
debug_assert!(square < 64);
patterns.get_box(square)
}

/// Generates a rook magic number for the square specified by `square`.
pub fn generate_rook_magic_number(&self, square: usize) -> u64 {
debug_assert!(square < 64);

let patterns = Arc::new(PatternsContainer::default());

let shift = ROOK_SHIFTS[square];
Expand All @@ -234,6 +243,8 @@ impl MagicContainer {

/// Generates a bishop magic number for the square specified by `square`.
pub fn generate_bishop_magic_number(&self, square: usize) -> u64 {
debug_assert!(square < 64);

let patterns = Arc::new(PatternsContainer::default());

let shift = BISHOP_SHIFTS[square];
Expand Down Expand Up @@ -288,6 +299,8 @@ impl MagicContainer {

/// Applies rook magic for the square specified by `square`, using built-in magic number from [ROOK_MAGIC_NUMBERS].
fn apply_rook_magic(&mut self, square: usize) {
debug_assert!(square < 64);

let patterns = Arc::new(PatternsContainer::default());

let shift = ROOK_SHIFTS[square];
Expand All @@ -313,6 +326,8 @@ impl MagicContainer {

/// Applies bishop magic for the square specified by `square`, using built-in magic number from [BISHOP_MAGIC_NUMBERS].
fn apply_bishop_magic(&mut self, square: usize) {
debug_assert!(square < 64);

let patterns = Arc::new(PatternsContainer::default());

let shift = BISHOP_SHIFTS[square];
Expand Down Expand Up @@ -371,16 +386,20 @@ impl MagicContainer {

/// Gets a rook mask for the square specified by `square`, without considering occupancy.
fn get_rook_mask(&self, square: usize, patterns: &PatternsContainer) -> u64 {
debug_assert!(square < 64);
(patterns.get_file(square) & !RANK_1_BB & !RANK_8_BB) | (patterns.get_rank(square) & !FILE_A_BB & !FILE_H_BB)
}

/// Gets a bishop mask for the square specified by `square`, without considering occupancy.
fn get_bishop_mask(&self, square: usize, patterns: &PatternsContainer) -> u64 {
debug_assert!(square < 64);
patterns.get_diagonals(square) & !EDGE_BB
}

/// Gets a rook attacks for the square specified by `square`, considering `occupancy_bb`.
fn get_rook_attacks(&self, occupancy_bb: u64, square: usize) -> u64 {
debug_assert!(square < 64);

let left = self.get_attacks(occupancy_bb, square, (-1, 0));
let right = self.get_attacks(occupancy_bb, square, (1, 0));
let top = self.get_attacks(occupancy_bb, square, (0, 1));
Expand All @@ -391,6 +410,8 @@ impl MagicContainer {

/// Gets a bishop attacks for the square specified by `square`, occupancy `occupancy_bb`.
fn get_bishop_attacks(&self, occupancy_bb: u64, square: usize) -> u64 {
debug_assert!(square < 64);

let top_right = self.get_attacks(occupancy_bb, square, (1, 1));
let top_left = self.get_attacks(occupancy_bb, square, (1, -1));
let down_right = self.get_attacks(occupancy_bb, square, (-1, 1));
Expand All @@ -402,6 +423,10 @@ impl MagicContainer {
/// Helper function to get all possible to move squares, considering `occupancy_bb`, starting from the square
/// specified by `square` and going into the `direction`.
fn get_attacks(&self, occupancy_bb: u64, square: usize, direction: (isize, isize)) -> u64 {
debug_assert!(square < 64);
debug_assert!(direction.0 >= -1 && direction.0 <= 1);
debug_assert!(direction.1 >= -1 && direction.1 <= 1);

let mut result = 0u64;
let mut current = ((square as isize) % 8 + direction.0, (square as isize) / 8 + direction.1);

Expand Down
2 changes: 2 additions & 0 deletions src/state/movescan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ pub fn scan_piece_moves<const PIECE: usize, const CAPTURES: bool>(
/// Gets `PIECE` mobility (by counting all possible moves at the position specified by `board`) with `color` and increases `dangered_king_squares` if the enemy
/// king is near to the squares included in the mobility.
pub fn get_piece_mobility<const PIECE: usize>(board: &Board, color: usize, aux: &mut MobilityAuxData) -> PieceMobility {
debug_assert!(color < 2);

let mut pieces_bb = board.pieces[color][PIECE];
let mut mobility_inner = 0;
let mut mobility_outer = 0;
Expand Down
10 changes: 10 additions & 0 deletions src/state/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ impl PatternsContainer {
/// . . . x . . . .
/// ```
pub fn get_file(&self, square: usize) -> u64 {
debug_assert!(square < 64);
self.file_patterns[square]
}

Expand All @@ -39,6 +40,7 @@ impl PatternsContainer {
/// . . . . . . . .
/// ```
pub fn get_rank(&self, square: usize) -> u64 {
debug_assert!(square < 64);
self.rank_patterns[square]
}

Expand All @@ -54,6 +56,7 @@ impl PatternsContainer {
/// x . . . . . x .
/// ```
pub fn get_diagonals(&self, square: usize) -> u64 {
debug_assert!(square < 64);
self.diagonal_patterns[square]
}

Expand All @@ -69,6 +72,7 @@ impl PatternsContainer {
/// . . . . . . . .
/// ```
pub fn get_jumps(&self, square: usize) -> u64 {
debug_assert!(square < 64);
self.jump_patterns[square]
}

Expand All @@ -84,6 +88,7 @@ impl PatternsContainer {
/// . . . . . . . .
/// ```
pub fn get_box(&self, square: usize) -> u64 {
debug_assert!(square < 64);
self.box_patterns[square]
}

Expand All @@ -99,6 +104,7 @@ impl PatternsContainer {
/// . . x . x . . .
/// ```
pub fn get_rail(&self, file: usize) -> u64 {
debug_assert!(file < 8);
self.rail_patterns[file]
}

Expand All @@ -114,6 +120,7 @@ impl PatternsContainer {
/// . . . . . . . .
/// ```
pub fn get_star(&self, square: usize) -> u64 {
debug_assert!(square < 64);
self.star_patterns[square]
}

Expand All @@ -129,6 +136,9 @@ impl PatternsContainer {
/// . . . . . . . .
/// ```
pub fn get_front(&self, color: usize, square: usize) -> u64 {
debug_assert!(color < 2);
debug_assert!(square < 64);

self.front_patterns[color][square]
}

Expand Down
Loading

0 comments on commit 6798754

Please sign in to comment.