From 4e72e2a964754611de85536c13ae069f85839b85 Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 31 Mar 2019 12:02:19 +0200 Subject: [PATCH] Assorted trivial cleanups 4/2019 No functional change. --- src/bitbase.cpp | 3 ++- src/bitboard.cpp | 10 ++-------- src/bitboard.h | 14 ++++++++++---- src/evaluate.cpp | 24 +++++++++++------------- src/misc.cpp | 6 ------ src/misc.h | 1 - src/pawns.cpp | 7 ++++--- src/pawns.h | 2 +- src/position.cpp | 6 +++--- src/position.h | 6 +++--- src/search.cpp | 7 ++++--- src/thread.cpp | 1 + src/timeman.cpp | 1 + src/ucioption.cpp | 1 + 14 files changed, 43 insertions(+), 46 deletions(-) diff --git a/src/bitbase.cpp b/src/bitbase.cpp index 8c8c4702..2b1a5517 100644 --- a/src/bitbase.cpp +++ b/src/bitbase.cpp @@ -27,7 +27,8 @@ namespace { - // There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7 + // There are 24 possible pawn squares: files A to D and ranks from 2 to 7. + // Positions with the pawn on files E to H will be mirrored before probing. constexpr unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608 // Each uint32_t stores results of 32 positions, one per bit diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 0ab244c7..281579c4 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -18,8 +18,8 @@ along with this program. If not, see . */ -#include #include +#include #include "bitboard.h" #include "misc.h" @@ -27,16 +27,10 @@ uint8_t PopCnt16[1 << 16]; uint8_t SquareDistance[SQUARE_NB][SQUARE_NB]; +Bitboard SquareBB[SQUARE_NB]; Bitboard LineBB[SQUARE_NB][SQUARE_NB]; 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, - CenterFiles, CenterFiles, - KingSide, KingSide, KingSide ^ FileEBB -}; Magic RookMagics[SQUARE_NB]; Magic BishopMagics[SQUARE_NB]; diff --git a/src/bitboard.h b/src/bitboard.h index 53e96f44..4e0267f1 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -65,14 +65,19 @@ constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB; constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB; constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB); +constexpr Bitboard KingFlank[FILE_NB] = { + QueenSide ^ FileDBB, QueenSide, QueenSide, + CenterFiles, CenterFiles, + KingSide, KingSide, KingSide ^ FileEBB +}; + extern uint8_t PopCnt16[1 << 16]; extern uint8_t SquareDistance[SQUARE_NB][SQUARE_NB]; +extern Bitboard SquareBB[SQUARE_NB]; extern Bitboard LineBB[SQUARE_NB][SQUARE_NB]; 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 @@ -148,6 +153,7 @@ inline Bitboard file_bb(Square s) { template constexpr Bitboard shift(Bitboard b) { return D == NORTH ? b << 8 : D == SOUTH ? b >> 8 + : D == NORTH+NORTH? b <<16 : D == SOUTH+SOUTH? b >>16 : D == EAST ? (b & ~FileHBB) << 1 : D == WEST ? (b & ~FileABB) >> 1 : D == NORTH_EAST ? (b & ~FileHBB) << 9 : D == NORTH_WEST ? (b & ~FileABB) << 7 : D == SOUTH_EAST ? (b & ~FileHBB) >> 7 : D == SOUTH_WEST ? (b & ~FileABB) >> 9 @@ -370,8 +376,8 @@ inline Square pop_lsb(Bitboard* b) { } -/// frontmost_sq() and backmost_sq() return the square corresponding to the -/// most/least advanced bit relative to the given color. +/// frontmost_sq() and backmost_sq() return the most/least advanced square in +/// the given bitboard relative to the given color. inline Square frontmost_sq(Color c, Bitboard b) { return c == WHITE ? msb(b) : lsb(b); } inline Square backmost_sq(Color c, Bitboard b) { return c == WHITE ? lsb(b) : msb(b); } diff --git a/src/evaluate.cpp b/src/evaluate.cpp index 7e6260c8..5750d82a 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include // For std::memset #include @@ -186,9 +187,8 @@ namespace { // is also calculated is ALL_PIECES. Bitboard attackedBy[COLOR_NB][PIECE_TYPE_NB]; - // attackedBy2[color] are the squares attacked by 2 pieces of a given color, - // possibly via x-ray or by one pawn and one piece. Diagonal x-ray through - // pawn or squares attacked by 2 pawns are not explicitly added. + // attackedBy2[color] are the squares attacked by at least 2 units of a given + // color, including x-rays. But diagonal x-rays through pawns are not computed. Bitboard attackedBy2[COLOR_NB]; // kingRing[color] are the squares adjacent to the king, plus (only for a @@ -241,8 +241,7 @@ namespace { attackedBy[Us][KING] = pos.attacks_from(ksq); attackedBy[Us][PAWN] = pe->pawn_attacks(Us); attackedBy[Us][ALL_PIECES] = attackedBy[Us][KING] | attackedBy[Us][PAWN]; - attackedBy2[Us] = (attackedBy[Us][KING] & attackedBy[Us][PAWN]) - | dblAttackByPawn; + attackedBy2[Us] = dblAttackByPawn | (attackedBy[Us][KING] & attackedBy[Us][PAWN]); // Init our king safety tables kingRing[Us] = attackedBy[Us][KING]; @@ -309,11 +308,11 @@ namespace { bb = OutpostRanks & ~pe->pawn_attacks_span(Them); if (bb & s) score += Outpost * (Pt == KNIGHT ? 4 : 2) - * (1 + bool(attackedBy[Us][PAWN] & s)); + * ((attackedBy[Us][PAWN] & s) ? 2 : 1); else if (bb &= b & ~pos.pieces(Us)) score += Outpost * (Pt == KNIGHT ? 2 : 1) - * (1 + bool(attackedBy[Us][PAWN] & bb)); + * ((attackedBy[Us][PAWN] & bb) ? 2 : 1); // Knight and Bishop bonus for being right behind a pawn if (shift(pos.pieces(PAWN)) & s) @@ -358,8 +357,8 @@ namespace { score += RookOnPawn * popcount(pos.pieces(Them, PAWN) & PseudoAttacks[ROOK][s]); // Bonus for rook on an open or semi-open file - if (pos.semiopen_file(Us, file_of(s))) - score += RookOnFile[bool(pos.semiopen_file(Them, file_of(s)))]; + if (pos.is_semiopen_file(Us, file_of(s))) + score += RookOnFile[bool(pos.is_semiopen_file(Them, file_of(s)))]; // Penalty when trapped by the king, even more if the king cannot castle else if (mob <= 3) @@ -672,7 +671,7 @@ namespace { bonus += make_score(k * w, k * w); } - } // rank > RANK_3 + } // r > RANK_3 // Scale down bonus for candidate passers which need more than one // pawn push to become passed, or have a pawn in front of them. @@ -717,7 +716,7 @@ namespace { // Find all squares which are at most three squares behind some friendly pawn Bitboard behind = pos.pieces(Us, PAWN); behind |= shift(behind); - behind |= shift(shift(behind)); + behind |= shift(behind); int bonus = popcount(safe) + popcount(behind & safe); int weight = pos.count(Us) @@ -777,8 +776,7 @@ namespace { if (sf == SCALE_FACTOR_NORMAL) { if ( pos.opposite_bishops() - && pos.non_pawn_material(WHITE) == BishopValueMg - && pos.non_pawn_material(BLACK) == BishopValueMg) + && pos.non_pawn_material() == 2 * BishopValueMg) sf = 16 + 4 * pe->passed_count(); else sf = std::min(40 + (pos.opposite_bishops() ? 2 : 7) * pos.count(strongSide), sf); diff --git a/src/misc.cpp b/src/misc.cpp index 449e07ce..8d3b202d 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -210,12 +210,6 @@ void prefetch(void* addr) { #endif -void prefetch2(void* addr) { - - prefetch(addr); - prefetch((uint8_t*)addr + 64); -} - namespace WinProcGroup { #ifndef _WIN32 diff --git a/src/misc.h b/src/misc.h index 4b238df5..ddd05e4e 100644 --- a/src/misc.h +++ b/src/misc.h @@ -31,7 +31,6 @@ const std::string engine_info(bool to_uci = false); void prefetch(void* addr); -void prefetch2(void* addr); void start_logger(const std::string& fname); void dbg_hit_on(bool b); diff --git a/src/pawns.cpp b/src/pawns.cpp index 095126d4..0a88c9f1 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include "bitboard.h" @@ -175,14 +176,14 @@ Value Entry::evaluate_shelter(const Position& pos, Square ksq) { constexpr Color Them = (Us == WHITE ? BLACK : WHITE); constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH); - constexpr Bitboard BlockRanks = (Us == WHITE ? Rank1BB | Rank2BB : Rank8BB | Rank7BB); + constexpr Bitboard BlockSquares = (Rank1BB | Rank2BB | Rank7BB | Rank8BB) + & (FileABB | FileHBB); Bitboard b = pos.pieces(PAWN) & ~forward_ranks_bb(Them, ksq); Bitboard ourPawns = b & pos.pieces(Us); Bitboard theirPawns = b & pos.pieces(Them); - Value safety = (shift(theirPawns) & (FileABB | FileHBB) & BlockRanks & ksq) ? - Value(374) : Value(5); + Value safety = (shift(theirPawns) & BlockSquares & ksq) ? Value(374) : Value(5); File center = clamp(file_of(ksq), FILE_B, FILE_G); for (File f = File(center - 1); f <= File(center + 1); ++f) diff --git a/src/pawns.h b/src/pawns.h index 88b55545..76c5ffc9 100644 --- a/src/pawns.h +++ b/src/pawns.h @@ -38,7 +38,7 @@ struct Entry { Bitboard passed_pawns(Color c) const { return passedPawns[c]; } Bitboard pawn_attacks_span(Color c) const { return pawnAttacksSpan[c]; } int weak_unopposed(Color c) const { return weakUnopposed[c]; } - int passed_count() const { return popcount(passedPawns[WHITE] | passedPawns[BLACK]); }; + int passed_count() const { return popcount(passedPawns[WHITE] | passedPawns[BLACK]); } template Score king_safety(const Position& pos) { diff --git a/src/position.cpp b/src/position.cpp index ada03fbc..8c6dc802 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include // For offsetof() #include // For std::memset, std::memcmp @@ -340,8 +341,8 @@ void Position::set_castling_right(Color c, Square rfrom) { Square kto = relative_square(c, cs == KING_SIDE ? SQ_G1 : SQ_C1); Square rto = relative_square(c, cs == KING_SIDE ? SQ_F1 : SQ_D1); - castlingPath[cr] = (between_bb(rfrom, rto) | between_bb(kfrom, kto) | rto | kto) - & ~(square_bb(kfrom) | rfrom); + castlingPath[cr] = (between_bb(rfrom, rto) | between_bb(kfrom, kto) | rto | kto) + & ~(square_bb(kfrom) | rfrom); } @@ -859,7 +860,6 @@ void Position::do_move(Move m, StateInfo& newSt, bool givesCheck) { // Update pawn hash key and prefetch access to pawnsTable st->pawnKey ^= Zobrist::psq[pc][from] ^ Zobrist::psq[pc][to]; - prefetch2(thisThread->pawnsTable[st->pawnKey]); // Reset rule 50 draw counter st->rule50 = 0; diff --git a/src/position.h b/src/position.h index 1078e03e..a3fb16b8 100644 --- a/src/position.h +++ b/src/position.h @@ -95,7 +95,7 @@ public: template int count() const; template const Square* squares(Color c) const; template Square square(Color c) const; - int semiopen_file(Color c, File f) const; + bool is_semiopen_file(Color c, File f) const; // Castling int castling_rights(Color c) const; @@ -262,7 +262,7 @@ inline Square Position::ep_square() const { return st->epSquare; } -inline int Position::semiopen_file(Color c, File f) const { +inline bool Position::is_semiopen_file(Color c, File f) const { return !(pieces(c, PAWN) & file_bb(f)); } @@ -321,7 +321,7 @@ inline bool Position::pawn_passed(Color c, Square s) const { inline bool Position::advanced_pawn_push(Move m) const { return type_of(moved_piece(m)) == PAWN - && relative_rank(sideToMove, from_sq(m)) > RANK_4; + && relative_rank(sideToMove, to_sq(m)) > RANK_5; } inline int Position::pawns_on_same_color_squares(Color c, Square s) const { diff --git a/src/search.cpp b/src/search.cpp index 44347f79..082be4f2 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include #include // For std::memset @@ -894,7 +895,7 @@ moves_loop: // When in check, search starts from here && move == ttMove && !rootNode && !excludedMove // Avoid recursive singular search - /* && ttValue != VALUE_NONE Already implicit in the next condition */ + /* && ttValue != VALUE_NONE Already implicit in the next condition */ && abs(ttValue) < VALUE_KNOWN_WIN && (tte->bound() & BOUND_LOWER) && tte->depth() >= depth - 3 * ONE_PLY @@ -1201,8 +1202,8 @@ moves_loop: // When in check, search starts from here } - // qsearch() is the quiescence search function, which is called by the main - // search function with depth zero, or recursively with depth less than ONE_PLY. + // qsearch() is the quiescence search function, which is called by the main search + // function with zero depth, or recursively with further decreasing depth per call. template Value qsearch(Position& pos, Stack* ss, Value alpha, Value beta, Depth depth) { diff --git a/src/thread.cpp b/src/thread.cpp index f216c321..2f1237a3 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -20,6 +20,7 @@ #include +#include // For std::count #include "movegen.h" #include "search.h" #include "thread.h" diff --git a/src/timeman.cpp b/src/timeman.cpp index fafde2aa..484aaa65 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 54f33c9a..813a0890 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -18,6 +18,7 @@ along with this program. If not, see . */ +#include #include #include #include -- 2.39.2