From: Ronald de Man Date: Sun, 18 Mar 2018 22:38:58 +0000 (+0100) Subject: Mark all compile-time constants as constexpr. X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=759b3c79cf94d101163f646b1eb2a9f9c64293ab Mark all compile-time constants as constexpr. To more clearly distinguish them from "const" local variables, this patch defines compile-time local constants as constexpr. This is consistent with the definition of PvNode as constexpr in search() and qsearch(). It also makes the code more robust, since the compiler will now check that those constants are indeed compile-time constants. We can go even one step further and define all the evaluation and search compile-time constants as constexpr. In generate_castling() I replaced "K" with "step", since K was incorrectly capitalised (in the Chess960 case). In timeman.cpp I had to make the non-local constants MaxRatio and StealRatio constepxr, since otherwise gcc would complain when calculating TMaxRatio and TStealRatio. (Strangely, I did not have to make Is64Bit constexpr even though it is used in ucioption.cpp in the calculation of constexpr MaxHashMB.) I have renamed PieceCount to pieceCount in material.h, since the values of the array are not compile-time constants. Some compile-time constants in tbprobe.cpp were overlooked. Sides and MaxFile are not compile-time constants, so were renamed to sides and maxFile. Non-functional change. --- diff --git a/src/bitbase.cpp b/src/bitbase.cpp index 3c9bf6e8..60048289 100644 --- a/src/bitbase.cpp +++ b/src/bitbase.cpp @@ -29,7 +29,7 @@ namespace { // There are 24 possible pawn squares: the first 4 files and ranks from 2 to 7 - const unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608 + constexpr unsigned MAX_INDEX = 2*24*64*64; // stm * psq * wksq * bksq = 196608 // Each uint32_t stores results of 32 positions, one per bit uint32_t KPKBitbase[MAX_INDEX / 32]; @@ -152,9 +152,9 @@ namespace { // as WIN, the position is classified as WIN, otherwise the current position is // classified as UNKNOWN. - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Result Good = (Us == WHITE ? WIN : DRAW); - const Result Bad = (Us == WHITE ? DRAW : WIN); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Result Good = (Us == WHITE ? WIN : DRAW); + constexpr Result Bad = (Us == WHITE ? DRAW : WIN); Result r = INVALID; Bitboard b = PseudoAttacks[KING][ksq[Us]]; diff --git a/src/bitboard.h b/src/bitboard.h index ed01eada..6032f47f 100644 --- a/src/bitboard.h +++ b/src/bitboard.h @@ -39,26 +39,26 @@ const std::string pretty(Bitboard b); } -const Bitboard AllSquares = ~Bitboard(0); -const Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL; - -const Bitboard FileABB = 0x0101010101010101ULL; -const Bitboard FileBBB = FileABB << 1; -const Bitboard FileCBB = FileABB << 2; -const Bitboard FileDBB = FileABB << 3; -const Bitboard FileEBB = FileABB << 4; -const Bitboard FileFBB = FileABB << 5; -const Bitboard FileGBB = FileABB << 6; -const Bitboard FileHBB = FileABB << 7; - -const Bitboard Rank1BB = 0xFF; -const Bitboard Rank2BB = Rank1BB << (8 * 1); -const Bitboard Rank3BB = Rank1BB << (8 * 2); -const Bitboard Rank4BB = Rank1BB << (8 * 3); -const Bitboard Rank5BB = Rank1BB << (8 * 4); -const Bitboard Rank6BB = Rank1BB << (8 * 5); -const Bitboard Rank7BB = Rank1BB << (8 * 6); -const Bitboard Rank8BB = Rank1BB << (8 * 7); +constexpr Bitboard AllSquares = ~Bitboard(0); +constexpr Bitboard DarkSquares = 0xAA55AA55AA55AA55ULL; + +constexpr Bitboard FileABB = 0x0101010101010101ULL; +constexpr Bitboard FileBBB = FileABB << 1; +constexpr Bitboard FileCBB = FileABB << 2; +constexpr Bitboard FileDBB = FileABB << 3; +constexpr Bitboard FileEBB = FileABB << 4; +constexpr Bitboard FileFBB = FileABB << 5; +constexpr Bitboard FileGBB = FileABB << 6; +constexpr Bitboard FileHBB = FileABB << 7; + +constexpr Bitboard Rank1BB = 0xFF; +constexpr Bitboard Rank2BB = Rank1BB << (8 * 1); +constexpr Bitboard Rank3BB = Rank1BB << (8 * 2); +constexpr Bitboard Rank4BB = Rank1BB << (8 * 3); +constexpr Bitboard Rank5BB = Rank1BB << (8 * 4); +constexpr Bitboard Rank6BB = Rank1BB << (8 * 5); +constexpr Bitboard Rank7BB = Rank1BB << (8 * 6); +constexpr Bitboard Rank8BB = Rank1BB << (8 * 7); extern int SquareDistance[SQUARE_NB][SQUARE_NB]; diff --git a/src/endgame.cpp b/src/endgame.cpp index 39db219e..55e4483d 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -31,7 +31,7 @@ namespace { // Table used to drive the king towards the edge of the board // in KX vs K and KQ vs KR endgames. - const int PushToEdges[SQUARE_NB] = { + constexpr int PushToEdges[SQUARE_NB] = { 100, 90, 80, 70, 70, 80, 90, 100, 90, 70, 60, 50, 50, 60, 70, 90, 80, 60, 40, 30, 30, 40, 60, 80, @@ -44,7 +44,7 @@ namespace { // Table used to drive the king towards a corner square of the // right color in KBN vs K endgames. - const int PushToCorners[SQUARE_NB] = { + constexpr int PushToCorners[SQUARE_NB] = { 200, 190, 180, 170, 160, 150, 140, 130, 190, 180, 170, 160, 150, 140, 130, 140, 180, 170, 155, 140, 140, 125, 140, 150, @@ -56,11 +56,11 @@ namespace { }; // Tables used to drive a piece towards or away from another piece - const int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 }; - const int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 }; + constexpr int PushClose[8] = { 0, 0, 100, 80, 60, 40, 20, 10 }; + constexpr int PushAway [8] = { 0, 5, 20, 40, 60, 80, 90, 100 }; // Pawn Rank based scaling factors used in KRPPKRP endgame - const int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 }; + constexpr int KRPPKRPScaleFactors[RANK_NB] = { 0, 9, 10, 14, 21, 44, 0, 0 }; #ifndef NDEBUG bool verify_material(const Position& pos, Color c, Value npm, int pawnsCnt) { diff --git a/src/evaluate.cpp b/src/evaluate.cpp index bb60107d..64e0eca4 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -74,35 +74,35 @@ using namespace Trace; namespace { - const Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB; - const Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB; - const Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB; - const Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB); + constexpr Bitboard QueenSide = FileABB | FileBBB | FileCBB | FileDBB; + constexpr Bitboard CenterFiles = FileCBB | FileDBB | FileEBB | FileFBB; + constexpr Bitboard KingSide = FileEBB | FileFBB | FileGBB | FileHBB; + constexpr Bitboard Center = (FileDBB | FileEBB) & (Rank4BB | Rank5BB); - const Bitboard KingFlank[FILE_NB] = { + constexpr Bitboard KingFlank[FILE_NB] = { QueenSide, QueenSide, QueenSide, CenterFiles, CenterFiles, KingSide, KingSide, KingSide }; // Threshold for lazy and space evaluation - const Value LazyThreshold = Value(1500); - const Value SpaceThreshold = Value(12222); + constexpr Value LazyThreshold = Value(1500); + constexpr Value SpaceThreshold = Value(12222); // KingAttackWeights[PieceType] contains king attack weights by piece type - const int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 78, 56, 45, 11 }; + constexpr int KingAttackWeights[PIECE_TYPE_NB] = { 0, 0, 78, 56, 45, 11 }; // Penalties for enemy's safe checks - const int QueenSafeCheck = 780; - const int RookSafeCheck = 880; - const int BishopSafeCheck = 435; - const int KnightSafeCheck = 790; + constexpr int QueenSafeCheck = 780; + constexpr int RookSafeCheck = 880; + constexpr int BishopSafeCheck = 435; + constexpr int KnightSafeCheck = 790; #define S(mg, eg) make_score(mg, eg) // MobilityBonus[PieceType-2][attacked] contains bonuses for middle and end game, // indexed by piece type and number of attacked squares in the mobility area. - const Score MobilityBonus[][32] = { + constexpr Score MobilityBonus[][32] = { { S(-75,-76), S(-57,-54), S( -9,-28), S( -2,-10), S( 6, 5), S( 14, 12), // Knights S( 22, 26), S( 29, 29), S( 36, 29) }, { S(-48,-59), S(-20,-23), S( 16, -3), S( 26, 13), S( 38, 24), S( 51, 42), // Bishops @@ -121,66 +121,66 @@ namespace { // Outpost[knight/bishop][supported by pawn] contains bonuses for minor // pieces if they occupy or can reach an outpost square, bigger if that // square is supported by a pawn. - const Score Outpost[][2] = { + constexpr Score Outpost[][2] = { { S(22, 6), S(36,12) }, // Knight { S( 9, 2), S(15, 5) } // Bishop }; // RookOnFile[semiopen/open] contains bonuses for each rook when there is // no (friendly) pawn on the rook file. - const Score RookOnFile[] = { S(20, 7), S(45, 20) }; + constexpr Score RookOnFile[] = { S(20, 7), S(45, 20) }; // ThreatByMinor/ByRook[attacked PieceType] contains bonuses according to // which piece type attacks which one. Attacks on lesser pieces which are // pawn-defended are not considered. - const Score ThreatByMinor[PIECE_TYPE_NB] = { + constexpr Score ThreatByMinor[PIECE_TYPE_NB] = { S(0, 0), S(0, 31), S(39, 42), S(57, 44), S(68, 112), S(47, 120) }; - const Score ThreatByRook[PIECE_TYPE_NB] = { + constexpr Score ThreatByRook[PIECE_TYPE_NB] = { S(0, 0), S(0, 24), S(38, 71), S(38, 61), S(0, 38), S(36, 38) }; // ThreatByKing[on one/on many] contains bonuses for king attacks on // pawns or pieces which are not pawn-defended. - const Score ThreatByKing[] = { S(3, 65), S(9, 145) }; + constexpr Score ThreatByKing[] = { S(3, 65), S(9, 145) }; // PassedRank[Rank] contains a bonus according to the rank of a passed pawn - const Score PassedRank[RANK_NB] = { + constexpr Score PassedRank[RANK_NB] = { S(0, 0), S(5, 7), S(5, 13), S(32, 42), S(70, 70), S(172, 170), S(217, 269) }; // PassedFile[File] contains a bonus according to the file of a passed pawn - const Score PassedFile[FILE_NB] = { + constexpr Score PassedFile[FILE_NB] = { S( 9, 10), S(2, 10), S(1, -8), S(-20,-12), S(-20,-12), S(1, -8), S(2, 10), S( 9, 10) }; // PassedDanger[Rank] contains a term to weight the passed score - const int PassedDanger[RANK_NB] = { 0, 0, 0, 2, 7, 12, 19 }; + constexpr int PassedDanger[RANK_NB] = { 0, 0, 0, 2, 7, 12, 19 }; // KingProtector[PieceType-2] contains a penalty according to distance from king - const Score KingProtector[] = { S(3, 5), S(4, 3), S(3, 0), S(1, -1) }; + constexpr Score KingProtector[] = { S(3, 5), S(4, 3), S(3, 0), S(1, -1) }; // Assorted bonuses and penalties - const Score BishopPawns = S( 8, 12); - const Score CloseEnemies = S( 7, 0); - const Score Connectivity = S( 3, 1); - const Score CorneredBishop = S( 50, 50); - const Score Hanging = S( 52, 30); - const Score HinderPassedPawn = S( 8, 1); - const Score KnightOnQueen = S( 21, 11); - const Score LongDiagonalBishop = S( 22, 0); - const Score MinorBehindPawn = S( 16, 0); - const Score PawnlessFlank = S( 20, 80); - const Score RookOnPawn = S( 8, 24); - const Score SliderOnQueen = S( 42, 21); - const Score ThreatByPawnPush = S( 47, 26); - const Score ThreatByRank = S( 16, 3); - const Score ThreatBySafePawn = S(175,168); - const Score TrappedRook = S( 92, 0); - const Score WeakQueen = S( 50, 10); - const Score WeakUnopposedPawn = S( 5, 25); + constexpr Score BishopPawns = S( 8, 12); + constexpr Score CloseEnemies = S( 7, 0); + constexpr Score Connectivity = S( 3, 1); + constexpr Score CorneredBishop = S( 50, 50); + constexpr Score Hanging = S( 52, 30); + constexpr Score HinderPassedPawn = S( 8, 1); + constexpr Score KnightOnQueen = S( 21, 11); + constexpr Score LongDiagonalBishop = S( 22, 0); + constexpr Score MinorBehindPawn = S( 16, 0); + constexpr Score PawnlessFlank = S( 20, 80); + constexpr Score RookOnPawn = S( 8, 24); + constexpr Score SliderOnQueen = S( 42, 21); + constexpr Score ThreatByPawnPush = S( 47, 26); + constexpr Score ThreatByRank = S( 16, 3); + constexpr Score ThreatBySafePawn = S(175,168); + constexpr Score TrappedRook = S( 92, 0); + constexpr Score WeakQueen = S( 50, 10); + constexpr Score WeakUnopposedPawn = S( 5, 25); #undef S @@ -250,10 +250,10 @@ namespace { template template void Evaluation::initialize() { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Direction Up = (Us == WHITE ? NORTH : SOUTH); - const Direction Down = (Us == WHITE ? SOUTH : NORTH); - const Bitboard LowRanks = (Us == WHITE ? Rank2BB | Rank3BB: Rank7BB | Rank6BB); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); + constexpr Direction Down = (Us == WHITE ? SOUTH : NORTH); + constexpr Bitboard LowRanks = (Us == WHITE ? Rank2BB | Rank3BB: Rank7BB | Rank6BB); // Find our pawns that are blocked or on the first two ranks Bitboard b = pos.pieces(Us, PAWN) & (shift(pos.pieces()) | LowRanks); @@ -287,8 +287,8 @@ namespace { template template Score Evaluation::pieces() { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Bitboard OutpostRanks = (Us == WHITE ? Rank4BB | Rank5BB | Rank6BB + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Bitboard OutpostRanks = (Us == WHITE ? Rank4BB | Rank5BB | Rank6BB : Rank5BB | Rank4BB | Rank3BB); const Square* pl = pos.squares(Us); @@ -404,8 +404,8 @@ namespace { template template Score Evaluation::king() const { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Bitboard Camp = (Us == WHITE ? AllSquares ^ Rank6BB ^ Rank7BB ^ Rank8BB : AllSquares ^ Rank1BB ^ Rank2BB ^ Rank3BB); const Square ksq = pos.square(Us); @@ -506,9 +506,9 @@ namespace { template template Score Evaluation::threats() const { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Direction Up = (Us == WHITE ? NORTH : SOUTH); - const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); + constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); Bitboard b, weak, defended, nonPawnEnemies, stronglyProtected, safeThreats; Score score = SCORE_ZERO; @@ -617,8 +617,8 @@ namespace { template template Score Evaluation::passed() const { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Direction Up = (Us == WHITE ? NORTH : SOUTH); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); auto king_proximity = [&](Color c, Square s) { return std::min(distance(pos.square(c), s), 5); @@ -715,8 +715,8 @@ namespace { template template Score Evaluation::space() const { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Bitboard SpaceMask = + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Bitboard SpaceMask = Us == WHITE ? CenterFiles & (Rank2BB | Rank3BB | Rank4BB) : CenterFiles & (Rank7BB | Rank6BB | Rank5BB); diff --git a/src/evaluate.h b/src/evaluate.h index 0004dd63..c3b0b2b9 100644 --- a/src/evaluate.h +++ b/src/evaluate.h @@ -30,7 +30,7 @@ class Position; namespace Eval { -const Value Tempo = Value(20); // Must be visible to search +constexpr Value Tempo = Value(20); // Must be visible to search extern std::atomic Contempt; diff --git a/src/material.cpp b/src/material.cpp index 4cfda03e..921e3007 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -31,7 +31,7 @@ namespace { // Polynomial material imbalance parameters - const int QuadraticOurs[][PIECE_TYPE_NB] = { + constexpr int QuadraticOurs[][PIECE_TYPE_NB] = { // OUR PIECES // pair pawn knight bishop rook queen {1667 }, // Bishop pair @@ -42,7 +42,7 @@ namespace { {-189, 24, 117, 133, -134, -10 } // Queen }; - const int QuadraticTheirs[][PIECE_TYPE_NB] = { + constexpr int QuadraticTheirs[][PIECE_TYPE_NB] = { // THEIR PIECES // pair pawn knight bishop rook queen { 0 }, // Bishop pair @@ -87,7 +87,7 @@ namespace { template int imbalance(const int pieceCount[][PIECE_TYPE_NB]) { - const Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); int bonus = 0; @@ -215,13 +215,13 @@ Entry* probe(const Position& pos) { // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder // for the bishop pair "extended piece", which allows us to be more flexible // in defining bishop pair bonuses. - const int PieceCount[COLOR_NB][PIECE_TYPE_NB] = { + const int pieceCount[COLOR_NB][PIECE_TYPE_NB] = { { pos.count(WHITE) > 1, pos.count(WHITE), pos.count(WHITE), pos.count(WHITE) , pos.count(WHITE), pos.count(WHITE) }, { pos.count(BLACK) > 1, pos.count(BLACK), pos.count(BLACK), pos.count(BLACK) , pos.count(BLACK), pos.count(BLACK) } }; - e->value = int16_t((imbalance(PieceCount) - imbalance(PieceCount)) / 16); + e->value = int16_t((imbalance(pieceCount) - imbalance(pieceCount)) / 16); return e; } diff --git a/src/movegen.cpp b/src/movegen.cpp index cb221d95..ed8745e8 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -28,7 +28,7 @@ namespace { template ExtMove* generate_castling(const Position& pos, ExtMove* moveList, Color us) { - static const bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO); + static constexpr bool KingSide = (Cr == WHITE_OO || Cr == BLACK_OO); if (pos.castling_impeded(Cr) || !pos.can_castle(Cr)) return moveList; @@ -42,10 +42,10 @@ namespace { assert(!pos.checkers()); - const Direction K = Chess960 ? kto > kfrom ? WEST : EAST - : KingSide ? WEST : EAST; + const Direction step = Chess960 ? kto > kfrom ? WEST : EAST + : KingSide ? WEST : EAST; - for (Square s = kto; s != kfrom; s += K) + for (Square s = kto; s != kfrom; s += step) if (pos.attackers_to(s) & enemies) return moveList; @@ -94,13 +94,13 @@ namespace { // Compute our parametrized parameters at compile time, named according to // the point of view of white side. - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB); - const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB); - const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); - const Direction Up = (Us == WHITE ? NORTH : SOUTH); - const Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST); - const Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB); + constexpr Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB); + constexpr Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); + constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); + constexpr Direction UpRight = (Us == WHITE ? NORTH_EAST : SOUTH_WEST); + constexpr Direction UpLeft = (Us == WHITE ? NORTH_WEST : SOUTH_EAST); Bitboard emptySquares; @@ -261,7 +261,7 @@ namespace { template ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target) { - const bool Checks = Type == QUIET_CHECKS; + constexpr bool Checks = Type == QUIET_CHECKS; moveList = generate_pawn_moves(pos, moveList, target); moveList = generate_moves(pos, moveList, Us, target); diff --git a/src/pawns.cpp b/src/pawns.cpp index 67bf59d1..56f5d723 100644 --- a/src/pawns.cpp +++ b/src/pawns.cpp @@ -32,20 +32,20 @@ namespace { #define S(mg, eg) make_score(mg, eg) // Isolated pawn penalty - const Score Isolated = S(13, 18); + constexpr Score Isolated = S(13, 18); // Backward pawn penalty - const Score Backward = S(24, 12); + constexpr Score Backward = S(24, 12); // Connected pawn bonus by opposed, phalanx, #support and rank Score Connected[2][2][3][RANK_NB]; // Doubled pawn penalty - const Score Doubled = S(18, 38); + constexpr Score Doubled = S(18, 38); // Weakness of our pawn shelter in front of the king by [isKingFile][distance from edge][rank]. // RANK_1 = 0 is used for files where we have no pawns or our pawn is behind our king. - const Value ShelterWeakness[][int(FILE_NB) / 2][RANK_NB] = { + constexpr Value ShelterWeakness[][int(FILE_NB) / 2][RANK_NB] = { { { V( 98), V(20), V(11), V(42), V( 83), V( 84), V(101) }, // Not On King file { V(103), V( 8), V(33), V(86), V( 87), V(105), V(113) }, { V(100), V( 2), V(65), V(95), V( 59), V( 89), V(115) }, @@ -59,7 +59,7 @@ namespace { // Danger of enemy pawns moving toward our king by [type][distance from edge][rank]. // For the unopposed and unblocked cases, RANK_1 = 0 is used when opponent has // no pawn on the given file, or their pawn is behind our king. - const Value StormDanger[][4][RANK_NB] = { + constexpr Value StormDanger[][4][RANK_NB] = { { { V( 0), V(-290), V(-274), V(57), V(41) }, // BlockedByKing { V( 0), V( 60), V( 144), V(39), V(13) }, { V( 0), V( 65), V( 141), V(41), V(34) }, @@ -80,7 +80,7 @@ namespace { // Max bonus for king safety. Corresponds to start position with all the pawns // in front of the king and no enemy pawn on the horizon. - const Value MaxSafetyBonus = V(258); + constexpr Value MaxSafetyBonus = V(258); #undef S #undef V @@ -88,8 +88,8 @@ namespace { template Score evaluate(const Position& pos, Pawns::Entry* e) { - const Color Them = (Us == WHITE ? BLACK : WHITE); - const Direction Up = (Us == WHITE ? NORTH : SOUTH); + constexpr Color Them = (Us == WHITE ? BLACK : WHITE); + constexpr Direction Up = (Us == WHITE ? NORTH : SOUTH); Bitboard b, neighbours, stoppers, doubled, supported, phalanx; Bitboard lever, leverPush; @@ -191,7 +191,7 @@ namespace Pawns { void init() { - static const int Seed[RANK_NB] = { 0, 13, 24, 18, 76, 100, 175, 330 }; + static constexpr int Seed[RANK_NB] = { 0, 13, 24, 18, 76, 100, 175, 330 }; for (int opposed = 0; opposed <= 1; ++opposed) for (int phalanx = 0; phalanx <= 1; ++phalanx) diff --git a/src/position.cpp b/src/position.cpp index 9cec2c14..c0e2232c 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -52,8 +52,8 @@ namespace { const string PieceToChar(" PNBRQK pnbrqk"); -const Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, - B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING }; +constexpr Piece Pieces[] = { W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING, + B_PAWN, B_KNIGHT, B_BISHOP, B_ROOK, B_QUEEN, B_KING }; // min_attacker() is a helper function used by see_ge() to locate the least // valuable attacker for the side to move, remove the attacker we just found @@ -1146,7 +1146,7 @@ void Position::flip() { bool Position::pos_is_ok() const { - const bool Fast = true; // Quick (default) or full check? + constexpr bool Fast = true; // Quick (default) or full check? if ( (sideToMove != WHITE && sideToMove != BLACK) || piece_on(square(WHITE)) != W_KING diff --git a/src/psqt.cpp b/src/psqt.cpp index cbdce81a..e0f15c06 100644 --- a/src/psqt.cpp +++ b/src/psqt.cpp @@ -35,7 +35,7 @@ namespace PSQT { // type on a given square a (middlegame, endgame) score pair is assigned. Table // is defined for files A..D and white side: it is symmetric for black side and // second half of the files. -const Score Bonus[][RANK_NB][int(FILE_NB) / 2] = { +constexpr Score Bonus[][RANK_NB][int(FILE_NB) / 2] = { { }, { // Pawn { S( 0, 0), S( 0, 0), S( 0, 0), S( 0, 0) }, diff --git a/src/search.cpp b/src/search.cpp index afc4a671..176006fc 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -63,12 +63,12 @@ namespace { enum NodeType { NonPV, PV }; // Sizes and phases of the skip-blocks, used for distributing search depths across the threads - const int SkipSize[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; - const int SkipPhase[] = { 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7 }; + constexpr int SkipSize[] = { 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4 }; + constexpr int SkipPhase[] = { 0, 1, 0, 1, 2, 3, 0, 1, 2, 3, 4, 5, 0, 1, 2, 3, 4, 5, 6, 7 }; // Razor and futility margins - const int RazorMargin1 = 590; - const int RazorMargin2 = 604; + constexpr int RazorMargin1 = 590; + constexpr int RazorMargin2 = 604; Value futility_margin(Depth d, bool improving) { return Value((175 - 50 * improving) * d / ONE_PLY); } diff --git a/src/search.h b/src/search.h index 1274b968..22f9f0d7 100644 --- a/src/search.h +++ b/src/search.h @@ -32,7 +32,7 @@ class Position; namespace Search { /// Threshold used for countermoves based pruning -const int CounterMovePruneThreshold = 0; +constexpr int CounterMovePruneThreshold = 0; /// Stack struct keeps track of the information we need to remember from nodes diff --git a/src/syzygy/tbprobe.cpp b/src/syzygy/tbprobe.cpp index b50275e3..722679fd 100644 --- a/src/syzygy/tbprobe.cpp +++ b/src/syzygy/tbprobe.cpp @@ -102,7 +102,7 @@ struct LR { static_assert(sizeof(LR) == 3, "LR tree entry must be 3 bytes"); -const int TBPIECES = 6; +constexpr int TBPIECES = 6; struct PairsData { int flags; @@ -208,7 +208,7 @@ int MapKK[10][SQUARE_NB]; // [MapA1D1D4][SQUARE_NB] bool pawns_comp(Square i, Square j) { return MapPawns[i] < MapPawns[j]; } int off_A1H8(Square sq) { return int(rank_of(sq)) - file_of(sq); } -const Value WDL_to_value[] = { +constexpr Value WDL_to_value[] = { -VALUE_MATE + MAX_PLY + 1, VALUE_DRAW - 2, VALUE_DRAW, @@ -255,8 +255,8 @@ class HashTable { typedef std::pair EntryPair; typedef std::pair Entry; - static const int TBHASHBITS = 10; - static const int HSHMAX = 5; + static constexpr int TBHASHBITS = 10; + static constexpr int HSHMAX = 5; Entry hashTable[1 << TBHASHBITS][HSHMAX]; @@ -315,9 +315,9 @@ public: TBFile(const std::string& f) { #ifndef _WIN32 - const char SepChar = ':'; + constexpr char SepChar = ':'; #else - const char SepChar = ';'; + constexpr char SepChar = ';'; #endif std::stringstream ss(Paths); std::string path; @@ -649,7 +649,7 @@ WDLScore map_score(WDLEntry*, File, int value, WDLScore) { return WDLScore(value int map_score(DTZEntry* entry, File f, int value, WDLScore wdl) { - const int WDLMap[] = { 1, 3, 0, 2, 0 }; + constexpr int WDLMap[] = { 1, 3, 0, 2, 0 }; int flags = entry->hasPawns ? entry->pawnTable.file[f].precomp->flags : entry->pieceTable.precomp->flags; @@ -682,7 +682,7 @@ int map_score(DTZEntry* entry, File f, int value, WDLScore wdl) { template::type> T do_probe_table(const Position& pos, Entry* entry, WDLScore wdl, ProbeState* result) { - const bool IsWDL = std::is_same::value; + constexpr bool IsWDL = std::is_same::value; Square squares[TBPIECES]; Piece pieces[TBPIECES]; @@ -1081,16 +1081,16 @@ void do_init(Entry& e, T& p, uint8_t* data) { data++; // First byte stores flags - const int Sides = IsWDL && (e.key != e.key2) ? 2 : 1; - const File MaxFile = e.hasPawns ? FILE_D : FILE_A; + const int sides = IsWDL && (e.key != e.key2) ? 2 : 1; + const File maxFile = e.hasPawns ? FILE_D : FILE_A; bool pp = e.hasPawns && e.pawnTable.pawnCount[1]; // Pawns on both sides assert(!pp || e.pawnTable.pawnCount[0]); - for (File f = FILE_A; f <= MaxFile; ++f) { + for (File f = FILE_A; f <= maxFile; ++f) { - for (int i = 0; i < Sides; i++) + for (int i = 0; i < sides; i++) item(p, i, f).precomp = new PairsData(); int order[][2] = { { *data & 0xF, pp ? *(data + 1) & 0xF : 0xF }, @@ -1098,36 +1098,36 @@ void do_init(Entry& e, T& p, uint8_t* data) { data += 1 + pp; for (int k = 0; k < e.pieceCount; ++k, ++data) - for (int i = 0; i < Sides; i++) + for (int i = 0; i < sides; i++) item(p, i, f).precomp->pieces[k] = Piece(i ? *data >> 4 : *data & 0xF); - for (int i = 0; i < Sides; ++i) + for (int i = 0; i < sides; ++i) set_groups(e, item(p, i, f).precomp, order[i], f); } data += (uintptr_t)data & 1; // Word alignment - for (File f = FILE_A; f <= MaxFile; ++f) - for (int i = 0; i < Sides; i++) + for (File f = FILE_A; f <= maxFile; ++f) + for (int i = 0; i < sides; i++) data = set_sizes(item(p, i, f).precomp, data); if (!IsWDL) - data = set_dtz_map(e, p, data, MaxFile); + data = set_dtz_map(e, p, data, maxFile); - for (File f = FILE_A; f <= MaxFile; ++f) - for (int i = 0; i < Sides; i++) { + for (File f = FILE_A; f <= maxFile; ++f) + for (int i = 0; i < sides; i++) { (d = item(p, i, f).precomp)->sparseIndex = (SparseEntry*)data; data += d->sparseIndexSize * sizeof(SparseEntry); } - for (File f = FILE_A; f <= MaxFile; ++f) - for (int i = 0; i < Sides; i++) { + for (File f = FILE_A; f <= maxFile; ++f) + for (int i = 0; i < sides; i++) { (d = item(p, i, f).precomp)->blockLength = (uint16_t*)data; data += d->blockLengthSize * sizeof(uint16_t); } - for (File f = FILE_A; f <= MaxFile; ++f) - for (int i = 0; i < Sides; i++) { + for (File f = FILE_A; f <= maxFile; ++f) + for (int i = 0; i < sides; i++) { data = (uint8_t*)(((uintptr_t)data + 0x3F) & ~0x3F); // 64 byte alignment (d = item(p, i, f).precomp)->data = data; data += d->blocksNum * d->sizeofBlock; @@ -1137,7 +1137,7 @@ void do_init(Entry& e, T& p, uint8_t* data) { template void* init(Entry& e, const Position& pos) { - const bool IsWDL = std::is_same::value; + constexpr bool IsWDL = std::is_same::value; static Mutex mutex; @@ -1158,7 +1158,7 @@ void* init(Entry& e, const Position& pos) { b += std::string(popcount(pos.pieces(BLACK, pt)), PieceToChar[pt]); } - const uint8_t TB_MAGIC[][4] = { { 0xD7, 0x66, 0x0C, 0xA5 }, + constexpr uint8_t TB_MAGIC[][4] = { { 0xD7, 0x66, 0x0C, 0xA5 }, { 0x71, 0xE8, 0x23, 0x5D } }; fname = (e.key == pos.material_key() ? w + 'v' + b : b + 'v' + w) diff --git a/src/timeman.cpp b/src/timeman.cpp index f6ead0b2..e63454eb 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -32,9 +32,9 @@ namespace { enum TimeType { OptimumTime, MaxTime }; - const int MoveHorizon = 50; // Plan time management at most this many moves ahead - const double MaxRatio = 7.3; // When in trouble, we can step over reserved time with this ratio - const double StealRatio = 0.34; // However we must not steal time from remaining moves over this ratio + constexpr int MoveHorizon = 50; // Plan time management at most this many moves ahead + constexpr double MaxRatio = 7.3; // When in trouble, we can step over reserved time with this ratio + constexpr double StealRatio = 0.34; // However we must not steal time from remaining moves over this ratio // move_importance() is a skew-logistic function based on naive statistical @@ -44,9 +44,9 @@ namespace { double move_importance(int ply) { - const double XScale = 6.85; - const double XShift = 64.5; - const double Skew = 0.171; + constexpr double XScale = 6.85; + constexpr double XShift = 64.5; + constexpr double Skew = 0.171; return pow((1 + exp((ply - XShift) / XScale)), -Skew) + DBL_MIN; // Ensure non-zero } @@ -54,8 +54,8 @@ namespace { template int remaining(int myTime, int movesToGo, int ply, int slowMover) { - const double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio); - const double TStealRatio = (T == OptimumTime ? 0 : StealRatio); + constexpr double TMaxRatio = (T == OptimumTime ? 1 : MaxRatio); + constexpr double TStealRatio = (T == OptimumTime ? 0 : StealRatio); double moveImportance = (move_importance(ply) * slowMover) / 100; double otherMovesImportance = 0; @@ -106,12 +106,12 @@ void TimeManagement::init(Search::LimitsType& limits, Color us, int ply) { startTime = limits.startTime; optimumTime = maximumTime = std::max(limits.time[us], minThinkingTime); - const int MaxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon; + const int maxMTG = limits.movestogo ? std::min(limits.movestogo, MoveHorizon) : MoveHorizon; // We calculate optimum time usage for different hypothetical "moves to go"-values // and choose the minimum of calculated search time values. Usually the greatest // hypMTG gives the minimum values. - for (int hypMTG = 1; hypMTG <= MaxMTG; ++hypMTG) + for (int hypMTG = 1; hypMTG <= maxMTG; ++hypMTG) { // Calculate thinking time for hypothetical "moves to go"-value int hypMyTime = limits.time[us] diff --git a/src/tt.h b/src/tt.h index ca7dfbfe..5f9c29e4 100644 --- a/src/tt.h +++ b/src/tt.h @@ -85,8 +85,8 @@ private: class TranspositionTable { - static const int CacheLineSize = 64; - static const int ClusterSize = 3; + static constexpr int CacheLineSize = 64; + static constexpr int ClusterSize = 3; struct Cluster { TTEntry entry[ClusterSize]; diff --git a/src/types.h b/src/types.h index 3415a620..2cf52ef1 100644 --- a/src/types.h +++ b/src/types.h @@ -80,28 +80,28 @@ #endif #ifdef USE_POPCNT -const bool HasPopCnt = true; +constexpr bool HasPopCnt = true; #else -const bool HasPopCnt = false; +constexpr bool HasPopCnt = false; #endif #ifdef USE_PEXT -const bool HasPext = true; +constexpr bool HasPext = true; #else -const bool HasPext = false; +constexpr bool HasPext = false; #endif #ifdef IS_64BIT -const bool Is64Bit = true; +constexpr bool Is64Bit = true; #else -const bool Is64Bit = false; +constexpr bool Is64Bit = false; #endif typedef uint64_t Key; typedef uint64_t Bitboard; -const int MAX_MOVES = 256; -const int MAX_PLY = 128; +constexpr int MAX_MOVES = 256; +constexpr int MAX_PLY = 128; /// A move needs 16 bits to be stored /// diff --git a/src/ucioption.cpp b/src/ucioption.cpp index f648c017..bca4a1f1 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -56,7 +56,7 @@ bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const void init(OptionsMap& o) { // at most 2^32 clusters. - const int MaxHashMB = Is64Bit ? 131072 : 2048; + constexpr int MaxHashMB = Is64Bit ? 131072 : 2048; o["Debug Log File"] << Option("", on_logger); o["Contempt"] << Option(12, -100, 100);