Skip to content

Commit 19d784d

Browse files
authored
Simplify promotion piece types (#886)
STC Elo | 1.67 +- 2.12 (95%) SPRT | 8.0+0.08s Threads=1 Hash=16MB LLR | 2.96 (-2.25, 2.89) [-2.75, 0.25] Games | N: 25954 W: 6717 L: 6592 D: 12645 Penta | [95, 2823, 7024, 2932, 103] https://recklesschess.space/test/13365/ No functional change. Bench: 2864276
1 parent da19c92 commit 19d784d

6 files changed

Lines changed: 19 additions & 21 deletions

File tree

src/board/makemove.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl Board {
111111
self.update_hash(rook, rook_to);
112112
}
113113
_ if mv.is_promotion() => {
114-
let promotion = Piece::new(stm, mv.promotion_piece().unwrap());
114+
let promotion = Piece::new(stm, mv.promo_piece_type());
115115

116116
self.remove_piece(piece, to);
117117
self.add_piece(promotion, to);

src/board/see.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ impl super::Board {
2323
// In the worst case, we lose a piece, but still end up with a non-negative balance
2424
balance -= self.piece_on(mv.from()).value();
2525

26-
if let Some(promotion) = mv.promotion_piece() {
27-
balance -= promotion.value();
26+
if mv.is_promotion() {
27+
balance -= mv.promo_piece_type().value();
2828
}
2929

3030
if balance >= 0 {
@@ -98,12 +98,12 @@ impl super::Board {
9898
}
9999

100100
let capture = self.piece_on(mv.to()).piece_type();
101+
let mut value = capture.value();
101102

102-
if let Some(promotion) = mv.promotion_piece() {
103-
capture.value() + promotion.value() - PieceType::Pawn.value()
104-
} else {
105-
capture.value()
103+
if mv.is_promotion() {
104+
value += mv.promo_piece_type().value() - PieceType::Pawn.value()
106105
}
106+
value
107107
}
108108

109109
fn least_valuable_attacker(&self, attackers: Bitboard) -> PieceType {

src/movepick.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl MovePicker {
171171
entry.score =
172172
16 * captured.value() + td.noisy_history.get(threats, td.board.moved_piece(mv), mv.to(), captured);
173173

174-
if mv.is_promotion() && mv.promotion_piece() == Some(PieceType::Queen) {
174+
if mv.is_promotion() && mv.promo_piece_type() == PieceType::Queen {
175175
entry.score += 4000;
176176
}
177177
}

src/nnue/accumulator/psq.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ impl PstAccumulator {
105105
pub fn update(&mut self, prev: &Self, board: &Board, king: Square, pov: Color) {
106106
let PstDelta { mv, piece, captured } = self.delta;
107107

108-
let resulting_piece = mv.promotion_piece().unwrap_or_else(|| piece.piece_type());
108+
let resulting_piece = if mv.is_promotion() { mv.promo_piece_type() } else { piece.piece_type() };
109109

110110
let add1 = pst_index(piece.piece_color(), resulting_piece, mv.to(), king, pov);
111111
let sub1 = pst_index(piece.piece_color(), piece.piece_type(), mv.from(), king, pov);

src/tb.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ fn reckless_move_to_tb_move(mv: Move) -> TbMove {
7979

8080
let mut tb_move: TbMove = (from << 6) | to;
8181

82-
if let Some(pt) = mv.promotion_piece() {
83-
let promotion_bits = promo_bits_from_piece(pt) & 0x7;
82+
if mv.is_promotion() {
83+
let promotion_bits = promo_bits_from_piece(mv.promo_piece_type()) & 0x7;
8484
tb_move |= promotion_bits << 12;
8585
}
8686

src/types/moves.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,9 @@ impl Move {
106106
matches!(self.kind(), MoveKind::DoublePush)
107107
}
108108

109-
pub const fn promotion_piece(self) -> Option<PieceType> {
110-
if self.is_promotion() {
111-
return Some(PieceType::new(((self.kind() as usize) & 3) + PieceType::Knight as usize));
112-
}
113-
None
109+
pub const fn promo_piece_type(self) -> PieceType {
110+
debug_assert!(self.is_promotion());
111+
PieceType::new(((self.kind() as usize) & 3) + PieceType::Knight as usize)
114112
}
115113

116114
pub fn to_uci(self, board: &Board) -> String {
@@ -124,11 +122,11 @@ impl Move {
124122
let mut output = format!("{}{}", self.from(), self.to());
125123

126124
if self.is_promotion() {
127-
match self.promotion_piece() {
128-
Some(PieceType::Knight) => output.push('n'),
129-
Some(PieceType::Bishop) => output.push('b'),
130-
Some(PieceType::Rook) => output.push('r'),
131-
Some(PieceType::Queen) => output.push('q'),
125+
match self.promo_piece_type() {
126+
PieceType::Knight => output.push('n'),
127+
PieceType::Bishop => output.push('b'),
128+
PieceType::Rook => output.push('r'),
129+
PieceType::Queen => output.push('q'),
132130
_ => (),
133131
}
134132
}

0 commit comments

Comments
 (0)