cozy-chesscozy-chess is a Chess and Chess960 (Fischer Random Chess) move generation library written in Rust that aims to provide competitive move generation performance. It is largely inspired by Jordan Bray's neat chess library. cozy-chess aims to be a safer alternative to chess that maintains correctness while providing similar performance.
no_std compatiblestd: Enable features that require std. Currently only used for the Error trait.pext: Enable PEXT bitboards.By default, Rust binaries target a baseline CPU to ensure maximum compatibility at the cost of performance. cozy-chess benefits significantly from features present in modern CPUs. For maximum performance, the target CPU can instead be set to native to use features supported by the machine running the build. Alternatively, the target CPU can be set to x86-64-v3, which will produce binaries that run on most modern CPUs. The target CPU may be changed by adding -C target-cpu=<CPU> to RUSTFLAGS.
PEXT bitboards are a faster variant of the magic bitboard algorithm used by cozy-chess. PEXT bitboards rely on an intrinsic introduced in the BMI2 CPU extension. However, it is not enabled by default, as PEXT bitboards are slower on AMD CPUs prior to Zen 3, which implement PEXT with microcode. PEXT bitboards can be enabled through the pext feature.
```rust
// Start position let board = Board::default(); let mut movelist = Vec::new(); board.generatemoves(|moves| { // Unpack dense move set into move list movelist.extend(moves); false }); asserteq!(move_list.len(), 20); ```
```rust
// Parse position from FEN
let board = "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1"
.parse::
let mut totalmoves = 0; let mut totalcaptures = 0;
let enemypieces = board.colors(!board.sidetomove()); board.generatemoves(|moves| { let mut captures = moves.clone(); // Bitmask to efficiently get all captures set-wise. // Excluding en passant square for convenience. captures.to &= enemy_pieces;
total_moves += moves.len();
total_captures += captures.len();
false
});
asserteq!(totalmoves, 48); asserteq!(totalcaptures, 8); ```
A perft implementation exists in examples/perft.rs:
text
$ cargo run --release --example perft -- 7
Compiling cozy-chess v0.3.0
Finished release [optimized] target(s) in 6.37s
Running `target\release\examples\perft.exe 7`
3195901860 nodes in 10.05s (318045465 nps)
pext feature. hash_without_ep method for fast equivalence checks excluding the en passant square.Board::same_position to check if two boards are equivalent under FIDE rules.Board::colored_pieces, a shorthand for board.colors(color) & board.pieces(piece).BitBoard::is_subset, BitBoard::is_superset, and BitBoard::is_disjoint.BitBoards now operate in a more set-wise manner instead of acting like a u64. Bit operators changed to match set operators.BitBoard::popcnt renamed to BitBoard::len for consistency with other data structures.BoardBuilder's fullmove_number field changed to a u16 for usability reasons.Board's FromStr implementation now parses both FEN and Shredder FEN.BitBoard no longer implements Iterator directly.const by default; Use the const variants if required.Board removed; The risk of panicking is accepted when *_unchecked methods are called.Square::try_offset fixed.FenParseError is no longer unnameable.