Skip to content

Commit 83eac08

Browse files
committed
Small cleanups (march 2021)
With help of @BM123499, @mstembera, @gvreuls, @noobpwnftw and @Fanael Thanks! Closes #3405 No functional change
1 parent ec42154 commit 83eac08

File tree

12 files changed

+107
-81
lines changed

12 files changed

+107
-81
lines changed

AUTHORS

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# List of authors for Stockfish, as of August 4, 2020
1+
# List of authors for Stockfish, as of March 24, 2021
22

33
# Founders of the Stockfish project and fishtest infrastructure
44
Tord Romstad (romstad)

src/bitbase.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ namespace {
150150
Bitboard b = attacks_bb<KING>(ksq[stm]);
151151

152152
while (b)
153-
r |= stm == WHITE ? db[index(BLACK, ksq[BLACK] , pop_lsb(b), psq)]
154-
: db[index(WHITE, pop_lsb(b), ksq[WHITE], psq)];
153+
r |= stm == WHITE ? db[index(BLACK, ksq[BLACK], pop_lsb(b), psq)]
154+
: db[index(WHITE, pop_lsb(b), ksq[WHITE], psq)];
155155

156156
if (stm == WHITE)
157157
{

src/evaluate.cpp

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@
5454

5555

5656
using namespace std;
57-
using namespace Stockfish::Eval::NNUE;
5857

5958
namespace Stockfish {
6059

@@ -396,7 +395,8 @@ namespace {
396395

397396
attackedBy[Us][Pt] = 0;
398397

399-
while (b1) {
398+
while (b1)
399+
{
400400
Square s = pop_lsb(b1);
401401

402402
// Find attacked squares, including x-ray attacks for bishops and rooks
@@ -1038,46 +1038,51 @@ namespace {
10381038
return v;
10391039
}
10401040

1041-
// specifically correct for cornered bishops to fix FRC with NNUE.
1041+
1042+
/// Fisher Random Chess: correction for cornered bishops, to fix chess960 play with NNUE
1043+
10421044
Value fix_FRC(const Position& pos) {
10431045

1044-
Value bAdjust = Value(0);
1046+
constexpr Bitboard Corners = 1ULL << SQ_A1 | 1ULL << SQ_H1 | 1ULL << SQ_A8 | 1ULL << SQ_H8;
10451047

1046-
constexpr Value p1=Value(209), p2=Value(136), p3=Value(148);
1048+
if (!(pos.pieces(BISHOP) & Corners))
1049+
return VALUE_ZERO;
10471050

1048-
Color Us = pos.side_to_move();
1049-
if ( (pos.pieces(Us, BISHOP) & relative_square(Us, SQ_A1))
1050-
&& (pos.pieces(Us, PAWN) & relative_square(Us, SQ_B2)))
1051-
{
1052-
bAdjust -= !pos.empty(relative_square(Us,SQ_B3)) ? p1
1053-
: pos.piece_on(relative_square(Us,SQ_C3)) == make_piece(Us, PAWN) ? p2
1054-
: p3;
1055-
}
1056-
if ( (pos.pieces(Us, BISHOP) & relative_square(Us, SQ_H1))
1057-
&& (pos.pieces(Us, PAWN) & relative_square(Us, SQ_G2)))
1058-
{
1059-
bAdjust -= !pos.empty(relative_square(Us,SQ_G3)) ? p1
1060-
: pos.piece_on(relative_square(Us,SQ_F3)) == make_piece(Us, PAWN) ? p2
1061-
: p3;
1062-
}
1063-
if ( (pos.pieces(~Us, BISHOP) & relative_square(Us, SQ_A8))
1064-
&& (pos.pieces(~Us, PAWN) & relative_square(Us, SQ_B7)))
1065-
{
1066-
bAdjust += !pos.empty(relative_square(Us,SQ_B6)) ? p1
1067-
: pos.piece_on(relative_square(Us,SQ_C6)) == make_piece(~Us, PAWN) ? p2
1068-
: p3;
1069-
}
1070-
if ( (pos.pieces(~Us, BISHOP) & relative_square(Us, SQ_H8))
1071-
&& (pos.pieces(~Us, PAWN) & relative_square(Us, SQ_G7)))
1072-
{
1073-
bAdjust += !pos.empty(relative_square(Us,SQ_G6)) ? p1
1074-
: pos.piece_on(relative_square(Us,SQ_F6)) == make_piece(~Us, PAWN) ? p2
1075-
: p3;
1076-
}
1077-
return bAdjust;
1051+
constexpr int penalty1 = -209;
1052+
constexpr int penalty2 = -136;
1053+
constexpr int penalty3 = -148;
1054+
1055+
int correction = 0;
1056+
1057+
if ( pos.piece_on(SQ_A1) == W_BISHOP
1058+
&& pos.piece_on(SQ_B2) == W_PAWN)
1059+
correction += !pos.empty(SQ_B3) ? penalty1
1060+
: pos.piece_on(SQ_C3) == W_PAWN ? penalty2
1061+
: penalty3;
1062+
1063+
if ( pos.piece_on(SQ_H1) == W_BISHOP
1064+
&& pos.piece_on(SQ_G2) == W_PAWN)
1065+
correction += !pos.empty(SQ_G3) ? penalty1
1066+
: pos.piece_on(SQ_F3) == W_PAWN ? penalty2
1067+
: penalty3;
1068+
1069+
if ( pos.piece_on(SQ_A8) == B_BISHOP
1070+
&& pos.piece_on(SQ_B7) == B_PAWN)
1071+
correction += !pos.empty(SQ_B6) ? -penalty1
1072+
: pos.piece_on(SQ_C6) == B_PAWN ? -penalty2
1073+
: -penalty3;
1074+
1075+
if ( pos.piece_on(SQ_H8) == B_BISHOP
1076+
&& pos.piece_on(SQ_G7) == B_PAWN)
1077+
correction += !pos.empty(SQ_G6) ? -penalty1
1078+
: pos.piece_on(SQ_F6) == B_PAWN ? -penalty2
1079+
: -penalty3;
1080+
1081+
return pos.side_to_move() == WHITE ? Value(correction)
1082+
: -Value(correction);
10781083
}
10791084

1080-
} // namespace
1085+
} // namespace Eval
10811086

10821087

10831088
/// evaluate() is the evaluator for the outer world. It returns a static
@@ -1092,14 +1097,19 @@ Value Eval::evaluate(const Position& pos) {
10921097
else
10931098
{
10941099
// Scale and shift NNUE for compatibility with search and classical evaluation
1095-
auto adjusted_NNUE = [&](){
1096-
int mat = pos.non_pawn_material() + 2 * PawnValueMg * pos.count<PAWN>();
1097-
Value nnueValue = NNUE::evaluate(pos) * (641 + mat / 32 - 4 * pos.rule50_count()) / 1024 + Tempo;
1100+
auto adjusted_NNUE = [&]()
1101+
{
1102+
int material = pos.non_pawn_material() + 2 * PawnValueMg * pos.count<PAWN>();
1103+
int scale = 641
1104+
+ material / 32
1105+
- 4 * pos.rule50_count();
1106+
1107+
Value nnue = NNUE::evaluate(pos) * scale / 1024 + Tempo;
10981108

10991109
if (pos.is_chess960())
1100-
nnueValue += fix_FRC(pos);
1110+
nnue += fix_FRC(pos);
11011111

1102-
return nnueValue;
1112+
return nnue;
11031113
};
11041114

11051115
// If there is PSQ imbalance use classical eval, with small probability if it is small

src/material.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ namespace {
7474

7575
bool is_KBPsK(const Position& pos, Color us) {
7676
return pos.non_pawn_material(us) == BishopValueMg
77-
&& pos.count<PAWN >(us) >= 1;
77+
&& pos.count<PAWN>(us) >= 1;
7878
}
7979

8080
bool is_KQKRPs(const Position& pos, Color us) {

src/misc.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -362,8 +362,12 @@ void std_aligned_free(void* ptr) {
362362
/// aligned_large_pages_alloc() will return suitably aligned memory, if possible using large pages.
363363

364364
#if defined(_WIN32)
365-
#if defined(_WIN64)
366-
static void* aligned_large_pages_alloc_win(size_t allocSize) {
365+
366+
static void* aligned_large_pages_alloc_windows(size_t allocSize) {
367+
368+
#if !defined(_WIN64)
369+
return nullptr;
370+
#else
367371

368372
HANDLE hProcessToken { };
369373
LUID luid { };
@@ -406,21 +410,18 @@ static void* aligned_large_pages_alloc_win(size_t allocSize) {
406410
CloseHandle(hProcessToken);
407411

408412
return mem;
413+
414+
#endif
409415
}
410-
#endif
411416

412417
void* aligned_large_pages_alloc(size_t allocSize) {
413418

414-
#if defined(_WIN64)
415419
// Try to allocate large pages
416-
void* mem = aligned_large_pages_alloc_win(allocSize);
420+
void* mem = aligned_large_pages_alloc_windows(allocSize);
417421

418422
// Fall back to regular, page aligned, allocation if necessary
419423
if (!mem)
420424
mem = VirtualAlloc(NULL, allocSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
421-
#else
422-
void* mem = VirtualAlloc(NULL, allocSize, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE);
423-
#endif
424425

425426
return mem;
426427
}
@@ -456,8 +457,9 @@ void aligned_large_pages_free(void* mem) {
456457
if (mem && !VirtualFree(mem, 0, MEM_RELEASE))
457458
{
458459
DWORD err = GetLastError();
459-
std::cerr << "Failed to free transposition table. Error code: 0x" <<
460-
std::hex << err << std::dec << std::endl;
460+
std::cerr << "Failed to free large page memory. Error code: 0x"
461+
<< std::hex << err
462+
<< std::dec << std::endl;
461463
exit(EXIT_FAILURE);
462464
}
463465
}

src/movegen.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ namespace {
182182

183183
Bitboard bb = piecesToMove & pos.pieces(Pt);
184184

185-
while (bb) {
185+
while (bb)
186+
{
186187
Square from = pop_lsb(bb);
187188

188189
Bitboard b = attacks_bb<Pt>(from, pos.pieces()) & target;

src/nnue/features/half_kp.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,8 @@ namespace Stockfish::Eval::NNUE::Features {
4040

4141
Square ksq = orient(perspective, pos.square<KING>(perspective));
4242
Bitboard bb = pos.pieces() & ~pos.pieces(KING);
43-
while (bb) {
43+
while (bb)
44+
{
4445
Square s = pop_lsb(bb);
4546
active->push_back(make_index(perspective, s, pos.piece_on(s), ksq));
4647
}

src/nnue/layers/affine_transform.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace Stockfish::Eval::NNUE::Layers {
9696
IndexType idx = k / 2 * kOutputDimensions * 4 + k % 2;
9797
sum[w[idx] < 0] += w[idx];
9898
}
99-
for (int sign : {-1, 1})
99+
for (int sign : { -1, 1 })
100100
while (sign * sum[sign == -1] > 258)
101101
{
102102
int maxK = 0, maxW = 0;
@@ -234,9 +234,9 @@ namespace Stockfish::Eval::NNUE::Layers {
234234
__m128i product1 = _mm_maddubs_epi16(a1, b1);
235235
__m128i product2 = _mm_maddubs_epi16(a2, b2);
236236
__m128i product3 = _mm_maddubs_epi16(a3, b3);
237-
product0 = _mm_adds_epi16(product0, product1);
238-
product2 = _mm_adds_epi16(product2, product3);
239-
product0 = _mm_adds_epi16(product0, product2);
237+
product0 = _mm_add_epi16(product0, product1);
238+
product2 = _mm_add_epi16(product2, product3);
239+
product0 = _mm_add_epi16(product0, product2);
240240
product0 = _mm_madd_epi16(product0, kOnes128);
241241
acc = _mm_add_epi32(acc, product0);
242242
};

src/pawns.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ namespace {
109109
e->blockedCount += popcount(shift<Up>(ourPawns) & (theirPawns | doubleAttackThem));
110110

111111
// Loop through all pawns of the current color and score each pawn
112-
while (b) {
112+
while (b)
113+
{
113114
s = pop_lsb(b);
114115

115116
assert(pos.piece_on(s) == make_piece(Us, PAWN));

src/search.cpp

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -612,12 +612,12 @@ namespace {
612612

613613
// Step 1. Initialize node
614614
Thread* thisThread = pos.this_thread();
615-
ss->inCheck = pos.checkers();
616-
priorCapture = pos.captured_piece();
617-
Color us = pos.side_to_move();
618-
moveCount = captureCount = quietCount = ss->moveCount = 0;
619-
bestValue = -VALUE_INFINITE;
620-
maxValue = VALUE_INFINITE;
615+
ss->inCheck = pos.checkers();
616+
priorCapture = pos.captured_piece();
617+
Color us = pos.side_to_move();
618+
moveCount = captureCount = quietCount = ss->moveCount = 0;
619+
bestValue = -VALUE_INFINITE;
620+
maxValue = VALUE_INFINITE;
621621
ss->distanceFromPv = (PvNode ? 0 : ss->distanceFromPv);
622622

623623
// Check for the available remaining time
@@ -917,6 +917,7 @@ namespace {
917917
return probCutBeta;
918918

919919
assert(probCutBeta < VALUE_INFINITE);
920+
920921
MovePicker mp(pos, ttMove, probCutBeta - ss->staticEval, &captureHistory);
921922
int probCutCount = 0;
922923
bool ttPv = ss->ttPv;
@@ -1121,6 +1122,7 @@ namespace {
11211122
{
11221123
Value singularBeta = ttValue - ((formerPv + 4) * depth) / 2;
11231124
Depth singularDepth = (depth - 1 + 3 * formerPv) / 2;
1125+
11241126
ss->excludedMove = move;
11251127
value = search<NonPV>(pos, ss, singularBeta - 1, singularBeta, singularDepth, cutNode);
11261128
ss->excludedMove = MOVE_NONE;
@@ -1205,15 +1207,19 @@ namespace {
12051207

12061208
// Decrease reduction if position is or has been on the PV
12071209
// and node is not likely to fail low. (~10 Elo)
1208-
if (ss->ttPv && !likelyFailLow)
1210+
if ( ss->ttPv
1211+
&& !likelyFailLow)
12091212
r -= 2;
12101213

12111214
// Increase reduction at root and non-PV nodes when the best move does not change frequently
1212-
if ((rootNode || !PvNode) && thisThread->rootDepth > 10 && thisThread->bestMoveChanges <= 2)
1215+
if ( (rootNode || !PvNode)
1216+
&& thisThread->rootDepth > 10
1217+
&& thisThread->bestMoveChanges <= 2)
12131218
r++;
12141219

12151220
// More reductions for late moves if position was not in previous PV
1216-
if (moveCountPruning && !formerPv)
1221+
if ( moveCountPruning
1222+
&& !formerPv)
12171223
r++;
12181224

12191225
// Decrease reduction if opponent's move count is high (~5 Elo)
@@ -1226,7 +1232,7 @@ namespace {
12261232

12271233
if (captureOrPromotion)
12281234
{
1229-
// Unless giving check, this capture is likely bad
1235+
// Increase reduction for non-checking captures likely to be bad
12301236
if ( !givesCheck
12311237
&& ss->staticEval + PieceValue[EG][pos.captured_piece()] + 210 * depth <= alpha)
12321238
r++;
@@ -1246,7 +1252,7 @@ namespace {
12461252

12471253
// Decrease reduction for moves that escape a capture. Filter out
12481254
// castling moves, because they are coded as "king captures rook" and
1249-
// hence break make_move(). (~2 Elo)
1255+
// hence break reverse_move() (~2 Elo)
12501256
else if ( type_of(move) == NORMAL
12511257
&& !pos.see_ge(reverse_move(move)))
12521258
r -= 2 + ss->ttPv - (type_of(movedPiece) == PAWN);
@@ -1408,8 +1414,9 @@ namespace {
14081414
assert(moveCount || !ss->inCheck || excludedMove || !MoveList<LEGAL>(pos).size());
14091415

14101416
if (!moveCount)
1411-
bestValue = excludedMove ? alpha
1412-
: ss->inCheck ? mated_in(ss->ply) : VALUE_DRAW;
1417+
bestValue = excludedMove ? alpha :
1418+
ss->inCheck ? mated_in(ss->ply)
1419+
: VALUE_DRAW;
14131420

14141421
// If there is a move which produces search value greater than alpha we update stats of searched moves
14151422
else if (bestMove)

src/syzygy/tbprobe.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ class TBFile : public std::ifstream {
192192
std::stringstream ss(Paths);
193193
std::string path;
194194

195-
while (std::getline(ss, path, SepChar)) {
195+
while (std::getline(ss, path, SepChar))
196+
{
196197
fname = path + "/" + f;
197198
std::ifstream::open(fname);
198199
if (is_open())
@@ -567,7 +568,8 @@ int decompress_pairs(PairsData* d, uint64_t idx) {
567568
int buf64Size = 64;
568569
Sym sym;
569570

570-
while (true) {
571+
while (true)
572+
{
571573
int len = 0; // This is the symbol length - d->min_sym_len
572574

573575
// Now get the symbol length. For any symbol s64 of length l right-padded
@@ -605,8 +607,8 @@ int decompress_pairs(PairsData* d, uint64_t idx) {
605607
// We binary-search for our value recursively expanding into the left and
606608
// right child symbols until we reach a leaf node where symlen[sym] + 1 == 1
607609
// that will store the value we need.
608-
while (d->symlen[sym]) {
609-
610+
while (d->symlen[sym])
611+
{
610612
Sym left = d->btree[sym].get<LR::Left>();
611613

612614
// If a symbol contains 36 sub-symbols (d->symlen[sym] + 1 = 36) and

src/thread.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,14 +128,16 @@ void Thread::idle_loop() {
128128

129129
void ThreadPool::set(size_t requested) {
130130

131-
if (size() > 0) { // destroy any existing thread(s)
131+
if (size() > 0) // destroy any existing thread(s)
132+
{
132133
main()->wait_for_search_finished();
133134

134135
while (size() > 0)
135136
delete back(), pop_back();
136137
}
137138

138-
if (requested > 0) { // create new thread(s)
139+
if (requested > 0) // create new thread(s)
140+
{
139141
push_back(new MainThread(0));
140142

141143
while (size() < requested)

0 commit comments

Comments
 (0)