From: Marco Costalba Date: Sun, 31 Mar 2019 09:47:36 +0000 (+0200) Subject: Assorted trivial cleanups 3/2019 (#2030) X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=82ad9ce9cfb0eff33f1d781f329f7c5dc0b277eb Assorted trivial cleanups 3/2019 (#2030) No functional change. --- diff --git a/src/bitboard.cpp b/src/bitboard.cpp index e4287f35..2adf2779 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -27,13 +27,12 @@ uint8_t PopCnt16[1 << 16]; uint8_t SquareDistance[SQUARE_NB][SQUARE_NB]; -Bitboard SquareBB[SQUARE_NB]; -Bitboard ForwardRanksBB[COLOR_NB][RANK_NB]; Bitboard BetweenBB[SQUARE_NB][SQUARE_NB]; Bitboard LineBB[SQUARE_NB][SQUARE_NB]; Bitboard DistanceRingBB[SQUARE_NB][8]; Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB]; Bitboard PawnAttacks[COLOR_NB][SQUARE_NB]; +Bitboard SquareBB[SQUARE_NB]; Bitboard KingFlank[FILE_NB] = { QueenSide ^ FileDBB, QueenSide, QueenSide, diff --git a/src/bitboard.h b/src/bitboard.h index aa29abf2..77986638 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -68,13 +68,13 @@ constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB); extern uint8_t PopCnt16[1 << 16]; extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB]; -extern Bitboard SquareBB[SQUARE_NB]; extern Bitboard BetweenBB[SQUARE_NB][SQUARE_NB]; extern Bitboard LineBB[SQUARE_NB][SQUARE_NB]; extern Bitboard DistanceRingBB[SQUARE_NB][8]; extern Bitboard PseudoAttacks[PIECE_TYPE_NB][SQUARE_NB]; extern Bitboard PawnAttacks[COLOR_NB][SQUARE_NB]; extern Bitboard KingFlank[FILE_NB]; +extern Bitboard SquareBB[SQUARE_NB]; /// Magic holds all magic bitboards relevant data for a single square @@ -102,15 +102,14 @@ struct Magic { extern Magic RookMagics[SQUARE_NB]; extern Magic BishopMagics[SQUARE_NB]; - -/// Overloads of bitwise operators between a Bitboard and a Square for testing -/// whether a given bit is set in a bitboard, and for setting and clearing bits. - inline Bitboard square_bb(Square s) { assert(s >= SQ_A1 && s <= SQ_H8); return SquareBB[s]; } - + +/// Overloads of bitwise operators between a Bitboard and a Square for testing +/// whether a given bit is set in a bitboard, and for setting and clearing bits. + inline Bitboard operator&( Bitboard b, Square s) { return b & square_bb(s); } inline Bitboard operator|( Bitboard b, Square s) { return b | square_bb(s); } inline Bitboard operator^( Bitboard b, Square s) { return b ^ square_bb(s); } diff --git a/src/endgame.cpp b/src/endgame.cpp index b1b3d664..efc41a98 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -76,10 +76,7 @@ namespace { if (file_of(pos.square(strongSide)) >= FILE_E) sq = Square(sq ^ 7); // Mirror SQ_H1 -> SQ_A1 - if (strongSide == BLACK) - sq = ~sq; - - return sq; + return strongSide == WHITE ? sq : ~sq; } } // namespace @@ -285,18 +282,18 @@ Value Endgame::operator()(const Position& pos) const { } -/// KNN vs KP. Simply push the opposing king to the corner. +/// KNN vs KP. Simply push the opposing king to the corner template<> Value Endgame::operator()(const Position& pos) const { - assert(verify_material(pos, strongSide, 2 * KnightValueMg, 0)); - assert(verify_material(pos, weakSide, VALUE_ZERO, 1)); + assert(verify_material(pos, strongSide, 2 * KnightValueMg, 0)); + assert(verify_material(pos, weakSide, VALUE_ZERO, 1)); - Value result = 2 * KnightValueEg - - PawnValueEg - + PushToEdges[pos.square(weakSide)]; + Value result = 2 * KnightValueEg + - PawnValueEg + + PushToEdges[pos.square(weakSide)]; - return strongSide == pos.side_to_move() ? result : -result; + return strongSide == pos.side_to_move() ? result : -result; } diff --git a/src/material.cpp b/src/material.cpp index 2e68ee68..ee5d4bce 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -149,9 +149,9 @@ Entry* probe(const Position& pos) { // OK, we didn't find any special evaluation function for the current material // configuration. Is there a suitable specialized scaling function? - const EndgameBase* sf; + const auto* sf = pos.this_thread()->endgames.probe(key); - if ((sf = pos.this_thread()->endgames.probe(key)) != nullptr) + if (sf) { e->scalingFunction[sf->strongSide] = sf; // Only strong color assigned return e; diff --git a/src/misc.h b/src/misc.h index 3cba4867..4b238df5 100644 --- a/src/misc.h +++ b/src/misc.h @@ -53,7 +53,7 @@ struct HashTable { Entry* operator[](Key key) { return &table[(uint32_t)key & (Size - 1)]; } private: - std::vector table = std::vector(Size); + std::vector table = std::vector(Size); // Allocate on the heap }; diff --git a/src/position.cpp b/src/position.cpp index 2bdf1009..84892d09 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -628,7 +628,7 @@ bool Position::pseudo_legal(const Move m) const { { // We have already handled promotion moves, so destination // cannot be on the 8th/1st rank. - if (rank_of(to) == relative_rank(us, RANK_8)) + if ((Rank8BB | Rank1BB) & to) return false; if ( !(attacks_from(from, us) & pieces(~us) & to) // Not a capture diff --git a/src/position.h b/src/position.h index ce13017d..03c00148 100644 --- a/src/position.h +++ b/src/position.h @@ -413,7 +413,7 @@ inline void Position::move_piece(Piece pc, Square from, Square to) { // index[from] is not updated and becomes stale. This works as long as index[] // is accessed just by known occupied squares. - Bitboard fromTo = square_bb(from) ^ square_bb(to); + Bitboard fromTo = square_bb(from) | square_bb(to); byTypeBB[ALL_PIECES] ^= fromTo; byTypeBB[type_of(pc)] ^= fromTo; byColorBB[color_of(pc)] ^= fromTo; diff --git a/src/search.cpp b/src/search.cpp index cfa737db..21b669c4 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -84,11 +84,10 @@ namespace { return d > 17 ? 0 : 29 * d * d + 138 * d - 134; } - // Add a small random component to draw evaluations to keep search dynamic - // and to avoid 3fold-blindness. + // Add a small random component to draw evaluations to avoid 3fold-blindness Value value_draw(Depth depth, Thread* thisThread) { return depth < 4 ? VALUE_DRAW - : VALUE_DRAW + Value(2 * (thisThread->nodes.load(std::memory_order_relaxed) % 2) - 1); + : VALUE_DRAW + Value(2 * (thisThread->nodes & 1) - 1); } // Skill structure is used to implement strength limit @@ -162,12 +161,12 @@ void Search::clear() { Time.availableNodes = 0; TT.clear(); Threads.clear(); - Tablebases::init(Options["SyzygyPath"]); // Free up mapped files + Tablebases::init(Options["SyzygyPath"]); // Free mapped files } -/// MainThread::search() is called by the main thread when the program receives -/// the UCI 'go' command. It searches from the root position and outputs the "bestmove". +/// MainThread::search() is started when the program receives the UCI 'go' +/// command. It searches from the root position and outputs the "bestmove". void MainThread::search() { @@ -221,8 +220,9 @@ void MainThread::search() { if (Limits.npmsec) Time.availableNodes += Limits.inc[us] - Threads.nodes_searched(); - // Check if there are threads with a better score than main thread Thread* bestThread = this; + + // Check if there are threads with a better score than main thread if ( Options["MultiPV"] == 1 && !Limits.depth && !Skill(Options["Skill Level"]).enabled() @@ -273,9 +273,9 @@ void MainThread::search() { void Thread::search() { - // To allow access to (ss-5) up to (ss+2), the stack must be oversized. + // To allow access to (ss-7) up to (ss+2), the stack must be oversized. // The former is needed to allow update_continuation_histories(ss-1, ...), - // which accesses its argument at ss-4, also near the root. + // which accesses its argument at ss-6, also near the root. // The latter is needed for statScores and killer initialization. Stack stack[MAX_PLY+10], *ss = stack+7; Move pv[MAX_PLY+1]; @@ -317,7 +317,7 @@ void Thread::search() { : Options["Analysis Contempt"] == "Black" && us == WHITE ? -ct : ct; - // In evaluate.cpp the evaluation is from the white point of view + // Evaluation score is from the white point of view contempt = (us == WHITE ? make_score(ct, ct / 2) : -make_score(ct, ct / 2)); @@ -832,8 +832,7 @@ namespace { } // Step 11. Internal iterative deepening (~2 Elo) - if ( depth >= 8 * ONE_PLY - && !ttMove) + if (depth >= 8 * ONE_PLY && !ttMove) { search(pos, ss, alpha, beta, depth - 7 * ONE_PLY, cutNode); @@ -847,6 +846,7 @@ moves_loop: // When in check, search starts from here const PieceToHistory* contHist[] = { (ss-1)->continuationHistory, (ss-2)->continuationHistory, nullptr, (ss-4)->continuationHistory, nullptr, (ss-6)->continuationHistory }; + Move countermove = thisThread->counterMoves[pos.piece_on(prevSq)][prevSq]; MovePicker mp(pos, ttMove, depth, &thisThread->mainHistory, @@ -854,8 +854,8 @@ moves_loop: // When in check, search starts from here contHist, countermove, ss->killers); - value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc + value = bestValue; // Workaround a bogus 'uninitialized' warning under gcc moveCountPruning = false; ttCapture = ttMove && pos.capture_or_promotion(ttMove); @@ -946,7 +946,7 @@ moves_loop: // When in check, search starts from here && bestValue > VALUE_MATED_IN_MAX_PLY) { // Skip quiet moves if movecount exceeds our FutilityMoveCount threshold - moveCountPruning = moveCount >= futility_move_count(improving,depth / ONE_PLY); + moveCountPruning = moveCount >= futility_move_count(improving, depth / ONE_PLY); if ( !captureOrPromotion && !givesCheck diff --git a/src/thread.cpp b/src/thread.cpp index 64dd9e18..f216c321 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -31,7 +31,7 @@ ThreadPool Threads; // Global object /// Thread constructor launches the thread and waits until it goes to sleep -/// in idle_loop(). Note that 'searching' and 'exit' should be alredy set. +/// in idle_loop(). Note that 'searching' and 'exit' should be already set. Thread::Thread(size_t n) : idx(n), stdThread(&Thread::idle_loop, this) {