@@ -10,6 +10,22 @@ pub struct HistoryTable {
1010 table : [ [ [ LargeScoreType ; NumberOf :: SQUARES ] ; NumberOf :: PIECE_TYPES ] ; NumberOf :: SIDES ] ,
1111}
1212
13+ /// Safe calculation of the bonus applied to quiet moves that are inserted into the history table.
14+ /// This uses `wrappinag_mul` and `wrapping_sub` to safely calculate the value.
15+ ///
16+ /// # Arguments
17+ ///
18+ /// - depth: The current depth
19+ ///
20+ /// # Returns
21+ ///
22+ /// The calculated history score.
23+ pub ( crate ) fn calculate_bonus_for_depth ( depth : i16 ) -> i16 {
24+ depth
25+ . saturating_mul ( Score :: HISTORY_MULT )
26+ . saturating_sub ( Score :: HISTORY_OFFSET )
27+ }
28+
1329impl HistoryTable {
1430 pub ( crate ) fn new ( ) -> Self {
1531 let table =
@@ -63,7 +79,9 @@ impl Default for HistoryTable {
6379
6480#[ cfg( test) ]
6581mod tests {
66- use super :: HistoryTable ;
82+ use crate :: defs:: MAX_DEPTH ;
83+
84+ use super :: { HistoryTable , calculate_bonus_for_depth} ;
6785 use chess:: { definitions:: Squares , pieces:: Piece , side:: Side } ;
6886
6987 #[ test]
@@ -94,4 +112,13 @@ mod tests {
94112 history_table. update ( side, piece, square, score) ;
95113 assert_eq ! ( history_table. get( side, piece, square) , score + score) ;
96114 }
115+
116+ #[ test]
117+ fn calculate_bonus_for_any_depth ( ) {
118+ for depth in 1 ..MAX_DEPTH {
119+ let bonus = calculate_bonus_for_depth ( depth as i16 ) ;
120+ assert ! ( bonus > 0 ) ;
121+ assert ! ( bonus as i32 <= i16 :: MAX . into( ) ) ;
122+ }
123+ }
97124}
0 commit comments