Skip to content

Commit 1403669

Browse files
Disservinpull[bot]
authored andcommitted
Simplify array initializations
also retire a few std::memset calls. Passed non-regresion STC: https://tests.stockfishchess.org/tests/view/65b8e162c865510db0276901 LLR: 2.95 (-2.94,2.94) <-1.75,0.25> Total: 97504 W: 25294 L: 25140 D: 47070 Ptnml(1-2): 378, 11102, 25667, 11198, 407 closes official-stockfish#5018 No functional change
1 parent df8ed63 commit 1403669

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

src/position.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "position.h"
2020

2121
#include <algorithm>
22+
#include <array>
2223
#include <cassert>
2324
#include <cctype>
2425
#include <cstddef>
@@ -107,9 +108,8 @@ inline int H1(Key h) { return h & 0x1fff; }
107108
inline int H2(Key h) { return (h >> 16) & 0x1fff; }
108109

109110
// Cuckoo tables with Zobrist hashes of valid reversible moves, and the moves themselves
110-
Key cuckoo[8192];
111-
Move cuckooMove[8192];
112-
111+
std::array<Key, 8192> cuckoo;
112+
std::array<Move, 8192> cuckooMove;
113113

114114
// Initializes at startup the various arrays used to compute hash keys
115115
void Position::init() {
@@ -130,8 +130,8 @@ void Position::init() {
130130
Zobrist::noPawns = rng.rand<Key>();
131131

132132
// Prepare the cuckoo tables
133-
std::memset(cuckoo, 0, sizeof(cuckoo));
134-
std::memset(cuckooMove, 0, sizeof(cuckooMove));
133+
cuckoo.fill(0);
134+
cuckooMove.fill(Move::none());
135135
[[maybe_unused]] int count = 0;
136136
for (Piece pc : Pieces)
137137
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)

src/search.cpp

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
#include <cassert>
2525
#include <cmath>
2626
#include <cstdlib>
27-
#include <cstring>
2827
#include <initializer_list>
2928
#include <iostream>
3029
#include <utility>
@@ -212,21 +211,26 @@ void Search::Worker::start_searching() {
212211
// consumed, the user stops the search, or the maximum search depth is reached.
213212
void Search::Worker::iterative_deepening() {
214213

214+
SearchManager* mainThread = (thread_idx == 0 ? main_manager() : nullptr);
215+
216+
Move pv[MAX_PLY + 1];
217+
218+
Depth lastBestMoveDepth = 0;
219+
Value lastBestScore = -VALUE_INFINITE;
220+
auto lastBestPV = std::vector{Move::none()};
221+
222+
Value alpha, beta;
223+
Value bestValue = -VALUE_INFINITE;
224+
Color us = rootPos.side_to_move();
225+
double timeReduction = 1, totBestMoveChanges = 0;
226+
int delta, iterIdx = 0;
227+
215228
// Allocate stack with extra size to allow access from (ss - 7) to (ss + 2):
216229
// (ss - 7) is needed for update_continuation_histories(ss - 1) which accesses (ss - 6),
217230
// (ss + 2) is needed for initialization of cutOffCnt and killers.
218-
Stack stack[MAX_PLY + 10], *ss = stack + 7;
219-
Move pv[MAX_PLY + 1];
220-
Value alpha, beta;
221-
Value lastBestScore = -VALUE_INFINITE;
222-
std::vector<Move> lastBestPV = {Move::none()};
223-
Depth lastBestMoveDepth = 0;
224-
SearchManager* mainThread = (thread_idx == 0 ? main_manager() : nullptr);
225-
double timeReduction = 1, totBestMoveChanges = 0;
226-
Color us = rootPos.side_to_move();
227-
int delta, iterIdx = 0;
228-
229-
std::memset(ss - 7, 0, 10 * sizeof(Stack));
231+
Stack stack[MAX_PLY + 10] = {};
232+
Stack* ss = stack + 7;
233+
230234
for (int i = 7; i > 0; --i)
231235
{
232236
(ss - i)->continuationHistory =
@@ -239,16 +243,12 @@ void Search::Worker::iterative_deepening() {
239243

240244
ss->pv = pv;
241245

242-
Value bestValue = -VALUE_INFINITE;
243-
244246
if (mainThread)
245247
{
246248
if (mainThread->bestPreviousScore == VALUE_INFINITE)
247-
for (int i = 0; i < 4; ++i)
248-
mainThread->iterValue[i] = VALUE_ZERO;
249+
mainThread->iterValue.fill(VALUE_ZERO);
249250
else
250-
for (int i = 0; i < 4; ++i)
251-
mainThread->iterValue[i] = mainThread->bestPreviousScore;
251+
mainThread->iterValue.fill(mainThread->bestPreviousScore);
252252
}
253253

254254
size_t multiPV = size_t(options["MultiPV"]);
@@ -489,7 +489,7 @@ void Search::Worker::clear() {
489489
h->fill(-71);
490490

491491

492-
for (int i = 1; i < MAX_MOVES; ++i)
492+
for (size_t i = 1; i < reductions.size(); ++i)
493493
reductions[i] = int((20.37 + std::log(size_t(options["Threads"])) / 2) * std::log(i));
494494
}
495495

src/search.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#ifndef SEARCH_H_INCLUDED
2020
#define SEARCH_H_INCLUDED
2121

22+
#include <array>
2223
#include <atomic>
2324
#include <cassert>
2425
#include <cstddef>
@@ -153,11 +154,11 @@ class SearchManager: public ISearchManager {
153154
int callsCnt;
154155
std::atomic_bool ponder;
155156

156-
double previousTimeReduction;
157-
Value bestPreviousScore;
158-
Value bestPreviousAverageScore;
159-
Value iterValue[4];
160-
bool stopOnPonderhit;
157+
std::array<Value, 4> iterValue;
158+
double previousTimeReduction;
159+
Value bestPreviousScore;
160+
Value bestPreviousAverageScore;
161+
bool stopOnPonderhit;
161162

162163
size_t id;
163164
};
@@ -233,7 +234,7 @@ class Worker {
233234
size_t thread_idx;
234235

235236
// Reductions lookup table initialized at startup
236-
int reductions[MAX_MOVES]; // [depth or moveNumber]
237+
std::array<int, MAX_MOVES> reductions; // [depth or moveNumber]
237238

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

0 commit comments

Comments
 (0)