1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use crate::prelude::*;
/// Encodes a move from one square to another on a chess board.
///
/// Users of this struct must be aware of and observe the following rules and
/// behavior:
///
/// * a move *must not* go from an origin to the same destination
/// * castling is encoded as a move from the king's starting square to the
/// rook's starting square
/// * the [`Move::promotion`] must not be called and for moves whose type is
/// not [`MoveType::Promotion`]
#[derive(Copy, Debug, Eq, PartialEq)]
#[derive_const(Clone)]
#[must_use]
pub struct Move(u16);
enumeration! {
/// Encodes the type of [`Move`] being made.
///
/// Note that a [`MoveType::EnPassant`] represents the *capture* of a pawn
/// en passant, not a two square pawn movement.
pub MoveType, [ Normal, Promotion, EnPassant, Castling ]
}
enumeration! {
/// Promotions can only result in four of the possible types of token, so we
/// create an enum of them specifically for the sake of bit-packing.
MovePromotion, [ Knight, Bishop, Rook, Queen ]
}
impl Move {
// A move needs 16 bits to be stored.
//
// * bit 0- 5: destination square (from 0 to 63)
// * bit 6-11: origin square (from 0 to 63)
// * bit 12-13: promotion token type - 2 (from KNIGHT-2 to QUEEN-2)
// * bit 14-15: special move flag: promotion (1), en passant (2), castling (3)
const DESTINATION_BITS: u8 = 6;
const ORIGIN_BITS: u8 = 6;
const PROMOTION_BITS: u8 = 2;
const MOVE_TYPE_BITS: u8 = 2;
const DESTINATION_SHIFT: u8 = 0;
const ORIGIN_SHIFT: u8 = Self::DESTINATION_SHIFT + Self::DESTINATION_BITS;
const PROMOTION_SHIFT: u8 = Self::ORIGIN_SHIFT + Self::ORIGIN_BITS;
const MOVE_TYPE_SHIFT: u8 = Self::PROMOTION_SHIFT + Self::PROMOTION_BITS;
const DESTINATION_MASK: u8 = (1 << Self::DESTINATION_BITS) - 1;
const ORIGIN_MASK: u8 = (1 << Self::ORIGIN_BITS) - 1;
const PROMOTION_MASK: u8 = (1 << Self::PROMOTION_BITS) - 1;
const MOVE_TYPE_MASK: u8 = (1 << Self::MOVE_TYPE_BITS) - 1;
/// Encodes a normal move from an `origin` square to a `destination` square.
pub const fn new(origin: Square, destination: Square) -> Self {
// TODO: in release builds, if the origin is the destination, this will
// produce logic errors; can we catch this without a performance
// penalty?
debug_assert!(origin != destination);
// ensure that squares actually are the size we think they are since
// we're going to bit-pack them
debug_assert!(origin .as_u8() < (1 << Self::ORIGIN_BITS));
debug_assert!(destination.as_u8() < (1 << Self::DESTINATION_BITS));
let bits_destination: u16 = destination.as_u8().into();
let bits_origin: u16 = origin .as_u8().into();
Self(
(bits_destination << Self::DESTINATION_SHIFT) |
(bits_origin << Self::ORIGIN_SHIFT) |
((MoveType::Normal as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Encodes a move of a pawn from an `origin` square to a `destination`
/// square that results in promotion to a knight.
pub const fn new_promote_knight(origin: Square, destination: Square) -> Self {
Self(
Self::new(origin, destination).0
| ((MovePromotion::Knight as u16) << Self::PROMOTION_SHIFT)
| ((MoveType::Promotion as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Encodes a move of a pawn from an `origin` square to a `destination`
/// square that results in promotion to a bishop.
pub const fn new_promote_bishop(origin: Square, destination: Square) -> Self {
Self(
Self::new(origin, destination).0
| ((MovePromotion::Bishop as u16) << Self::PROMOTION_SHIFT)
| ((MoveType::Promotion as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Encodes a move of a pawn from an `origin` square to a `destination`
/// square that results in promotion to a rook.
pub const fn new_promote_rook(origin: Square, destination: Square) -> Self {
Self(
Self::new(origin, destination).0
| ((MovePromotion::Rook as u16) << Self::PROMOTION_SHIFT)
| ((MoveType::Promotion as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Encodes a move of a pawn from an `origin` square to a `destination`
/// square that results in promotion to a queen.
pub const fn new_promote_queen(origin: Square, destination: Square) -> Self {
Self(
Self::new(origin, destination).0
| ((MovePromotion::Queen as u16) << Self::PROMOTION_SHIFT)
| ((MoveType::Promotion as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Encodes the capture of a pawn en passant by a token starting on
/// `origin` and ending on the `pawn`'s square.
pub const fn new_en_passant(origin: Square, pawn: Square) -> Self {
Self(
Self::new(origin, pawn).0
| ((MoveType::EnPassant as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Encodes castling between `king` and a `rook` on their respective
/// starting squares. The destination squares must be inferred by the rules
/// of castling.
pub const fn new_castling(king: Square, rook: Square) -> Self {
Self(
Self::new(king, rook).0
| ((MoveType::Castling as u16) << Self::MOVE_TYPE_SHIFT)
)
}
/// Returns the square the moving token began on.
pub const fn origin(self) -> Square {
let bits = self.extract(Self::ORIGIN_SHIFT, Self::ORIGIN_MASK);
unsafe_optimization!(
Square::from_u8(bits).unwrap(),
Square::from_u8_unchecked(bits),
)
}
/// For all types of move other than [`MoveType::Castling`], returns the
/// square the token finishes on.
///
/// For castling, encodes the position the rook begins on. The actual
/// destination square of the rook and king must be inferred by the rules of
/// castling.
pub const fn destination(self) -> Square {
let bits = self.extract(Self::DESTINATION_SHIFT, Self::DESTINATION_MASK);
unsafe_optimization!(
Square::from_u8(bits).unwrap(),
Square::from_u8_unchecked(bits),
)
}
/// Returns the type of move encoded.
pub const fn move_type(self) -> MoveType {
let bits = self.extract(Self::MOVE_TYPE_SHIFT, Self::MOVE_TYPE_MASK);
unsafe_optimization!(
MoveType::from_u8(bits).unwrap(),
MoveType::from_u8_unchecked(bits),
)
}
/// Returns the type of token a pawn is being promoted to during this move.
///
/// This method *must not* be called unless the move is a
/// [`MoveType::Promotion`]. For all other move types, the result of this
/// method is undefined and must not be expected to produce reliable
/// behavior.
pub const fn promotion(self) -> Token {
debug_assert!(MoveType::Promotion == self.move_type());
// We exclude pawns and kings from the type of token that may be
// promoted to (so that this information fits in two bits), so we need
// to add the value of a knight in order to get the value of the token
// being promoted to.
//
// This conversion could/should be implemented on the MovePromotion enum
// itself, but given that it's *only* used here that's overkill.
let bits = self.extract(Self::PROMOTION_SHIFT, Self::PROMOTION_MASK)
+ Token::Knight.as_u8();
unsafe_optimization!(
Token::from_u8(bits).unwrap(),
Token::from_u8_unchecked(bits),
)
}
const fn extract(self, shift: u8, mask: u8) -> u8 {
#[allow(clippy::cast_possible_truncation)] {
(self.0 >> shift) as u8 & mask
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn derives() {
let mv = Move::new(Square::A1, Square::A2);
assert_eq!(mv, mv.clone());
assert_ne!("", format!("{mv:?}"));
}
#[test]
fn new() {
let mv = Move::new(Square::C3, Square::F7);
assert_eq!(Square::C3, mv.origin());
assert_eq!(Square::F7, mv.destination());
assert_eq!(MoveType::Normal, mv.move_type());
}
#[test]
fn new_promote_knight() {
let mv = Move::new_promote_knight(Square::D6, Square::B1);
assert_eq!(Square::D6, mv.origin());
assert_eq!(Square::B1, mv.destination());
assert_eq!(MoveType::Promotion, mv.move_type());
assert_eq!(Token::Knight, mv.promotion());
}
#[test]
fn new_promote_bishop() {
let mv = Move::new_promote_bishop(Square::C6, Square::C7);
assert_eq!(Square::C6, mv.origin());
assert_eq!(Square::C7, mv.destination());
assert_eq!(MoveType::Promotion, mv.move_type());
assert_eq!(Token::Bishop, mv.promotion());
}
#[test]
fn new_promote_rook() {
let mv = Move::new_promote_rook(Square::A8, Square::A7);
assert_eq!(Square::A8, mv.origin());
assert_eq!(Square::A7, mv.destination());
assert_eq!(MoveType::Promotion, mv.move_type());
assert_eq!(Token::Rook, mv.promotion());
}
#[test]
fn new_promote_queen() {
let mv = Move::new_promote_queen(Square::C5, Square::C4);
assert_eq!(Square::C5, mv.origin());
assert_eq!(Square::C4, mv.destination());
assert_eq!(MoveType::Promotion, mv.move_type());
assert_eq!(Token::Queen, mv.promotion());
}
#[test]
fn new_en_passant() {
let mv = Move::new_en_passant(Square::G7, Square::E1);
assert_eq!(Square::G7, mv.origin());
assert_eq!(Square::E1, mv.destination());
assert_eq!(MoveType::EnPassant, mv.move_type());
}
#[test]
fn new_castling() {
let mv = Move::new_castling(Square::E8, Square::H8);
assert_eq!(Square::E8, mv.origin());
assert_eq!(Square::H8, mv.destination());
assert_eq!(MoveType::Castling, mv.move_type());
}
}