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];
// 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]];
}
-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];
// 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,
// 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,
};
// 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) {
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
// 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
template<Tracing T> template<Color Us>
void Evaluation<T>::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<Down>(pos.pieces()) | LowRanks);
template<Tracing T> template<Color Us, PieceType Pt>
Score Evaluation<T>::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<Pt>(Us);
template<Tracing T> template<Color Us>
Score Evaluation<T>::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<KING>(Us);
template<Tracing T> template<Color Us>
Score Evaluation<T>::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;
template<Tracing T> template<Color Us>
Score Evaluation<T>::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<KING>(c), s), 5);
template<Tracing T> template<Color Us>
Score Evaluation<T>::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);
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<Score> Contempt;
// 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
{-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
template<Color Us>
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;
// 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<BISHOP>(WHITE) > 1, pos.count<PAWN>(WHITE), pos.count<KNIGHT>(WHITE),
pos.count<BISHOP>(WHITE) , pos.count<ROOK>(WHITE), pos.count<QUEEN >(WHITE) },
{ pos.count<BISHOP>(BLACK) > 1, pos.count<PAWN>(BLACK), pos.count<KNIGHT>(BLACK),
pos.count<BISHOP>(BLACK) , pos.count<ROOK>(BLACK), pos.count<QUEEN >(BLACK) } };
- e->value = int16_t((imbalance<WHITE>(PieceCount) - imbalance<BLACK>(PieceCount)) / 16);
+ e->value = int16_t((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
return e;
}
template<CastlingRight Cr, bool Checks, bool Chess960>
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;
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;
// 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;
template<Color Us, GenType Type>
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<Us, Type>(pos, moveList, target);
moveList = generate_moves<KNIGHT, Checks>(pos, moveList, Us, target);
#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) },
// 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) },
// 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
template<Color Us>
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;
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)
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
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<KING>(WHITE)) != W_KING
// 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) },
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);
}
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
static_assert(sizeof(LR) == 3, "LR tree entry must be 3 bytes");
-const int TBPIECES = 6;
+constexpr int TBPIECES = 6;
struct PairsData {
int flags;
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,
typedef std::pair<WDLEntry*, DTZEntry*> EntryPair;
typedef std::pair<Key, EntryPair> 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];
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;
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;
template<typename Entry, typename T = typename Ret<Entry>::type>
T do_probe_table(const Position& pos, Entry* entry, WDLScore wdl, ProbeState* result) {
- const bool IsWDL = std::is_same<Entry, WDLEntry>::value;
+ constexpr bool IsWDL = std::is_same<Entry, WDLEntry>::value;
Square squares[TBPIECES];
Piece pieces[TBPIECES];
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 },
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;
template<typename Entry>
void* init(Entry& e, const Position& pos) {
- const bool IsWDL = std::is_same<Entry, WDLEntry>::value;
+ constexpr bool IsWDL = std::is_same<Entry, WDLEntry>::value;
static Mutex mutex;
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)
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
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
}
template<TimeType T>
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;
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]
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];
#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
///
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);