Skip to content

Commit

Permalink
Simplify array initializations
Browse files Browse the repository at this point in the history
Simplify a few array initializations, also retire a few std::memset calls.
  • Loading branch information
PikaCat-OuO committed Jan 31, 2024
1 parent 2c5ca25 commit 65e32c7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 26 deletions.
40 changes: 20 additions & 20 deletions src/search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <initializer_list>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -179,21 +178,26 @@ void Search::Worker::start_searching() {
// consumed, the user stops the search, or the maximum search depth is reached.
void Search::Worker::iterative_deepening() {

SearchManager* mainThread = (thread_idx == 0 ? main_manager() : nullptr);

Move pv[MAX_PLY + 1];

Depth lastBestMoveDepth = 0;
Value lastBestScore = -VALUE_INFINITE;
auto lastBestPV = std::vector{Move::none()};

Value alpha, beta;
Value bestValue = -VALUE_INFINITE;
Color us = rootPos.side_to_move();
double timeReduction = 1, totBestMoveChanges = 0;
int delta, iterIdx = 0;

// Allocate stack with extra size to allow access from (ss - 7) to (ss + 2):
// (ss - 7) is needed for update_continuation_histories(ss - 1) which accesses (ss - 6),
// (ss + 2) is needed for initialization of cutOffCnt and killers.
Stack stack[MAX_PLY + 10], *ss = stack + 7;
Move pv[MAX_PLY + 1];
Value alpha, beta;
Value lastBestScore = -VALUE_INFINITE;
std::vector<Move> lastBestPV = {Move::none()};
Depth lastBestMoveDepth = 0;
SearchManager* mainThread = (thread_idx == 0 ? main_manager() : nullptr);
double timeReduction = 1, totBestMoveChanges = 0;
Color us = rootPos.side_to_move();
int delta, iterIdx = 0;

std::memset(ss - 7, 0, 10 * sizeof(Stack));
Stack stack[MAX_PLY + 10] = {};
Stack* ss = stack + 7;

for (int i = 7; i > 0; --i)
{
(ss - i)->continuationHistory =
Expand All @@ -206,16 +210,12 @@ void Search::Worker::iterative_deepening() {

ss->pv = pv;

Value bestValue = -VALUE_INFINITE;

if (mainThread)
{
if (mainThread->bestPreviousScore == VALUE_INFINITE)
for (int i = 0; i < 4; ++i)
mainThread->iterValue[i] = VALUE_ZERO;
mainThread->iterValue.fill(VALUE_ZERO);
else
for (int i = 0; i < 4; ++i)
mainThread->iterValue[i] = mainThread->bestPreviousScore;
mainThread->iterValue.fill(mainThread->bestPreviousScore);
}

size_t multiPV = size_t(options["MultiPV"]);
Expand Down Expand Up @@ -425,7 +425,7 @@ void Search::Worker::clear() {
for (auto& h : to)
h->fill(-71);

for (int i = 1; i < MAX_MOVES; ++i)
for (size_t i = 1; i < reductions.size(); ++i)
reductions[i] = int((18.00 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
}

Expand Down
13 changes: 7 additions & 6 deletions src/search.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#ifndef SEARCH_H_INCLUDED
#define SEARCH_H_INCLUDED

#include <array>
#include <atomic>
#include <cassert>
#include <cstddef>
Expand Down Expand Up @@ -155,11 +156,11 @@ class SearchManager: public ISearchManager {
int callsCnt;
std::atomic_bool ponder;

double previousTimeReduction;
Value bestPreviousScore;
Value bestPreviousAverageScore;
Value iterValue[4];
bool stopOnPonderhit;
std::array<Value, 4> iterValue;
double previousTimeReduction;
Value bestPreviousScore;
Value bestPreviousAverageScore;
bool stopOnPonderhit;

size_t id;
};
Expand Down Expand Up @@ -235,7 +236,7 @@ class Worker {
size_t thread_idx;

// Reductions lookup table initialized at startup
int reductions[MAX_PLY + 10]; // [depth or moveNumber]
std::array<int, MAX_PLY + 10> reductions; // [depth or moveNumber]

// The main thread has a SearchManager, the others have a NullSearchManager
std::unique_ptr<ISearchManager> manager;
Expand Down

0 comments on commit 65e32c7

Please sign in to comment.