Skip to content
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
29 changes: 16 additions & 13 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{
evaluation::correct_eval,
movepick::{MovePicker, Stage},
stack::Stack,
thread::{RootMove, Status, ThreadData},
thread::{PlyArray, RootMove, Status, ThreadData},
time::Limits,
transposition::{Bound, TtDepth},
types::{
Expand Down Expand Up @@ -125,6 +125,8 @@ pub fn start(td: &mut ThreadData, report: Report, thread_count: usize) {

loop {
td.stack = Stack::new();
td.cutoff_count = PlyArray::default();
td.excluded = PlyArray::default();
td.root_delta = beta - alpha;

// Root Search
Expand Down Expand Up @@ -269,7 +271,7 @@ fn search<NODE: NodeType>(

let stm = td.board.side_to_move();
let in_check = td.board.in_check();
let excluded = td.stack[ply].excluded.is_present();
let excluded = td.excluded[ply].is_present();

if !NODE::ROOT && NODE::PV {
td.pv_table.clear(ply as usize);
Expand Down Expand Up @@ -448,7 +450,7 @@ fn search<NODE: NodeType>(
td.stack[ply].tt_pv = tt_pv;
td.stack[ply].reduction = 0;
td.stack[ply].move_count = 0;
td.stack[ply + 2].cutoff_count = 0;
td.cutoff_count[ply + 2] = 0;

// Quiet move ordering using eval difference
if !NODE::ROOT && !in_check && !excluded && td.stack[ply - 1].mv.is_quiet() && is_valid(td.stack[ply - 1].eval) {
Expand Down Expand Up @@ -528,7 +530,7 @@ fn search<NODE: NodeType>(
>= beta
+ (-9 * depth + 108 * tt_pv as i32
- 96 * improvement / 1024
- 18 * (td.stack[ply + 1].cutoff_count < 2) as i32
- 18 * (td.cutoff_count[ply + 1] < 2) as i32
+ 320)
.max(2)
&& ply as i32 >= td.nmp_min_ply
Expand Down Expand Up @@ -600,7 +602,7 @@ fn search<NODE: NodeType>(
break;
}

if mv == td.stack[ply].excluded {
if mv == td.excluded[ply] {
continue;
}

Expand Down Expand Up @@ -653,10 +655,10 @@ fn search<NODE: NodeType>(
let singular_beta = tt_score - singular_margin;
let singular_depth = (depth - 1) / 2;

td.stack[ply].excluded = tt_move;
td.excluded[ply] = tt_move;
td.stack[ply].mv = Move::NULL;
singular_score = search::<NonPV>(td, singular_beta - 1, singular_beta, singular_depth, cut_node, ply);
td.stack[ply].excluded = Move::NULL;
td.excluded[ply] = Move::NULL;
td.stack[ply].tt_pv = tt_pv;

if td.shared.status.get() == Status::STOPPED {
Expand Down Expand Up @@ -705,7 +707,7 @@ fn search<NODE: NodeType>(
let mut tt_move_score = Score::NONE;

while let Some(mv) = move_picker.next::<NODE>(td, skip_quiets, ply) {
if mv == td.stack[ply].excluded {
if mv == td.excluded[ply] {
continue;
}

Expand All @@ -732,7 +734,8 @@ fn search<NODE: NodeType>(
&& !td.board.is_direct_check(mv)
&& is_quiet
&& !is_win(beta)
&& move_count >= (2697 + 77 * improvement / 16 + 1510 * depth * depth + 70 * history / 1024) / 1024
&& move_count as i32
>= (2697 + 77 * improvement / 16 + 1510 * depth * depth + 70 * history / 1024) / 1024
{
skip_quiets = true;
continue;
Expand Down Expand Up @@ -838,7 +841,7 @@ fn search<NODE: NodeType>(
reduction -= 939;
}

if td.stack[ply + 1].cutoff_count > 2 {
if td.cutoff_count[ply + 1] > 2 {
reduction += 992;
reduction += 384 * (!NODE::PV && !cut_node) as i32;
}
Expand Down Expand Up @@ -901,7 +904,7 @@ fn search<NODE: NodeType>(
reduction += (402 - 232 * improvement / 128).min(1426);
}

if td.stack[ply + 1].cutoff_count > 2 {
if td.cutoff_count[ply + 1] > 2 {
reduction += 1454;
reduction += 256 * (!NODE::PV && !cut_node) as i32;
}
Expand Down Expand Up @@ -995,7 +998,7 @@ fn search<NODE: NodeType>(

if score >= beta {
bound = Bound::Lower;
td.stack[ply].cutoff_count += 1;
td.cutoff_count[ply] += 1;
break;
}

Expand Down Expand Up @@ -1074,7 +1077,7 @@ fn search<NODE: NodeType>(
let prior_move = td.stack[ply - 1].mv;
if prior_move.is_quiet() {
let factor = 88
+ (17 * td.stack[ply - 1].move_count).min(229)
+ (17 * td.stack[ply - 1].move_count as i32).min(229)
+ 110 * (prior_move == td.stack[ply - 1].tt_move) as i32
+ 144 * (!in_check && best_score <= eval - 97) as i32
+ 306 * (is_valid(td.stack[ply - 1].eval) && best_score <= -td.stack[ply - 1].eval - 136) as i32;
Expand Down
6 changes: 1 addition & 5 deletions src/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,9 @@ pub struct StackEntry {
pub mv: Move,
pub piece: Piece,
pub eval: i32,
pub excluded: Move,
pub tt_move: Move,
pub tt_pv: bool,
pub cutoff_count: i32,
pub move_count: i32,
pub move_count: u16,
pub reduction: i32,
pub conthist: *mut [[i16; 64]; 13],
pub contcorrhist: *mut [[i16; 64]; 13],
Expand All @@ -55,10 +53,8 @@ impl Default for StackEntry {
mv: Move::NULL,
piece: Piece::None,
eval: Score::NONE,
excluded: Move::NULL,
tt_move: Move::NULL,
tt_pv: false,
cutoff_count: 0,
move_count: 0,
reduction: 0,
conthist: std::ptr::null_mut(),
Expand Down
36 changes: 33 additions & 3 deletions src/thread.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::sync::{
Arc,
atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering},
use std::{
ops::{Index, IndexMut},
sync::{
Arc,
atomic::{AtomicBool, AtomicU32, AtomicU64, AtomicUsize, Ordering},
},
};

use crate::{
Expand Down Expand Up @@ -132,6 +135,29 @@ impl Default for SharedContext {
}
}

pub struct PlyArray<T, const N: usize> {
data: [T; N],
}

impl<T, const N: usize> Index<isize> for PlyArray<T, N> {
type Output = T;
fn index(&self, index: isize) -> &T {
&self.data[(index + 8) as usize]
}
}

impl<T, const N: usize> IndexMut<isize> for PlyArray<T, N> {
fn index_mut(&mut self, index: isize) -> &mut T {
&mut self.data[(index + 8) as usize]
}
}

impl<T: Copy + Default, const N: usize> Default for PlyArray<T, N> {
fn default() -> Self {
Self { data: [T::default(); N] }
}
}

pub struct ThreadData {
pub id: usize,
pub shared: Arc<SharedContext>,
Expand All @@ -158,6 +184,8 @@ pub struct ThreadData {
pub pv_index: usize,
pub pv_start: usize,
pub pv_end: usize,
pub cutoff_count: PlyArray<i32, { MAX_PLY + 16 }>,
pub excluded: PlyArray<Move, { MAX_PLY + 16 }>,
}

impl ThreadData {
Expand Down Expand Up @@ -191,6 +219,8 @@ impl ThreadData {
pv_index: 0,
pv_start: 0,
pv_end: 0,
cutoff_count: PlyArray::default(),
excluded: PlyArray::default(),
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/types/moves.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,3 +159,9 @@ impl Move {
output
}
}

impl Default for Move {
fn default() -> Self {
Move::NULL
}
}
Loading