Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Quickly check for check in pseudo legality test #242

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "camel"
version = "1.5.1"
version = "1.5.2"
edition = "2021"

[dependencies]
Expand Down Expand Up @@ -30,3 +30,7 @@ harness = false
[[bench]]
name = "eval"
harness = false

[[bench]]
name = "see"
harness = false
33 changes: 33 additions & 0 deletions benches/see.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use camel::{
moves::gen::MoveStage,
position::{fen::FromFen, Position},
search::see,
};
use criterion::{criterion_group, criterion_main, Criterion};

fn see_1(c: &mut Criterion) {
let position =
Position::from_fen("rnbqkb1r/1p1p1ppp/p3pn2/8/2PNP3/8/PP3PPP/RNBQKB1R w KQkq - 1 6")
.unwrap();
let moves = position.moves(MoveStage::All);
let mov = moves.iter().find(|mov| mov.to_string() == "d4e6").unwrap();

c.bench_function("see_1", |b| {
b.iter(|| see::see::<false>(*mov, &position.board));
});
}

fn see_2(c: &mut Criterion) {
let position =
Position::from_fen("rnbqkb1r/1p1p1ppp/p3p3/8/2PNn3/2N5/PP3PPP/R1BQKB1R w KQkq - 0 7")
.unwrap();
let moves = position.moves(MoveStage::All);
let mov = moves.iter().find(|mov| mov.to_string() == "c3e4").unwrap();

c.bench_function("see_2", |b| {
b.iter(|| see::see::<false>(*mov, &position.board));
});
}

criterion_group!(see, see_1, see_2,);
criterion_main!(see);
14 changes: 13 additions & 1 deletion src/moves/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use self::{
attacks::specials::pawn_attacks,
gen::{piece_attacks, MoveDirection},
gen::{piece_attacks, square_attackers, MoveDirection},
};
use crate::position::{
bitboard::Bitboard, board::Piece, square::Square, CastlingRights, Color, Position,
Expand Down Expand Up @@ -130,6 +130,18 @@ impl Move {
return false;
}

// Basic check test for king moves.
if piece == Piece::King
&& square_attackers::<true>(
&position.board,
self.to(),
position.side_to_move.opposite(),
)
.is_not_empty()
{
return false;
}

match self.flag() {
MoveFlag::Quiet if piece == Piece::Pawn => {
to_color.is_none() && self.to().file() == self.from().file()
Expand Down
14 changes: 6 additions & 8 deletions src/search/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ pub struct SearchConstraint {

impl SearchConstraint {
pub fn should_stop_search(&self) -> bool {
if self.threads_stop.load(std::sync::atomic::Ordering::Acquire)
|| self.global_stop.load(std::sync::atomic::Ordering::Acquire)
{
if self.threads_stop.load(Ordering::Acquire) || self.global_stop.load(Ordering::Acquire) {
return true;
}

if self.ponder_mode.load(std::sync::atomic::Ordering::Acquire) {
if self.ponder_mode.load(Ordering::Acquire) {
return false;
}

Expand All @@ -44,7 +42,7 @@ impl SearchConstraint {
}

pub fn pondering(&self) -> bool {
self.ponder_mode.load(std::sync::atomic::Ordering::Acquire)
self.ponder_mode.load(Ordering::Acquire)
}

pub fn remaining_time(&self) -> Option<Duration> {
Expand All @@ -64,7 +62,7 @@ mod tests {
use crate::search::constraint::TimeConstraint;
use std::{
sync::{
atomic::{AtomicBool, AtomicU16},
atomic::{AtomicBool, AtomicU16, Ordering},
Arc,
},
thread,
Expand Down Expand Up @@ -98,7 +96,7 @@ mod tests {

#[test]
fn stop_search_external_order() {
let stop_now = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
let stop_now = std::sync::Arc::new(AtomicBool::new(false));
let constraint = SearchConstraint {
time_constraint: Some(TimeConstraint {
initial_instant: Instant::now(),
Expand All @@ -113,7 +111,7 @@ mod tests {

assert!(!constraint.should_stop_search());

stop_now.store(true, std::sync::atomic::Ordering::Release);
stop_now.store(true, Ordering::Release);

assert!(constraint.should_stop_search());
assert!(constraint.remaining_time().unwrap() > Duration::from_millis(90));
Expand Down
8 changes: 4 additions & 4 deletions src/search/see.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ pub fn see<const RETURN_EARLY: bool>(mov: Move, board: &Board) -> ValueScore {
}

// We need an auxiliary board to perform the search.
// We also store the max score when it is our turn.
// We also store a standing pat when it is our turn to move.
let mut board = *board;
let mut max_score = ValueScore::MIN;
let mut stand_pat = ValueScore::MIN;

// Make our move.
let mut on_square = piece;
Expand All @@ -41,7 +41,7 @@ pub fn see<const RETURN_EARLY: bool>(mov: Move, board: &Board) -> ValueScore {
if RETURN_EARLY && score >= 0 {
return score;
}
max_score = max_score.max(score);
stand_pat = stand_pat.max(score);
} else if RETURN_EARLY && score < 0 {
return score;
}
Expand All @@ -66,7 +66,7 @@ pub fn see<const RETURN_EARLY: bool>(mov: Move, board: &Board) -> ValueScore {
}
}

max_score.max(score)
stand_pat.max(score)
}

#[cfg(test)]
Expand Down
5 changes: 2 additions & 3 deletions src/search/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl TableEntry {
data: (score_type as u32)
| ((depth as u32 & 0x3F) << 2)
| ((age as u32) << 8)
| ((hash as u32) << 9),
| (hash as u32 & 0xFFFF_FE00),
}
}

Expand Down Expand Up @@ -79,8 +79,7 @@ impl TableEntry {
}

fn same_hash(&self, hash: u64) -> bool {
// We store 23 bits of the hash, so we need to compare the first 23 bits.
(self.data >> 9) == (hash as u32 & 0x7F_FFFF)
self.data & 0xFFFF_FE00 == (hash as u32 & 0xFFFF_FE00)
}

fn age(&self) -> bool {
Expand Down
Loading