From: Alexander Kure Date: Mon, 31 Oct 2011 04:38:44 +0000 (-0400) Subject: Replaced macros Min() and Max() with corresponding STL algorithms std::min() and... X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=5c8af7ccb8f59f901740d5a8f4a9270f69487583;ds=sidebyside Replaced macros Min() and Max() with corresponding STL algorithms std::min() and std::max() --- diff --git a/src/bitboard.cpp b/src/bitboard.cpp index 3bed69be..415f9869 100644 --- a/src/bitboard.cpp +++ b/src/bitboard.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "bitboard.h" #include "bitcount.h" @@ -160,7 +161,7 @@ void init_bitboards() { for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++) for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++) - SquareDistance[s1][s2] = Max(file_distance(s1, s2), rank_distance(s1, s2)); + SquareDistance[s1][s2] = std::max(file_distance(s1, s2), rank_distance(s1, s2)); SquaresByColorBB[DARK] = 0xAA55AA55AA55AA55ULL; SquaresByColorBB[LIGHT] = ~SquaresByColorBB[DARK]; @@ -247,7 +248,7 @@ void init_bitboards() { int f = file_distance(s1, s2); int r = rank_distance(s1, s2); - Square d = (s2 - s1) / Max(f, r); + Square d = (s2 - s1) / std::max(f, r); for (Square s3 = s1 + d; s3 != s2; s3 += d) set_bit(&BetweenBB[s1][s2], s3); diff --git a/src/endgame.cpp b/src/endgame.cpp index a4526ff6..4e6de809 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -18,6 +18,7 @@ */ #include +#include #include "bitcount.h" #include "endgame.h" @@ -632,7 +633,7 @@ ScaleFactor Endgame::apply(const Position& pos) const { || pos.pawn_is_passed(strongerSide, wpsq2)) return SCALE_FACTOR_NONE; - Rank r = Max(relative_rank(strongerSide, wpsq1), relative_rank(strongerSide, wpsq2)); + Rank r = std::max(relative_rank(strongerSide, wpsq1), relative_rank(strongerSide, wpsq2)); if ( file_distance(bksq, wpsq1) <= 1 && file_distance(bksq, wpsq2) <= 1 diff --git a/src/evaluate.cpp b/src/evaluate.cpp index f39cd5b3..558f05b0 100644 --- a/src/evaluate.cpp +++ b/src/evaluate.cpp @@ -21,6 +21,7 @@ #include #include #include +#include #include "bitcount.h" #include "evaluate.h" @@ -698,7 +699,7 @@ namespace { // the number and types of the enemy's attacking pieces, the number of // attacked and undefended squares around our king, the square of the // king, and the quality of the pawn shelter. - attackUnits = Min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2) + attackUnits = std::min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2) + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + count_1s(undefended)) + InitKingDanger[relative_square(Us, ksq)] - mg_value(ei.pi->king_shelter(pos, ksq)) / 32; @@ -762,7 +763,7 @@ namespace { attackUnits += KnightCheckBonus * count_1s(b); // To index KingDangerTable[] attackUnits must be in [0, 99] range - attackUnits = Min(99, Max(0, attackUnits)); + attackUnits = std::min(99, std::max(0, attackUnits)); // Finally, extract the king danger score from the KingDangerTable[] // array and subtract the score from evaluation. Set also margins[] @@ -933,7 +934,7 @@ namespace { continue; pliesToGo = 2 * movesToGo - int(c == pos.side_to_move()); - pliesToQueen[c] = Min(pliesToQueen[c], pliesToGo); + pliesToQueen[c] = std::min(pliesToQueen[c], pliesToGo); } } @@ -1003,7 +1004,7 @@ namespace { while (b2) // This while-loop could be replaced with LSB/MSB (depending on color) { d = square_distance(blockSq, pop_1st_bit(&b2)) - 2; - movesToGo = Min(movesToGo, d); + movesToGo = std::min(movesToGo, d); } } @@ -1013,7 +1014,7 @@ namespace { while (b2) // This while-loop could be replaced with LSB/MSB (depending on color) { d = square_distance(blockSq, pop_1st_bit(&b2)) - 2; - movesToGo = Min(movesToGo, d); + movesToGo = std::min(movesToGo, d); } // If obstacle can be destroyed with an immediate pawn exchange / sacrifice, @@ -1027,7 +1028,7 @@ namespace { // Plies needed for the king to capture all the blocking pawns d = square_distance(pos.king_square(loserSide), blockSq); - minKingDist = Min(minKingDist, d); + minKingDist = std::min(minKingDist, d); kingptg = (minKingDist + blockersCount) * 2; } @@ -1126,9 +1127,9 @@ namespace { t[i] = Value(int(0.4 * i * i)); if (i > 0) - t[i] = Min(t[i], t[i - 1] + MaxSlope); + t[i] = std::min(t[i], t[i - 1] + MaxSlope); - t[i] = Min(t[i], Peak); + t[i] = std::min(t[i], Peak); } // Then apply the weights and get the final KingDangerTable[] array diff --git a/src/history.h b/src/history.h index 4d84be70..f95faa0f 100644 --- a/src/history.h +++ b/src/history.h @@ -20,8 +20,9 @@ #if !defined(HISTORY_H_INCLUDED) #define HISTORY_H_INCLUDED -#include #include "types.h" +#include +#include /// The History class stores statistics about how often different moves /// have been successful or unsuccessful during the current search. These @@ -64,7 +65,7 @@ inline Value History::gain(Piece p, Square to) const { } inline void History::update_gain(Piece p, Square to, Value g) { - maxGains[p][to] = Max(g, maxGains[p][to] - 1); + maxGains[p][to] = std::max(g, maxGains[p][to] - 1); } #endif // !defined(HISTORY_H_INCLUDED) diff --git a/src/lock.h b/src/lock.h index 939b7da0..b64293cf 100644 --- a/src/lock.h +++ b/src/lock.h @@ -38,9 +38,11 @@ typedef pthread_cond_t WaitCondition; #else +#define NOMINMAX // disable macros min() and max() #define WIN32_LEAN_AND_MEAN #include #undef WIN32_LEAN_AND_MEAN +#undef NOMINMAX // Default fast and race free locks and condition variables #if !defined(OLD_LOCKS) diff --git a/src/material.cpp b/src/material.cpp index 7ad5dc47..6171c70a 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -19,6 +19,7 @@ #include #include +#include #include "material.h" @@ -203,13 +204,13 @@ MaterialInfo* MaterialInfoTable::get_material_info(const Position& pos) const { if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMidgame) { mi->factor[WHITE] = uint8_t - (npm_w == npm_b || npm_w < RookValueMidgame ? 0 : NoPawnsSF[Min(pos.piece_count(WHITE, BISHOP), 2)]); + (npm_w == npm_b || npm_w < RookValueMidgame ? 0 : NoPawnsSF[std::min(pos.piece_count(WHITE, BISHOP), 2)]); } if (pos.piece_count(BLACK, PAWN) == 0 && npm_b - npm_w <= BishopValueMidgame) { mi->factor[BLACK] = uint8_t - (npm_w == npm_b || npm_b < RookValueMidgame ? 0 : NoPawnsSF[Min(pos.piece_count(BLACK, BISHOP), 2)]); + (npm_w == npm_b || npm_b < RookValueMidgame ? 0 : NoPawnsSF[std::min(pos.piece_count(BLACK, BISHOP), 2)]); } // Compute the space weight diff --git a/src/misc.cpp b/src/misc.cpp index d5383643..10c779ed 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -29,6 +29,7 @@ #else #define _CRT_SECURE_NO_DEPRECATE +#define NOMINMAX // disable macros min() and max() #include #include @@ -43,6 +44,7 @@ #include #include #include +#include #include "bitcount.h" #include "misc.h" @@ -155,16 +157,16 @@ int cpu_count() { #if defined(_MSC_VER) SYSTEM_INFO s; GetSystemInfo(&s); - return Min(s.dwNumberOfProcessors, MAX_THREADS); + return std::min(int(s.dwNumberOfProcessors), MAX_THREADS); #else # if defined(_SC_NPROCESSORS_ONLN) - return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS); + return std::min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS); # elif defined(__hpux) struct pst_dynamic psd; if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1) return 1; - return Min(psd.psd_proc_cnt, MAX_THREADS); + return std::min(psd.psd_proc_cnt, MAX_THREADS); # else return 1; # endif @@ -232,7 +234,7 @@ int input_available() { GetNumberOfConsoleInputEvents(inh, &nchars); // Read data from console without removing it from the buffer - if (nchars <= 0 || !PeekConsoleInput(inh, rec, Min(nchars, 256), &recCnt)) + if (nchars <= 0 || !PeekConsoleInput(inh, rec, std::min(int(nchars), 256), &recCnt)) return 0; // Search for at least one keyboard event diff --git a/src/movegen.cpp b/src/movegen.cpp index eb390cd1..dd849c15 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -18,6 +18,7 @@ */ #include +#include #include "bitcount.h" #include "movegen.h" @@ -516,12 +517,12 @@ namespace { // (including the final square), and all the squares between the rook's initial // and final squares (including the final square), must be vacant except for // the king and castling rook. - for (Square s = Min(kfrom, kto); s <= Max(kfrom, kto); s++) + for (Square s = std::min(kfrom, kto); s <= std::max(kfrom, kto); s++) if ( (s != kfrom && s != rfrom && !pos.square_is_empty(s)) ||(pos.attackers_to(s) & pos.pieces(them))) return mlist; - for (Square s = Min(rfrom, rto); s <= Max(rfrom, rto); s++) + for (Square s = std::min(rfrom, rto); s <= std::max(rfrom, rto); s++) if (s != kfrom && s != rfrom && !pos.square_is_empty(s)) return mlist; diff --git a/src/position.cpp b/src/position.cpp index 12485fbb..0f6a3feb 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -22,6 +22,7 @@ #include #include #include +#include #include "bitcount.h" #include "movegen.h" @@ -223,7 +224,7 @@ void Position::from_fen(const string& fenStr, bool isChess960) { // Convert from fullmove starting from 1 to ply starting from 0, // handle also common incorrect FEN with fullmove = 0. - startPosPly = Max(2 * (startPosPly - 1), 0) + int(sideToMove == BLACK); + startPosPly = std::max(2 * (startPosPly - 1), 0) + int(sideToMove == BLACK); st->key = compute_key(); st->pawnKey = compute_pawn_key(); @@ -1330,7 +1331,7 @@ int Position::see(Move m) const { // Having built the swap list, we negamax through it to find the best // achievable score from the point of view of the side to move. while (--slIndex) - swapList[slIndex-1] = Min(-swapList[slIndex], swapList[slIndex-1]); + swapList[slIndex-1] = std::min(-swapList[slIndex], swapList[slIndex-1]); return swapList[0]; } @@ -1502,7 +1503,7 @@ bool Position::is_draw() const { // Draw by repetition? if (!SkipRepetition) { - int i = 4, e = Min(st->rule50, st->pliesFromNull); + int i = 4, e = std::min(st->rule50, st->pliesFromNull); if (i <= e) { diff --git a/src/search.cpp b/src/search.cpp index 30e05020..d55a1e17 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include "book.h" #include "evaluate.h" @@ -128,7 +129,7 @@ namespace { inline Value futility_margin(Depth d, int mn) { - return d < 7 * ONE_PLY ? FutilityMargins[Max(d, 1)][Min(mn, 63)] + return d < 7 * ONE_PLY ? FutilityMargins[std::max(int(d), 1)][std::min(mn, 63)] : 2 * VALUE_INFINITE; } @@ -144,7 +145,7 @@ namespace { template inline Depth reduction(Depth d, int mn) { - return (Depth) Reductions[PvNode][Min(d / ONE_PLY, 63)][Min(mn, 63)]; + return (Depth) Reductions[PvNode][std::min(int(d) / ONE_PLY, 63)][std::min(mn, 63)]; } // Easy move margin. An easy move candidate must be at least this much @@ -196,6 +197,7 @@ namespace { bool connected_threat(const Position& pos, Move m, Move threat); Value refine_eval(const TTEntry* tte, Value defaultEval, int ply); void update_history(const Position& pos, Move move, Depth depth, Move movesSearched[], int moveCount); + void update_gains(const Position& pos, Move move, Value before, Value after); void do_skill_level(Move* best, Move* ponder); int current_search_time(int set = 0); @@ -290,7 +292,7 @@ namespace { *dangerous = true; } - return Min(result, ONE_PLY); + return std::min(result, ONE_PLY); } } // namespace @@ -372,7 +374,7 @@ bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) { // Set best NodesBetweenPolls interval to avoid lagging under time pressure if (Limits.maxNodes) - NodesBetweenPolls = Min(Limits.maxNodes, 30000); + NodesBetweenPolls = std::min(Limits.maxNodes, 30000); else if (Limits.time && Limits.time < 1000) NodesBetweenPolls = 1000; else if (Limits.time && Limits.time < 5000) @@ -416,7 +418,7 @@ bool think(Position& pos, const SearchLimits& limits, Move searchMoves[]) { // Do we have to play with skill handicap? In this case enable MultiPV that // we will use behind the scenes to retrieve a set of possible moves. SkillLevelEnabled = (SkillLevel < 20); - MultiPV = (SkillLevelEnabled ? Max(UCIMultiPV, 4) : UCIMultiPV); + MultiPV = (SkillLevelEnabled ? std::max(UCIMultiPV, 4) : UCIMultiPV); // Wake up needed threads and reset maxPly counter for (int i = 0; i < Threads.size(); i++) @@ -502,7 +504,7 @@ namespace { *ponderMove = bestMove = easyMove = skillBest = skillPonder = MOVE_NONE; depth = aspirationDelta = 0; value = alpha = -VALUE_INFINITE, beta = VALUE_INFINITE; - ss->currentMove = MOVE_NULL; // Hack to skip update gains + ss->currentMove = MOVE_NULL; // Hack to skip update_gains() // Moves to search are verified and copied Rml.init(pos, searchMoves); @@ -526,7 +528,7 @@ namespace { Rml.bestMoveChanges = 0; // MultiPV loop. We perform a full root search for each PV line - for (MultiPVIdx = 0; MultiPVIdx < Min(MultiPV, (int)Rml.size()); MultiPVIdx++) + for (MultiPVIdx = 0; MultiPVIdx < std::min(MultiPV, (int)Rml.size()); MultiPVIdx++) { // Calculate dynamic aspiration window based on previous iterations if (depth >= 5 && abs(Rml[MultiPVIdx].prevScore) < VALUE_KNOWN_WIN) @@ -534,11 +536,11 @@ namespace { int prevDelta1 = bestValues[depth - 1] - bestValues[depth - 2]; int prevDelta2 = bestValues[depth - 2] - bestValues[depth - 3]; - aspirationDelta = Min(Max(abs(prevDelta1) + abs(prevDelta2) / 2, 16), 24); + aspirationDelta = std::min(std::max(abs(prevDelta1) + abs(prevDelta2) / 2, 16), 24); aspirationDelta = (aspirationDelta + 7) / 8 * 8; // Round to match grainSize - alpha = Max(Rml[MultiPVIdx].prevScore - aspirationDelta, -VALUE_INFINITE); - beta = Min(Rml[MultiPVIdx].prevScore + aspirationDelta, VALUE_INFINITE); + alpha = std::max(Rml[MultiPVIdx].prevScore - aspirationDelta, -VALUE_INFINITE); + beta = std::min(Rml[MultiPVIdx].prevScore + aspirationDelta, VALUE_INFINITE); } else { @@ -550,7 +552,7 @@ namespace { // research with bigger window until not failing high/low anymore. do { // Search starts from ss+1 to allow referencing (ss-1). This is - // needed by update gains and ss copy when splitting at Root. + // needed by update_gains() and ss copy when splitting at Root. value = search(pos, ss+1, alpha, beta, depth * ONE_PLY); // Bring to front the best move. It is critical that sorting is @@ -584,7 +586,7 @@ namespace { // protocol requires to send all the PV lines also if are still // to be searched and so refer to the previous search's score. if ((value > alpha && value < beta) || current_search_time() > 2000) - for (int i = 0; i < Min(UCIMultiPV, (int)Rml.size()); i++) + for (int i = 0; i < std::min(UCIMultiPV, (int)Rml.size()); i++) { bool updated = (i <= MultiPVIdx); @@ -606,7 +608,7 @@ namespace { // research, otherwise exit the fail high/low loop. if (value >= beta) { - beta = Min(beta + aspirationDelta, VALUE_INFINITE); + beta = std::min(beta + aspirationDelta, VALUE_INFINITE); aspirationDelta += aspirationDelta / 2; } else if (value <= alpha) @@ -614,7 +616,7 @@ namespace { AspirationFailLow = true; StopOnPonderhit = false; - alpha = Max(alpha - aspirationDelta, -VALUE_INFINITE); + alpha = std::max(alpha - aspirationDelta, -VALUE_INFINITE); aspirationDelta += aspirationDelta / 2; } else @@ -766,8 +768,8 @@ namespace { // Step 3. Mate distance pruning if (!RootNode) { - alpha = Max(value_mated_in(ss->ply), alpha); - beta = Min(value_mate_in(ss->ply+1), beta); + alpha = std::max(value_mated_in(ss->ply), alpha); + beta = std::min(value_mate_in(ss->ply+1), beta); if (alpha >= beta) return alpha; } @@ -819,17 +821,8 @@ namespace { TT.store(posKey, VALUE_NONE, VALUE_TYPE_NONE, DEPTH_NONE, MOVE_NONE, ss->eval, ss->evalMargin); } - // Update gain for the parent non-capture move given the static position - // evaluation before and after the move. - if ( (move = (ss-1)->currentMove) != MOVE_NULL - && (ss-1)->eval != VALUE_NONE - && ss->eval != VALUE_NONE - && pos.captured_piece_type() == PIECE_TYPE_NONE - && !is_special(move)) - { - Square to = move_to(move); - H.update_gain(pos.piece_on(to), to, -(ss-1)->eval - ss->eval); - } + // Save gain for the parent non-capture move + update_gains(pos, (ss-1)->currentMove, (ss-1)->eval, ss->eval); // Step 6. Razoring (is omitted in PV nodes) if ( !PvNode @@ -1688,8 +1681,8 @@ split_point_start: // At split points actual search starts from here Value v = value_from_tt(tte->value(), ply); return ( tte->depth() >= depth - || v >= Max(VALUE_MATE_IN_PLY_MAX, beta) - || v < Min(VALUE_MATED_IN_PLY_MAX, beta)) + || v >= std::max(VALUE_MATE_IN_PLY_MAX, beta) + || v < std::min(VALUE_MATED_IN_PLY_MAX, beta)) && ( ((tte->type() & VALUE_TYPE_LOWER) && v >= beta) || ((tte->type() & VALUE_TYPE_UPPER) && v < beta)); @@ -1734,6 +1727,20 @@ split_point_start: // At split points actual search starts from here } + // update_gains() updates the gains table of a non-capture move given + // the static position evaluation before and after the move. + + void update_gains(const Position& pos, Move m, Value before, Value after) { + + if ( m != MOVE_NULL + && before != VALUE_NONE + && after != VALUE_NONE + && pos.captured_piece_type() == PIECE_TYPE_NONE + && !is_special(m)) + H.update_gain(pos.piece_on(move_to(m)), move_to(m), -(before + after)); + } + + // current_search_time() returns the number of milliseconds which have passed // since the beginning of the current search. @@ -2009,9 +2016,9 @@ split_point_start: // At split points actual search starts from here // Rml list is already sorted by score in descending order int s; int max_s = -VALUE_INFINITE; - int size = Min(MultiPV, (int)Rml.size()); + int size = std::min(MultiPV, (int)Rml.size()); int max = Rml[0].score; - int var = Min(max - Rml[size - 1].score, PawnValueMidgame); + int var = std::min(max - Rml[size - 1].score, int(PawnValueMidgame)); int wk = 120 - 2 * SkillLevel; // PRNG sequence should be non deterministic diff --git a/src/timeman.cpp b/src/timeman.cpp index e29d4e46..b6916900 100644 --- a/src/timeman.cpp +++ b/src/timeman.cpp @@ -18,6 +18,7 @@ */ #include +#include #include "misc.h" #include "search.h" @@ -64,7 +65,7 @@ namespace { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 3, 3, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1 }; - int move_importance(int ply) { return MoveImportance[Min(ply, 511)]; } + int move_importance(int ply) { return MoveImportance[std::min(ply, 511)]; } /// Function Prototypes @@ -114,28 +115,28 @@ void TimeManager::init(const SearchLimits& limits, int currentPly) // We calculate optimum time usage for different hypothetic "moves to go"-values and choose the // minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values. - for (hypMTG = 1; hypMTG <= (limits.movesToGo ? Min(limits.movesToGo, MoveHorizon) : MoveHorizon); hypMTG++) + for (hypMTG = 1; hypMTG <= (limits.movesToGo ? std::min(limits.movesToGo, MoveHorizon) : MoveHorizon); hypMTG++) { // Calculate thinking time for hypothetic "moves to go"-value hypMyTime = limits.time + limits.increment * (hypMTG - 1) - emergencyBaseTime - - emergencyMoveTime * Min(hypMTG, emergencyMoveHorizon); + - emergencyMoveTime * std::min(hypMTG, emergencyMoveHorizon); - hypMyTime = Max(hypMyTime, 0); + hypMyTime = std::max(hypMyTime, 0); t1 = minThinkingTime + remaining(hypMyTime, hypMTG, currentPly); t2 = minThinkingTime + remaining(hypMyTime, hypMTG, currentPly); - optimumSearchTime = Min(optimumSearchTime, t1); - maximumSearchTime = Min(maximumSearchTime, t2); + optimumSearchTime = std::min(optimumSearchTime, t1); + maximumSearchTime = std::min(maximumSearchTime, t2); } if (Options["Ponder"].value()) optimumSearchTime += optimumSearchTime / 4; // Make sure that maxSearchTime is not over absoluteMaxSearchTime - optimumSearchTime = Min(optimumSearchTime, maximumSearchTime); + optimumSearchTime = std::min(optimumSearchTime, maximumSearchTime); } @@ -156,6 +157,6 @@ namespace { float ratio1 = (TMaxRatio * thisMoveImportance) / float(TMaxRatio * thisMoveImportance + otherMovesImportance); float ratio2 = (thisMoveImportance + TStealRatio * otherMovesImportance) / float(thisMoveImportance + otherMovesImportance); - return int(floor(myTime * Min(ratio1, ratio2))); + return int(floor(myTime * std::min(ratio1, ratio2))); } } diff --git a/src/types.h b/src/types.h index 9ac0787e..841c794a 100644 --- a/src/types.h +++ b/src/types.h @@ -46,9 +46,6 @@ typedef unsigned __int64 uint64_t; #endif -#define Min(x, y) (((x) < (y)) ? (x) : (y)) -#define Max(x, y) (((x) < (y)) ? (y) : (x)) - //// //// Configuration ////