endif
endif
-### 3.8 Link Time Optimization, it works since gcc 4.5 but not on mingw under Windows.
+### 3.8 Link Time Optimization
### This is a mix of compile and link time options because the lto link phase
### needs access to the optimization flags.
ifeq ($(optimize),yes)
stm = Color ((idx >> 12) & 0x01);
psq = make_square(File((idx >> 13) & 0x3), Rank(RANK_7 - ((idx >> 15) & 0x7)));
- // Check if two pieces are on the same square or if a king can be captured
+ // Invalid if two pieces are on the same square or if a king can be captured
if ( distance(ksq[WHITE], ksq[BLACK]) <= 1
|| ksq[WHITE] == psq
|| ksq[BLACK] == psq
|| (stm == WHITE && (pawn_attacks_bb(WHITE, psq) & ksq[BLACK])))
result = INVALID;
- // Immediate win if a pawn can be promoted without getting captured
+ // Win if the pawn can be promoted without getting captured
else if ( stm == WHITE
&& rank_of(psq) == RANK_7
- && ksq[stm] != psq + NORTH
- && ( distance(ksq[~stm], psq + NORTH) > 1
- || (attacks_bb<KING>(ksq[stm]) & (psq + NORTH))))
+ && ksq[WHITE] != psq + NORTH
+ && ( distance(ksq[BLACK], psq + NORTH) > 1
+ || (distance(ksq[WHITE], psq + NORTH) == 1)))
result = WIN;
- // Immediate draw if it is a stalemate or a king captures undefended pawn
+ // Draw if it is stalemate or the black king can capture the pawn
else if ( stm == BLACK
- && ( !(attacks_bb<KING>(ksq[stm]) & ~(attacks_bb<KING>(ksq[~stm]) | pawn_attacks_bb(~stm, psq)))
- || (attacks_bb<KING>(ksq[stm]) & psq & ~attacks_bb<KING>(ksq[~stm]))))
+ && ( !(attacks_bb<KING>(ksq[BLACK]) & ~(attacks_bb<KING>(ksq[WHITE]) | pawn_attacks_bb(WHITE, psq)))
+ || (attacks_bb<KING>(ksq[BLACK]) & ~attacks_bb<KING>(ksq[WHITE]) & psq)))
result = DRAW;
// Position will be classified later
Bitboard RookTable[0x19000]; // To store rook attacks
Bitboard BishopTable[0x1480]; // To store bishop attacks
- void init_magics(Bitboard table[], Magic magics[], Direction directions[]);
+ void init_magics(PieceType pt, Bitboard table[], Magic magics[]);
}
for (Square s2 = SQ_A1; s2 <= SQ_H8; ++s2)
SquareDistance[s1][s2] = std::max(distance<File>(s1, s2), distance<Rank>(s1, s2));
- Direction RookDirections[] = { NORTH, EAST, SOUTH, WEST };
- Direction BishopDirections[] = { NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST };
-
- init_magics(RookTable, RookMagics, RookDirections);
- init_magics(BishopTable, BishopMagics, BishopDirections);
+ init_magics(ROOK, RookTable, RookMagics);
+ init_magics(BISHOP, BishopTable, BishopMagics);
for (Square s1 = SQ_A1; s1 <= SQ_H8; ++s1)
{
namespace {
- Bitboard sliding_attack(Direction directions[], Square sq, Bitboard occupied) {
+ Bitboard sliding_attack(PieceType pt, Square sq, Bitboard occupied) {
Bitboard attacks = 0;
+ Direction RookDirections[4] = {NORTH, SOUTH, EAST, WEST};
+ Direction BishopDirections[4] = {NORTH_EAST, SOUTH_EAST, SOUTH_WEST, NORTH_WEST};
- for (int i = 0; i < 4; ++i)
+ for(Direction d : (pt == ROOK ? RookDirections : BishopDirections))
{
Square s = sq;
- while(safe_destination(s, directions[i]) && !(occupied & s))
- attacks |= (s += directions[i]);
+ while(safe_destination(s, d) && !(occupied & s))
+ attacks |= (s += d);
}
return attacks;
// www.chessprogramming.org/Magic_Bitboards. In particular, here we use the so
// called "fancy" approach.
- void init_magics(Bitboard table[], Magic magics[], Direction directions[]) {
+ void init_magics(PieceType pt, Bitboard table[], Magic magics[]) {
// Optimal PRNG seeds to pick the correct magics in the shortest time
int seeds[][RANK_NB] = { { 8977, 44560, 54343, 38998, 5731, 95205, 104912, 17020 },
// the number of 1s of the mask. Hence we deduce the size of the shift to
// apply to the 64 or 32 bits word to get the index.
Magic& m = magics[s];
- m.mask = sliding_attack(directions, s, 0) & ~edges;
+ m.mask = sliding_attack(pt, s, 0) & ~edges;
m.shift = (Is64Bit ? 64 : 32) - popcount(m.mask);
// Set the offset for the attacks table of the square. We have individual
b = size = 0;
do {
occupancy[size] = b;
- reference[size] = sliding_attack(directions, s, b);
+ reference[size] = sliding_attack(pt, s, b);
if (HasPext)
m.attacks[pext(b, m.mask)] = reference[size];
/// the given color and on the given square is a passed pawn.
inline Bitboard passed_pawn_span(Color c, Square s) {
- return forward_ranks_bb(c, s) & (adjacent_files_bb(s) | file_bb(s));
+ return pawn_attack_span(c, s) | forward_file_bb(c, s);
}
}
- // Evaluation::space() computes the space evaluation for a given side. The
- // space evaluation is a simple bonus based on the number of safe squares
- // available for minor pieces on the central four files on ranks 2--4. Safe
- // squares one, two or three squares behind a friendly pawn are counted
- // twice. Finally, the space bonus is multiplied by a weight. The aim is to
- // improve play on game opening.
+ // Evaluation::space() computes a space evaluation for a given side, aiming to improve game
+ // play in the opening. It is based on the number of safe squares on the 4 central files
+ // on ranks 2 to 4. Completely safe squares behind a friendly pawn are counted twice.
+ // Finally, the space bonus is multiplied by a weight which decreases according to occupancy.
template<Tracing T> template<Color Us>
Score Evaluation<T>::space() const {
+ // Early exit if, for example, both queens or 6 minor pieces have been exchanged
if (pos.non_pawn_material() < SpaceThreshold)
return SCORE_ZERO;
/// MovePicker constructor for the main search
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const ButterflyHistory* mh, const LowPlyHistory* lp,
- const CapturePieceToHistory* cph, const PieceToHistory** ch, Move cm, Move* killers, int pl)
+ const CapturePieceToHistory* cph, const PieceToHistory** ch, Move cm, const Move* killers, int pl)
: pos(p), mainHistory(mh), lowPlyHistory(lp), captureHistory(cph), continuationHistory(ch),
ttMove(ttm), refutations{{killers[0], 0}, {killers[1], 0}, {cm, 0}}, depth(d), ply(pl) {
/// the move's from and to squares, see www.chessprogramming.org/Butterfly_Boards
typedef Stats<int16_t, 10692, COLOR_NB, int(SQUARE_NB) * int(SQUARE_NB)> ButterflyHistory;
-/// LowPlyHistory at higher depths records successful quiet moves on plies 0 to 3
-/// and quiet moves which are/were in the PV (ttPv)
-/// It get cleared with each new search and get filled during iterative deepening
+/// At higher depths LowPlyHistory records successful quiet moves near the root and quiet
+/// moves which are/were in the PV (ttPv)
+/// It is cleared with each new search and filled during iterative deepening
constexpr int MAX_LPH = 4;
typedef Stats<int16_t, 10692, MAX_LPH, int(SQUARE_NB) * int(SQUARE_NB)> LowPlyHistory;
const CapturePieceToHistory*,
const PieceToHistory**,
Move,
- Move*,
+ const Move*,
int);
Move next_move(bool skipQuiets = false);
&& !(theirPawns & adjacent_files_bb(s)))
score -= Doubled;
else
- score -= Isolated
- + WeakUnopposed * !opposed;
+ score -= Isolated
+ + WeakUnopposed * !opposed;
}
else if (backward)
- score -= Backward
- + WeakUnopposed * !opposed;
+ score -= Backward
+ + WeakUnopposed * !opposed;
if (!support)
- score -= Doubled * doubled
- + WeakLever * more_than_one(lever);
+ score -= Doubled * doubled
+ + WeakLever * more_than_one(lever);
}
return score;
/// penalty for a king, looking at the king file and the two closest files.
template<Color Us>
-Score Entry::evaluate_shelter(const Position& pos, Square ksq) {
+Score Entry::evaluate_shelter(const Position& pos, Square ksq) const {
constexpr Color Them = ~Us;
Score do_king_safety(const Position& pos);
template<Color Us>
- Score evaluate_shelter(const Position& pos, Square ksq);
+ Score evaluate_shelter(const Position& pos, Square ksq) const;
Key key;
Score scores[COLOR_NB];
// tables are initialized by flipping and changing the sign of the white scores.
void init() {
- for (Piece pc = W_PAWN; pc <= W_KING; ++pc)
+ for (Piece pc : {W_PAWN, W_KNIGHT, W_BISHOP, W_ROOK, W_QUEEN, W_KING})
{
Score score = make_score(PieceValue[MG][pc], PieceValue[EG][pc]);
ttPv = PvNode || (ttHit && tte->is_pv());
formerPv = ttPv && !PvNode;
- if (ttPv && depth > 12 && ss->ply - 1 < MAX_LPH && !pos.captured_piece() && is_ok((ss-1)->currentMove))
+ if (ttPv && depth > 12 && ss->ply - 1 < MAX_LPH && !priorCapture && is_ok((ss-1)->currentMove))
thisThread->lowPlyHistory[ss->ply - 1][from_to((ss-1)->currentMove)] << stat_bonus(depth - 5);
// thisThread->ttHitAverage can be used to approximate the running average of ttHit
auto moveList = MoveList<LEGAL>(pos);
size_t totalCount = moveList.size(), moveCount = 0;
- for (const Move& move : moveList)
+ for (const Move move : moveList)
{
if ( !pos.capture(move)
&& (!CheckZeroingMoves || type_of(pos.moved_piece(move)) != PAWN))
LeadPawnsSize[leadPawnsCnt][f] = idx;
}
- // Add entries in TB tables if the corresponding ".rtbw" file exsists
+ // Add entries in TB tables if the corresponding ".rtbw" file exists
for (PieceType p1 = PAWN; p1 < KING; ++p1) {
TBTables.add({KING, p1, KING});
StateInfo st;
int minDTZ = 0xFFFF;
- for (const Move& move : MoveList<LEGAL>(pos))
+ for (const Move move : MoveList<LEGAL>(pos))
{
bool zeroing = pos.capture(move) || type_of(pos.moved_piece(move)) == PAWN;
{
opt_scale = std::min(0.008 + std::pow(ply + 3.0, 0.5) / 250.0,
0.2 * limits.time[us] / double(timeLeft));
- max_scale = 4 + std::min(36, ply) / 12.0;
+ max_scale = std::min(7.0, 4.0 + ply / 12.0);
}
// x moves in y seconds (+ z increment)
#include <cassert>
#include <cctype>
-#include <climits>
#include <cstdint>
#include <cstdlib>
#include <algorithm>
typedef int Depth;
enum : int {
-
DEPTH_QS_CHECKS = 0,
DEPTH_QS_NO_CHECKS = -1,
DEPTH_QS_RECAPTURES = -5,
}
#define ENABLE_BASE_OPERATORS_ON(T) \
-constexpr T operator+(T d1, T d2) { return T(int(d1) + int(d2)); } \
-constexpr T operator-(T d1, T d2) { return T(int(d1) - int(d2)); } \
+constexpr T operator+(T d1, int d2) { return T(int(d1) + d2); } \
+constexpr T operator-(T d1, int d2) { return T(int(d1) - d2); } \
constexpr T operator-(T d) { return T(-int(d)); } \
-inline T& operator+=(T& d1, T d2) { return d1 = d1 + d2; } \
-inline T& operator-=(T& d1, T d2) { return d1 = d1 - d2; }
+inline T& operator+=(T& d1, int d2) { return d1 = d1 + d2; } \
+inline T& operator-=(T& d1, int d2) { return d1 = d1 - d2; }
#define ENABLE_INCR_OPERATORS_ON(T) \
inline T& operator++(T& d) { return d = T(int(d) + 1); } \
ENABLE_FULL_OPERATORS_ON(Direction)
ENABLE_INCR_OPERATORS_ON(PieceType)
-ENABLE_INCR_OPERATORS_ON(Piece)
ENABLE_INCR_OPERATORS_ON(Square)
ENABLE_INCR_OPERATORS_ON(File)
ENABLE_INCR_OPERATORS_ON(Rank)
#undef ENABLE_INCR_OPERATORS_ON
#undef ENABLE_BASE_OPERATORS_ON
-/// Additional operators to add integers to a Value
-constexpr Value operator+(Value v, int i) { return Value(int(v) + i); }
-constexpr Value operator-(Value v, int i) { return Value(int(v) - i); }
-inline Value& operator+=(Value& v, int i) { return v = v + i; }
-inline Value& operator-=(Value& v, int i) { return v = v - i; }
-
/// Additional operators to add a Direction to a Square
constexpr Square operator+(Square s, Direction d) { return Square(int(s) + int(d)); }
constexpr Square operator-(Square s, Direction d) { return Square(int(s) - int(d)); }