#include <cstring>
#include <iostream>
+#include <algorithm>
#include "bitboard.h"
#include "bitcount.h"
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];
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);
*/
#include <cassert>
+#include <algorithm>
#include "bitcount.h"
#include "endgame.h"
|| 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
#include <iostream>
#include <iomanip>
#include <sstream>
+#include <algorithm>
#include "bitcount.h"
#include "evaluate.h"
// 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<Max15>(undefended))
+ InitKingDanger[relative_square(Us, ksq)]
- mg_value(ei.pi->king_shelter<Us>(pos, ksq)) / 32;
attackUnits += KnightCheckBonus * count_1s<Max15>(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[]
continue;
pliesToGo = 2 * movesToGo - int(c == pos.side_to_move());
- pliesToQueen[c] = Min(pliesToQueen[c], pliesToGo);
+ pliesToQueen[c] = std::min(pliesToQueen[c], pliesToGo);
}
}
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);
}
}
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,
// 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;
}
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
#if !defined(HISTORY_H_INCLUDED)
#define HISTORY_H_INCLUDED
-#include <cstring>
#include "types.h"
+#include <cstring>
+#include <algorithm>
/// The History class stores statistics about how often different moves
/// have been successful or unsuccessful during the current search. These
}
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)
#else
+#define NOMINMAX // disable macros min() and max()
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
+#undef NOMINMAX
// Default fast and race free locks and condition variables
#if !defined(OLD_LOCKS)
#include <cassert>
#include <cstring>
+#include <algorithm>
#include "material.h"
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
#else
#define _CRT_SECURE_NO_DEPRECATE
+#define NOMINMAX // disable macros min() and max()
#include <windows.h>
#include <sys/timeb.h>
#include <iomanip>
#include <iostream>
#include <sstream>
+#include <algorithm>
#include "bitcount.h"
#include "misc.h"
#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
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
*/
#include <cassert>
+#include <algorithm>
#include "bitcount.h"
#include "movegen.h"
// (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;
#include <fstream>
#include <iostream>
#include <sstream>
+#include <algorithm>
#include "bitcount.h"
#include "movegen.h"
// 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();
// 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];
}
// 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)
{
#include <iostream>
#include <sstream>
#include <vector>
+#include <algorithm>
#include "book.h"
#include "evaluate.h"
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;
}
template <bool PvNode> 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
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);
*dangerous = true;
}
- return Min(result, ONE_PLY);
+ return std::min(result, ONE_PLY);
}
} // namespace
// 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)
// 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++)
*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);
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)
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
{
// 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<Root>(pos, ss+1, alpha, beta, depth * ONE_PLY);
// Bring to front the best move. It is critical that sorting is
// 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);
// 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)
AspirationFailLow = true;
StopOnPonderhit = false;
- alpha = Max(alpha - aspirationDelta, -VALUE_INFINITE);
+ alpha = std::max(alpha - aspirationDelta, -VALUE_INFINITE);
aspirationDelta += aspirationDelta / 2;
}
else
// 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;
}
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
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));
}
+ // 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.
// 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
*/
#include <cmath>
+#include <algorithm>
#include "misc.h"
#include "search.h"
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
// 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<OptimumTime>(hypMyTime, hypMTG, currentPly);
t2 = minThinkingTime + remaining<MaxTime>(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<bool>())
optimumSearchTime += optimumSearchTime / 4;
// Make sure that maxSearchTime is not over absoluteMaxSearchTime
- optimumSearchTime = Min(optimumSearchTime, maximumSearchTime);
+ optimumSearchTime = std::min(optimumSearchTime, maximumSearchTime);
}
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)));
}
}
#endif
-#define Min(x, y) (((x) < (y)) ? (x) : (y))
-#define Max(x, y) (((x) < (y)) ? (y) : (x))
-
////
//// Configuration
////