DroidFish: Updated stockfish to a development version to fix problems on quad-core ARM CPUs.

This commit is contained in:
Peter Osterlund
2012-06-04 16:25:51 +00:00
parent 7344b26a53
commit bcdbb5c82a
37 changed files with 2441 additions and 2650 deletions

View File

@@ -19,12 +19,14 @@
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>
#include <istream>
#include <vector> #include <vector>
#include "misc.h" #include "misc.h"
#include "position.h" #include "position.h"
#include "search.h" #include "search.h"
#include "thread.h" #include "thread.h"
#include "tt.h"
#include "ucioption.h" #include "ucioption.h"
using namespace std; using namespace std;
@@ -57,34 +59,39 @@ static const char* Defaults[] = {
/// format (defaults are the positions defined above) and the type of the /// format (defaults are the positions defined above) and the type of the
/// limit value: depth (default), time in secs or number of nodes. /// limit value: depth (default), time in secs or number of nodes.
void benchmark(int argc, char* argv[]) { void benchmark(const Position& current, istream& is) {
vector<string> fens; string token;
Search::LimitsType limits; Search::LimitsType limits;
int time; vector<string> fens;
int64_t nodes = 0;
// Assign default values to missing arguments // Assign default values to missing arguments
string ttSize = argc > 2 ? argv[2] : "128"; string ttSize = (is >> token) ? token : "128";
string threads = argc > 3 ? argv[3] : "1"; string threads = (is >> token) ? token : "1";
string valStr = argc > 4 ? argv[4] : "12"; string limit = (is >> token) ? token : "12";
string fenFile = argc > 5 ? argv[5] : "default"; string fenFile = (is >> token) ? token : "default";
string valType = argc > 6 ? argv[6] : "depth"; string limitType = (is >> token) ? token : "depth";
Options["Hash"] = ttSize; Options["Hash"] = ttSize;
Options["Threads"] = threads; Options["Threads"] = threads;
Options["OwnBook"] = false; TT.clear();
if (valType == "time") if (limitType == "time")
limits.maxTime = 1000 * atoi(valStr.c_str()); // maxTime is in ms limits.movetime = 1000 * atoi(limit.c_str()); // movetime is in ms
else if (valType == "nodes") else if (limitType == "nodes")
limits.maxNodes = atoi(valStr.c_str()); limits.nodes = atoi(limit.c_str());
else else
limits.maxDepth = atoi(valStr.c_str()); limits.depth = atoi(limit.c_str());
if (fenFile != "default") if (fenFile == "default")
fens.assign(Defaults, Defaults + 16);
else if (fenFile == "current")
fens.push_back(current.to_fen());
else
{ {
string fen; string fen;
ifstream file(fenFile.c_str()); ifstream file(fenFile.c_str());
@@ -101,34 +108,34 @@ void benchmark(int argc, char* argv[]) {
file.close(); file.close();
} }
else
fens.assign(Defaults, Defaults + 16);
time = system_time(); int64_t nodes = 0;
Time time = Time::current_time();
for (size_t i = 0; i < fens.size(); i++) for (size_t i = 0; i < fens.size(); i++)
{ {
Position pos(fens[i], false, 0); Position pos(fens[i], Options["UCI_Chess960"], Threads.main_thread());
cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl; cerr << "\nPosition: " << i + 1 << '/' << fens.size() << endl;
if (valType == "perft") if (limitType == "perft")
{ {
int64_t cnt = Search::perft(pos, limits.maxDepth * ONE_PLY); int64_t cnt = Search::perft(pos, limits.depth * ONE_PLY);
cerr << "\nPerft " << limits.maxDepth << " leaf nodes: " << cnt << endl; cerr << "\nPerft " << limits.depth << " leaf nodes: " << cnt << endl;
nodes += cnt; nodes += cnt;
} }
else else
{ {
Threads.start_thinking(pos, limits); Threads.start_searching(pos, limits, vector<Move>());
Threads.wait_for_search_finished();
nodes += Search::RootPosition.nodes_searched(); nodes += Search::RootPosition.nodes_searched();
} }
} }
time = system_time() - time; int e = time.elapsed();
cerr << "\n===========================" cerr << "\n==========================="
<< "\nTotal time (ms) : " << time << "\nTotal time (ms) : " << e
<< "\nNodes searched : " << nodes << "\nNodes searched : " << nodes
<< "\nNodes/second : " << int(nodes / (time / 1000.0)) << endl; << "\nNodes/second : " << int(nodes / (e / 1000.0)) << endl;
} }

View File

@@ -25,43 +25,47 @@
namespace { namespace {
enum Result { enum Result {
RESULT_UNKNOWN, INVALID = 0,
RESULT_INVALID, UNKNOWN = 1,
RESULT_WIN, DRAW = 2,
RESULT_DRAW WIN = 4
}; };
inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); }
struct KPKPosition { struct KPKPosition {
Result classify_knowns(int index);
Result classify(int index, Result db[]); Result classify_leaf(int idx);
Result classify(int idx, Result db[]);
private: private:
void from_index(int index); template<Color Us> Result classify(const Result db[]) const;
Result classify_white(const Result db[]);
Result classify_black(const Result db[]);
Bitboard wk_attacks() const { return StepAttacksBB[W_KING][whiteKingSquare]; }
Bitboard bk_attacks() const { return StepAttacksBB[B_KING][blackKingSquare]; }
Bitboard pawn_attacks() const { return StepAttacksBB[W_PAWN][pawnSquare]; }
Square whiteKingSquare, blackKingSquare, pawnSquare; template<Color Us> Bitboard k_attacks() const {
Color sideToMove; return Us == WHITE ? StepAttacksBB[W_KING][wksq] : StepAttacksBB[B_KING][bksq];
}
Bitboard p_attacks() const { return StepAttacksBB[W_PAWN][psq]; }
void decode_index(int idx);
Square wksq, bksq, psq;
Color stm;
}; };
// The possible pawns squares are 24, the first 4 files and ranks from 2 to 7 // The possible pawns squares are 24, the first 4 files and ranks from 2 to 7
const int IndexMax = 2 * 24 * 64 * 64; // color * wp_sq * wk_sq * bk_sq = 196608 const int IndexMax = 2 * 24 * 64 * 64; // stm * wp_sq * wk_sq * bk_sq = 196608
// Each uint32_t stores results of 32 positions, one per bit // Each uint32_t stores results of 32 positions, one per bit
uint32_t KPKBitbase[IndexMax / 32]; uint32_t KPKBitbase[IndexMax / 32];
int compute_index(Square wksq, Square bksq, Square wpsq, Color stm); int index(Square wksq, Square bksq, Square psq, Color stm);
} }
uint32_t probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm) { uint32_t probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm) {
int index = compute_index(wksq, bksq, wpsq, stm); int idx = index(wksq, bksq, wpsq, stm);
return KPKBitbase[idx / 32] & (1 << (idx & 31));
return KPKBitbase[index / 32] & (1 << (index & 31));
} }
@@ -69,24 +73,23 @@ void kpk_bitbase_init() {
Result db[IndexMax]; Result db[IndexMax];
KPKPosition pos; KPKPosition pos;
int index, bit, repeat = 1; int idx, bit, repeat = 1;
// Initialize table // Initialize table with known win / draw positions
for (index = 0; index < IndexMax; index++) for (idx = 0; idx < IndexMax; idx++)
db[index] = pos.classify_knowns(index); db[idx] = pos.classify_leaf(idx);
// Iterate until all positions are classified (30 cycles needed) // Iterate until all positions are classified (30 cycles needed)
while (repeat) while (repeat)
for (repeat = index = 0; index < IndexMax; index++) for (repeat = idx = 0; idx < IndexMax; idx++)
if ( db[index] == RESULT_UNKNOWN if (db[idx] == UNKNOWN && (db[idx] = pos.classify(idx, db)) != UNKNOWN)
&& pos.classify(index, db) != RESULT_UNKNOWN)
repeat = 1; repeat = 1;
// Map 32 position results into one KPKBitbase[] entry // Map 32 position results into one KPKBitbase[] entry
for (index = 0; index < IndexMax / 32; index++) for (idx = 0; idx < IndexMax / 32; idx++)
for (bit = 0; bit < 32; bit++) for (bit = 0; bit < 32; bit++)
if (db[32 * index + bit] == RESULT_WIN) if (db[32 * idx + bit] == WIN)
KPKBitbase[index] |= (1 << bit); KPKBitbase[idx] |= 1 << bit;
} }
@@ -102,170 +105,126 @@ namespace {
// bit 13-14: white pawn file (from FILE_A to FILE_D) // bit 13-14: white pawn file (from FILE_A to FILE_D)
// bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1) // bit 15-17: white pawn rank - 1 (from RANK_2 - 1 to RANK_7 - 1)
int compute_index(Square wksq, Square bksq, Square wpsq, Color stm) { int index(Square w, Square b, Square p, Color c) {
assert(file_of(wpsq) <= FILE_D); assert(file_of(p) <= FILE_D);
int p = file_of(wpsq) + 4 * (rank_of(wpsq) - 1); return c + (b << 1) + (w << 7) + (file_of(p) << 13) + ((rank_of(p) - 1) << 15);
int r = stm + 2 * bksq + 128 * wksq + 8192 * p;
assert(r >= 0 && r < IndexMax);
return r;
} }
void KPKPosition::from_index(int index) { void KPKPosition::decode_index(int idx) {
int s = index >> 13; stm = Color(idx & 1);
sideToMove = Color(index & 1); bksq = Square((idx >> 1) & 63);
blackKingSquare = Square((index >> 1) & 63); wksq = Square((idx >> 7) & 63);
whiteKingSquare = Square((index >> 7) & 63); psq = make_square(File((idx >> 13) & 3), Rank((idx >> 15) + 1));
pawnSquare = make_square(File(s & 3), Rank((s >> 2) + 1));
} }
Result KPKPosition::classify_knowns(int index) { Result KPKPosition::classify_leaf(int idx) {
from_index(index); decode_index(idx);
// Check if two pieces are on the same square // Check if two pieces are on the same square or if a king can be captured
if ( whiteKingSquare == pawnSquare if ( wksq == psq || wksq == bksq || bksq == psq
|| whiteKingSquare == blackKingSquare || (k_attacks<WHITE>() & bksq)
|| blackKingSquare == pawnSquare) || (stm == WHITE && (p_attacks() & bksq)))
return RESULT_INVALID; return INVALID;
// Check if a king can be captured // The position is an immediate win if it is white to move and the white
if ( bit_is_set(wk_attacks(), blackKingSquare) // pawn can be promoted without getting captured.
|| (bit_is_set(pawn_attacks(), blackKingSquare) && sideToMove == WHITE)) if ( rank_of(psq) == RANK_7
return RESULT_INVALID; && stm == WHITE
&& wksq != psq + DELTA_N
// The position is an immediate win if it is white to move and the && ( square_distance(bksq, psq + DELTA_N) > 1
// white pawn can be promoted without getting captured. ||(k_attacks<WHITE>() & (psq + DELTA_N))))
if ( rank_of(pawnSquare) == RANK_7 return WIN;
&& sideToMove == WHITE
&& whiteKingSquare != pawnSquare + DELTA_N
&& ( square_distance(blackKingSquare, pawnSquare + DELTA_N) > 1
|| bit_is_set(wk_attacks(), pawnSquare + DELTA_N)))
return RESULT_WIN;
// Check for known draw positions // Check for known draw positions
// //
// Case 1: Stalemate // Case 1: Stalemate
if ( sideToMove == BLACK if ( stm == BLACK
&& !(bk_attacks() & ~(wk_attacks() | pawn_attacks()))) && !(k_attacks<BLACK>() & ~(k_attacks<WHITE>() | p_attacks())))
return RESULT_DRAW; return DRAW;
// Case 2: King can capture pawn // Case 2: King can capture undefended pawn
if ( sideToMove == BLACK if ( stm == BLACK
&& bit_is_set(bk_attacks(), pawnSquare) && !bit_is_set(wk_attacks(), pawnSquare)) && (k_attacks<BLACK>() & psq & ~k_attacks<WHITE>()))
return RESULT_DRAW; return DRAW;
// Case 3: Black king in front of white pawn // Case 3: Black king in front of white pawn
if ( blackKingSquare == pawnSquare + DELTA_N if ( bksq == psq + DELTA_N
&& rank_of(pawnSquare) < RANK_7) && rank_of(psq) < RANK_7)
return RESULT_DRAW; return DRAW;
// Case 4: White king in front of pawn and black has opposition // Case 4: White king in front of pawn and black has opposition
if ( whiteKingSquare == pawnSquare + DELTA_N if ( stm == WHITE
&& blackKingSquare == pawnSquare + DELTA_N + DELTA_N + DELTA_N && wksq == psq + DELTA_N
&& rank_of(pawnSquare) < RANK_5 && bksq == wksq + DELTA_N + DELTA_N
&& sideToMove == WHITE) && rank_of(psq) < RANK_5)
return RESULT_DRAW; return DRAW;
// Case 5: Stalemate with rook pawn // Case 5: Stalemate with rook pawn
if ( blackKingSquare == SQ_A8 if ( bksq == SQ_A8
&& file_of(pawnSquare) == FILE_A) && file_of(psq) == FILE_A)
return RESULT_DRAW; return DRAW;
return RESULT_UNKNOWN; // Case 6: White king trapped on the rook file
if ( file_of(wksq) == FILE_A
&& file_of(psq) == FILE_A
&& rank_of(wksq) > rank_of(psq)
&& bksq == wksq + 2)
return DRAW;
return UNKNOWN;
} }
Result KPKPosition::classify(int index, Result db[]) { template<Color Us>
Result KPKPosition::classify(const Result db[]) const {
from_index(index); // White to Move: If one move leads to a position classified as RESULT_WIN,
db[index] = (sideToMove == WHITE ? classify_white(db) : classify_black(db)); // the result of the current position is RESULT_WIN. If all moves lead to
return db[index]; // positions classified as RESULT_DRAW, the current position is classified
} // RESULT_DRAW otherwise the current position is classified as RESULT_UNKNOWN.
//
Result KPKPosition::classify_white(const Result db[]) { // Black to Move: If one move leads to a position classified as RESULT_DRAW,
// the result of the current position is RESULT_DRAW. If all moves lead to
// If one move leads to a position classified as RESULT_WIN, the result // positions classified as RESULT_WIN, the position is classified RESULT_WIN.
// of the current position is RESULT_WIN. If all moves lead to positions
// classified as RESULT_DRAW, the current position is classified RESULT_DRAW
// otherwise the current position is classified as RESULT_UNKNOWN.
bool unknownFound = false;
Bitboard b;
Square s;
Result r;
// King moves
b = wk_attacks();
while (b)
{
s = pop_1st_bit(&b);
r = db[compute_index(s, blackKingSquare, pawnSquare, BLACK)];
if (r == RESULT_WIN)
return RESULT_WIN;
if (r == RESULT_UNKNOWN)
unknownFound = true;
}
// Pawn moves
if (rank_of(pawnSquare) < RANK_7)
{
s = pawnSquare + DELTA_N;
r = db[compute_index(whiteKingSquare, blackKingSquare, s, BLACK)];
if (r == RESULT_WIN)
return RESULT_WIN;
if (r == RESULT_UNKNOWN)
unknownFound = true;
// Double pawn push
if (rank_of(s) == RANK_3 && r != RESULT_INVALID)
{
s += DELTA_N;
r = db[compute_index(whiteKingSquare, blackKingSquare, s, BLACK)];
if (r == RESULT_WIN)
return RESULT_WIN;
if (r == RESULT_UNKNOWN)
unknownFound = true;
}
}
return unknownFound ? RESULT_UNKNOWN : RESULT_DRAW;
}
Result KPKPosition::classify_black(const Result db[]) {
// If one move leads to a position classified as RESULT_DRAW, the result
// of the current position is RESULT_DRAW. If all moves lead to positions
// classified as RESULT_WIN, the position is classified as RESULT_WIN.
// Otherwise, the current position is classified as RESULT_UNKNOWN. // Otherwise, the current position is classified as RESULT_UNKNOWN.
bool unknownFound = false; Result r = INVALID;
Bitboard b; Bitboard b = k_attacks<Us>();
Square s;
Result r;
// King moves
b = bk_attacks();
while (b) while (b)
{ {
s = pop_1st_bit(&b); r |= Us == WHITE ? db[index(pop_1st_bit(&b), bksq, psq, BLACK)]
r = db[compute_index(whiteKingSquare, s, pawnSquare, WHITE)]; : db[index(wksq, pop_1st_bit(&b), psq, WHITE)];
if (r == RESULT_DRAW) if (Us == WHITE && (r & WIN))
return RESULT_DRAW; return WIN;
if (r == RESULT_UNKNOWN) if (Us == BLACK && (r & DRAW))
unknownFound = true; return DRAW;
} }
return unknownFound ? RESULT_UNKNOWN : RESULT_WIN;
if (Us == WHITE && rank_of(psq) < RANK_7)
{
Square s = psq + DELTA_N;
r |= db[index(wksq, bksq, s, BLACK)]; // Single push
if (rank_of(s) == RANK_3 && s != wksq && s != bksq)
r |= db[index(wksq, bksq, s + DELTA_N, BLACK)]; // Double push
if (r & WIN)
return WIN;
}
return r & UNKNOWN ? UNKNOWN : Us == WHITE ? DRAW : WIN;
}
Result KPKPosition::classify(int idx, Result db[]) {
decode_index(idx);
return stm == WHITE ? classify<WHITE>(db) : classify<BLACK>(db);
} }
} }

View File

@@ -25,30 +25,29 @@
#include "bitcount.h" #include "bitcount.h"
#include "rkiss.h" #include "rkiss.h"
CACHE_LINE_ALIGNMENT
Bitboard RMasks[64]; Bitboard RMasks[64];
Bitboard RMagics[64]; Bitboard RMagics[64];
Bitboard* RAttacks[64]; Bitboard* RAttacks[64];
int RShifts[64]; unsigned RShifts[64];
Bitboard BMasks[64]; Bitboard BMasks[64];
Bitboard BMagics[64]; Bitboard BMagics[64];
Bitboard* BAttacks[64]; Bitboard* BAttacks[64];
int BShifts[64]; unsigned BShifts[64];
Bitboard SetMaskBB[65];
Bitboard ClearMaskBB[65];
Bitboard SquareBB[64];
Bitboard FileBB[8]; Bitboard FileBB[8];
Bitboard RankBB[8]; Bitboard RankBB[8];
Bitboard NeighboringFilesBB[8]; Bitboard AdjacentFilesBB[8];
Bitboard ThisAndNeighboringFilesBB[8]; Bitboard ThisAndAdjacentFilesBB[8];
Bitboard InFrontBB[2][8]; Bitboard InFrontBB[2][8];
Bitboard StepAttacksBB[16][64]; Bitboard StepAttacksBB[16][64];
Bitboard BetweenBB[64][64]; Bitboard BetweenBB[64][64];
Bitboard SquaresInFrontMask[2][64]; Bitboard ForwardBB[2][64];
Bitboard PassedPawnMask[2][64]; Bitboard PassedPawnMask[2][64];
Bitboard AttackSpanMask[2][64]; Bitboard AttackSpanMask[2][64];
Bitboard PseudoAttacks[6][64]; Bitboard PseudoAttacks[6][64];
uint8_t BitCount8Bit[256]; uint8_t BitCount8Bit[256];
@@ -59,31 +58,16 @@ namespace {
CACHE_LINE_ALIGNMENT CACHE_LINE_ALIGNMENT
int BSFTable[64]; int BSFTable[64];
Bitboard RookTable[0x19000]; // Storage space for rook attacks int MS1BTable[256];
Bitboard BishopTable[0x1480]; // Storage space for bishop attacks Bitboard RTable[0x19000]; // Storage space for rook attacks
Bitboard BTable[0x1480]; // Storage space for bishop attacks
void init_magic_bitboards(PieceType pt, Bitboard* attacks[], Bitboard magics[], typedef unsigned (Fn)(Square, Bitboard);
Bitboard masks[], int shifts[]);
void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
Bitboard masks[], unsigned shifts[], Square deltas[], Fn index);
} }
/// print_bitboard() prints a bitboard in an easily readable format to the
/// standard output. This is sometimes useful for debugging.
void print_bitboard(Bitboard b) {
for (Rank r = RANK_8; r >= RANK_1; r--)
{
std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
for (File f = FILE_A; f <= FILE_H; f++)
std::cout << "| " << (bit_is_set(b, make_square(f, r)) ? "X " : " ");
std::cout << "|\n";
}
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
}
/// first_1() finds the least significant nonzero bit in a nonzero bitboard. /// first_1() finds the least significant nonzero bit in a nonzero bitboard.
/// pop_1st_bit() finds and clears the least significant nonzero bit in a /// pop_1st_bit() finds and clears the least significant nonzero bit in a
/// nonzero bitboard. /// nonzero bitboard.
@@ -108,87 +92,103 @@ Square first_1(Bitboard b) {
return Square(BSFTable[(fold * 0x783A9B23) >> 26]); return Square(BSFTable[(fold * 0x783A9B23) >> 26]);
} }
// Use type-punning Square pop_1st_bit(Bitboard* b) {
union b_union {
Bitboard b; Bitboard bb = *b;
struct { *b = bb & (bb - 1);
#if defined (BIGENDIAN) bb ^= (bb - 1);
uint32_t h; uint32_t fold = unsigned(bb) ^ unsigned(bb >> 32);
uint32_t l; return Square(BSFTable[(fold * 0x783A9B23) >> 26]);
#else }
uint32_t l;
uint32_t h;
#endif
} dw;
};
Square pop_1st_bit(Bitboard* bb) { Square last_1(Bitboard b) {
b_union u; unsigned b32;
Square ret; int result = 0;
u.b = *bb; if (b > 0xFFFFFFFF)
{
b >>= 32;
result = 32;
}
if (u.dw.l) b32 = unsigned(b);
{
ret = Square(BSFTable[((u.dw.l ^ (u.dw.l - 1)) * 0x783A9B23) >> 26]); if (b32 > 0xFFFF)
u.dw.l &= (u.dw.l - 1); {
*bb = u.b; b32 >>= 16;
return ret; result += 16;
} }
ret = Square(BSFTable[((~(u.dw.h ^ (u.dw.h - 1))) * 0x783A9B23) >> 26]);
u.dw.h &= (u.dw.h - 1); if (b32 > 0xFF)
*bb = u.b; {
return ret; b32 >>= 8;
result += 8;
}
return Square(result + MS1BTable[b32]);
} }
#endif // !defined(USE_BSFQ) #endif // !defined(USE_BSFQ)
/// bitboards_init() initializes various bitboard arrays. It is called during /// Bitboards::print() prints a bitboard in an easily readable format to the
/// standard output. This is sometimes useful for debugging.
void Bitboards::print(Bitboard b) {
for (Rank rank = RANK_8; rank >= RANK_1; rank--)
{
std::cout << "+---+---+---+---+---+---+---+---+" << '\n';
for (File file = FILE_A; file <= FILE_H; file++)
std::cout << "| " << ((b & make_square(file, rank)) ? "X " : " ");
std::cout << "|\n";
}
std::cout << "+---+---+---+---+---+---+---+---+" << std::endl;
}
/// Bitboards::init() initializes various bitboard arrays. It is called during
/// program initialization. /// program initialization.
void bitboards_init() { void Bitboards::init() {
for (int k = 0, i = 0; i < 8; i++)
while (k < (2 << i))
MS1BTable[k++] = i;
for (Bitboard b = 0; b < 256; b++) for (Bitboard b = 0; b < 256; b++)
BitCount8Bit[b] = (uint8_t)popcount<Max15>(b); BitCount8Bit[b] = (uint8_t)popcount<Max15>(b);
for (Square s = SQ_A1; s <= SQ_H8; s++) for (Square s = SQ_A1; s <= SQ_H8; s++)
{ SquareBB[s] = 1ULL << s;
SetMaskBB[s] = 1ULL << s;
ClearMaskBB[s] = ~SetMaskBB[s];
}
ClearMaskBB[SQ_NONE] = ~0ULL;
FileBB[FILE_A] = FileABB; FileBB[FILE_A] = FileABB;
RankBB[RANK_1] = Rank1BB; RankBB[RANK_1] = Rank1BB;
for (int f = FILE_B; f <= FILE_H; f++) for (int i = 1; i < 8; i++)
{ {
FileBB[f] = FileBB[f - 1] << 1; FileBB[i] = FileBB[i - 1] << 1;
RankBB[f] = RankBB[f - 1] << 8; RankBB[i] = RankBB[i - 1] << 8;
} }
for (int f = FILE_A; f <= FILE_H; f++) for (File f = FILE_A; f <= FILE_H; f++)
{ {
NeighboringFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0); AdjacentFilesBB[f] = (f > FILE_A ? FileBB[f - 1] : 0) | (f < FILE_H ? FileBB[f + 1] : 0);
ThisAndNeighboringFilesBB[f] = FileBB[f] | NeighboringFilesBB[f]; ThisAndAdjacentFilesBB[f] = FileBB[f] | AdjacentFilesBB[f];
} }
for (int rw = RANK_7, rb = RANK_2; rw >= RANK_1; rw--, rb++) for (Rank r = RANK_1; r < RANK_8; r++)
{ InFrontBB[WHITE][r] = ~(InFrontBB[BLACK][r + 1] = InFrontBB[BLACK][r] | RankBB[r]);
InFrontBB[WHITE][rw] = InFrontBB[WHITE][rw + 1] | RankBB[rw + 1];
InFrontBB[BLACK][rb] = InFrontBB[BLACK][rb - 1] | RankBB[rb - 1];
}
for (Color c = WHITE; c <= BLACK; c++) for (Color c = WHITE; c <= BLACK; c++)
for (Square s = SQ_A1; s <= SQ_H8; s++) for (Square s = SQ_A1; s <= SQ_H8; s++)
{ {
SquaresInFrontMask[c][s] = in_front_bb(c, s) & file_bb(s); ForwardBB[c][s] = InFrontBB[c][rank_of(s)] & FileBB[file_of(s)];
PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_neighboring_files_bb(file_of(s)); PassedPawnMask[c][s] = InFrontBB[c][rank_of(s)] & ThisAndAdjacentFilesBB[file_of(s)];
AttackSpanMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(file_of(s)); AttackSpanMask[c][s] = InFrontBB[c][rank_of(s)] & AdjacentFilesBB[file_of(s)];
} }
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++) for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
@@ -216,56 +216,52 @@ void bitboards_init() {
{ {
Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]); Square to = s + Square(c == WHITE ? steps[pt][k] : -steps[pt][k]);
if (square_is_ok(to) && square_distance(s, to) < 3) if (is_ok(to) && square_distance(s, to) < 3)
set_bit(&StepAttacksBB[make_piece(c, pt)][s], to); StepAttacksBB[make_piece(c, pt)][s] |= to;
} }
init_magic_bitboards(ROOK, RAttacks, RMagics, RMasks, RShifts); Square RDeltas[] = { DELTA_N, DELTA_E, DELTA_S, DELTA_W };
init_magic_bitboards(BISHOP, BAttacks, BMagics, BMasks, BShifts); Square BDeltas[] = { DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW };
init_magics(RTable, RAttacks, RMagics, RMasks, RShifts, RDeltas, magic_index<ROOK>);
init_magics(BTable, BAttacks, BMagics, BMasks, BShifts, BDeltas, magic_index<BISHOP>);
for (Square s = SQ_A1; s <= SQ_H8; s++) for (Square s = SQ_A1; s <= SQ_H8; s++)
{ {
PseudoAttacks[BISHOP][s] = bishop_attacks_bb(s, 0); PseudoAttacks[QUEEN][s] = PseudoAttacks[BISHOP][s] = attacks_bb<BISHOP>(s, 0);
PseudoAttacks[ROOK][s] = rook_attacks_bb(s, 0); PseudoAttacks[QUEEN][s] |= PseudoAttacks[ ROOK][s] = attacks_bb< ROOK>(s, 0);
PseudoAttacks[QUEEN][s] = queen_attacks_bb(s, 0);
} }
for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++) for (Square s1 = SQ_A1; s1 <= SQ_H8; s1++)
for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++) for (Square s2 = SQ_A1; s2 <= SQ_H8; s2++)
if (bit_is_set(PseudoAttacks[QUEEN][s1], s2)) if (PseudoAttacks[QUEEN][s1] & s2)
{ {
Square delta = (s2 - s1) / square_distance(s1, s2); Square delta = (s2 - s1) / square_distance(s1, s2);
for (Square s = s1 + delta; s != s2; s += delta) for (Square s = s1 + delta; s != s2; s += delta)
set_bit(&BetweenBB[s1][s2], s); BetweenBB[s1][s2] |= s;
} }
} }
namespace { namespace {
Bitboard sliding_attacks(PieceType pt, Square sq, Bitboard occupied) { Bitboard sliding_attack(Square deltas[], Square sq, Bitboard occupied) {
Square deltas[][4] = { { DELTA_N, DELTA_E, DELTA_S, DELTA_W }, Bitboard attack = 0;
{ DELTA_NE, DELTA_SE, DELTA_SW, DELTA_NW } };
Bitboard attacks = 0;
Square* delta = (pt == ROOK ? deltas[0] : deltas[1]);
for (int i = 0; i < 4; i++) for (int i = 0; i < 4; i++)
{ for (Square s = sq + deltas[i];
Square s = sq + delta[i]; is_ok(s) && square_distance(s, s - deltas[i]) == 1;
s += deltas[i])
while (square_is_ok(s) && square_distance(s, s - delta[i]) == 1)
{ {
set_bit(&attacks, s); attack |= s;
if (bit_is_set(occupied, s)) if (occupied & s)
break; break;
s += delta[i];
} }
}
return attacks; return attack;
} }
@@ -291,22 +287,22 @@ namespace {
} }
// init_magic_bitboards() computes all rook and bishop magics at startup. // init_magics() computes all rook and bishop attacks at startup. Magic
// Magic bitboards are used to look up attacks of sliding pieces. As reference // bitboards are used to look up attacks of sliding pieces. As a reference see
// see chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we // chessprogramming.wikispaces.com/Magic+Bitboards. In particular, here we
// use the so called "fancy" approach. // use the so called "fancy" approach.
void init_magic_bitboards(PieceType pt, Bitboard* attacks[], Bitboard magics[], void init_magics(Bitboard table[], Bitboard* attacks[], Bitboard magics[],
Bitboard masks[], int shifts[]) { Bitboard masks[], unsigned shifts[], Square deltas[], Fn index) {
int MagicBoosters[][8] = { { 3191, 2184, 1310, 3618, 2091, 1308, 2452, 3996 }, int MagicBoosters[][8] = { { 3191, 2184, 1310, 3618, 2091, 1308, 2452, 3996 },
{ 1059, 3608, 605, 3234, 3326, 38, 2029, 3043 } }; { 1059, 3608, 605, 3234, 3326, 38, 2029, 3043 } };
RKISS rk; RKISS rk;
Bitboard occupancy[4096], reference[4096], edges, b; Bitboard occupancy[4096], reference[4096], edges, b;
int i, size, index, booster; int i, size, booster;
// attacks[s] is a pointer to the beginning of the attacks table for square 's' // attacks[s] is a pointer to the beginning of the attacks table for square 's'
attacks[SQ_A1] = (pt == ROOK ? RookTable : BishopTable); attacks[SQ_A1] = table;
for (Square s = SQ_A1; s <= SQ_H8; s++) for (Square s = SQ_A1; s <= SQ_H8; s++)
{ {
@@ -318,15 +314,15 @@ namespace {
// all the attacks for each possible subset of the mask and so is 2 power // all the attacks for each possible subset of the mask and so is 2 power
// the number of 1s of the mask. Hence we deduce the size of the shift to // 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. // apply to the 64 or 32 bits word to get the index.
masks[s] = sliding_attacks(pt, s, 0) & ~edges; masks[s] = sliding_attack(deltas, s, 0) & ~edges;
shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]); shifts[s] = (Is64Bit ? 64 : 32) - popcount<Max15>(masks[s]);
// Use Carry-Rippler trick to enumerate all subsets of masks[s] and // Use Carry-Rippler trick to enumerate all subsets of masks[s] and
// store the corresponding sliding attacks bitboard in reference[]. // store the corresponding sliding attack bitboard in reference[].
b = size = 0; b = size = 0;
do { do {
occupancy[size] = b; occupancy[size] = b;
reference[size++] = sliding_attacks(pt, s, b); reference[size++] = sliding_attack(deltas, s, b);
b = (b - masks[s]) & masks[s]; b = (b - masks[s]) & masks[s];
} while (b); } while (b);
@@ -349,14 +345,12 @@ namespace {
// effect of verifying the magic. // effect of verifying the magic.
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
index = (pt == ROOK ? rook_index(s, occupancy[i]) Bitboard& attack = attacks[s][index(s, occupancy[i])];
: bishop_index(s, occupancy[i]));
if (!attacks[s][index]) if (attack && attack != reference[i])
attacks[s][index] = reference[i];
else if (attacks[s][index] != reference[i])
break; break;
attack = reference[i];
} }
} while (i != size); } while (i != size);
} }

View File

@@ -23,62 +23,67 @@
#include "types.h" #include "types.h"
namespace Bitboards {
extern void init();
extern void print(Bitboard b);
}
CACHE_LINE_ALIGNMENT
extern Bitboard RMasks[64];
extern Bitboard RMagics[64];
extern Bitboard* RAttacks[64];
extern unsigned RShifts[64];
extern Bitboard BMasks[64];
extern Bitboard BMagics[64];
extern Bitboard* BAttacks[64];
extern unsigned BShifts[64];
extern Bitboard SquareBB[64];
extern Bitboard FileBB[8]; extern Bitboard FileBB[8];
extern Bitboard NeighboringFilesBB[8];
extern Bitboard ThisAndNeighboringFilesBB[8];
extern Bitboard RankBB[8]; extern Bitboard RankBB[8];
extern Bitboard AdjacentFilesBB[8];
extern Bitboard ThisAndAdjacentFilesBB[8];
extern Bitboard InFrontBB[2][8]; extern Bitboard InFrontBB[2][8];
extern Bitboard SetMaskBB[65];
extern Bitboard ClearMaskBB[65];
extern Bitboard StepAttacksBB[16][64]; extern Bitboard StepAttacksBB[16][64];
extern Bitboard BetweenBB[64][64]; extern Bitboard BetweenBB[64][64];
extern Bitboard ForwardBB[2][64];
extern Bitboard SquaresInFrontMask[2][64];
extern Bitboard PassedPawnMask[2][64]; extern Bitboard PassedPawnMask[2][64];
extern Bitboard AttackSpanMask[2][64]; extern Bitboard AttackSpanMask[2][64];
extern uint64_t RMagics[64];
extern int RShifts[64];
extern Bitboard RMasks[64];
extern Bitboard* RAttacks[64];
extern uint64_t BMagics[64];
extern int BShifts[64];
extern Bitboard BMasks[64];
extern Bitboard* BAttacks[64];
extern Bitboard PseudoAttacks[6][64]; extern Bitboard PseudoAttacks[6][64];
extern uint8_t BitCount8Bit[256];
/// Overloads of bitwise operators between a Bitboard and a Square for testing
/// whether a given bit is set in a bitboard, and for setting and clearing bits.
/// Functions for testing whether a given bit is set in a bitboard, and for inline Bitboard operator&(Bitboard b, Square s) {
/// setting and clearing bits. return b & SquareBB[s];
inline Bitboard bit_is_set(Bitboard b, Square s) {
return b & SetMaskBB[s];
} }
inline void set_bit(Bitboard* b, Square s) { inline Bitboard& operator|=(Bitboard& b, Square s) {
*b |= SetMaskBB[s]; return b |= SquareBB[s];
} }
inline void clear_bit(Bitboard* b, Square s) { inline Bitboard& operator^=(Bitboard& b, Square s) {
*b &= ClearMaskBB[s]; return b ^= SquareBB[s];
}
inline Bitboard operator|(Bitboard b, Square s) {
return b | SquareBB[s];
}
inline Bitboard operator^(Bitboard b, Square s) {
return b ^ SquareBB[s];
} }
/// Functions used to update a bitboard after a move. This is faster /// more_than_one() returns true if in 'b' there is more than one bit set
/// then calling a sequence of clear_bit() + set_bit()
inline Bitboard make_move_bb(Square from, Square to) { inline bool more_than_one(Bitboard b) {
return SetMaskBB[from] | SetMaskBB[to]; return b & (b - 1);
}
inline void do_move_bb(Bitboard* b, Bitboard move_bb) {
*b ^= move_bb;
} }
@@ -102,19 +107,19 @@ inline Bitboard file_bb(Square s) {
} }
/// neighboring_files_bb takes a file as input and returns a bitboard representing /// adjacent_files_bb takes a file as input and returns a bitboard representing
/// all squares on the neighboring files. /// all squares on the adjacent files.
inline Bitboard neighboring_files_bb(File f) { inline Bitboard adjacent_files_bb(File f) {
return NeighboringFilesBB[f]; return AdjacentFilesBB[f];
} }
/// this_and_neighboring_files_bb takes a file as input and returns a bitboard /// this_and_adjacent_files_bb takes a file as input and returns a bitboard
/// representing all squares on the given and neighboring files. /// representing all squares on the given and adjacent files.
inline Bitboard this_and_neighboring_files_bb(File f) { inline Bitboard this_and_adjacent_files_bb(File f) {
return ThisAndNeighboringFilesBB[f]; return ThisAndAdjacentFilesBB[f];
} }
@@ -133,71 +138,30 @@ inline Bitboard in_front_bb(Color c, Square s) {
} }
/// Functions for computing sliding attack bitboards. rook_attacks_bb(), /// between_bb returns a bitboard representing all squares between two squares.
/// bishop_attacks_bb() and queen_attacks_bb() all take a square and a /// For instance, between_bb(SQ_C4, SQ_F7) returns a bitboard with the bits for
/// bitboard of occupied squares as input, and return a bitboard representing /// square d5 and e6 set. If s1 and s2 are not on the same line, file or diagonal,
/// all squares attacked by a rook, bishop or queen on the given square. /// 0 is returned.
#if defined(IS_64BIT) inline Bitboard between_bb(Square s1, Square s2) {
FORCE_INLINE unsigned rook_index(Square s, Bitboard occ) {
return unsigned(((occ & RMasks[s]) * RMagics[s]) >> RShifts[s]);
}
FORCE_INLINE unsigned bishop_index(Square s, Bitboard occ) {
return unsigned(((occ & BMasks[s]) * BMagics[s]) >> BShifts[s]);
}
#else // if !defined(IS_64BIT)
FORCE_INLINE unsigned rook_index(Square s, Bitboard occ) {
Bitboard b = occ & RMasks[s];
return unsigned(int(b) * int(RMagics[s]) ^ int(b >> 32) * int(RMagics[s] >> 32)) >> RShifts[s];
}
FORCE_INLINE unsigned bishop_index(Square s, Bitboard occ) {
Bitboard b = occ & BMasks[s];
return unsigned(int(b) * int(BMagics[s]) ^ int(b >> 32) * int(BMagics[s] >> 32)) >> BShifts[s];
}
#endif
inline Bitboard rook_attacks_bb(Square s, Bitboard occ) {
return RAttacks[s][rook_index(s, occ)];
}
inline Bitboard bishop_attacks_bb(Square s, Bitboard occ) {
return BAttacks[s][bishop_index(s, occ)];
}
inline Bitboard queen_attacks_bb(Square s, Bitboard blockers) {
return rook_attacks_bb(s, blockers) | bishop_attacks_bb(s, blockers);
}
/// squares_between returns a bitboard representing all squares between
/// two squares. For instance, squares_between(SQ_C4, SQ_F7) returns a
/// bitboard with the bits for square d5 and e6 set. If s1 and s2 are not
/// on the same line, file or diagonal, EmptyBoardBB is returned.
inline Bitboard squares_between(Square s1, Square s2) {
return BetweenBB[s1][s2]; return BetweenBB[s1][s2];
} }
/// squares_in_front_of takes a color and a square as input, and returns a /// forward_bb takes a color and a square as input, and returns a bitboard
/// bitboard representing all squares along the line in front of the square, /// representing all squares along the line in front of the square, from the
/// from the point of view of the given color. Definition of the table is: /// point of view of the given color. Definition of the table is:
/// SquaresInFrontOf[c][s] = in_front_bb(c, s) & file_bb(s) /// ForwardBB[c][s] = in_front_bb(c, s) & file_bb(s)
inline Bitboard squares_in_front_of(Color c, Square s) { inline Bitboard forward_bb(Color c, Square s) {
return SquaresInFrontMask[c][s]; return ForwardBB[c][s];
} }
/// passed_pawn_mask takes a color and a square as input, and returns a /// passed_pawn_mask takes a color and a square as input, and returns a
/// bitboard mask which can be used to test if a pawn of the given color on /// bitboard mask which can be used to test if a pawn of the given color on
/// the given square is a passed pawn. Definition of the table is: /// the given square is a passed pawn. Definition of the table is:
/// PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_neighboring_files_bb(s) /// PassedPawnMask[c][s] = in_front_bb(c, s) & this_and_adjacent_files_bb(s)
inline Bitboard passed_pawn_mask(Color c, Square s) { inline Bitboard passed_pawn_mask(Color c, Square s) {
return PassedPawnMask[c][s]; return PassedPawnMask[c][s];
@@ -207,7 +171,7 @@ inline Bitboard passed_pawn_mask(Color c, Square s) {
/// attack_span_mask takes a color and a square as input, and returns a bitboard /// attack_span_mask takes a color and a square as input, and returns a bitboard
/// representing all squares that can be attacked by a pawn of the given color /// representing all squares that can be attacked by a pawn of the given color
/// when it moves along its file starting from the given square. Definition is: /// when it moves along its file starting from the given square. Definition is:
/// AttackSpanMask[c][s] = in_front_bb(c, s) & neighboring_files_bb(s); /// AttackSpanMask[c][s] = in_front_bb(c, s) & adjacent_files_bb(s);
inline Bitboard attack_span_mask(Color c, Square s) { inline Bitboard attack_span_mask(Color c, Square s) {
return AttackSpanMask[c][s]; return AttackSpanMask[c][s];
@@ -219,7 +183,7 @@ inline Bitboard attack_span_mask(Color c, Square s) {
inline bool squares_aligned(Square s1, Square s2, Square s3) { inline bool squares_aligned(Square s1, Square s2, Square s3) {
return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3]) return (BetweenBB[s1][s2] | BetweenBB[s1][s3] | BetweenBB[s2][s3])
& ( SetMaskBB[s1] | SetMaskBB[s2] | SetMaskBB[s3]); & ( SquareBB[s1] | SquareBB[s2] | SquareBB[s3]);
} }
@@ -227,8 +191,32 @@ inline bool squares_aligned(Square s1, Square s2, Square s3) {
/// the same color of the given square. /// the same color of the given square.
inline Bitboard same_color_squares(Square s) { inline Bitboard same_color_squares(Square s) {
return bit_is_set(0xAA55AA55AA55AA55ULL, s) ? 0xAA55AA55AA55AA55ULL return Bitboard(0xAA55AA55AA55AA55ULL) & s ? 0xAA55AA55AA55AA55ULL
: ~0xAA55AA55AA55AA55ULL; : ~0xAA55AA55AA55AA55ULL;
}
/// Functions for computing sliding attack bitboards. Function attacks_bb() takes
/// a square and a bitboard of occupied squares as input, and returns a bitboard
/// representing all squares attacked by Pt (bishop or rook) on the given square.
template<PieceType Pt>
FORCE_INLINE unsigned magic_index(Square s, Bitboard occ) {
Bitboard* const Masks = Pt == ROOK ? RMasks : BMasks;
Bitboard* const Magics = Pt == ROOK ? RMagics : BMagics;
unsigned* const Shifts = Pt == ROOK ? RShifts : BShifts;
if (Is64Bit)
return unsigned(((occ & Masks[s]) * Magics[s]) >> Shifts[s]);
unsigned lo = unsigned(occ) & unsigned(Masks[s]);
unsigned hi = unsigned(occ >> 32) & unsigned(Masks[s] >> 32);
return (lo * unsigned(Magics[s]) ^ hi * unsigned(Magics[s] >> 32)) >> Shifts[s];
}
template<PieceType Pt>
inline Bitboard attacks_bb(Square s, Bitboard occ) {
return (Pt == ROOK ? RAttacks : BAttacks)[s][magic_index<Pt>(s, occ)];
} }
@@ -241,9 +229,15 @@ inline Bitboard same_color_squares(Square s) {
#if defined(_MSC_VER) && !defined(__INTEL_COMPILER) #if defined(_MSC_VER) && !defined(__INTEL_COMPILER)
FORCE_INLINE Square first_1(Bitboard b) { FORCE_INLINE Square first_1(Bitboard b) {
unsigned long index; unsigned long index;
_BitScanForward64(&index, b); _BitScanForward64(&index, b);
return (Square) index; return (Square) index;
}
FORCE_INLINE Square last_1(Bitboard b) {
unsigned long index;
_BitScanReverse64(&index, b);
return (Square) index;
} }
#else #else
@@ -252,6 +246,12 @@ FORCE_INLINE Square first_1(Bitboard b) { // Assembly code by Heinz van Saanen
__asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) ); __asm__("bsfq %1, %0": "=r"(dummy): "rm"(b) );
return (Square) dummy; return (Square) dummy;
} }
FORCE_INLINE Square last_1(Bitboard b) {
Bitboard dummy;
__asm__("bsrq %1, %0": "=r"(dummy): "rm"(b) );
return (Square) dummy;
}
#endif #endif
FORCE_INLINE Square pop_1st_bit(Bitboard* b) { FORCE_INLINE Square pop_1st_bit(Bitboard* b) {
@@ -263,12 +263,9 @@ FORCE_INLINE Square pop_1st_bit(Bitboard* b) {
#else // if !defined(USE_BSFQ) #else // if !defined(USE_BSFQ)
extern Square first_1(Bitboard b); extern Square first_1(Bitboard b);
extern Square last_1(Bitboard b);
extern Square pop_1st_bit(Bitboard* b); extern Square pop_1st_bit(Bitboard* b);
#endif #endif
extern void print_bitboard(Bitboard b);
extern void bitboards_init();
#endif // !defined(BITBOARD_H_INCLUDED) #endif // !defined(BITBOARD_H_INCLUDED)

View File

@@ -306,25 +306,23 @@ namespace {
const Key* ZobEnPassant = PolyGlotRandoms + 772; const Key* ZobEnPassant = PolyGlotRandoms + 772;
const Key* ZobTurn = PolyGlotRandoms + 780; const Key* ZobTurn = PolyGlotRandoms + 780;
// PieceOffset is calculated as 64 * (PolyPiece ^ 1) where PolyPiece
// is: BP = 0, WP = 1, BN = 2, WN = 3 ... BK = 10, WK = 11
const int PieceOffset[] = { 0, 64, 192, 320, 448, 576, 704, 0,
0, 0, 128, 256, 384, 512, 640 };
// book_key() returns the PolyGlot hash key of the given position // book_key() returns the PolyGlot hash key of the given position
uint64_t book_key(const Position& pos) { uint64_t book_key(const Position& pos) {
uint64_t key = 0; uint64_t key = 0;
Bitboard b = pos.occupied_squares(); Bitboard b = pos.pieces();
while (b) while (b)
{ {
// Piece offset is at 64 * polyPiece where polyPiece is defined as:
// BP = 0, WP = 1, BN = 2, WN = 3, ... BK = 10, WK = 11
Square s = pop_1st_bit(&b); Square s = pop_1st_bit(&b);
key ^= ZobPiece[PieceOffset[pos.piece_on(s)] + s]; Piece p = pos.piece_on(s);
int polyPiece = 2 * (type_of(p) - 1) + (color_of(p) == WHITE);
key ^= ZobPiece[64 * polyPiece + s];
} }
b = (pos.can_castle(WHITE_OO) << 0) | (pos.can_castle(WHITE_OOO) << 1) b = pos.can_castle(ALL_CASTLES);
| (pos.can_castle(BLACK_OO) << 2) | (pos.can_castle(BLACK_OOO) << 3);
while (b) while (b)
key ^= ZobCastle[pop_1st_bit(&b)]; key ^= ZobCastle[pop_1st_bit(&b)];
@@ -342,7 +340,7 @@ namespace {
Book::Book() : size(0) { Book::Book() : size(0) {
for (int i = abs(system_time() % 10000); i > 0; i--) for (int i = Time::current_time().msec() % 10000; i > 0; i--)
RKiss.rand<unsigned>(); // Make random number generation less deterministic RKiss.rand<unsigned>(); // Make random number generation less deterministic
} }
@@ -380,10 +378,13 @@ bool Book::open(const char* fName) {
ifstream::open(fName, ifstream::in | ifstream::binary | ios::ate); ifstream::open(fName, ifstream::in | ifstream::binary | ios::ate);
if (!is_open()) if (!is_open())
{
clear();
return false; // Silently fail if the file is not found return false; // Silently fail if the file is not found
}
// Get the book size in number of entries, we are already at the end of file // Get the book size in number of entries, we are already at the end of file
size = tellg() / sizeof(BookEntry); size = (size_t)tellg() / sizeof(BookEntry);
if (!good()) if (!good())
{ {
@@ -421,7 +422,7 @@ Move Book::probe(const Position& pos, const string& fName, bool pickBest) {
// Choose book move according to its score. If a move has a very // Choose book move according to its score. If a move has a very
// high score it has higher probability to be choosen than a move // high score it has higher probability to be choosen than a move
// with lower score. Note that first entry is always chosen. // with lower score. Note that first entry is always chosen.
if ( (RKiss.rand<unsigned>() % sum < e.count) if ( (sum && RKiss.rand<unsigned>() % sum < e.count)
|| (pickBest && e.count == best)) || (pickBest && e.count == best))
move = Move(e.move); move = Move(e.move);
} }

View File

@@ -22,10 +22,9 @@
#include "bitcount.h" #include "bitcount.h"
#include "endgame.h" #include "endgame.h"
#include "pawns.h" #include "movegen.h"
using std::string; using std::string;
using namespace std;
extern uint32_t probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm); extern uint32_t probe_kpk_bitbase(Square wksq, Square wpsq, Square bksq, Color stm);
@@ -73,12 +72,12 @@ namespace {
string sides[] = { code.substr(code.find('K', 1)), // Weaker string sides[] = { code.substr(code.find('K', 1)), // Weaker
code.substr(0, code.find('K', 1)) }; // Stronger code.substr(0, code.find('K', 1)) }; // Stronger
transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower); std::transform(sides[c].begin(), sides[c].end(), sides[c].begin(), tolower);
string fen = sides[0] + char('0' + int(8 - code.length())) string fen = sides[0] + char('0' + int(8 - code.length()))
+ sides[1] + "/8/8/8/8/8/8/8 w - - 0 10"; + sides[1] + "/8/8/8/8/8/8/8 w - - 0 10";
return Position(fen, false, 0).material_key(); return Position(fen, false, NULL).material_key();
} }
template<typename M> template<typename M>
@@ -117,10 +116,8 @@ Endgames::~Endgames() {
template<EndgameType E> template<EndgameType E>
void Endgames::add(const string& code) { void Endgames::add(const string& code) {
typedef typename eg_family<E>::type T; map((Endgame<E>*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
map((Endgame<E>*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
map((T*)0)[key(code, WHITE)] = new Endgame<E>(WHITE);
map((T*)0)[key(code, BLACK)] = new Endgame<E>(BLACK);
} }
@@ -134,6 +131,13 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO); assert(pos.non_pawn_material(weakerSide) == VALUE_ZERO);
assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO); assert(pos.piece_count(weakerSide, PAWN) == VALUE_ZERO);
// Stalemate detection with lone king
if ( pos.side_to_move() == weakerSide
&& !pos.in_check()
&& !MoveList<MV_LEGAL>(pos).size()) {
return VALUE_DRAW;
}
Square winnerKSq = pos.king_square(strongerSide); Square winnerKSq = pos.king_square(strongerSide);
Square loserKSq = pos.king_square(weakerSide); Square loserKSq = pos.king_square(weakerSide);
@@ -144,9 +148,9 @@ Value Endgame<KXK>::operator()(const Position& pos) const {
if ( pos.piece_count(strongerSide, QUEEN) if ( pos.piece_count(strongerSide, QUEEN)
|| pos.piece_count(strongerSide, ROOK) || pos.piece_count(strongerSide, ROOK)
|| pos.piece_count(strongerSide, BISHOP) > 1) || pos.bishop_pair(strongerSide)) {
// TODO: check for two equal-colored bishops! result += VALUE_KNOWN_WIN;
result += VALUE_KNOWN_WIN; }
return strongerSide == pos.side_to_move() ? result : -result; return strongerSide == pos.side_to_move() ? result : -result;
} }
@@ -402,11 +406,11 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
// No assertions about the material of weakerSide, because we want draws to // No assertions about the material of weakerSide, because we want draws to
// be detected even when the weaker side has some pawns. // be detected even when the weaker side has some pawns.
Bitboard pawns = pos.pieces(PAWN, strongerSide); Bitboard pawns = pos.pieces(strongerSide, PAWN);
File pawnFile = file_of(pos.piece_list(strongerSide, PAWN)[0]); File pawnFile = file_of(pos.piece_list(strongerSide, PAWN)[0]);
// All pawns are on a single rook file ? // All pawns are on a single rook file ?
if ( (pawnFile == FILE_A || pawnFile == FILE_H) if ( (pawnFile == FILE_A || pawnFile == FILE_H)
&& !(pawns & ~file_bb(pawnFile))) && !(pawns & ~file_bb(pawnFile)))
{ {
Square bishopSq = pos.piece_list(strongerSide, BISHOP)[0]; Square bishopSq = pos.piece_list(strongerSide, BISHOP)[0];
@@ -417,7 +421,7 @@ ScaleFactor Endgame<KBPsK>::operator()(const Position& pos) const {
&& abs(file_of(kingSq) - pawnFile) <= 1) && abs(file_of(kingSq) - pawnFile) <= 1)
{ {
// The bishop has the wrong color, and the defending king is on the // The bishop has the wrong color, and the defending king is on the
// file of the pawn(s) or the neighboring file. Find the rank of the // file of the pawn(s) or the adjacent file. Find the rank of the
// frontmost pawn. // frontmost pawn.
Rank rank; Rank rank;
if (strongerSide == WHITE) if (strongerSide == WHITE)
@@ -456,12 +460,12 @@ ScaleFactor Endgame<KQKRPs>::operator()(const Position& pos) const {
Square kingSq = pos.king_square(weakerSide); Square kingSq = pos.king_square(weakerSide);
if ( relative_rank(weakerSide, kingSq) <= RANK_2 if ( relative_rank(weakerSide, kingSq) <= RANK_2
&& relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4 && relative_rank(weakerSide, pos.king_square(strongerSide)) >= RANK_4
&& (pos.pieces(ROOK, weakerSide) & rank_bb(relative_rank(weakerSide, RANK_3))) && (pos.pieces(weakerSide, ROOK) & rank_bb(relative_rank(weakerSide, RANK_3)))
&& (pos.pieces(PAWN, weakerSide) & rank_bb(relative_rank(weakerSide, RANK_2))) && (pos.pieces(weakerSide, PAWN) & rank_bb(relative_rank(weakerSide, RANK_2)))
&& (pos.attacks_from<KING>(kingSq) & pos.pieces(PAWN, weakerSide))) && (pos.attacks_from<KING>(kingSq) & pos.pieces(weakerSide, PAWN)))
{ {
Square rsq = pos.piece_list(weakerSide, ROOK)[0]; Square rsq = pos.piece_list(weakerSide, ROOK)[0];
if (pos.attacks_from<PAWN>(rsq, strongerSide) & pos.pieces(PAWN, weakerSide)) if (pos.attacks_from<PAWN>(rsq, strongerSide) & pos.pieces(weakerSide, PAWN))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
} }
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
@@ -639,14 +643,14 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
assert(pos.piece_count(weakerSide, PAWN) == 0); assert(pos.piece_count(weakerSide, PAWN) == 0);
Square ksq = pos.king_square(weakerSide); Square ksq = pos.king_square(weakerSide);
Bitboard pawns = pos.pieces(PAWN, strongerSide); Bitboard pawns = pos.pieces(strongerSide, PAWN);
// Are all pawns on the 'a' file? // Are all pawns on the 'a' file?
if (!(pawns & ~FileABB)) if (!(pawns & ~FileABB))
{ {
// Does the defending king block the pawns? // Does the defending king block the pawns?
if ( square_distance(ksq, relative_square(strongerSide, SQ_A8)) <= 1 if ( square_distance(ksq, relative_square(strongerSide, SQ_A8)) <= 1
|| ( file_of(ksq) == FILE_A || ( file_of(ksq) == FILE_A
&& !in_front_bb(strongerSide, ksq) & pawns)) && !in_front_bb(strongerSide, ksq) & pawns))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
} }
@@ -655,7 +659,7 @@ ScaleFactor Endgame<KPsK>::operator()(const Position& pos) const {
{ {
// Does the defending king block the pawns? // Does the defending king block the pawns?
if ( square_distance(ksq, relative_square(strongerSide, SQ_H8)) <= 1 if ( square_distance(ksq, relative_square(strongerSide, SQ_H8)) <= 1
|| ( file_of(ksq) == FILE_H || ( file_of(ksq) == FILE_H
&& !in_front_bb(strongerSide, ksq) & pawns)) && !in_front_bb(strongerSide, ksq) & pawns))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
} }
@@ -706,9 +710,9 @@ ScaleFactor Endgame<KBPKB>::operator()(const Position& pos) const {
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
else else
{ {
Bitboard path = squares_in_front_of(strongerSide, pawnSq); Bitboard path = forward_bb(strongerSide, pawnSq);
if (path & pos.pieces(KING, weakerSide)) if (path & pos.pieces(weakerSide, KING))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
if ( (pos.attacks_from<BISHOP>(weakerBishopSq) & path) if ( (pos.attacks_from<BISHOP>(weakerBishopSq) & path)
@@ -769,20 +773,20 @@ ScaleFactor Endgame<KBPPKB>::operator()(const Position& pos) const {
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;
case 1: case 1:
// Pawns on neighboring files. Draw if defender firmly controls the square // Pawns on adjacent files. Draw if defender firmly controls the square
// in front of the frontmost pawn's path, and the square diagonally behind // in front of the frontmost pawn's path, and the square diagonally behind
// this square on the file of the other pawn. // this square on the file of the other pawn.
if ( ksq == blockSq1 if ( ksq == blockSq1
&& opposite_colors(ksq, wbsq) && opposite_colors(ksq, wbsq)
&& ( bbsq == blockSq2 && ( bbsq == blockSq2
|| (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(BISHOP, weakerSide)) || (pos.attacks_from<BISHOP>(blockSq2) & pos.pieces(weakerSide, BISHOP))
|| abs(r1 - r2) >= 2)) || abs(r1 - r2) >= 2))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
else if ( ksq == blockSq2 else if ( ksq == blockSq2
&& opposite_colors(ksq, wbsq) && opposite_colors(ksq, wbsq)
&& ( bbsq == blockSq1 && ( bbsq == blockSq1
|| (pos.attacks_from<BISHOP>(blockSq1) & pos.pieces(BISHOP, weakerSide)))) || (pos.attacks_from<BISHOP>(blockSq1) & pos.pieces(weakerSide, BISHOP))))
return SCALE_FACTOR_DRAW; return SCALE_FACTOR_DRAW;
else else
return SCALE_FACTOR_NONE; return SCALE_FACTOR_NONE;

View File

@@ -61,11 +61,12 @@ enum EndgameType {
}; };
/// Some magic to detect family type of endgame from its enum value /// Endgame functions can be of two types according if return a Value or a
/// ScaleFactor. Type eg_fun<int>::type equals to either ScaleFactor or Value
/// depending if the template parameter is 0 or 1.
template<bool> struct bool_to_type { typedef Value type; }; template<int> struct eg_fun { typedef Value type; };
template<> struct bool_to_type<true> { typedef ScaleFactor type; }; template<> struct eg_fun<1> { typedef ScaleFactor type; };
template<EndgameType E> struct eg_family : public bool_to_type<(E > SCALE_FUNS)> {};
/// Base and derived templates for endgame evaluation and scaling functions /// Base and derived templates for endgame evaluation and scaling functions
@@ -79,7 +80,7 @@ struct EndgameBase {
}; };
template<EndgameType E, typename T = typename eg_family<E>::type> template<EndgameType E, typename T = typename eg_fun<(E > SCALE_FUNS)>::type>
struct Endgame : public EndgameBase<T> { struct Endgame : public EndgameBase<T> {
explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {} explicit Endgame(Color c) : strongerSide(c), weakerSide(~c) {}
@@ -93,18 +94,18 @@ private:
/// Endgames class stores in two std::map the pointers to endgame evaluation /// Endgames class stores in two std::map the pointers to endgame evaluation
/// and scaling base objects. Then we use polymorphism to invoke the actual /// and scaling base objects. Then we use polymorphism to invoke the actual
/// endgame function calling its operator() method that is virtual. /// endgame function calling its operator() that is virtual.
class Endgames { class Endgames {
typedef std::map<Key, EndgameBase<Value>*> M1; typedef std::map<Key, EndgameBase<eg_fun<0>::type>*> M1;
typedef std::map<Key, EndgameBase<ScaleFactor>*> M2; typedef std::map<Key, EndgameBase<eg_fun<1>::type>*> M2;
M1 m1; M1 m1;
M2 m2; M2 m2;
M1& map(Value*) { return m1; } M1& map(M1::mapped_type) { return m1; }
M2& map(ScaleFactor*) { return m2; } M2& map(M2::mapped_type) { return m2; }
template<EndgameType E> void add(const std::string& code); template<EndgameType E> void add(const std::string& code);
@@ -112,9 +113,8 @@ public:
Endgames(); Endgames();
~Endgames(); ~Endgames();
template<typename T> EndgameBase<T>* get(Key key) { template<typename T> T probe(Key key, T& eg)
return map((T*)0).count(key) ? map((T*)0)[key] : NULL; { return eg = map(eg).count(key) ? map(eg)[key] : NULL; }
}
}; };
#endif // !defined(ENDGAME_H_INCLUDED) #endif // !defined(ENDGAME_H_INCLUDED)

View File

@@ -18,7 +18,6 @@
*/ */
#include <cassert> #include <cassert>
#include <iostream>
#include <iomanip> #include <iomanip>
#include <sstream> #include <sstream>
#include <algorithm> #include <algorithm>
@@ -37,8 +36,8 @@ namespace {
struct EvalInfo { struct EvalInfo {
// Pointers to material and pawn hash table entries // Pointers to material and pawn hash table entries
MaterialInfo* mi; MaterialEntry* mi;
PawnInfo* pi; PawnEntry* pi;
// attackedBy[color][piece type] is a bitboard representing all squares // attackedBy[color][piece type] is a bitboard representing all squares
// attacked by a given color and piece type, attackedBy[color][0] contains // attacked by a given color and piece type, attackedBy[color][0] contains
@@ -151,13 +150,16 @@ namespace {
#undef S #undef S
// Bonus for having the side to move (modified by Joona Kiiski)
const Score Tempo = make_score(24, 11);
// Rooks and queens on the 7th rank (modified by Joona Kiiski) // Rooks and queens on the 7th rank (modified by Joona Kiiski)
const Score RookOn7thBonus = make_score(47, 98); const Score RookOn7thBonus = make_score(47, 98);
const Score QueenOn7thBonus = make_score(27, 54); const Score QueenOn7thBonus = make_score(27, 54);
// Rooks on open files (modified by Joona Kiiski) // Rooks on open files (modified by Joona Kiiski)
const Score RookOpenFileBonus = make_score(43, 43); const Score RookOpenFileBonus = make_score(43, 21);
const Score RookHalfOpenFileBonus = make_score(19, 19); const Score RookHalfOpenFileBonus = make_score(19, 10);
// Penalty for rooks trapped inside a friendly king which has lost the // Penalty for rooks trapped inside a friendly king which has lost the
// right to castle. // right to castle.
@@ -168,6 +170,9 @@ namespace {
// happen in Chess960 games. // happen in Chess960 games.
const Score TrappedBishopA1H1Penalty = make_score(100, 100); const Score TrappedBishopA1H1Penalty = make_score(100, 100);
// Penalty for an undefended bishop or knight
const Score UndefendedMinorPenalty = make_score(25, 10);
// The SpaceMask[Color] contains the area of the board which is considered // The SpaceMask[Color] contains the area of the board which is considered
// by the space evaluation. In the middle game, each side is given a bonus // by the space evaluation. In the middle game, each side is given a bonus
// based on how many squares inside this area are safe and available for // based on how many squares inside this area are safe and available for
@@ -220,8 +225,8 @@ namespace {
std::stringstream TraceStream; std::stringstream TraceStream;
enum TracedType { enum TracedType {
PST = 8, IMBALANCE = 9, MOBILITY = 10, THREAT = 11, PST = 8, IMBALANCE = 9, MOBILITY = 10, THREAT = 11,
PASSED = 12, UNSTOPPABLE = 13, SPACE = 14, TOTAL = 15 PASSED = 12, UNSTOPPABLE = 13, SPACE = 14, TOTAL = 15
}; };
// Function prototypes // Function prototypes
@@ -248,42 +253,129 @@ namespace {
Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei); Score evaluate_unstoppable_pawns(const Position& pos, EvalInfo& ei);
inline Score apply_weight(Score v, Score weight); Value interpolate(const Score& v, Phase ph, ScaleFactor sf);
Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf);
Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight); Score weight_option(const std::string& mgOpt, const std::string& egOpt, Score internalWeight);
void init_safety();
double to_cp(Value v); double to_cp(Value v);
void trace_add(int idx, Score term_w, Score term_b = SCORE_ZERO); void trace_add(int idx, Score term_w, Score term_b = SCORE_ZERO);
void trace_row(const char* name, int idx);
} }
/// evaluate() is the main evaluation function. It always computes two namespace Eval {
/// values, an endgame score and a middle game score, and interpolates
/// between them based on the remaining material. Color RootColor;
Value evaluate(const Position& pos, Value& margin) { return do_evaluate<false>(pos, margin); }
/// evaluate() is the main evaluation function. It always computes two
/// values, an endgame score and a middle game score, and interpolates
/// between them based on the remaining material.
Value evaluate(const Position& pos, Value& margin) {
return do_evaluate<false>(pos, margin);
}
/// init() computes evaluation weights from the corresponding UCI parameters
/// and setup king tables.
void init() {
Weights[Mobility] = weight_option("Mobility (Middle Game)", "Mobility (Endgame)", WeightsInternal[Mobility]);
Weights[PassedPawns] = weight_option("Passed Pawns (Middle Game)", "Passed Pawns (Endgame)", WeightsInternal[PassedPawns]);
Weights[Space] = weight_option("Space", "Space", WeightsInternal[Space]);
Weights[KingDangerUs] = weight_option("Cowardice", "Cowardice", WeightsInternal[KingDangerUs]);
Weights[KingDangerThem] = weight_option("Aggressiveness", "Aggressiveness", WeightsInternal[KingDangerThem]);
// King safety is asymmetrical. Our king danger level is weighted by
// "Cowardice" UCI parameter, instead the opponent one by "Aggressiveness".
// If running in analysis mode, make sure we use symmetrical king safety. We
// do this by replacing both Weights[kingDangerUs] and Weights[kingDangerThem]
// by their average.
if (Options["UCI_AnalyseMode"])
Weights[KingDangerUs] = Weights[KingDangerThem] = (Weights[KingDangerUs] + Weights[KingDangerThem]) / 2;
const int MaxSlope = 30;
const int Peak = 1280;
for (int t = 0, i = 1; i < 100; i++)
{
t = std::min(Peak, std::min(int(0.4 * i * i), t + MaxSlope));
KingDangerTable[1][i] = apply_weight(make_score(t, 0), Weights[KingDangerUs]);
KingDangerTable[0][i] = apply_weight(make_score(t, 0), Weights[KingDangerThem]);
}
}
/// trace() is like evaluate() but instead of a value returns a string suitable
/// to be print on stdout with the detailed descriptions and values of each
/// evaluation term. Used mainly for debugging.
std::string trace(const Position& pos) {
Value margin;
std::string totals;
RootColor = pos.side_to_move();
TraceStream.str("");
TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision(2);
memset(TracedScores, 0, 2 * 16 * sizeof(Score));
do_evaluate<true>(pos, margin);
totals = TraceStream.str();
TraceStream.str("");
TraceStream << std::setw(21) << "Eval term " << "| White | Black | Total \n"
<< " | MG EG | MG EG | MG EG \n"
<< "---------------------+-------------+-------------+---------------\n";
trace_row("Material, PST, Tempo", PST);
trace_row("Material imbalance", IMBALANCE);
trace_row("Pawns", PAWN);
trace_row("Knights", KNIGHT);
trace_row("Bishops", BISHOP);
trace_row("Rooks", ROOK);
trace_row("Queens", QUEEN);
trace_row("Mobility", MOBILITY);
trace_row("King safety", KING);
trace_row("Threats", THREAT);
trace_row("Passed pawns", PASSED);
trace_row("Unstoppable pawns", UNSTOPPABLE);
trace_row("Space", SPACE);
TraceStream << "---------------------+-------------+-------------+---------------\n";
trace_row("Total", TOTAL);
TraceStream << totals;
return TraceStream.str();
}
} // namespace Eval
namespace { namespace {
template<bool Trace> template<bool Trace>
Value do_evaluate(const Position& pos, Value& margin) { Value do_evaluate(const Position& pos, Value& margin) {
assert(!pos.in_check());
EvalInfo ei; EvalInfo ei;
Value margins[2]; Value margins[2];
Score score, mobilityWhite, mobilityBlack; Score score, mobilityWhite, mobilityBlack;
assert(pos.thread() >= 0 && pos.thread() < MAX_THREADS);
assert(!pos.in_check());
// Initialize score by reading the incrementally updated scores included
// in the position object (material + piece square tables).
score = pos.value();
// margins[] store the uncertainty estimation of position's evaluation // margins[] store the uncertainty estimation of position's evaluation
// that typically is used by the search for pruning decisions. // that typically is used by the search for pruning decisions.
margins[WHITE] = margins[BLACK] = VALUE_ZERO; margins[WHITE] = margins[BLACK] = VALUE_ZERO;
// Initialize score by reading the incrementally updated scores included
// in the position object (material + piece square tables) and adding
// Tempo bonus. Score is computed from the point of view of white.
score = pos.psq_score() + (pos.side_to_move() == WHITE ? Tempo : -Tempo);
// Probe the material hash table // Probe the material hash table
ei.mi = Threads[pos.thread()].materialTable.material_info(pos); ei.mi = pos.this_thread()->materialTable.probe(pos);
score += ei.mi->material_value(); score += ei.mi->material_value();
// If we have a specialized evaluation function for the current material // If we have a specialized evaluation function for the current material
@@ -295,7 +387,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
} }
// Probe the pawn hash table // Probe the pawn hash table
ei.pi = Threads[pos.thread()].pawnTable.pawn_info(pos); ei.pi = pos.this_thread()->pawnTable.probe(pos);
score += ei.pi->pawns_value(); score += ei.pi->pawns_value();
// Initialize attack and king safety bitboards // Initialize attack and king safety bitboards
@@ -339,7 +431,7 @@ Value do_evaluate(const Position& pos, Value& margin) {
// If we don't already have an unusual scale factor, check for opposite // If we don't already have an unusual scale factor, check for opposite
// colored bishop endgames, and use a lower scale for those. // colored bishop endgames, and use a lower scale for those.
if ( ei.mi->game_phase() < PHASE_MIDGAME if ( ei.mi->game_phase() < PHASE_MIDGAME
&& pos.opposite_colored_bishops() && pos.opposite_bishops()
&& sf == SCALE_FACTOR_NORMAL) && sf == SCALE_FACTOR_NORMAL)
{ {
// Only the two bishops ? // Only the two bishops ?
@@ -357,14 +449,13 @@ Value do_evaluate(const Position& pos, Value& margin) {
sf = ScaleFactor(50); sf = ScaleFactor(50);
} }
// Interpolate between the middle game and the endgame score
margin = margins[pos.side_to_move()]; margin = margins[pos.side_to_move()];
Value v = scale_by_game_phase(score, ei.mi->game_phase(), sf); Value v = interpolate(score, ei.mi->game_phase(), sf);
// In case of tracing add all single evaluation contributions for both white and black // In case of tracing add all single evaluation contributions for both white and black
if (Trace) if (Trace)
{ {
trace_add(PST, pos.value()); trace_add(PST, pos.psq_score());
trace_add(IMBALANCE, ei.mi->material_value()); trace_add(IMBALANCE, ei.mi->material_value());
trace_add(PAWN, ei.pi->pawns_value()); trace_add(PAWN, ei.pi->pawns_value());
trace_add(MOBILITY, apply_weight(mobilityWhite, Weights[Mobility]), apply_weight(mobilityBlack, Weights[Mobility])); trace_add(MOBILITY, apply_weight(mobilityWhite, Weights[Mobility]), apply_weight(mobilityBlack, Weights[Mobility]));
@@ -387,34 +478,6 @@ Value do_evaluate(const Position& pos, Value& margin) {
return pos.side_to_move() == WHITE ? v : -v; return pos.side_to_move() == WHITE ? v : -v;
} }
} // namespace
/// read_weights() reads evaluation weights from the corresponding UCI parameters
void read_evaluation_uci_options(Color us) {
// King safety is asymmetrical. Our king danger level is weighted by
// "Cowardice" UCI parameter, instead the opponent one by "Aggressiveness".
const int kingDangerUs = (us == WHITE ? KingDangerUs : KingDangerThem);
const int kingDangerThem = (us == WHITE ? KingDangerThem : KingDangerUs);
Weights[Mobility] = weight_option("Mobility (Middle Game)", "Mobility (Endgame)", WeightsInternal[Mobility]);
Weights[PassedPawns] = weight_option("Passed Pawns (Middle Game)", "Passed Pawns (Endgame)", WeightsInternal[PassedPawns]);
Weights[Space] = weight_option("Space", "Space", WeightsInternal[Space]);
Weights[kingDangerUs] = weight_option("Cowardice", "Cowardice", WeightsInternal[KingDangerUs]);
Weights[kingDangerThem] = weight_option("Aggressiveness", "Aggressiveness", WeightsInternal[KingDangerThem]);
// If running in analysis mode, make sure we use symmetrical king safety. We do this
// by replacing both Weights[kingDangerUs] and Weights[kingDangerThem] by their average.
if (Options["UCI_AnalyseMode"])
Weights[kingDangerUs] = Weights[kingDangerThem] = (Weights[kingDangerUs] + Weights[kingDangerThem]) / 2;
init_safety();
}
namespace {
// init_eval_info() initializes king bitboards for given color adding // init_eval_info() initializes king bitboards for given color adding
// pawn attacks. To be done at the beginning of the evaluation. // pawn attacks. To be done at the beginning of the evaluation.
@@ -454,10 +517,10 @@ namespace {
// Increase bonus if supported by pawn, especially if the opponent has // Increase bonus if supported by pawn, especially if the opponent has
// no minor piece which can exchange the outpost piece. // no minor piece which can exchange the outpost piece.
if (bonus && bit_is_set(ei.attackedBy[Us][PAWN], s)) if (bonus && (ei.attackedBy[Us][PAWN] & s))
{ {
if ( !pos.pieces(KNIGHT, Them) if ( !pos.pieces(Them, KNIGHT)
&& !(same_color_squares(s) & pos.pieces(BISHOP, Them))) && !(same_color_squares(s) & pos.pieces(Them, BISHOP)))
bonus += bonus + bonus / 2; bonus += bonus + bonus / 2;
else else
bonus += bonus / 2; bonus += bonus / 2;
@@ -488,16 +551,14 @@ namespace {
if (Piece == KNIGHT || Piece == QUEEN) if (Piece == KNIGHT || Piece == QUEEN)
b = pos.attacks_from<Piece>(s); b = pos.attacks_from<Piece>(s);
else if (Piece == BISHOP) else if (Piece == BISHOP)
b = bishop_attacks_bb(s, pos.occupied_squares() & ~pos.pieces(QUEEN, Us)); b = attacks_bb<BISHOP>(s, pos.pieces() ^ pos.pieces(Us, QUEEN));
else if (Piece == ROOK) else if (Piece == ROOK)
b = rook_attacks_bb(s, pos.occupied_squares() & ~pos.pieces(ROOK, QUEEN, Us)); b = attacks_bb<ROOK>(s, pos.pieces() ^ pos.pieces(Us, ROOK, QUEEN));
else else
assert(false); assert(false);
// Update attack info
ei.attackedBy[Us][Piece] |= b; ei.attackedBy[Us][Piece] |= b;
// King attacks
if (b & ei.kingRing[Them]) if (b & ei.kingRing[Them])
{ {
ei.kingAttackersCount[Us]++; ei.kingAttackersCount[Us]++;
@@ -507,20 +568,31 @@ namespace {
ei.kingAdjacentZoneAttacksCount[Us] += popcount<Max15>(bb); ei.kingAdjacentZoneAttacksCount[Us] += popcount<Max15>(bb);
} }
// Mobility
mob = (Piece != QUEEN ? popcount<Max15>(b & mobilityArea) mob = (Piece != QUEEN ? popcount<Max15>(b & mobilityArea)
: popcount<Full >(b & mobilityArea)); : popcount<Full >(b & mobilityArea));
mobility += MobilityBonus[Piece][mob]; mobility += MobilityBonus[Piece][mob];
// Add a bonus if a slider is pinning an enemy piece
if ( (Piece == BISHOP || Piece == ROOK || Piece == QUEEN)
&& (PseudoAttacks[Piece][pos.king_square(Them)] & s))
{
b = BetweenBB[s][pos.king_square(Them)] & pos.pieces();
assert(b);
if (!more_than_one(b) && (b & pos.pieces(Them)))
score += ThreatBonus[Piece][type_of(pos.piece_on(first_1(b)))];
}
// Decrease score if we are attacked by an enemy pawn. Remaining part // Decrease score if we are attacked by an enemy pawn. Remaining part
// of threat evaluation must be done later when we have full attack info. // of threat evaluation must be done later when we have full attack info.
if (bit_is_set(ei.attackedBy[Them][PAWN], s)) if (ei.attackedBy[Them][PAWN] & s)
score -= ThreatenedByPawnPenalty[Piece]; score -= ThreatenedByPawnPenalty[Piece];
// Bishop and knight outposts squares // Bishop and knight outposts squares
if ( (Piece == BISHOP || Piece == KNIGHT) if ( (Piece == BISHOP || Piece == KNIGHT)
&& !(pos.pieces(PAWN, Them) & attack_span_mask(Us, s))) && !(pos.pieces(Them, PAWN) & attack_span_mask(Us, s)))
score += evaluate_outposts<Piece, Us>(pos, ei, s); score += evaluate_outposts<Piece, Us>(pos, ei, s);
// Queen or rook on 7th rank // Queen or rook on 7th rank
@@ -542,7 +614,7 @@ namespace {
Square d = pawn_push(Us) + (file_of(s) == FILE_A ? DELTA_E : DELTA_W); Square d = pawn_push(Us) + (file_of(s) == FILE_A ? DELTA_E : DELTA_W);
if (pos.piece_on(s + d) == make_piece(Us, PAWN)) if (pos.piece_on(s + d) == make_piece(Us, PAWN))
{ {
if (!pos.square_is_empty(s + d + pawn_push(Us))) if (!pos.is_empty(s + d + pawn_push(Us)))
score -= 2*TrappedBishopA1H1Penalty; score -= 2*TrappedBishopA1H1Penalty;
else if (pos.piece_on(s + 2*d) == make_piece(Us, PAWN)) else if (pos.piece_on(s + 2*d) == make_piece(Us, PAWN))
score -= TrappedBishopA1H1Penalty; score -= TrappedBishopA1H1Penalty;
@@ -608,15 +680,25 @@ namespace {
const Color Them = (Us == WHITE ? BLACK : WHITE); const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard b; Bitboard b, undefendedMinors, weakEnemies;
Score score = SCORE_ZERO; Score score = SCORE_ZERO;
// Undefended minors get penalized even if not under attack
undefendedMinors = pos.pieces(Them)
& (pos.pieces(BISHOP) | pos.pieces(KNIGHT))
& ~ei.attackedBy[Them][0];
if (undefendedMinors)
score += more_than_one(undefendedMinors) ? UndefendedMinorPenalty * 2
: UndefendedMinorPenalty;
// Enemy pieces not defended by a pawn and under our attack // Enemy pieces not defended by a pawn and under our attack
Bitboard weakEnemies = pos.pieces(Them) weakEnemies = pos.pieces(Them)
& ~ei.attackedBy[Them][PAWN] & ~ei.attackedBy[Them][PAWN]
& ei.attackedBy[Us][0]; & ei.attackedBy[Us][0];
if (!weakEnemies) if (!weakEnemies)
return SCORE_ZERO; return score;
// Add bonus according to type of attacked enemy piece and to the // Add bonus according to type of attacked enemy piece and to the
// type of attacking piece, from knights to queens. Kings are not // type of attacking piece, from knights to queens. Kings are not
@@ -670,8 +752,8 @@ namespace {
int attackUnits; int attackUnits;
const Square ksq = pos.king_square(Us); const Square ksq = pos.king_square(Us);
// King shelter // King shelter and enemy pawns storm
Score score = ei.pi->king_shelter<Us>(pos, ksq); Score score = ei.pi->king_safety<Us>(pos, ksq);
// King safety. This is quite complicated, and is almost certainly far // King safety. This is quite complicated, and is almost certainly far
// from optimally tuned. // from optimally tuned.
@@ -693,7 +775,7 @@ namespace {
attackUnits = std::min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2) attackUnits = std::min(25, (ei.kingAttackersCount[Them] * ei.kingAttackersWeight[Them]) / 2)
+ 3 * (ei.kingAdjacentZoneAttacksCount[Them] + popcount<Max15>(undefended)) + 3 * (ei.kingAdjacentZoneAttacksCount[Them] + popcount<Max15>(undefended))
+ InitKingDanger[relative_square(Us, ksq)] + InitKingDanger[relative_square(Us, ksq)]
- mg_value(ei.pi->king_shelter<Us>(pos, ksq)) / 32; - mg_value(score) / 32;
// Analyse enemy's safe queen contact checks. First find undefended // Analyse enemy's safe queen contact checks. First find undefended
// squares around the king attacked by enemy queen... // squares around the king attacked by enemy queen...
@@ -761,8 +843,8 @@ namespace {
// value that will be used for pruning because this value can sometimes // value that will be used for pruning because this value can sometimes
// be very big, and so capturing a single attacking piece can therefore // be very big, and so capturing a single attacking piece can therefore
// result in a score change far bigger than the value of the captured piece. // result in a score change far bigger than the value of the captured piece.
score -= KingDangerTable[Us][attackUnits]; score -= KingDangerTable[Us == Eval::RootColor][attackUnits];
margins[Us] += mg_value(KingDangerTable[Us][attackUnits]); margins[Us] += mg_value(KingDangerTable[Us == Eval::RootColor][attackUnits]);
} }
if (Trace) if (Trace)
@@ -812,16 +894,16 @@ namespace {
ebonus -= Value(square_distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr); ebonus -= Value(square_distance(pos.king_square(Us), blockSq + pawn_push(Us)) * rr);
// If the pawn is free to advance, increase bonus // If the pawn is free to advance, increase bonus
if (pos.square_is_empty(blockSq)) if (pos.is_empty(blockSq))
{ {
squaresToQueen = squares_in_front_of(Us, s); squaresToQueen = forward_bb(Us, s);
defendedSquares = squaresToQueen & ei.attackedBy[Us][0]; defendedSquares = squaresToQueen & ei.attackedBy[Us][0];
// If there is an enemy rook or queen attacking the pawn from behind, // If there is an enemy rook or queen attacking the pawn from behind,
// add all X-ray attacks by the rook or queen. Otherwise consider only // add all X-ray attacks by the rook or queen. Otherwise consider only
// the squares in the pawn's path attacked or occupied by the enemy. // the squares in the pawn's path attacked or occupied by the enemy.
if ( (squares_in_front_of(Them, s) & pos.pieces(ROOK, QUEEN, Them)) if ( (forward_bb(Them, s) & pos.pieces(Them, ROOK, QUEEN))
&& (squares_in_front_of(Them, s) & pos.pieces(ROOK, QUEEN, Them) & pos.attacks_from<ROOK>(s))) && (forward_bb(Them, s) & pos.pieces(Them, ROOK, QUEEN) & pos.attacks_from<ROOK>(s)))
unsafeSquares = squaresToQueen; unsafeSquares = squaresToQueen;
else else
unsafeSquares = squaresToQueen & (ei.attackedBy[Them][0] | pos.pieces(Them)); unsafeSquares = squaresToQueen & (ei.attackedBy[Them][0] | pos.pieces(Them));
@@ -841,7 +923,7 @@ namespace {
// Increase the bonus if the passed pawn is supported by a friendly pawn // Increase the bonus if the passed pawn is supported by a friendly pawn
// on the same rank and a bit smaller if it's on the previous rank. // on the same rank and a bit smaller if it's on the previous rank.
supportingPawns = pos.pieces(PAWN, Us) & neighboring_files_bb(file_of(s)); supportingPawns = pos.pieces(Us, PAWN) & adjacent_files_bb(file_of(s));
if (supportingPawns & rank_bb(s)) if (supportingPawns & rank_bb(s))
ebonus += Value(r * 20); ebonus += Value(r * 20);
@@ -858,7 +940,7 @@ namespace {
{ {
if (pos.non_pawn_material(Them) <= KnightValueMidgame) if (pos.non_pawn_material(Them) <= KnightValueMidgame)
ebonus += ebonus / 4; ebonus += ebonus / 4;
else if (pos.pieces(ROOK, QUEEN, Them)) else if (pos.pieces(Them, ROOK, QUEEN))
ebonus -= ebonus / 4; ebonus -= ebonus / 4;
} }
score += make_score(mbonus, ebonus); score += make_score(mbonus, ebonus);
@@ -896,7 +978,7 @@ namespace {
{ {
s = pop_1st_bit(&b); s = pop_1st_bit(&b);
queeningSquare = relative_square(c, make_square(file_of(s), RANK_8)); queeningSquare = relative_square(c, make_square(file_of(s), RANK_8));
queeningPath = squares_in_front_of(c, s); queeningPath = forward_bb(c, s);
// Compute plies to queening and check direct advancement // Compute plies to queening and check direct advancement
movesToGo = rank_distance(s, queeningSquare) - int(relative_rank(c, s) == RANK_2); movesToGo = rank_distance(s, queeningSquare) - int(relative_rank(c, s) == RANK_2);
@@ -909,7 +991,7 @@ namespace {
// Opponent king cannot block because path is defended and position // Opponent king cannot block because path is defended and position
// is not in check. So only friendly pieces can be blockers. // is not in check. So only friendly pieces can be blockers.
assert(!pos.in_check()); assert(!pos.in_check());
assert((queeningPath & pos.occupied_squares()) == (queeningPath & pos.pieces(c))); assert((queeningPath & pos.pieces()) == (queeningPath & pos.pieces(c)));
// Add moves needed to free the path from friendly pieces and retest condition // Add moves needed to free the path from friendly pieces and retest condition
movesToGo += popcount<Max15>(queeningPath & pos.pieces(c)); movesToGo += popcount<Max15>(queeningPath & pos.pieces(c));
@@ -931,7 +1013,7 @@ namespace {
loserSide = ~winnerSide; loserSide = ~winnerSide;
// Step 3. Can the losing side possibly create a new passed pawn and thus prevent the loss? // Step 3. Can the losing side possibly create a new passed pawn and thus prevent the loss?
b = candidates = pos.pieces(PAWN, loserSide); b = candidates = pos.pieces(loserSide, PAWN);
while (b) while (b)
{ {
@@ -944,8 +1026,8 @@ namespace {
// Check if (without even considering any obstacles) we're too far away or doubled // Check if (without even considering any obstacles) we're too far away or doubled
if ( pliesToQueen[winnerSide] + 3 <= pliesToGo if ( pliesToQueen[winnerSide] + 3 <= pliesToGo
|| (squares_in_front_of(loserSide, s) & pos.pieces(PAWN, loserSide))) || (forward_bb(loserSide, s) & pos.pieces(loserSide, PAWN)))
clear_bit(&candidates, s); candidates ^= s;
} }
// If any candidate is already a passed pawn it _may_ promote in time. We give up. // If any candidate is already a passed pawn it _may_ promote in time. We give up.
@@ -967,9 +1049,9 @@ namespace {
pliesToGo = 2 * movesToGo - int(loserSide == pos.side_to_move()); pliesToGo = 2 * movesToGo - int(loserSide == pos.side_to_move());
// Generate list of blocking pawns and supporters // Generate list of blocking pawns and supporters
supporters = neighboring_files_bb(file_of(s)) & candidates; supporters = adjacent_files_bb(file_of(s)) & candidates;
opposed = squares_in_front_of(loserSide, s) & pos.pieces(PAWN, winnerSide); opposed = forward_bb(loserSide, s) & pos.pieces(winnerSide, PAWN);
blockers = passed_pawn_mask(loserSide, s) & pos.pieces(PAWN, winnerSide); blockers = passed_pawn_mask(loserSide, s) & pos.pieces(winnerSide, PAWN);
assert(blockers); assert(blockers);
@@ -1026,7 +1108,7 @@ namespace {
} }
// Winning pawn is unstoppable and will promote as first, return big score // Winning pawn is unstoppable and will promote as first, return big score
Score score = make_score(0, (Value) 0x500 - 0x20 * pliesToQueen[winnerSide]); Score score = make_score(0, (Value) 1280 - 32 * pliesToQueen[winnerSide]);
return winnerSide == WHITE ? score : -score; return winnerSide == WHITE ? score : -score;
} }
@@ -1046,12 +1128,12 @@ namespace {
// SpaceMask[]. A square is unsafe if it is attacked by an enemy // SpaceMask[]. A square is unsafe if it is attacked by an enemy
// pawn, or if it is undefended and attacked by an enemy piece. // pawn, or if it is undefended and attacked by an enemy piece.
Bitboard safe = SpaceMask[Us] Bitboard safe = SpaceMask[Us]
& ~pos.pieces(PAWN, Us) & ~pos.pieces(Us, PAWN)
& ~ei.attackedBy[Them][PAWN] & ~ei.attackedBy[Them][PAWN]
& (ei.attackedBy[Us][0] | ~ei.attackedBy[Them][0]); & (ei.attackedBy[Us][0] | ~ei.attackedBy[Them][0]);
// Find all squares which are at most three squares behind some friendly pawn // Find all squares which are at most three squares behind some friendly pawn
Bitboard behind = pos.pieces(PAWN, Us); Bitboard behind = pos.pieces(Us, PAWN);
behind |= (Us == WHITE ? behind >> 8 : behind << 8); behind |= (Us == WHITE ? behind >> 8 : behind << 8);
behind |= (Us == WHITE ? behind >> 16 : behind << 16); behind |= (Us == WHITE ? behind >> 16 : behind << 16);
@@ -1059,18 +1141,10 @@ namespace {
} }
// apply_weight() applies an evaluation weight to a value trying to prevent overflow // interpolate() interpolates between a middle game and an endgame score,
inline Score apply_weight(Score v, Score w) {
return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
(int(eg_value(v)) * eg_value(w)) / 0x100);
}
// scale_by_game_phase() interpolates between a middle game and an endgame score,
// based on game phase. It also scales the return value by a ScaleFactor array. // based on game phase. It also scales the return value by a ScaleFactor array.
Value scale_by_game_phase(const Score& v, Phase ph, ScaleFactor sf) { Value interpolate(const Score& v, Phase ph, ScaleFactor sf) {
assert(mg_value(v) > -VALUE_INFINITE && mg_value(v) < VALUE_INFINITE); assert(mg_value(v) > -VALUE_INFINITE && mg_value(v) < VALUE_INFINITE);
assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE); assert(eg_value(v) > -VALUE_INFINITE && eg_value(v) < VALUE_INFINITE);
@@ -1095,33 +1169,6 @@ namespace {
} }
// init_safety() initizes the king safety evaluation, based on UCI
// parameters. It is called from read_weights().
void init_safety() {
const Value MaxSlope = Value(30);
const Value Peak = Value(1280);
Value t[100];
// First setup the base table
for (int i = 0; i < 100; i++)
{
t[i] = Value(int(0.4 * i * i));
if (i > 0)
t[i] = std::min(t[i], t[i - 1] + MaxSlope);
t[i] = std::min(t[i], Peak);
}
// Then apply the weights and get the final KingDangerTable[] array
for (Color c = WHITE; c <= BLACK; c++)
for (int i = 0; i < 100; i++)
KingDangerTable[c][i] = apply_weight(make_score(t[i], 0), Weights[KingDangerUs + c]);
}
// A couple of little helpers used by tracing code, to_cp() converts a value to // A couple of little helpers used by tracing code, to_cp() converts a value to
// a double in centipawns scale, trace_add() stores white and black scores. // a double in centipawns scale, trace_add() stores white and black scores.
@@ -1129,14 +1176,15 @@ namespace {
void trace_add(int idx, Score wScore, Score bScore) { void trace_add(int idx, Score wScore, Score bScore) {
TracedScores[WHITE][idx] = wScore; TracedScores[WHITE][idx] = wScore;
TracedScores[BLACK][idx] = bScore; TracedScores[BLACK][idx] = bScore;
} }
// trace_row() is an helper function used by tracing code to register the // trace_row() is an helper function used by tracing code to register the
// values of a single evaluation term. // values of a single evaluation term.
void trace_row(const char *name, int idx) { void trace_row(const char* name, int idx) {
Score wScore = TracedScores[WHITE][idx]; Score wScore = TracedScores[WHITE][idx];
Score bScore = TracedScores[BLACK][idx]; Score bScore = TracedScores[BLACK][idx];
@@ -1159,47 +1207,3 @@ namespace {
} }
} }
} }
/// trace_evaluate() is like evaluate() but instead of a value returns a string
/// suitable to be print on stdout with the detailed descriptions and values of
/// each evaluation term. Used mainly for debugging.
std::string trace_evaluate(const Position& pos) {
Value margin;
std::string totals;
TraceStream.str("");
TraceStream << std::showpoint << std::showpos << std::fixed << std::setprecision(2);
memset(TracedScores, 0, 2 * 16 * sizeof(Score));
do_evaluate<true>(pos, margin);
totals = TraceStream.str();
TraceStream.str("");
TraceStream << std::setw(21) << "Eval term " << "| White | Black | Total \n"
<< " | MG EG | MG EG | MG EG \n"
<< "---------------------+-------------+-------------+---------------\n";
trace_row("Material, PST, Tempo", PST);
trace_row("Material imbalance", IMBALANCE);
trace_row("Pawns", PAWN);
trace_row("Knights", KNIGHT);
trace_row("Bishops", BISHOP);
trace_row("Rooks", ROOK);
trace_row("Queens", QUEEN);
trace_row("Mobility", MOBILITY);
trace_row("King safety", KING);
trace_row("Threats", THREAT);
trace_row("Passed pawns", PASSED);
trace_row("Unstoppable pawns", UNSTOPPABLE);
trace_row("Space", SPACE);
TraceStream << "---------------------+-------------+-------------+---------------\n";
trace_row("Total", TOTAL);
TraceStream << totals;
return TraceStream.str();
}

View File

@@ -24,8 +24,14 @@
class Position; class Position;
namespace Eval {
extern Color RootColor;
extern void init();
extern Value evaluate(const Position& pos, Value& margin); extern Value evaluate(const Position& pos, Value& margin);
extern std::string trace_evaluate(const Position& pos); extern std::string trace(const Position& pos);
extern void read_evaluation_uci_options(Color sideToMove);
}
#endif // !defined(EVALUATE_H_INCLUDED) #endif // !defined(EVALUATE_H_INCLUDED)

View File

@@ -20,9 +20,10 @@
#if !defined(HISTORY_H_INCLUDED) #if !defined(HISTORY_H_INCLUDED)
#define HISTORY_H_INCLUDED #define HISTORY_H_INCLUDED
#include "types.h"
#include <cstring>
#include <algorithm> #include <algorithm>
#include <cstring>
#include "types.h"
/// The History class stores statistics about how often different moves /// The History class stores statistics about how often different moves
/// have been successful or unsuccessful during the current search. These /// have been successful or unsuccessful during the current search. These

View File

@@ -21,37 +21,32 @@
#include <string> #include <string>
#include "bitboard.h" #include "bitboard.h"
#include "misc.h" #include "evaluate.h"
#include "position.h" #include "position.h"
#include "search.h" #include "search.h"
#include "thread.h" #include "thread.h"
#include "tt.h"
#include "ucioption.h"
using namespace std; extern void uci_loop(const std::string&);
extern void uci_loop();
extern void benchmark(int argc, char* argv[]);
extern void kpk_bitbase_init(); extern void kpk_bitbase_init();
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
bitboards_init(); std::cout << engine_info() << std::endl;
Bitboards::init();
Position::init(); Position::init();
kpk_bitbase_init(); kpk_bitbase_init();
Search::init(); Search::init();
Threads.init(); Threads.init();
Eval::init();
TT.set_size(Options["Hash"]);
cout << engine_info() << endl; std::string args;
if (argc == 1) for (int i = 1; i < argc; i++)
uci_loop(); args += std::string(argv[i]) + " ";
else if (string(argv[1]) == "bench") uci_loop(args);
benchmark(argc, argv);
else
cerr << "\nUsage: stockfish bench [hash size = 128] [threads = 1] "
<< "[limit = 12] [fen positions file = default] "
<< "[limited by depth, time, nodes or perft = depth]" << endl;
Threads.exit();
} }

View File

@@ -17,9 +17,9 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <algorithm>
#include <cassert> #include <cassert>
#include <cstring> #include <cstring>
#include <algorithm>
#include "material.h" #include "material.h"
@@ -84,67 +84,57 @@ namespace {
} // namespace } // namespace
/// MaterialInfoTable c'tor and d'tor allocate and free the space for Endgames /// MaterialTable::probe() takes a position object as input, looks up a MaterialEntry
/// object, and returns a pointer to it. If the material configuration is not
/// already present in the table, it is computed and stored there, so we don't
/// have to recompute everything when the same material configuration occurs again.
void MaterialInfoTable::init() { Base::init(); if (!funcs) funcs = new Endgames(); } MaterialEntry* MaterialTable::probe(const Position& pos) {
MaterialInfoTable::~MaterialInfoTable() { delete funcs; }
/// MaterialInfoTable::material_info() takes a position object as input,
/// computes or looks up a MaterialInfo object, and returns a pointer to it.
/// If the material configuration is not already present in the table, it
/// is stored there, so we don't have to recompute everything when the
/// same material configuration occurs again.
MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
Key key = pos.material_key(); Key key = pos.material_key();
MaterialInfo* mi = probe(key); MaterialEntry* e = entries[key];
// If mi->key matches the position's material hash key, it means that we // If e->key matches the position's material hash key, it means that we
// have analysed this material configuration before, and we can simply // have analysed this material configuration before, and we can simply
// return the information we found the last time instead of recomputing it. // return the information we found the last time instead of recomputing it.
if (mi->key == key) if (e->key == key)
return mi; return e;
// Initialize MaterialInfo entry memset(e, 0, sizeof(MaterialEntry));
memset(mi, 0, sizeof(MaterialInfo)); e->key = key;
mi->key = key; e->factor[WHITE] = e->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL;
mi->factor[WHITE] = mi->factor[BLACK] = (uint8_t)SCALE_FACTOR_NORMAL; e->gamePhase = MaterialTable::game_phase(pos);
// Store game phase
mi->gamePhase = MaterialInfoTable::game_phase(pos);
// Let's look if we have a specialized evaluation function for this // Let's look if we have a specialized evaluation function for this
// particular material configuration. First we look for a fixed // particular material configuration. First we look for a fixed
// configuration one, then a generic one if previous search failed. // configuration one, then a generic one if previous search failed.
if ((mi->evaluationFunction = funcs->get<Value>(key)) != NULL) if (endgames.probe(key, e->evaluationFunction))
return mi; return e;
if (is_KXK<WHITE>(pos)) if (is_KXK<WHITE>(pos))
{ {
mi->evaluationFunction = &EvaluateKXK[WHITE]; e->evaluationFunction = &EvaluateKXK[WHITE];
return mi; return e;
} }
if (is_KXK<BLACK>(pos)) if (is_KXK<BLACK>(pos))
{ {
mi->evaluationFunction = &EvaluateKXK[BLACK]; e->evaluationFunction = &EvaluateKXK[BLACK];
return mi; return e;
} }
if (!pos.pieces(PAWN) && !pos.pieces(ROOK) && !pos.pieces(QUEEN)) if (!pos.pieces(PAWN) && !pos.pieces(ROOK) && !pos.pieces(QUEEN))
{ {
// Minor piece endgame with at least one minor piece per side and // Minor piece endgame with at least one minor piece per side and
// no pawns. Note that the case KmmK is already handled by KXK. // no pawns. Note that the case KmmK is already handled by KXK.
assert((pos.pieces(KNIGHT, WHITE) | pos.pieces(BISHOP, WHITE))); assert((pos.pieces(WHITE, KNIGHT) | pos.pieces(WHITE, BISHOP)));
assert((pos.pieces(KNIGHT, BLACK) | pos.pieces(BISHOP, BLACK))); assert((pos.pieces(BLACK, KNIGHT) | pos.pieces(BLACK, BISHOP)));
if ( pos.piece_count(WHITE, BISHOP) + pos.piece_count(WHITE, KNIGHT) <= 2 if ( pos.piece_count(WHITE, BISHOP) + pos.piece_count(WHITE, KNIGHT) <= 2
&& pos.piece_count(BLACK, BISHOP) + pos.piece_count(BLACK, KNIGHT) <= 2) && pos.piece_count(BLACK, BISHOP) + pos.piece_count(BLACK, KNIGHT) <= 2)
{ {
mi->evaluationFunction = &EvaluateKmmKm[pos.side_to_move()]; e->evaluationFunction = &EvaluateKmmKm[pos.side_to_move()];
return mi; return e;
} }
} }
@@ -155,26 +145,26 @@ MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
// scaling functions and we need to decide which one to use. // scaling functions and we need to decide which one to use.
EndgameBase<ScaleFactor>* sf; EndgameBase<ScaleFactor>* sf;
if ((sf = funcs->get<ScaleFactor>(key)) != NULL) if (endgames.probe(key, sf))
{ {
mi->scalingFunction[sf->color()] = sf; e->scalingFunction[sf->color()] = sf;
return mi; return e;
} }
// Generic scaling functions that refer to more then one material // Generic scaling functions that refer to more then one material
// distribution. Should be probed after the specialized ones. // distribution. Should be probed after the specialized ones.
// Note that these ones don't return after setting the function. // Note that these ones don't return after setting the function.
if (is_KBPsKs<WHITE>(pos)) if (is_KBPsKs<WHITE>(pos))
mi->scalingFunction[WHITE] = &ScaleKBPsK[WHITE]; e->scalingFunction[WHITE] = &ScaleKBPsK[WHITE];
if (is_KBPsKs<BLACK>(pos)) if (is_KBPsKs<BLACK>(pos))
mi->scalingFunction[BLACK] = &ScaleKBPsK[BLACK]; e->scalingFunction[BLACK] = &ScaleKBPsK[BLACK];
if (is_KQKRPs<WHITE>(pos)) if (is_KQKRPs<WHITE>(pos))
mi->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE]; e->scalingFunction[WHITE] = &ScaleKQKRPs[WHITE];
else if (is_KQKRPs<BLACK>(pos)) else if (is_KQKRPs<BLACK>(pos))
mi->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK]; e->scalingFunction[BLACK] = &ScaleKQKRPs[BLACK];
Value npm_w = pos.non_pawn_material(WHITE); Value npm_w = pos.non_pawn_material(WHITE);
Value npm_b = pos.non_pawn_material(BLACK); Value npm_b = pos.non_pawn_material(BLACK);
@@ -184,32 +174,32 @@ MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
if (pos.piece_count(BLACK, PAWN) == 0) if (pos.piece_count(BLACK, PAWN) == 0)
{ {
assert(pos.piece_count(WHITE, PAWN) >= 2); assert(pos.piece_count(WHITE, PAWN) >= 2);
mi->scalingFunction[WHITE] = &ScaleKPsK[WHITE]; e->scalingFunction[WHITE] = &ScaleKPsK[WHITE];
} }
else if (pos.piece_count(WHITE, PAWN) == 0) else if (pos.piece_count(WHITE, PAWN) == 0)
{ {
assert(pos.piece_count(BLACK, PAWN) >= 2); assert(pos.piece_count(BLACK, PAWN) >= 2);
mi->scalingFunction[BLACK] = &ScaleKPsK[BLACK]; e->scalingFunction[BLACK] = &ScaleKPsK[BLACK];
} }
else if (pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1) else if (pos.piece_count(WHITE, PAWN) == 1 && pos.piece_count(BLACK, PAWN) == 1)
{ {
// This is a special case because we set scaling functions // This is a special case because we set scaling functions
// for both colors instead of only one. // for both colors instead of only one.
mi->scalingFunction[WHITE] = &ScaleKPKP[WHITE]; e->scalingFunction[WHITE] = &ScaleKPKP[WHITE];
mi->scalingFunction[BLACK] = &ScaleKPKP[BLACK]; e->scalingFunction[BLACK] = &ScaleKPKP[BLACK];
} }
} }
// No pawns makes it difficult to win, even with a material advantage // No pawns makes it difficult to win, even with a material advantage
if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMidgame) if (pos.piece_count(WHITE, PAWN) == 0 && npm_w - npm_b <= BishopValueMidgame)
{ {
mi->factor[WHITE] = (uint8_t) e->factor[WHITE] = (uint8_t)
(npm_w == npm_b || npm_w < RookValueMidgame ? 0 : NoPawnsSF[std::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) if (pos.piece_count(BLACK, PAWN) == 0 && npm_b - npm_w <= BishopValueMidgame)
{ {
mi->factor[BLACK] = (uint8_t) e->factor[BLACK] = (uint8_t)
(npm_w == npm_b || npm_b < RookValueMidgame ? 0 : NoPawnsSF[std::min(pos.piece_count(BLACK, BISHOP), 2)]); (npm_w == npm_b || npm_b < RookValueMidgame ? 0 : NoPawnsSF[std::min(pos.piece_count(BLACK, BISHOP), 2)]);
} }
@@ -219,7 +209,7 @@ MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
int minorPieceCount = pos.piece_count(WHITE, KNIGHT) + pos.piece_count(WHITE, BISHOP) int minorPieceCount = pos.piece_count(WHITE, KNIGHT) + pos.piece_count(WHITE, BISHOP)
+ pos.piece_count(BLACK, KNIGHT) + pos.piece_count(BLACK, BISHOP); + pos.piece_count(BLACK, KNIGHT) + pos.piece_count(BLACK, BISHOP);
mi->spaceWeight = minorPieceCount * minorPieceCount; e->spaceWeight = minorPieceCount * minorPieceCount;
} }
// Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder // Evaluate the material imbalance. We use PIECE_TYPE_NONE as a place holder
@@ -231,16 +221,16 @@ MaterialInfo* MaterialInfoTable::material_info(const Position& pos) const {
{ pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT), { pos.piece_count(BLACK, BISHOP) > 1, pos.piece_count(BLACK, PAWN), pos.piece_count(BLACK, KNIGHT),
pos.piece_count(BLACK, BISHOP) , pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } }; pos.piece_count(BLACK, BISHOP) , pos.piece_count(BLACK, ROOK), pos.piece_count(BLACK, QUEEN) } };
mi->value = (int16_t)((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16); e->value = (int16_t)((imbalance<WHITE>(pieceCount) - imbalance<BLACK>(pieceCount)) / 16);
return mi; return e;
} }
/// MaterialInfoTable::imbalance() calculates imbalance comparing piece count of each /// MaterialTable::imbalance() calculates imbalance comparing piece count of each
/// piece type for both colors. /// piece type for both colors.
template<Color Us> template<Color Us>
int MaterialInfoTable::imbalance(const int pieceCount[][8]) { int MaterialTable::imbalance(const int pieceCount[][8]) {
const Color Them = (Us == WHITE ? BLACK : WHITE); const Color Them = (Us == WHITE ? BLACK : WHITE);
@@ -272,11 +262,11 @@ int MaterialInfoTable::imbalance(const int pieceCount[][8]) {
} }
/// MaterialInfoTable::game_phase() calculates the phase given the current /// MaterialTable::game_phase() calculates the phase given the current
/// position. Because the phase is strictly a function of the material, it /// position. Because the phase is strictly a function of the material, it
/// is stored in MaterialInfo. /// is stored in MaterialEntry.
Phase MaterialInfoTable::game_phase(const Position& pos) { Phase MaterialTable::game_phase(const Position& pos) {
Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK); Value npm = pos.non_pawn_material(WHITE) + pos.non_pawn_material(BLACK);

View File

@@ -21,8 +21,8 @@
#define MATERIAL_H_INCLUDED #define MATERIAL_H_INCLUDED
#include "endgame.h" #include "endgame.h"
#include "misc.h"
#include "position.h" #include "position.h"
#include "tt.h"
#include "types.h" #include "types.h"
const int MaterialTableSize = 8192; const int MaterialTableSize = 8192;
@@ -34,7 +34,7 @@ enum Phase {
}; };
/// MaterialInfo is a class which contains various information about a /// MaterialEntry is a class which contains various information about a
/// material configuration. It contains a material balance evaluation, /// material configuration. It contains a material balance evaluation,
/// a function pointer to a special endgame evaluation function (which in /// a function pointer to a special endgame evaluation function (which in
/// most cases is NULL, meaning that the standard evaluation function will /// most cases is NULL, meaning that the standard evaluation function will
@@ -44,9 +44,9 @@ enum Phase {
/// For instance, in KRB vs KR endgames, the score is scaled down by a factor /// For instance, in KRB vs KR endgames, the score is scaled down by a factor
/// of 4, which will result in scores of absolute value less than one pawn. /// of 4, which will result in scores of absolute value less than one pawn.
class MaterialInfo { class MaterialEntry {
friend class MaterialInfoTable; friend struct MaterialTable;
public: public:
Score material_value() const; Score material_value() const;
@@ -67,32 +67,28 @@ private:
}; };
/// The MaterialInfoTable class represents a pawn hash table. The most important /// The MaterialTable class represents a material hash table. The most important
/// method is material_info(), which returns a pointer to a MaterialInfo object. /// method is probe(), which returns a pointer to a MaterialEntry object.
class MaterialInfoTable : public SimpleHash<MaterialInfo, MaterialTableSize> { struct MaterialTable {
public:
~MaterialInfoTable(); MaterialEntry* probe(const Position& pos);
void init();
MaterialInfo* material_info(const Position& pos) const;
static Phase game_phase(const Position& pos); static Phase game_phase(const Position& pos);
template<Color Us> static int imbalance(const int pieceCount[][8]);
private: HashTable<MaterialEntry, MaterialTableSize> entries;
template<Color Us> Endgames endgames;
static int imbalance(const int pieceCount[][8]);
Endgames* funcs;
}; };
/// MaterialInfo::scale_factor takes a position and a color as input, and /// MaterialEntry::scale_factor takes a position and a color as input, and
/// returns a scale factor for the given color. We have to provide the /// returns a scale factor for the given color. We have to provide the
/// position in addition to the color, because the scale factor need not /// position in addition to the color, because the scale factor need not
/// to be a constant: It can also be a function which should be applied to /// to be a constant: It can also be a function which should be applied to
/// the position. For instance, in KBP vs K endgames, a scaling function /// the position. For instance, in KBP vs K endgames, a scaling function
/// which checks for draws with rook pawns and wrong-colored bishops. /// which checks for draws with rook pawns and wrong-colored bishops.
inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) const { inline ScaleFactor MaterialEntry::scale_factor(const Position& pos, Color c) const {
if (!scalingFunction[c]) if (!scalingFunction[c])
return ScaleFactor(factor[c]); return ScaleFactor(factor[c]);
@@ -101,23 +97,23 @@ inline ScaleFactor MaterialInfo::scale_factor(const Position& pos, Color c) cons
return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf; return sf == SCALE_FACTOR_NONE ? ScaleFactor(factor[c]) : sf;
} }
inline Value MaterialInfo::evaluate(const Position& pos) const { inline Value MaterialEntry::evaluate(const Position& pos) const {
return (*evaluationFunction)(pos); return (*evaluationFunction)(pos);
} }
inline Score MaterialInfo::material_value() const { inline Score MaterialEntry::material_value() const {
return make_score(value, value); return make_score(value, value);
} }
inline int MaterialInfo::space_weight() const { inline int MaterialEntry::space_weight() const {
return spaceWeight; return spaceWeight;
} }
inline Phase MaterialInfo::game_phase() const { inline Phase MaterialEntry::game_phase() const {
return gamePhase; return gamePhase;
} }
inline bool MaterialInfo::specialized_eval_exists() const { inline bool MaterialEntry::specialized_eval_exists() const {
return evaluationFunction != NULL; return evaluationFunction != NULL;
} }

View File

@@ -17,45 +17,23 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#if defined(_MSC_VER)
#define _CRT_SECURE_NO_DEPRECATE
#define NOMINMAX // disable macros min() and max()
#include <windows.h>
#include <sys/timeb.h>
#else
# include <sys/time.h>
# include <sys/types.h>
# include <unistd.h>
# if defined(__hpux)
# include <sys/pstat.h>
# endif
#endif
#if !defined(NO_PREFETCH)
# include <xmmintrin.h>
#endif
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include "bitcount.h"
#include "misc.h" #include "misc.h"
#include "thread.h" #include "thread.h"
#if defined(__hpux)
# include <sys/pstat.h>
#endif
using namespace std; using namespace std;
/// Version number. If Version is left empty, then Tag plus current /// Version number. If Version is left empty, then Tag plus current
/// date (in the format YYMMDD) is used as a version number. /// date (in the format YYMMDD) is used as a version number.
static const string Version = "2.2.2"; static const string Version = "";
static const string Tag = ""; static const string Tag = "";
@@ -73,19 +51,17 @@ const string engine_info(bool to_uci) {
string month, day, year; string month, day, year;
stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008" stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
s << "Stockfish " << Version;
if (Version.empty()) if (Version.empty())
{ {
date >> month >> day >> year; date >> month >> day >> year;
s << "Stockfish " << Tag s << Tag << setfill('0') << " " << year.substr(2)
<< setfill('0') << " " << year.substr(2) << setw(2) << (1 + months.find(month) / 4) << setw(2) << day;
<< setw(2) << (1 + months.find(month) / 4)
<< setw(2) << day << cpu64 << popcnt;
} }
else
s << "Stockfish " << Version << cpu64 << popcnt;
s << (to_uci ? "\nid author ": " by ") s << cpu64 << popcnt << (to_uci ? "\nid author ": " by ")
<< "Tord Romstad, Marco Costalba and Joona Kiiski"; << "Tord Romstad, Marco Costalba and Joona Kiiski";
return s.str(); return s.str();
@@ -112,39 +88,85 @@ void dbg_print() {
} }
/// system_time() returns the current system time, measured in milliseconds /// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
/// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
/// can toggle the logging of std::cout and std:cin at runtime while preserving
/// usual i/o functionality and without changing a single line of code!
/// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
int system_time() { class Logger {
#if defined(_MSC_VER) Logger() : in(cin.rdbuf(), file), out(cout.rdbuf(), file) {}
struct _timeb t; ~Logger() { start(false); }
_ftime(&t);
return int(t.time * 1000 + t.millitm); struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
#else
struct timeval t; Tie(streambuf* b, ofstream& f) : buf(b), file(f) {}
gettimeofday(&t, NULL);
return t.tv_sec * 1000 + t.tv_usec / 1000; int sync() { return file.rdbuf()->pubsync(), buf->pubsync(); }
#endif int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
} int underflow() { return buf->sgetc(); }
int uflow() { return log(buf->sbumpc(), ">> "); }
int log(int c, const char* prefix) {
static int last = '\n';
if (last == '\n')
file.rdbuf()->sputn(prefix, 3);
return last = file.rdbuf()->sputc((char)c);
}
streambuf* buf;
ofstream& file;
};
ofstream file;
Tie in, out;
public:
static void start(bool b) {
static Logger l;
if (b && !l.file.is_open())
{
l.file.open("io_log.txt", ifstream::out | ifstream::app);
cin.rdbuf(&l.in);
cout.rdbuf(&l.out);
}
else if (!b && l.file.is_open())
{
cout.rdbuf(l.out.buf);
cin.rdbuf(l.in.buf);
l.file.close();
}
}
};
/// Trampoline helper to avoid moving Logger to misc.h
void start_logger(bool b) { Logger::start(b); }
/// cpu_count() tries to detect the number of CPU cores /// cpu_count() tries to detect the number of CPU cores
int cpu_count() { int cpu_count() {
#if defined(_MSC_VER) #if defined(_WIN32) || defined(_WIN64)
SYSTEM_INFO s; SYSTEM_INFO s;
GetSystemInfo(&s); GetSystemInfo(&s);
return std::min(int(s.dwNumberOfProcessors), MAX_THREADS); return s.dwNumberOfProcessors;
#else #else
# if defined(_SC_NPROCESSORS_ONLN) # if defined(_SC_NPROCESSORS_ONLN)
return std::min((int)sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS); return sysconf(_SC_NPROCESSORS_ONLN);
# elif defined(__hpux) # elif defined(__hpux)
struct pst_dynamic psd; struct pst_dynamic psd;
if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1) if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
return 1; return 1;
return std::min((int)psd.psd_proc_cnt, MAX_THREADS); return psd.psd_proc_cnt;
# else # else
return 1; return 1;
# endif # endif
@@ -156,24 +178,16 @@ int cpu_count() {
/// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap /// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap
/// conversion from milliseconds to struct timespec, as used by pthreads. /// conversion from milliseconds to struct timespec, as used by pthreads.
void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) { void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
#if defined(_MSC_VER) #if defined(_WIN32) || defined(_WIN64)
int tm = msec; int tm = msec;
#else #else
struct timeval t; timespec ts, *tm = &ts;
struct timespec abstime, *tm = &abstime; uint64_t ms = Time::current_time().msec() + msec;
gettimeofday(&t, NULL); ts.tv_sec = ms / 1000;
ts.tv_nsec = (ms % 1000) * 1000000LL;
abstime.tv_sec = t.tv_sec + (msec / 1000);
abstime.tv_nsec = (t.tv_usec + (msec % 1000) * 1000) * 1000;
if (abstime.tv_nsec > 1000000000LL)
{
abstime.tv_sec += 1;
abstime.tv_nsec -= 1000000000LL;
}
#endif #endif
cond_timedwait(sleepCond, sleepLock, tm); cond_timedwait(sleepCond, sleepLock, tm);
@@ -189,6 +203,8 @@ void prefetch(char*) {}
#else #else
# include <xmmintrin.h>
void prefetch(char* addr) { void prefetch(char* addr) {
# if defined(__INTEL_COMPILER) || defined(__ICL) # if defined(__INTEL_COMPILER) || defined(__ICL)

View File

@@ -22,15 +22,15 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <vector>
#include "lock.h"
#include "types.h" #include "types.h"
extern const std::string engine_info(bool to_uci = false); extern const std::string engine_info(bool to_uci = false);
extern int system_time();
extern int cpu_count(); extern int cpu_count();
extern void timed_wait(WaitCondition*, Lock*, int); extern void timed_wait(WaitCondition&, Lock&, int);
extern void prefetch(char* addr); extern void prefetch(char* addr);
extern void start_logger(bool b);
extern void dbg_hit_on(bool b); extern void dbg_hit_on(bool b);
extern void dbg_hit_on_c(bool c, bool b); extern void dbg_hit_on_c(bool c, bool b);
@@ -38,13 +38,36 @@ extern void dbg_mean_of(int v);
extern void dbg_print(); extern void dbg_print();
class Position; class Position;
extern Move move_from_uci(const Position& pos, const std::string& str); extern Move move_from_uci(const Position& pos, std::string& str);
extern const std::string move_to_uci(Move m, bool chess960); extern const std::string move_to_uci(Move m, bool chess960);
extern const std::string move_to_san(Position& pos, Move m); extern const std::string move_to_san(Position& pos, Move m);
struct Log : public std::ofstream { struct Log : public std::ofstream {
Log(const std::string& f = "log.txt") : std::ofstream(f.c_str(), std::ios::out | std::ios::app) {} Log(const std::string& f = "log.txt") : std::ofstream(f.c_str(), std::ios::out | std::ios::app) {}
~Log() { if (is_open()) close(); } ~Log() { if (is_open()) close(); }
}; };
struct Time {
void restart() { system_time(&t); }
uint64_t msec() const { return time_to_msec(t); }
int elapsed() const { return int(current_time().msec() - time_to_msec(t)); }
static Time current_time() { Time t; t.restart(); return t; }
private:
sys_time_t t;
};
template<class Entry, int Size>
struct HashTable {
HashTable() : e(Size, Entry()) { memset(&e[0], 0, sizeof(Entry) * Size); }
Entry* operator[](Key k) { return &e[(uint32_t)k & (Size - 1)]; }
private:
std::vector<Entry> e;
};
#endif // !defined(MISC_H_INCLUDED) #endif // !defined(MISC_H_INCLUDED)

View File

@@ -18,7 +18,6 @@
*/ */
#include <cassert> #include <cassert>
#include <cstring>
#include <string> #include <string>
#include "movegen.h" #include "movegen.h"
@@ -47,7 +46,7 @@ const string move_to_uci(Move m, bool chess960) {
to = from + (file_of(to) == FILE_H ? Square(2) : -Square(2)); to = from + (file_of(to) == FILE_H ? Square(2) : -Square(2));
if (is_promotion(m)) if (is_promotion(m))
promotion = char(tolower(piece_type_to_char(promotion_piece_type(m)))); promotion = char(tolower(piece_type_to_char(promotion_type(m))));
return square_to_string(from) + square_to_string(to) + promotion; return square_to_string(from) + square_to_string(to) + promotion;
} }
@@ -57,7 +56,10 @@ const string move_to_uci(Move m, bool chess960) {
/// simple coordinate notation and returns an equivalent Move if any. /// simple coordinate notation and returns an equivalent Move if any.
/// Moves are guaranteed to be legal. /// Moves are guaranteed to be legal.
Move move_from_uci(const Position& pos, const string& str) { Move move_from_uci(const Position& pos, string& str) {
if (str.length() == 5) // Junior could send promotion in uppercase
str[4] = char(tolower(str[4]));
for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml) for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
if (str == move_to_uci(ml.move(), pos.is_chess960())) if (str == move_to_uci(ml.move(), pos.is_chess960()))
@@ -85,7 +87,7 @@ const string move_to_san(Position& pos, Move m) {
bool ambiguousMove, ambiguousFile, ambiguousRank; bool ambiguousMove, ambiguousFile, ambiguousRank;
Square sq, from = from_sq(m); Square sq, from = from_sq(m);
Square to = to_sq(m); Square to = to_sq(m);
PieceType pt = type_of(pos.piece_on(from)); PieceType pt = type_of(pos.piece_moved(m));
string san; string san;
if (is_castle(m)) if (is_castle(m))
@@ -98,8 +100,8 @@ const string move_to_san(Position& pos, Move m) {
// Disambiguation if we have more then one piece with destination 'to' // Disambiguation if we have more then one piece with destination 'to'
// note that for pawns is not needed because starting file is explicit. // note that for pawns is not needed because starting file is explicit.
attackers = pos.attackers_to(to) & pos.pieces(pt, pos.side_to_move()); attackers = pos.attackers_to(to) & pos.pieces(pos.side_to_move(), pt);
clear_bit(&attackers, from); attackers ^= from;
ambiguousMove = ambiguousFile = ambiguousRank = false; ambiguousMove = ambiguousFile = ambiguousRank = false;
while (attackers) while (attackers)
@@ -143,7 +145,7 @@ const string move_to_san(Position& pos, Move m) {
if (is_promotion(m)) if (is_promotion(m))
{ {
san += '='; san += '=';
san += piece_type_to_char(promotion_piece_type(m)); san += piece_type_to_char(promotion_type(m));
} }
} }

View File

@@ -29,55 +29,35 @@
/// Version used for pawns, where the 'from' square is given as a delta from the 'to' square /// Version used for pawns, where the 'from' square is given as a delta from the 'to' square
#define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_1st_bit(&b); \ #define SERIALIZE_PAWNS(b, d) while (b) { Square to = pop_1st_bit(&b); \
(*mlist++).move = make_move(to + (d), to); } (*mlist++).move = make_move(to - (d), to); }
namespace { namespace {
enum CastlingSide { KING_SIDE, QUEEN_SIDE };
template<CastlingSide Side, bool OnlyChecks> template<CastlingSide Side, bool OnlyChecks>
MoveStack* generate_castle_moves(const Position& pos, MoveStack* mlist, Color us) { MoveStack* generate_castle(const Position& pos, MoveStack* mlist, Color us) {
const CastleRight CR[] = { Side ? WHITE_OOO : WHITE_OO, if (pos.castle_impeded(us, Side) || !pos.can_castle(make_castle_right(us, Side)))
Side ? BLACK_OOO : BLACK_OO };
if (!pos.can_castle(CR[us]))
return mlist; return mlist;
// After castling, the rook and king final positions are the same in Chess960 // After castling, the rook and king final positions are the same in Chess960
// as they would be in standard chess. // as they would be in standard chess.
Square kfrom = pos.king_square(us); Square kfrom = pos.king_square(us);
Square rfrom = pos.castle_rook_square(CR[us]); Square rfrom = pos.castle_rook_square(us, Side);
Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1); Square kto = relative_square(us, Side == KING_SIDE ? SQ_G1 : SQ_C1);
Square rto = relative_square(us, Side == KING_SIDE ? SQ_F1 : SQ_D1);
Bitboard enemies = pos.pieces(~us); Bitboard enemies = pos.pieces(~us);
assert(!pos.in_check()); assert(!pos.in_check());
assert(pos.piece_on(kfrom) == make_piece(us, KING));
assert(pos.piece_on(rfrom) == make_piece(us, ROOK));
// Unimpeded rule: All the squares between the king's initial and final squares
// (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 = std::min(rfrom, rto), e = std::max(rfrom, rto); s <= e; s++)
if (s != kfrom && s != rfrom && !pos.square_is_empty(s))
return mlist;
for (Square s = std::min(kfrom, kto), e = std::max(kfrom, kto); s <= e; s++) for (Square s = std::min(kfrom, kto), e = std::max(kfrom, kto); s <= e; s++)
if ( (s != kfrom && s != rfrom && !pos.square_is_empty(s)) if ( s != kfrom // We are not in check
||(pos.attackers_to(s) & enemies)) && (pos.attackers_to(s) & enemies))
return mlist; return mlist;
// Because we generate only legal castling moves we need to verify that // Because we generate only legal castling moves we need to verify that
// when moving the castling rook we do not discover some hidden checker. // when moving the castling rook we do not discover some hidden checker.
// For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1. // For instance an enemy queen in SQ_A1 when castling rook is in SQ_B1.
if (pos.is_chess960()) if ( pos.is_chess960()
{ && (pos.attackers_to(kto, pos.pieces() ^ rfrom) & enemies))
Bitboard occ = pos.occupied_squares();
clear_bit(&occ, rfrom);
if (pos.attackers_to(kto, occ) & enemies)
return mlist; return mlist;
}
(*mlist++).move = make_castle(kfrom, rfrom); (*mlist++).move = make_castle(kfrom, rfrom);
@@ -91,35 +71,20 @@ namespace {
template<Square Delta> template<Square Delta>
inline Bitboard move_pawns(Bitboard p) { inline Bitboard move_pawns(Bitboard p) {
return Delta == DELTA_N ? p << 8 : Delta == DELTA_S ? p >> 8 : return Delta == DELTA_N ? p << 8
Delta == DELTA_NE ? p << 9 : Delta == DELTA_SE ? p >> 7 : : Delta == DELTA_S ? p >> 8
Delta == DELTA_NW ? p << 7 : Delta == DELTA_SW ? p >> 9 : p; : Delta == DELTA_NE ? (p & ~FileHBB) << 9
} : Delta == DELTA_SE ? (p & ~FileHBB) >> 7
: Delta == DELTA_NW ? (p & ~FileABB) << 7
: Delta == DELTA_SW ? (p & ~FileABB) >> 9 : 0;
template<Square Delta>
inline MoveStack* generate_pawn_captures(MoveStack* mlist, Bitboard pawns, Bitboard target) {
const Bitboard TFileABB = ( Delta == DELTA_NE
|| Delta == DELTA_SE ? FileABB : FileHBB);
Bitboard b = move_pawns<Delta>(pawns) & target & ~TFileABB;
SERIALIZE_PAWNS(b, -Delta);
return mlist;
} }
template<MoveType Type, Square Delta> template<MoveType Type, Square Delta>
inline MoveStack* generate_promotions(MoveStack* mlist, Bitboard pawnsOn7, Bitboard target, Square ksq) { inline MoveStack* generate_promotions(MoveStack* mlist, Bitboard pawnsOn7, Bitboard target, Square ksq) {
const Bitboard TFileABB = ( Delta == DELTA_NE
|| Delta == DELTA_SE ? FileABB : FileHBB);
Bitboard b = move_pawns<Delta>(pawnsOn7) & target; Bitboard b = move_pawns<Delta>(pawnsOn7) & target;
if (Delta != DELTA_N && Delta != DELTA_S)
b &= ~TFileABB;
while (b) while (b)
{ {
Square to = pop_1st_bit(&b); Square to = pop_1st_bit(&b);
@@ -127,30 +92,32 @@ namespace {
if (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION) if (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION)
(*mlist++).move = make_promotion(to - Delta, to, QUEEN); (*mlist++).move = make_promotion(to - Delta, to, QUEEN);
if (Type == MV_NON_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION) if (Type == MV_QUIET || Type == MV_EVASION || Type == MV_NON_EVASION)
{ {
(*mlist++).move = make_promotion(to - Delta, to, ROOK); (*mlist++).move = make_promotion(to - Delta, to, ROOK);
(*mlist++).move = make_promotion(to - Delta, to, BISHOP); (*mlist++).move = make_promotion(to - Delta, to, BISHOP);
(*mlist++).move = make_promotion(to - Delta, to, KNIGHT); (*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
} }
// Knight-promotion is the only one that can give a check (direct or // Knight-promotion is the only one that can give a direct check not
// discovered) not already included in the queen-promotion. // already included in the queen-promotion.
if (Type == MV_NON_CAPTURE_CHECK && bit_is_set(StepAttacksBB[W_KNIGHT][to], ksq)) if (Type == MV_QUIET_CHECK && (StepAttacksBB[W_KNIGHT][to] & ksq))
(*mlist++).move = make_promotion(to - Delta, to, KNIGHT); (*mlist++).move = make_promotion(to - Delta, to, KNIGHT);
else else
(void)ksq; // Silence a warning under MSVC (void)ksq; // Silence a warning under MSVC
} }
return mlist; return mlist;
} }
template<Color Us, MoveType Type> template<Color Us, MoveType Type>
MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq) { MoveStack* generate_pawn_moves(const Position& pos, MoveStack* mlist, Bitboard target, Square ksq = SQ_NONE) {
// Calculate our parametrized parameters at compile time, named according to // Compute our parametrized parameters at compile time, named according to
// the point of view of white side. // the point of view of white side.
const Color Them = (Us == WHITE ? BLACK : WHITE); const Color Them = (Us == WHITE ? BLACK : WHITE);
const Bitboard TRank8BB = (Us == WHITE ? Rank8BB : Rank1BB);
const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB); const Bitboard TRank7BB = (Us == WHITE ? Rank7BB : Rank2BB);
const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB); const Bitboard TRank3BB = (Us == WHITE ? Rank3BB : Rank6BB);
const Square UP = (Us == WHITE ? DELTA_N : DELTA_S); const Square UP = (Us == WHITE ? DELTA_N : DELTA_S);
@@ -159,8 +126,8 @@ namespace {
Bitboard b1, b2, dc1, dc2, emptySquares; Bitboard b1, b2, dc1, dc2, emptySquares;
Bitboard pawnsOn7 = pos.pieces(PAWN, Us) & TRank7BB; Bitboard pawnsOn7 = pos.pieces(Us, PAWN) & TRank7BB;
Bitboard pawnsNotOn7 = pos.pieces(PAWN, Us) & ~TRank7BB; Bitboard pawnsNotOn7 = pos.pieces(Us, PAWN) & ~TRank7BB;
Bitboard enemies = (Type == MV_EVASION ? pos.pieces(Them) & target: Bitboard enemies = (Type == MV_EVASION ? pos.pieces(Them) & target:
Type == MV_CAPTURE ? target : pos.pieces(Them)); Type == MV_CAPTURE ? target : pos.pieces(Them));
@@ -168,7 +135,7 @@ namespace {
// Single and double pawn pushes, no promotions // Single and double pawn pushes, no promotions
if (Type != MV_CAPTURE) if (Type != MV_CAPTURE)
{ {
emptySquares = (Type == MV_NON_CAPTURE ? target : pos.empty_squares()); emptySquares = (Type == MV_QUIET ? target : ~pos.pieces());
b1 = move_pawns<UP>(pawnsNotOn7) & emptySquares; b1 = move_pawns<UP>(pawnsNotOn7) & emptySquares;
b2 = move_pawns<UP>(b1 & TRank3BB) & emptySquares; b2 = move_pawns<UP>(b1 & TRank3BB) & emptySquares;
@@ -179,9 +146,8 @@ namespace {
b2 &= target; b2 &= target;
} }
if (Type == MV_NON_CAPTURE_CHECK) if (Type == MV_QUIET_CHECK)
{ {
// Consider only direct checks
b1 &= pos.attacks_from<PAWN>(ksq, Them); b1 &= pos.attacks_from<PAWN>(ksq, Them);
b2 &= pos.attacks_from<PAWN>(ksq, Them); b2 &= pos.attacks_from<PAWN>(ksq, Them);
@@ -199,15 +165,15 @@ namespace {
} }
} }
SERIALIZE_PAWNS(b1, -UP); SERIALIZE_PAWNS(b1, UP);
SERIALIZE_PAWNS(b2, -UP -UP); SERIALIZE_PAWNS(b2, UP + UP);
} }
// Promotions and underpromotions // Promotions and underpromotions
if (pawnsOn7) if (pawnsOn7 && (Type != MV_EVASION || (target & TRank8BB)))
{ {
if (Type == MV_CAPTURE) if (Type == MV_CAPTURE)
emptySquares = pos.empty_squares(); emptySquares = ~pos.pieces();
if (Type == MV_EVASION) if (Type == MV_EVASION)
emptySquares &= target; emptySquares &= target;
@@ -220,17 +186,20 @@ namespace {
// Standard and en-passant captures // Standard and en-passant captures
if (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION) if (Type == MV_CAPTURE || Type == MV_EVASION || Type == MV_NON_EVASION)
{ {
mlist = generate_pawn_captures<RIGHT>(mlist, pawnsNotOn7, enemies); b1 = move_pawns<RIGHT>(pawnsNotOn7) & enemies;
mlist = generate_pawn_captures<LEFT >(mlist, pawnsNotOn7, enemies); b2 = move_pawns<LEFT >(pawnsNotOn7) & enemies;
SERIALIZE_PAWNS(b1, RIGHT);
SERIALIZE_PAWNS(b2, LEFT);
if (pos.ep_square() != SQ_NONE) if (pos.ep_square() != SQ_NONE)
{ {
assert(rank_of(pos.ep_square()) == (Us == WHITE ? RANK_6 : RANK_3)); assert(rank_of(pos.ep_square()) == relative_rank(Us, RANK_6));
// An en passant capture can be an evasion only if the checking piece // An en passant capture can be an evasion only if the checking piece
// is the double pushed pawn and so is in the target. Otherwise this // is the double pushed pawn and so is in the target. Otherwise this
// is a discovery check and we are forced to do otherwise. // is a discovery check and we are forced to do otherwise.
if (Type == MV_EVASION && !bit_is_set(target, pos.ep_square() - UP)) if (Type == MV_EVASION && !(target & (pos.ep_square() - UP)))
return mlist; return mlist;
b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them); b1 = pawnsNotOn7 & pos.attacks_from<PAWN>(pos.ep_square(), Them);
@@ -251,73 +220,56 @@ namespace {
Color us, const CheckInfo& ci) { Color us, const CheckInfo& ci) {
assert(Pt != KING && Pt != PAWN); assert(Pt != KING && Pt != PAWN);
Bitboard checkSqs, b; Bitboard b, target;
Square from; Square from;
const Square* pl = pos.piece_list(us, Pt); const Square* pl = pos.piece_list(us, Pt);
if ((from = *pl++) == SQ_NONE) if (*pl != SQ_NONE)
return mlist;
checkSqs = ci.checkSq[Pt] & pos.empty_squares();
do
{ {
if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN) target = ci.checkSq[Pt] & ~pos.pieces(); // Non capture checks only
&& !(PseudoAttacks[Pt][from] & checkSqs))
continue;
if (ci.dcCandidates && bit_is_set(ci.dcCandidates, from)) do {
continue; from = *pl;
b = pos.attacks_from<Pt>(from) & checkSqs; if ( (Pt == BISHOP || Pt == ROOK || Pt == QUEEN)
SERIALIZE(b); && !(PseudoAttacks[Pt][from] & target))
continue;
} while ((from = *pl++) != SQ_NONE); if (ci.dcCandidates && (ci.dcCandidates & from))
continue;
b = pos.attacks_from<Pt>(from) & target;
SERIALIZE(b);
} while (*++pl != SQ_NONE);
}
return mlist; return mlist;
} }
template<>
FORCE_INLINE MoveStack* generate_direct_checks<PAWN>(const Position& p, MoveStack* m,
Color us, const CheckInfo& ci) {
return us == WHITE ? generate_pawn_moves<WHITE, MV_NON_CAPTURE_CHECK>(p, m, ci.dcCandidates, ci.ksq)
: generate_pawn_moves<BLACK, MV_NON_CAPTURE_CHECK>(p, m, ci.dcCandidates, ci.ksq);
}
template<PieceType Pt, MoveType Type>
FORCE_INLINE MoveStack* generate_piece_moves(const Position& p, MoveStack* m, Color us, Bitboard t) {
assert(Pt == PAWN);
return us == WHITE ? generate_pawn_moves<WHITE, Type>(p, m, t, SQ_NONE)
: generate_pawn_moves<BLACK, Type>(p, m, t, SQ_NONE);
}
template<PieceType Pt> template<PieceType Pt>
FORCE_INLINE MoveStack* generate_piece_moves(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { FORCE_INLINE MoveStack* generate_moves(const Position& pos, MoveStack* mlist,
Color us, Bitboard target) {
assert(Pt != KING && Pt != PAWN);
Bitboard b; Bitboard b;
Square from; Square from;
const Square* pl = pos.piece_list(us, Pt); const Square* pl = pos.piece_list(us, Pt);
if (*pl != SQ_NONE) if (*pl != SQ_NONE)
{
do { do {
from = *pl; from = *pl;
b = pos.attacks_from<Pt>(from) & target; b = pos.attacks_from<Pt>(from) & target;
SERIALIZE(b); SERIALIZE(b);
} while (*++pl != SQ_NONE); } while (*++pl != SQ_NONE);
}
return mlist; return mlist;
} }
template<> template<>
FORCE_INLINE MoveStack* generate_piece_moves<KING>(const Position& pos, MoveStack* mlist, Color us, Bitboard target) { FORCE_INLINE MoveStack* generate_moves<KING>(const Position& pos, MoveStack* mlist,
Color us, Bitboard target) {
Square from = pos.king_square(us); Square from = pos.king_square(us);
Bitboard b = pos.attacks_from<KING>(from) & target; Bitboard b = pos.attacks_from<KING>(from) & target;
SERIALIZE(b); SERIALIZE(b);
@@ -330,7 +282,7 @@ namespace {
/// generate<MV_CAPTURE> generates all pseudo-legal captures and queen /// generate<MV_CAPTURE> generates all pseudo-legal captures and queen
/// promotions. Returns a pointer to the end of the move list. /// promotions. Returns a pointer to the end of the move list.
/// ///
/// generate<MV_NON_CAPTURE> generates all pseudo-legal non-captures and /// generate<MV_QUIET> generates all pseudo-legal non-captures and
/// underpromotions. Returns a pointer to the end of the move list. /// underpromotions. Returns a pointer to the end of the move list.
/// ///
/// generate<MV_NON_EVASION> generates all pseudo-legal captures and /// generate<MV_NON_EVASION> generates all pseudo-legal captures and
@@ -339,7 +291,7 @@ namespace {
template<MoveType Type> template<MoveType Type>
MoveStack* generate(const Position& pos, MoveStack* mlist) { MoveStack* generate(const Position& pos, MoveStack* mlist) {
assert(Type == MV_CAPTURE || Type == MV_NON_CAPTURE || Type == MV_NON_EVASION); assert(Type == MV_CAPTURE || Type == MV_QUIET || Type == MV_NON_EVASION);
assert(!pos.in_check()); assert(!pos.in_check());
Color us = pos.side_to_move(); Color us = pos.side_to_move();
@@ -348,23 +300,25 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
if (Type == MV_CAPTURE) if (Type == MV_CAPTURE)
target = pos.pieces(~us); target = pos.pieces(~us);
else if (Type == MV_NON_CAPTURE) else if (Type == MV_QUIET)
target = pos.empty_squares(); target = ~pos.pieces();
else if (Type == MV_NON_EVASION) else if (Type == MV_NON_EVASION)
target = pos.pieces(~us) | pos.empty_squares(); target = ~pos.pieces(us);
mlist = generate_piece_moves<PAWN, Type>(pos, mlist, us, target); mlist = (us == WHITE ? generate_pawn_moves<WHITE, Type>(pos, mlist, target)
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target); : generate_pawn_moves<BLACK, Type>(pos, mlist, target));
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target); mlist = generate_moves<KNIGHT>(pos, mlist, us, target);
mlist = generate_piece_moves<QUEEN>(pos, mlist, us, target); mlist = generate_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<KING>(pos, mlist, us, target); mlist = generate_moves<ROOK>(pos, mlist, us, target);
mlist = generate_moves<QUEEN>(pos, mlist, us, target);
mlist = generate_moves<KING>(pos, mlist, us, target);
if (Type != MV_CAPTURE && pos.can_castle(us)) if (Type != MV_CAPTURE && pos.can_castle(us))
{ {
mlist = generate_castle_moves<KING_SIDE, false>(pos, mlist, us); mlist = generate_castle<KING_SIDE, false>(pos, mlist, us);
mlist = generate_castle_moves<QUEEN_SIDE, false>(pos, mlist, us); mlist = generate_castle<QUEEN_SIDE, false>(pos, mlist, us);
} }
return mlist; return mlist;
@@ -372,14 +326,14 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) {
// Explicit template instantiations // Explicit template instantiations
template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist); template MoveStack* generate<MV_CAPTURE>(const Position& pos, MoveStack* mlist);
template MoveStack* generate<MV_NON_CAPTURE>(const Position& pos, MoveStack* mlist); template MoveStack* generate<MV_QUIET>(const Position& pos, MoveStack* mlist);
template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist); template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
/// generate<MV_NON_CAPTURE_CHECK> generates all pseudo-legal non-captures and knight /// generate<MV_QUIET_CHECK> generates all pseudo-legal non-captures and knight
/// underpromotions that give check. Returns a pointer to the end of the move list. /// underpromotions that give check. Returns a pointer to the end of the move list.
template<> template<>
MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist) { MoveStack* generate<MV_QUIET_CHECK>(const Position& pos, MoveStack* mlist) {
assert(!pos.in_check()); assert(!pos.in_check());
@@ -395,7 +349,7 @@ MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist)
if (pt == PAWN) if (pt == PAWN)
continue; // Will be generated togheter with direct checks continue; // Will be generated togheter with direct checks
Bitboard b = pos.attacks_from(Piece(pt), from) & pos.empty_squares(); Bitboard b = pos.attacks_from(Piece(pt), from) & ~pos.pieces();
if (pt == KING) if (pt == KING)
b &= ~PseudoAttacks[QUEEN][ci.ksq]; b &= ~PseudoAttacks[QUEEN][ci.ksq];
@@ -403,7 +357,9 @@ MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist)
SERIALIZE(b); SERIALIZE(b);
} }
mlist = generate_direct_checks<PAWN>(pos, mlist, us, ci); mlist = (us == WHITE ? generate_pawn_moves<WHITE, MV_QUIET_CHECK>(pos, mlist, ci.dcCandidates, ci.ksq)
: generate_pawn_moves<BLACK, MV_QUIET_CHECK>(pos, mlist, ci.dcCandidates, ci.ksq));
mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, ci); mlist = generate_direct_checks<KNIGHT>(pos, mlist, us, ci);
mlist = generate_direct_checks<BISHOP>(pos, mlist, us, ci); mlist = generate_direct_checks<BISHOP>(pos, mlist, us, ci);
mlist = generate_direct_checks<ROOK>(pos, mlist, us, ci); mlist = generate_direct_checks<ROOK>(pos, mlist, us, ci);
@@ -411,8 +367,8 @@ MoveStack* generate<MV_NON_CAPTURE_CHECK>(const Position& pos, MoveStack* mlist)
if (pos.can_castle(us)) if (pos.can_castle(us))
{ {
mlist = generate_castle_moves<KING_SIDE, true>(pos, mlist, us); mlist = generate_castle<KING_SIDE, true>(pos, mlist, us);
mlist = generate_castle_moves<QUEEN_SIDE, true>(pos, mlist, us); mlist = generate_castle<QUEEN_SIDE, true>(pos, mlist, us);
} }
return mlist; return mlist;
@@ -454,7 +410,7 @@ MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
// If queen and king are far or not on a diagonal line we can safely // If queen and king are far or not on a diagonal line we can safely
// remove all the squares attacked in the other direction becuase are // remove all the squares attacked in the other direction becuase are
// not reachable by the king anyway. // not reachable by the king anyway.
if (squares_between(ksq, checksq) || !bit_is_set(PseudoAttacks[BISHOP][checksq], ksq)) if (between_bb(ksq, checksq) || !(PseudoAttacks[BISHOP][checksq] & ksq))
sliderAttacks |= PseudoAttacks[QUEEN][checksq]; sliderAttacks |= PseudoAttacks[QUEEN][checksq];
// Otherwise we need to use real rook attacks to check if king is safe // Otherwise we need to use real rook attacks to check if king is safe
@@ -478,13 +434,15 @@ MoveStack* generate<MV_EVASION>(const Position& pos, MoveStack* mlist) {
return mlist; return mlist;
// Blocking evasions or captures of the checking piece // Blocking evasions or captures of the checking piece
target = squares_between(checksq, ksq) | checkers; target = between_bb(checksq, ksq) | checkers;
mlist = generate_piece_moves<PAWN, MV_EVASION>(pos, mlist, us, target); mlist = (us == WHITE ? generate_pawn_moves<WHITE, MV_EVASION>(pos, mlist, target)
mlist = generate_piece_moves<KNIGHT>(pos, mlist, us, target); : generate_pawn_moves<BLACK, MV_EVASION>(pos, mlist, target));
mlist = generate_piece_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_piece_moves<ROOK>(pos, mlist, us, target); mlist = generate_moves<KNIGHT>(pos, mlist, us, target);
return generate_piece_moves<QUEEN>(pos, mlist, us, target); mlist = generate_moves<BISHOP>(pos, mlist, us, target);
mlist = generate_moves<ROOK>(pos, mlist, us, target);
return generate_moves<QUEEN>(pos, mlist, us, target);
} }

View File

@@ -24,8 +24,8 @@
enum MoveType { enum MoveType {
MV_CAPTURE, MV_CAPTURE,
MV_NON_CAPTURE, MV_QUIET,
MV_NON_CAPTURE_CHECK, MV_QUIET_CHECK,
MV_EVASION, MV_EVASION,
MV_NON_EVASION, MV_NON_EVASION,
MV_LEGAL MV_LEGAL

View File

@@ -23,39 +23,24 @@
#include "movegen.h" #include "movegen.h"
#include "movepick.h" #include "movepick.h"
#include "search.h"
#include "types.h"
namespace { namespace {
enum MovegenPhase { enum Sequencer {
PH_TT_MOVE, // Transposition table move MAIN_SEARCH, CAPTURES_S1, KILLERS_S1, QUIETS_1_S1, QUIETS_2_S1, BAD_CAPTURES_S1,
PH_GOOD_CAPTURES, // Queen promotions and captures with SEE values >= captureThreshold (captureThreshold <= 0) EVASION, EVASIONS_S2,
PH_GOOD_PROBCUT, // Queen promotions and captures with SEE values > captureThreshold (captureThreshold >= 0) QSEARCH_0, CAPTURES_S3, QUIET_CHECKS_S3,
PH_KILLERS, // Killer moves from the current ply QSEARCH_1, CAPTURES_S4,
PH_NONCAPTURES_1, // Non-captures and underpromotions with positive score PROBCUT, CAPTURES_S5,
PH_NONCAPTURES_2, // Non-captures and underpromotions with non-positive score RECAPTURE, CAPTURES_S6,
PH_BAD_CAPTURES, // Queen promotions and captures with SEE values < captureThreshold (captureThreshold <= 0) STOP
PH_EVASIONS, // Check evasions
PH_QCAPTURES, // Captures in quiescence search
PH_QRECAPTURES, // Recaptures in quiescence search
PH_QCHECKS, // Non-capture checks in quiescence search
PH_STOP
}; };
CACHE_LINE_ALIGNMENT
const uint8_t MainSearchTable[] = { PH_TT_MOVE, PH_GOOD_CAPTURES, PH_KILLERS, PH_NONCAPTURES_1, PH_NONCAPTURES_2, PH_BAD_CAPTURES, PH_STOP };
const uint8_t EvasionTable[] = { PH_TT_MOVE, PH_EVASIONS, PH_STOP };
const uint8_t QsearchWithChecksTable[] = { PH_TT_MOVE, PH_QCAPTURES, PH_QCHECKS, PH_STOP };
const uint8_t QsearchWithoutChecksTable[] = { PH_TT_MOVE, PH_QCAPTURES, PH_STOP };
const uint8_t QsearchRecapturesTable[] = { PH_TT_MOVE, PH_QRECAPTURES, PH_STOP };
const uint8_t ProbCutTable[] = { PH_TT_MOVE, PH_GOOD_PROBCUT, PH_STOP };
// Unary predicate used by std::partition to split positive scores from remaining // Unary predicate used by std::partition to split positive scores from remaining
// ones so to sort separately the two sets, and with the second sort delayed. // ones so to sort separately the two sets, and with the second sort delayed.
inline bool has_positive_score(const MoveStack& move) { return move.score > 0; } inline bool has_positive_score(const MoveStack& move) { return move.score > 0; }
// Picks and pushes to the front the best move in range [firstMove, lastMove), // Picks and moves to the front the best move in the range [firstMove, lastMove),
// it is faster than sorting all the moves in advance when moves are few, as // it is faster than sorting all the moves in advance when moves are few, as
// normally are the possible captures. // normally are the possible captures.
inline MoveStack* pick_best(MoveStack* firstMove, MoveStack* lastMove) inline MoveStack* pick_best(MoveStack* firstMove, MoveStack* lastMove)
@@ -65,7 +50,8 @@ namespace {
} }
} }
/// Constructors for the MovePicker class. As arguments we pass information
/// Constructors of the MovePicker class. As arguments we pass information
/// to help it to return the presumably good moves first, to decide which /// to help it to return the presumably good moves first, to decide which
/// moves to return (in the quiescence search, for instance, we only want to /// moves to return (in the quiescence search, for instance, we only want to
/// search captures, promotions and some checks) and about how important good /// search captures, promotions and some checks) and about how important good
@@ -73,18 +59,20 @@ namespace {
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
Search::Stack* ss, Value beta) : pos(p), H(h), depth(d) { Search::Stack* ss, Value beta) : pos(p), H(h), depth(d) {
captureThreshold = 0;
badCaptures = moves + MAX_MOVES;
assert(d > DEPTH_ZERO); assert(d > DEPTH_ZERO);
captureThreshold = 0;
curMove = lastMove = moves;
lastBadCapture = moves + MAX_MOVES - 1;
if (p.in_check()) if (p.in_check())
{ phase = EVASION;
killers[0].move = killers[1].move = MOVE_NONE;
phasePtr = EvasionTable;
}
else else
{ {
phase = MAIN_SEARCH;
killers[0].move = ss->killers[0]; killers[0].move = ss->killers[0];
killers[1].move = ss->killers[1]; killers[1].move = ss->killers[1];
@@ -95,137 +83,59 @@ MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
// Consider negative captures as good if still enough to reach beta // Consider negative captures as good if still enough to reach beta
else if (ss && ss->eval > beta) else if (ss && ss->eval > beta)
captureThreshold = beta - ss->eval; captureThreshold = beta - ss->eval;
phasePtr = MainSearchTable;
} }
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE); ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
phasePtr += int(ttMove == MOVE_NONE) - 1; lastMove += (ttMove != MOVE_NONE);
go_next_phase();
} }
MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h, Square recaptureSq) MovePicker::MovePicker(const Position& p, Move ttm, Depth d, const History& h,
: pos(p), H(h) { Square sq) : pos(p), H(h), curMove(moves), lastMove(moves) {
assert(d <= DEPTH_ZERO); assert(d <= DEPTH_ZERO);
if (p.in_check()) if (p.in_check())
phasePtr = EvasionTable; phase = EVASION;
else if (d >= DEPTH_QS_CHECKS)
phasePtr = QsearchWithChecksTable;
else if (d >= DEPTH_QS_RECAPTURES)
{
phasePtr = QsearchWithoutChecksTable;
// Skip TT move if is not a capture or a promotion, this avoids else if (d > DEPTH_QS_NO_CHECKS)
// qsearch tree explosion due to a possible perpetual check or phase = QSEARCH_0;
// similar rare cases when TT table is full.
if (ttm != MOVE_NONE && !pos.is_capture_or_promotion(ttm)) else if (d > DEPTH_QS_RECAPTURES)
{
phase = QSEARCH_1;
// Skip TT move if is not a capture or a promotion, this avoids qsearch
// tree explosion due to a possible perpetual check or similar rare cases
// when TT table is full.
if (ttm && !pos.is_capture_or_promotion(ttm))
ttm = MOVE_NONE; ttm = MOVE_NONE;
} }
else else
{ {
phasePtr = QsearchRecapturesTable; phase = RECAPTURE;
recaptureSquare = recaptureSq; recaptureSquare = sq;
ttm = MOVE_NONE; ttm = MOVE_NONE;
} }
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE); ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
phasePtr += int(ttMove == MOVE_NONE) - 1; lastMove += (ttMove != MOVE_NONE);
go_next_phase();
} }
MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType parentCapture) MovePicker::MovePicker(const Position& p, Move ttm, const History& h, PieceType pt)
: pos(p), H(h) { : pos(p), H(h), curMove(moves), lastMove(moves) {
assert (!pos.in_check()); assert(!pos.in_check());
// In ProbCut we consider only captures better than parent's move phase = PROBCUT;
captureThreshold = PieceValueMidgame[Piece(parentCapture)];
phasePtr = ProbCutTable;
if ( ttm != MOVE_NONE
&& (!pos.is_capture(ttm) || pos.see(ttm) <= captureThreshold))
ttm = MOVE_NONE;
// In ProbCut we generate only captures better than parent's captured piece
captureThreshold = PieceValueMidgame[pt];
ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE); ttMove = (ttm && pos.is_pseudo_legal(ttm) ? ttm : MOVE_NONE);
phasePtr += int(ttMove == MOVE_NONE) - 1;
go_next_phase();
}
if (ttMove && (!pos.is_capture(ttMove) || pos.see(ttMove) <= captureThreshold))
ttMove = MOVE_NONE;
/// MovePicker::go_next_phase() generates, scores and sorts the next bunch lastMove += (ttMove != MOVE_NONE);
/// of moves when there are no more moves to try for the current phase.
void MovePicker::go_next_phase() {
curMove = moves;
phase = *(++phasePtr);
switch (phase) {
case PH_TT_MOVE:
lastMove = curMove + 1;
return;
case PH_GOOD_CAPTURES:
case PH_GOOD_PROBCUT:
lastMove = generate<MV_CAPTURE>(pos, moves);
score_captures();
return;
case PH_KILLERS:
curMove = killers;
lastMove = curMove + 2;
return;
case PH_NONCAPTURES_1:
lastNonCapture = lastMove = generate<MV_NON_CAPTURE>(pos, moves);
score_noncaptures();
lastMove = std::partition(curMove, lastMove, has_positive_score);
sort<MoveStack>(curMove, lastMove);
return;
case PH_NONCAPTURES_2:
curMove = lastMove;
lastMove = lastNonCapture;
if (depth >= 3 * ONE_PLY)
sort<MoveStack>(curMove, lastMove);
return;
case PH_BAD_CAPTURES:
// Bad captures SEE value is already calculated so just pick
// them in order to get SEE move ordering.
curMove = badCaptures;
lastMove = moves + MAX_MOVES;
return;
case PH_EVASIONS:
assert(pos.in_check());
lastMove = generate<MV_EVASION>(pos, moves);
score_evasions();
return;
case PH_QCAPTURES:
lastMove = generate<MV_CAPTURE>(pos, moves);
score_captures();
return;
case PH_QRECAPTURES:
lastMove = generate<MV_CAPTURE>(pos, moves);
return;
case PH_QCHECKS:
lastMove = generate<MV_NON_CAPTURE_CHECK>(pos, moves);
return;
case PH_STOP:
lastMove = curMove + 1; // Avoid another go_next_phase() call
return;
default:
assert(false);
return;
}
} }
@@ -250,7 +160,6 @@ void MovePicker::score_captures() {
// some SEE calls in case we get a cutoff (idea from Pablo Vazquez). // some SEE calls in case we get a cutoff (idea from Pablo Vazquez).
Move m; Move m;
// Use MVV/LVA ordering
for (MoveStack* cur = moves; cur != lastMove; cur++) for (MoveStack* cur = moves; cur != lastMove; cur++)
{ {
m = cur->move; m = cur->move;
@@ -258,32 +167,28 @@ void MovePicker::score_captures() {
- type_of(pos.piece_moved(m)); - type_of(pos.piece_moved(m));
if (is_promotion(m)) if (is_promotion(m))
cur->score += PieceValueMidgame[Piece(promotion_piece_type(m))]; cur->score += PieceValueMidgame[promotion_type(m)];
} }
} }
void MovePicker::score_noncaptures() { void MovePicker::score_noncaptures() {
Move m; Move m;
Square from;
for (MoveStack* cur = moves; cur != lastMove; cur++) for (MoveStack* cur = moves; cur != lastMove; cur++)
{ {
m = cur->move; m = cur->move;
from = from_sq(m); cur->score = H.value(pos.piece_moved(m), to_sq(m));
cur->score = H.value(pos.piece_on(from), to_sq(m));
} }
} }
void MovePicker::score_evasions() { void MovePicker::score_evasions() {
// Try good captures ordered by MVV/LVA, then non-captures if // Try good captures ordered by MVV/LVA, then non-captures if destination square
// destination square is not under attack, ordered by history // is not under attack, ordered by history value, then bad-captures and quiet
// value, and at the end bad-captures and non-captures with a // moves with a negative SEE. This last group is ordered by the SEE score.
// negative SEE. This last group is ordered by the SEE score.
Move m; Move m;
int seeScore; int seeScore;
// Skip if we don't have at least two moves to order
if (lastMove < moves + 2) if (lastMove < moves + 2)
return; return;
@@ -300,6 +205,67 @@ void MovePicker::score_evasions() {
} }
} }
/// MovePicker::generate_next() generates, scores and sorts the next bunch of moves,
/// when there are no more moves to try for the current phase.
void MovePicker::generate_next() {
curMove = moves;
switch (++phase) {
case CAPTURES_S1: case CAPTURES_S3: case CAPTURES_S4: case CAPTURES_S5: case CAPTURES_S6:
lastMove = generate<MV_CAPTURE>(pos, moves);
score_captures();
return;
case KILLERS_S1:
curMove = killers;
lastMove = curMove + 2;
return;
case QUIETS_1_S1:
lastQuiet = lastMove = generate<MV_QUIET>(pos, moves);
score_noncaptures();
lastMove = std::partition(curMove, lastMove, has_positive_score);
sort<MoveStack>(curMove, lastMove);
return;
case QUIETS_2_S1:
curMove = lastMove;
lastMove = lastQuiet;
if (depth >= 3 * ONE_PLY)
sort<MoveStack>(curMove, lastMove);
return;
case BAD_CAPTURES_S1:
// Just pick them in reverse order to get MVV/LVA ordering
curMove = moves + MAX_MOVES - 1;
lastMove = lastBadCapture;
return;
case EVASIONS_S2:
lastMove = generate<MV_EVASION>(pos, moves);
score_evasions();
return;
case QUIET_CHECKS_S3:
lastMove = generate<MV_QUIET_CHECK>(pos, moves);
return;
case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT: case RECAPTURE:
phase = STOP;
case STOP:
lastMove = curMove + 1; // Avoid another next_phase() call
return;
default:
assert(false);
}
}
/// MovePicker::next_move() is the most important method of the MovePicker class. /// MovePicker::next_move() is the most important method of the MovePicker class.
/// It returns a new pseudo legal move every time it is called, until there /// It returns a new pseudo legal move every time it is called, until there
/// are no more moves left. It picks the move with the biggest score from a list /// are no more moves left. It picks the move with the biggest score from a list
@@ -314,50 +280,38 @@ Move MovePicker::next_move() {
while (true) while (true)
{ {
while (curMove == lastMove) while (curMove == lastMove)
go_next_phase(); generate_next();
switch (phase) { switch (phase) {
case PH_TT_MOVE: case MAIN_SEARCH: case EVASION: case QSEARCH_0: case QSEARCH_1: case PROBCUT:
curMove++; curMove++;
return ttMove; return ttMove;
break;
case PH_GOOD_CAPTURES: case CAPTURES_S1:
move = pick_best(curMove++, lastMove)->move; move = pick_best(curMove++, lastMove)->move;
if (move != ttMove) if (move != ttMove)
{ {
assert(captureThreshold <= 0); // Otherwise we must use see instead of see_sign assert(captureThreshold <= 0); // Otherwise we cannot use see_sign()
// Check for a non negative SEE now if (pos.see_sign(move) >= captureThreshold)
int seeValue = pos.see_sign(move);
if (seeValue >= captureThreshold)
return move; return move;
// Losing capture, move it to the tail of the array // Losing capture, move it to the tail of the array
(--badCaptures)->move = move; (lastBadCapture--)->move = move;
badCaptures->score = seeValue;
} }
break; break;
case PH_GOOD_PROBCUT: case KILLERS_S1:
move = pick_best(curMove++, lastMove)->move;
if ( move != ttMove
&& pos.see(move) > captureThreshold)
return move;
break;
case PH_KILLERS:
move = (curMove++)->move; move = (curMove++)->move;
if ( move != MOVE_NONE if ( move != MOVE_NONE
&& pos.is_pseudo_legal(move) && pos.is_pseudo_legal(move)
&& move != ttMove && move != ttMove
&& !pos.is_capture(move)) && !pos.is_capture(move))
return move; return move;
break; break;
case PH_NONCAPTURES_1: case QUIETS_1_S1: case QUIETS_2_S1:
case PH_NONCAPTURES_2:
move = (curMove++)->move; move = (curMove++)->move;
if ( move != ttMove if ( move != ttMove
&& move != killers[0].move && move != killers[0].move
@@ -365,35 +319,38 @@ Move MovePicker::next_move() {
return move; return move;
break; break;
case PH_BAD_CAPTURES: case BAD_CAPTURES_S1:
move = pick_best(curMove++, lastMove)->move; return (curMove--)->move;
return move;
case PH_EVASIONS: case EVASIONS_S2: case CAPTURES_S3: case CAPTURES_S4:
case PH_QCAPTURES:
move = pick_best(curMove++, lastMove)->move; move = pick_best(curMove++, lastMove)->move;
if (move != ttMove) if (move != ttMove)
return move; return move;
break; break;
case PH_QRECAPTURES: case CAPTURES_S5:
move = (curMove++)->move; move = pick_best(curMove++, lastMove)->move;
if (move != ttMove && pos.see(move) > captureThreshold)
return move;
break;
case CAPTURES_S6:
move = pick_best(curMove++, lastMove)->move;
if (to_sq(move) == recaptureSquare) if (to_sq(move) == recaptureSquare)
return move; return move;
break; break;
case PH_QCHECKS: case QUIET_CHECKS_S3:
move = (curMove++)->move; move = (curMove++)->move;
if (move != ttMove) if (move != ttMove)
return move; return move;
break; break;
case PH_STOP: case STOP:
return MOVE_NONE; return MOVE_NONE;
default: default:
assert(false); assert(false);
break;
} }
} }
} }

View File

@@ -25,13 +25,13 @@
#include "search.h" #include "search.h"
#include "types.h" #include "types.h"
/// MovePicker is a class which is used to pick one pseudo legal move at a time
/// from the current position. It is initialized with a Position object and a few /// MovePicker class is used to pick one pseudo legal move at a time from the
/// moves we have reason to believe are good. The most important method is /// current position. The most important method is next_move(), which returns a
/// MovePicker::next_move(), which returns a new pseudo legal move each time /// new pseudo legal move each time it is called, until there are no moves left,
/// it is called, until there are no moves left, when MOVE_NONE is returned. /// when MOVE_NONE is returned. In order to improve the efficiency of the alpha
/// In order to improve the efficiency of the alpha beta algorithm, MovePicker /// beta algorithm, MovePicker attempts to return the moves which are most likely
/// attempts to return the moves which are most likely to get a cut-off first. /// to get a cut-off first.
class MovePicker { class MovePicker {
@@ -39,15 +39,15 @@ class MovePicker {
public: public:
MovePicker(const Position&, Move, Depth, const History&, Search::Stack*, Value); MovePicker(const Position&, Move, Depth, const History&, Search::Stack*, Value);
MovePicker(const Position&, Move, Depth, const History&, Square recaptureSq); MovePicker(const Position&, Move, Depth, const History&, Square);
MovePicker(const Position&, Move, const History&, PieceType parentCapture); MovePicker(const Position&, Move, const History&, PieceType);
Move next_move(); Move next_move();
private: private:
void score_captures(); void score_captures();
void score_noncaptures(); void score_noncaptures();
void score_evasions(); void score_evasions();
void go_next_phase(); void generate_next();
const Position& pos; const Position& pos;
const History& H; const History& H;
@@ -56,8 +56,7 @@ private:
MoveStack killers[2]; MoveStack killers[2];
Square recaptureSquare; Square recaptureSquare;
int captureThreshold, phase; int captureThreshold, phase;
const uint8_t* phasePtr; MoveStack *curMove, *lastMove, *lastQuiet, *lastBadCapture;
MoveStack *curMove, *lastMove, *lastNonCapture, *badCaptures;
MoveStack moves[MAX_MOVES]; MoveStack moves[MAX_MOVES];
}; };

View File

@@ -26,6 +26,7 @@
namespace { namespace {
#define V Value
#define S(mg, eg) make_score(mg, eg) #define S(mg, eg) make_score(mg, eg)
// Doubled pawn penalty by opposed flag and file // Doubled pawn penalty by opposed flag and file
@@ -63,58 +64,65 @@ namespace {
const Score PawnStructureWeight = S(233, 201); const Score PawnStructureWeight = S(233, 201);
#undef S // Weakness of our pawn shelter in front of the king indexed by [king pawn][rank]
const Value ShelterWeakness[2][8] =
{ { V(141), V(0), V(38), V(102), V(128), V(141), V(141) },
{ V( 61), V(0), V(16), V( 44), V( 56), V( 61), V( 61) } };
inline Score apply_weight(Score v, Score w) { // Danger of enemy pawns moving toward our king indexed by [pawn blocked][rank]
return make_score((int(mg_value(v)) * mg_value(w)) / 0x100, const Value StormDanger[2][8] =
(int(eg_value(v)) * eg_value(w)) / 0x100); { { V(26), V(0), V(128), V(51), V(26) },
} { V(13), V(0), V( 64), V(25), V(13) } };
// Max bonus for king safety. Corresponds to start position with all the pawns
// in front of the king and no enemy pawn on the horizont.
const Value MaxSafetyBonus = V(263);
#undef S
#undef V
} }
/// PawnInfoTable::pawn_info() takes a position object as input, computes /// PawnTable::probe() takes a position object as input, computes a PawnEntry
/// a PawnInfo object, and returns a pointer to it. The result is also stored /// object, and returns a pointer to it. The result is also stored in a hash
/// in an hash table, so we don't have to recompute everything when the same /// table, so we don't have to recompute everything when the same pawn structure
/// pawn structure occurs again. /// occurs again.
PawnInfo* PawnInfoTable::pawn_info(const Position& pos) const { PawnEntry* PawnTable::probe(const Position& pos) {
Key key = pos.pawn_key(); Key key = pos.pawn_key();
PawnInfo* pi = probe(key); PawnEntry* e = entries[key];
// If pi->key matches the position's pawn hash key, it means that we // If e->key matches the position's pawn hash key, it means that we
// have analysed this pawn structure before, and we can simply return // have analysed this pawn structure before, and we can simply return
// the information we found the last time instead of recomputing it. // the information we found the last time instead of recomputing it.
if (pi->key == key) if (e->key == key)
return pi; return e;
// Initialize PawnInfo entry e->key = key;
pi->key = key; e->passedPawns[WHITE] = e->passedPawns[BLACK] = 0;
pi->passedPawns[WHITE] = pi->passedPawns[BLACK] = 0; e->kingSquares[WHITE] = e->kingSquares[BLACK] = SQ_NONE;
pi->kingSquares[WHITE] = pi->kingSquares[BLACK] = SQ_NONE; e->halfOpenFiles[WHITE] = e->halfOpenFiles[BLACK] = 0xFF;
pi->halfOpenFiles[WHITE] = pi->halfOpenFiles[BLACK] = 0xFF;
// Calculate pawn attacks Bitboard wPawns = pos.pieces(WHITE, PAWN);
Bitboard wPawns = pos.pieces(PAWN, WHITE); Bitboard bPawns = pos.pieces(BLACK, PAWN);
Bitboard bPawns = pos.pieces(PAWN, BLACK); e->pawnAttacks[WHITE] = ((wPawns & ~FileHBB) << 9) | ((wPawns & ~FileABB) << 7);
pi->pawnAttacks[WHITE] = ((wPawns << 9) & ~FileABB) | ((wPawns << 7) & ~FileHBB); e->pawnAttacks[BLACK] = ((bPawns & ~FileHBB) >> 7) | ((bPawns & ~FileABB) >> 9);
pi->pawnAttacks[BLACK] = ((bPawns >> 7) & ~FileABB) | ((bPawns >> 9) & ~FileHBB);
// Evaluate pawns for both colors and weight the result e->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, e)
pi->value = evaluate_pawns<WHITE>(pos, wPawns, bPawns, pi) - evaluate_pawns<BLACK>(pos, bPawns, wPawns, e);
- evaluate_pawns<BLACK>(pos, bPawns, wPawns, pi);
pi->value = apply_weight(pi->value, PawnStructureWeight); e->value = apply_weight(e->value, PawnStructureWeight);
return pi; return e;
} }
/// PawnInfoTable::evaluate_pawns() evaluates each pawn of the given color /// PawnTable::evaluate_pawns() evaluates each pawn of the given color
template<Color Us> template<Color Us>
Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns, Score PawnTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
Bitboard theirPawns, PawnInfo* pi) { Bitboard theirPawns, PawnEntry* e) {
const Color Them = (Us == WHITE ? BLACK : WHITE); const Color Them = (Us == WHITE ? BLACK : WHITE);
@@ -135,32 +143,32 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
r = rank_of(s); r = rank_of(s);
// This file cannot be half open // This file cannot be half open
pi->halfOpenFiles[Us] &= ~(1 << f); e->halfOpenFiles[Us] &= ~(1 << f);
// Our rank plus previous one. Used for chain detection // Our rank plus previous one. Used for chain detection
b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1)); b = rank_bb(r) | rank_bb(Us == WHITE ? r - Rank(1) : r + Rank(1));
// Flag the pawn as passed, isolated, doubled or member of a pawn // Flag the pawn as passed, isolated, doubled or member of a pawn
// chain (but not the backward one). // chain (but not the backward one).
chain = ourPawns & adjacent_files_bb(f) & b;
isolated = !(ourPawns & adjacent_files_bb(f));
doubled = ourPawns & forward_bb(Us, s);
opposed = theirPawns & forward_bb(Us, s);
passed = !(theirPawns & passed_pawn_mask(Us, s)); passed = !(theirPawns & passed_pawn_mask(Us, s));
doubled = ourPawns & squares_in_front_of(Us, s);
opposed = theirPawns & squares_in_front_of(Us, s);
isolated = !(ourPawns & neighboring_files_bb(f));
chain = ourPawns & neighboring_files_bb(f) & b;
// Test for backward pawn // Test for backward pawn
backward = false; backward = false;
// If the pawn is passed, isolated, or member of a pawn chain it cannot // If the pawn is passed, isolated, or member of a pawn chain it cannot
// be backward. If there are friendly pawns behind on neighboring files // be backward. If there are friendly pawns behind on adjacent files
// or if can capture an enemy pawn it cannot be backward either. // or if can capture an enemy pawn it cannot be backward either.
if ( !(passed | isolated | chain) if ( !(passed | isolated | chain)
&& !(ourPawns & attack_span_mask(Them, s)) && !(ourPawns & attack_span_mask(Them, s))
&& !(pos.attacks_from<PAWN>(s, Us) & theirPawns)) && !(pos.attacks_from<PAWN>(s, Us) & theirPawns))
{ {
// We now know that there are no friendly pawns beside or behind this // We now know that there are no friendly pawns beside or behind this
// pawn on neighboring files. We now check whether the pawn is // pawn on adjacent files. We now check whether the pawn is
// backward by looking in the forward direction on the neighboring // backward by looking in the forward direction on the adjacent
// files, and seeing whether we meet a friendly or an enemy pawn first. // files, and seeing whether we meet a friendly or an enemy pawn first.
b = pos.attacks_from<PAWN>(s, Us); b = pos.attacks_from<PAWN>(s, Us);
@@ -178,8 +186,8 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
// A not passed pawn is a candidate to become passed if it is free to // A not passed pawn is a candidate to become passed if it is free to
// advance and if the number of friendly pawns beside or behind this // advance and if the number of friendly pawns beside or behind this
// pawn on neighboring files is higher or equal than the number of // pawn on adjacent files is higher or equal than the number of
// enemy pawns in the forward direction on the neighboring files. // enemy pawns in the forward direction on the adjacent files.
candidate = !(opposed | passed | backward | isolated) candidate = !(opposed | passed | backward | isolated)
&& (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0 && (b = attack_span_mask(Them, s + pawn_push(Us)) & ourPawns) != 0
&& popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns); && popcount<Max15>(b) >= popcount<Max15>(attack_span_mask(Us, s) & theirPawns);
@@ -188,7 +196,7 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
// full attack info to evaluate passed pawns. Only the frontmost passed // full attack info to evaluate passed pawns. Only the frontmost passed
// pawn on each file is considered a true passed pawn. // pawn on each file is considered a true passed pawn.
if (passed && !doubled) if (passed && !doubled)
set_bit(&(pi->passedPawns[Us]), s); e->passedPawns[Us] |= s;
// Score this pawn // Score this pawn
if (isolated) if (isolated)
@@ -206,35 +214,69 @@ Score PawnInfoTable::evaluate_pawns(const Position& pos, Bitboard ourPawns,
if (candidate) if (candidate)
value += CandidateBonus[relative_rank(Us, s)]; value += CandidateBonus[relative_rank(Us, s)];
} }
return value; return value;
} }
/// PawnInfo::updateShelter() calculates and caches king shelter. It is called /// PawnEntry::shelter_storm() calculates shelter and storm penalties for the file
/// only when king square changes, about 20% of total king_shelter() calls. /// the king is on, as well as the two adjacent files.
template<Color Us> template<Color Us>
Score PawnInfo::updateShelter(const Position& pos, Square ksq) { Value PawnEntry::shelter_storm(const Position& pos, Square ksq) {
const int Shift = (Us == WHITE ? 8 : -8); const Color Them = (Us == WHITE ? BLACK : WHITE);
Bitboard pawns; Value safety = MaxSafetyBonus;
int r, shelter = 0; Bitboard b = pos.pieces(PAWN) & (in_front_bb(Us, ksq) | rank_bb(ksq));
Bitboard ourPawns = b & pos.pieces(Us) & ~rank_bb(ksq);
Bitboard theirPawns = b & pos.pieces(Them);
Rank rkUs, rkThem;
File kf = file_of(ksq);
if (relative_rank(Us, ksq) <= RANK_4) kf = (kf == FILE_A) ? kf++ : (kf == FILE_H) ? kf-- : kf;
for (int f = kf - 1; f <= kf + 1; f++)
{ {
pawns = pos.pieces(PAWN, Us) & this_and_neighboring_files_bb(file_of(ksq)); // Shelter penalty is higher for the pawn in front of the king
r = ksq & (7 << 3); b = ourPawns & FileBB[f];
for (int i = 0; i < 3; i++) rkUs = b ? rank_of(Us == WHITE ? first_1(b) : ~last_1(b)) : RANK_1;
{ safety -= ShelterWeakness[f != kf][rkUs];
r += Shift;
shelter += BitCount8Bit[(pawns >> r) & 0xFF] << (6 - i); // Storm danger is smaller if enemy pawn is blocked
} b = theirPawns & FileBB[f];
rkThem = b ? rank_of(Us == WHITE ? first_1(b) : ~last_1(b)) : RANK_1;
safety -= StormDanger[rkThem == rkUs + 1][rkThem];
} }
return safety;
}
/// PawnEntry::update_safety() calculates and caches a bonus for king safety. It is
/// called only when king square changes, about 20% of total king_safety() calls.
template<Color Us>
Score PawnEntry::update_safety(const Position& pos, Square ksq) {
kingSquares[Us] = ksq; kingSquares[Us] = ksq;
kingShelters[Us] = make_score(shelter, 0); castleRights[Us] = pos.can_castle(Us);
return kingShelters[Us];
if (relative_rank(Us, ksq) > RANK_4)
return kingSafety[Us] = SCORE_ZERO;
Value bonus = shelter_storm<Us>(pos, ksq);
// If we can castle use the bonus after the castle if is bigger
if (pos.can_castle(make_castle_right(Us, KING_SIDE)))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_G1)));
if (pos.can_castle(make_castle_right(Us, QUEEN_SIDE)))
bonus = std::max(bonus, shelter_storm<Us>(pos, relative_square(Us, SQ_C1)));
return kingSafety[Us] = make_score(bonus, 0);
} }
// Explicit template instantiation // Explicit template instantiation
template Score PawnInfo::updateShelter<WHITE>(const Position& pos, Square ksq); template Score PawnEntry::update_safety<WHITE>(const Position& pos, Square ksq);
template Score PawnInfo::updateShelter<BLACK>(const Position& pos, Square ksq); template Score PawnEntry::update_safety<BLACK>(const Position& pos, Square ksq);

View File

@@ -20,22 +20,22 @@
#if !defined(PAWNS_H_INCLUDED) #if !defined(PAWNS_H_INCLUDED)
#define PAWNS_H_INCLUDED #define PAWNS_H_INCLUDED
#include "misc.h"
#include "position.h" #include "position.h"
#include "tt.h"
#include "types.h" #include "types.h"
const int PawnTableSize = 16384; const int PawnTableSize = 16384;
/// PawnInfo is a class which contains various information about a pawn /// PawnEntry is a class which contains various information about a pawn
/// structure. Currently, it only includes a middle game and an end game /// structure. Currently, it only includes a middle game and an end game
/// pawn structure evaluation, and a bitboard of passed pawns. We may want /// pawn structure evaluation, and a bitboard of passed pawns. We may want
/// to add further information in the future. A lookup to the pawn hash /// to add further information in the future. A lookup to the pawn hash
/// table (performed by calling the pawn_info method in a PawnInfoTable /// table (performed by calling the probe method in a PawnTable object)
/// object) returns a pointer to a PawnInfo object. /// returns a pointer to a PawnEntry object.
class PawnInfo { class PawnEntry {
friend class PawnInfoTable; friend struct PawnTable;
public: public:
Score pawns_value() const; Score pawns_value() const;
@@ -46,62 +46,69 @@ public:
int has_open_file_to_right(Color c, File f) const; int has_open_file_to_right(Color c, File f) const;
template<Color Us> template<Color Us>
Score king_shelter(const Position& pos, Square ksq); Score king_safety(const Position& pos, Square ksq);
private: private:
template<Color Us> template<Color Us>
Score updateShelter(const Position& pos, Square ksq); Score update_safety(const Position& pos, Square ksq);
template<Color Us>
Value shelter_storm(const Position& pos, Square ksq);
Key key; Key key;
Bitboard passedPawns[2]; Bitboard passedPawns[2];
Bitboard pawnAttacks[2]; Bitboard pawnAttacks[2];
Square kingSquares[2]; Square kingSquares[2];
int castleRights[2];
Score value; Score value;
int halfOpenFiles[2]; int halfOpenFiles[2];
Score kingShelters[2]; Score kingSafety[2];
}; };
/// The PawnInfoTable class represents a pawn hash table. The most important /// The PawnTable class represents a pawn hash table. The most important
/// method is pawn_info, which returns a pointer to a PawnInfo object. /// method is probe, which returns a pointer to a PawnEntry object.
class PawnInfoTable : public SimpleHash<PawnInfo, PawnTableSize> { struct PawnTable {
public:
PawnInfo* pawn_info(const Position& pos) const; PawnEntry* probe(const Position& pos);
private:
template<Color Us> template<Color Us>
static Score evaluate_pawns(const Position& pos, Bitboard ourPawns, Bitboard theirPawns, PawnInfo* pi); static Score evaluate_pawns(const Position& pos, Bitboard ourPawns,
Bitboard theirPawns, PawnEntry* e);
HashTable<PawnEntry, PawnTableSize> entries;
}; };
inline Score PawnInfo::pawns_value() const { inline Score PawnEntry::pawns_value() const {
return value; return value;
} }
inline Bitboard PawnInfo::pawn_attacks(Color c) const { inline Bitboard PawnEntry::pawn_attacks(Color c) const {
return pawnAttacks[c]; return pawnAttacks[c];
} }
inline Bitboard PawnInfo::passed_pawns(Color c) const { inline Bitboard PawnEntry::passed_pawns(Color c) const {
return passedPawns[c]; return passedPawns[c];
} }
inline int PawnInfo::file_is_half_open(Color c, File f) const { inline int PawnEntry::file_is_half_open(Color c, File f) const {
return halfOpenFiles[c] & (1 << int(f)); return halfOpenFiles[c] & (1 << int(f));
} }
inline int PawnInfo::has_open_file_to_left(Color c, File f) const { inline int PawnEntry::has_open_file_to_left(Color c, File f) const {
return halfOpenFiles[c] & ((1 << int(f)) - 1); return halfOpenFiles[c] & ((1 << int(f)) - 1);
} }
inline int PawnInfo::has_open_file_to_right(Color c, File f) const { inline int PawnEntry::has_open_file_to_right(Color c, File f) const {
return halfOpenFiles[c] & ~((1 << int(f+1)) - 1); return halfOpenFiles[c] & ~((1 << int(f+1)) - 1);
} }
template<Color Us> template<Color Us>
inline Score PawnInfo::king_shelter(const Position& pos, Square ksq) { inline Score PawnEntry::king_safety(const Position& pos, Square ksq) {
return kingSquares[Us] == ksq ? kingShelters[Us] : updateShelter<Us>(pos, ksq); return kingSquares[Us] == ksq && castleRights[Us] == pos.can_castle(Us)
? kingSafety[Us] : update_safety<Us>(pos, ksq);
} }
#endif // !defined(PAWNS_H_INCLUDED) #endif // !defined(PAWNS_H_INCLUDED)

View File

@@ -0,0 +1,109 @@
/*
Stockfish, a UCI chess playing engine derived from Glaurung 2.1
Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
Copyright (C) 2008-2012 Marco Costalba, Joona Kiiski, Tord Romstad
Stockfish is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Stockfish is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined(PLATFORM_H_INCLUDED)
#define PLATFORM_H_INCLUDED
#if defined(_MSC_VER)
// Disable some silly and noisy warning from MSVC compiler
#pragma warning(disable: 4127) // Conditional expression is constant
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
#pragma warning(disable: 4996) // Function _ftime() may be unsafe
// MSVC does not support <inttypes.h>
typedef signed __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef signed __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
# include <inttypes.h>
#endif
#if !defined(_WIN32) && !defined(_WIN64) // Linux - Unix
# include <sys/time.h>
typedef timeval sys_time_t;
inline void system_time(sys_time_t* t) { gettimeofday(t, NULL); }
inline uint64_t time_to_msec(const sys_time_t& t) { return t.tv_sec * 1000LL + t.tv_usec / 1000; }
# include <pthread.h>
typedef pthread_mutex_t Lock;
typedef pthread_cond_t WaitCondition;
typedef pthread_t NativeHandle;
typedef void*(*pt_start_fn)(void*);
# define lock_init(x) pthread_mutex_init(&(x), NULL)
# define lock_grab(x) pthread_mutex_lock(&(x))
# define lock_release(x) pthread_mutex_unlock(&(x))
# define lock_destroy(x) pthread_mutex_destroy(&(x))
# define cond_destroy(x) pthread_cond_destroy(&(x))
# define cond_init(x) pthread_cond_init(&(x), NULL)
# define cond_signal(x) pthread_cond_signal(&(x))
# define cond_wait(x,y) pthread_cond_wait(&(x),&(y))
# define cond_timedwait(x,y,z) pthread_cond_timedwait(&(x),&(y),z)
# define thread_create(x,f,t) !pthread_create(&(x),NULL,(pt_start_fn)f,t)
# define thread_join(x) pthread_join(x, NULL)
#else // Windows and MinGW
# include <sys/timeb.h>
typedef _timeb sys_time_t;
inline void system_time(sys_time_t* t) { _ftime(t); }
inline uint64_t time_to_msec(const sys_time_t& t) { return t.time * 1000LL + t.millitm; }
#if !defined(NOMINMAX)
# define NOMINMAX // disable macros min() and max()
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN
#undef NOMINMAX
// We use critical sections on Windows to support Windows XP and older versions,
// unfortunatly cond_wait() is racy between lock_release() and WaitForSingleObject()
// but apart from this they have the same speed performance of SRW locks.
typedef CRITICAL_SECTION Lock;
typedef HANDLE WaitCondition;
typedef HANDLE NativeHandle;
# define lock_init(x) InitializeCriticalSection(&(x))
# define lock_grab(x) EnterCriticalSection(&(x))
# define lock_release(x) LeaveCriticalSection(&(x))
# define lock_destroy(x) DeleteCriticalSection(&(x))
# define cond_init(x) { x = CreateEvent(0, FALSE, FALSE, 0); }
# define cond_destroy(x) CloseHandle(x)
# define cond_signal(x) SetEvent(x)
# define cond_wait(x,y) { lock_release(y); WaitForSingleObject(x, INFINITE); lock_grab(y); }
# define cond_timedwait(x,y,z) { lock_release(y); WaitForSingleObject(x,z); lock_grab(y); }
# define thread_create(x,f,t) (x = CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)f,t,0,NULL), x != NULL)
# define thread_join(x) { WaitForSingleObject(x, INFINITE); CloseHandle(x); }
#endif
#endif // !defined(PLATFORM_H_INCLUDED)

File diff suppressed because it is too large Load Diff

View File

@@ -29,6 +29,7 @@
/// The checkInfo struct is initialized at c'tor time and keeps info used /// The checkInfo struct is initialized at c'tor time and keeps info used
/// to detect if a move gives check. /// to detect if a move gives check.
class Position; class Position;
class Thread;
struct CheckInfo { struct CheckInfo {
@@ -50,7 +51,7 @@ struct StateInfo {
Key pawnKey, materialKey; Key pawnKey, materialKey;
Value npMaterial[2]; Value npMaterial[2];
int castleRights, rule50, pliesFromNull; int castleRights, rule50, pliesFromNull;
Score value; Score psqScore;
Square epSquare; Square epSquare;
Key key; Key key;
@@ -59,6 +60,14 @@ struct StateInfo {
StateInfo* previous; StateInfo* previous;
}; };
struct ReducedStateInfo {
Key pawnKey, materialKey;
Value npMaterial[2];
int castleRights, rule50, pliesFromNull;
Score psqScore;
Square epSquare;
};
/// The position data structure. A position consists of the following data: /// The position data structure. A position consists of the following data:
/// ///
@@ -83,65 +92,45 @@ struct StateInfo {
/// * A counter for detecting 50 move rule draws. /// * A counter for detecting 50 move rule draws.
class Position { class Position {
// No copy c'tor or assignment operator allowed
Position(const Position&);
Position& operator=(const Position&);
public: public:
Position() {} Position() {}
Position(const Position& pos, int th) { copy(pos, th); } Position(const Position& p) { *this = p; }
Position(const std::string& fen, bool isChess960, int th); Position(const Position& p, Thread* t) { *this = p; thisThread = t; }
Position(const std::string& f, bool c960, Thread* t) { from_fen(f, c960, t); }
void operator=(const Position&);
// Text input/output // Text input/output
void copy(const Position& pos, int th); void from_fen(const std::string& fen, bool isChess960, Thread* th);
void from_fen(const std::string& fen, bool isChess960);
const std::string to_fen() const; const std::string to_fen() const;
void print(Move m = MOVE_NONE) const; void print(Move m = MOVE_NONE) const;
// The piece on a given square // Position representation
Piece piece_on(Square s) const; Bitboard pieces() const;
Piece piece_moved(Move m) const;
bool square_is_empty(Square s) const;
// Side to move
Color side_to_move() const;
// Bitboard representation of the position
Bitboard empty_squares() const;
Bitboard occupied_squares() const;
Bitboard pieces(Color c) const;
Bitboard pieces(PieceType pt) const; Bitboard pieces(PieceType pt) const;
Bitboard pieces(PieceType pt, Color c) const;
Bitboard pieces(PieceType pt1, PieceType pt2) const; Bitboard pieces(PieceType pt1, PieceType pt2) const;
Bitboard pieces(PieceType pt1, PieceType pt2, Color c) const; Bitboard pieces(Color c) const;
Bitboard pieces(Color c, PieceType pt) const;
// Number of pieces of each color and type Bitboard pieces(Color c, PieceType pt1, PieceType pt2) const;
Piece piece_on(Square s) const;
Square king_square(Color c) const;
Square ep_square() const;
bool is_empty(Square s) const;
const Square* piece_list(Color c, PieceType pt) const;
int piece_count(Color c, PieceType pt) const; int piece_count(Color c, PieceType pt) const;
// The en passant square // Castling
Square ep_square() const; int can_castle(CastleRight f) const;
int can_castle(Color c) const;
bool castle_impeded(Color c, CastlingSide s) const;
Square castle_rook_square(Color c, CastlingSide s) const;
// Current king position for each color // Checking
Square king_square(Color c) const; bool in_check() const;
Bitboard checkers() const;
// Castling rights
bool can_castle(CastleRight f) const;
bool can_castle(Color c) const;
Square castle_rook_square(CastleRight f) const;
// Bitboards for pinned pieces and discovered check candidates
Bitboard discovered_check_candidates() const; Bitboard discovered_check_candidates() const;
Bitboard pinned_pieces() const; Bitboard pinned_pieces() const;
// Checking pieces and under check information // Attacks to/from a given square
Bitboard checkers() const;
bool in_check() const;
// Piece lists
const Square* piece_list(Color c, PieceType pt) const;
// Information about attacks to or from a given square
Bitboard attackers_to(Square s) const; Bitboard attackers_to(Square s) const;
Bitboard attackers_to(Square s, Bitboard occ) const; Bitboard attackers_to(Square s, Bitboard occ) const;
Bitboard attacks_from(Piece p, Square s) const; Bitboard attacks_from(Piece p, Square s) const;
@@ -157,12 +146,14 @@ public:
bool is_capture(Move m) const; bool is_capture(Move m) const;
bool is_capture_or_promotion(Move m) const; bool is_capture_or_promotion(Move m) const;
bool is_passed_pawn_push(Move m) const; bool is_passed_pawn_push(Move m) const;
Piece piece_moved(Move m) const;
// Piece captured with previous moves
PieceType captured_piece_type() const; PieceType captured_piece_type() const;
// Information about pawns // Piece specific
bool pawn_is_passed(Color c, Square s) const; bool pawn_is_passed(Color c, Square s) const;
bool pawn_on_7th(Color c) const;
bool opposite_bishops() const;
bool bishop_pair(Color c) const;
// Doing and undoing moves // Doing and undoing moves
void do_move(Move m, StateInfo& st); void do_move(Move m, StateInfo& st);
@@ -180,37 +171,32 @@ public:
Key pawn_key() const; Key pawn_key() const;
Key material_key() const; Key material_key() const;
// Incremental evaluation // Incremental piece-square evaluation
Score value() const; Score psq_score() const;
Score psq_delta(Piece p, Square from, Square to) const;
Value non_pawn_material(Color c) const; Value non_pawn_material(Color c) const;
Score pst_delta(Piece piece, Square from, Square to) const;
// Other properties of the position // Other properties of the position
template<bool SkipRepetition> bool is_draw() const; Color side_to_move() const;
int startpos_ply_counter() const; int startpos_ply_counter() const;
bool opposite_colored_bishops() const;
bool has_pawn_on_7th(Color c) const;
bool is_chess960() const; bool is_chess960() const;
Thread* this_thread() const;
// Current thread ID searching on the position
int thread() const;
int64_t nodes_searched() const; int64_t nodes_searched() const;
void set_nodes_searched(int64_t n); void set_nodes_searched(int64_t n);
template<bool SkipRepetition> bool is_draw() const;
// Position consistency check, for debugging // Position consistency check, for debugging
bool pos_is_ok(int* failedStep = NULL) const; bool pos_is_ok(int* failedStep = NULL) const;
void flip_me(); void flip();
// Global initialization // Global initialization
static void init(); static void init();
private: private:
// Initialization helpers (used while setting up a position)
// Initialization helper functions (used while setting up a position)
void clear(); void clear();
void put_piece(Piece p, Square s); void put_piece(Piece p, Square s);
void set_castle_right(Color c, Square rsq); void set_castle_right(Color c, Square rfrom);
bool move_is_legal(const Move m) const; bool move_is_legal(const Move m) const;
// Helper template functions // Helper template functions
@@ -223,40 +209,33 @@ private:
Key compute_material_key() const; Key compute_material_key() const;
// Computing incremental evaluation scores and material counts // Computing incremental evaluation scores and material counts
Score pst(Piece p, Square s) const; Score compute_psq_score() const;
Score compute_value() const;
Value compute_non_pawn_material(Color c) const; Value compute_non_pawn_material(Color c) const;
// Board // Board and pieces
Piece board[64]; // [square] Piece board[64]; // [square]
// Bitboards
Bitboard byTypeBB[8]; // [pieceType] Bitboard byTypeBB[8]; // [pieceType]
Bitboard byColorBB[2]; // [color] Bitboard byColorBB[2]; // [color]
Bitboard occupied;
// Piece counts
int pieceCount[2][8]; // [color][pieceType] int pieceCount[2][8]; // [color][pieceType]
// Piece lists
Square pieceList[2][8][16]; // [color][pieceType][index] Square pieceList[2][8][16]; // [color][pieceType][index]
int index[64]; // [square] int index[64]; // [square]
// Other info // Other info
int castleRightsMask[64]; // [square] int castleRightsMask[64]; // [square]
Square castleRookSquare[16]; // [castleRight] Square castleRookSquare[2][2]; // [color][side]
Bitboard castlePath[2][2]; // [color][side]
StateInfo startState; StateInfo startState;
int64_t nodes; int64_t nodes;
int startPosPly; int startPosPly;
Color sideToMove; Color sideToMove;
int threadID; Thread* thisThread;
StateInfo* st; StateInfo* st;
int chess960; int chess960;
// Static variables // Static variables
static Score pieceSquareTable[16][64]; // [piece][square] static Score pieceSquareTable[16][64]; // [piece][square]
static Key zobrist[2][8][64]; // [color][pieceType][square]/[piece count] static Key zobrist[2][8][64]; // [color][pieceType][square]/[piece count]
static Key zobEp[64]; // [square] static Key zobEp[8]; // [file]
static Key zobCastle[16]; // [castleRight] static Key zobCastle[16]; // [castleRight]
static Key zobSideToMove; static Key zobSideToMove;
static Key zobExclusion; static Key zobExclusion;
@@ -278,7 +257,7 @@ inline Piece Position::piece_moved(Move m) const {
return board[from_sq(m)]; return board[from_sq(m)];
} }
inline bool Position::square_is_empty(Square s) const { inline bool Position::is_empty(Square s) const {
return board[s] == NO_PIECE; return board[s] == NO_PIECE;
} }
@@ -286,32 +265,28 @@ inline Color Position::side_to_move() const {
return sideToMove; return sideToMove;
} }
inline Bitboard Position::occupied_squares() const { inline Bitboard Position::pieces() const {
return occupied; return byTypeBB[ALL_PIECES];
}
inline Bitboard Position::empty_squares() const {
return ~occupied;
}
inline Bitboard Position::pieces(Color c) const {
return byColorBB[c];
} }
inline Bitboard Position::pieces(PieceType pt) const { inline Bitboard Position::pieces(PieceType pt) const {
return byTypeBB[pt]; return byTypeBB[pt];
} }
inline Bitboard Position::pieces(PieceType pt, Color c) const {
return byTypeBB[pt] & byColorBB[c];
}
inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const { inline Bitboard Position::pieces(PieceType pt1, PieceType pt2) const {
return byTypeBB[pt1] | byTypeBB[pt2]; return byTypeBB[pt1] | byTypeBB[pt2];
} }
inline Bitboard Position::pieces(PieceType pt1, PieceType pt2, Color c) const { inline Bitboard Position::pieces(Color c) const {
return (byTypeBB[pt1] | byTypeBB[pt2]) & byColorBB[c]; return byColorBB[c];
}
inline Bitboard Position::pieces(Color c, PieceType pt) const {
return byColorBB[c] & byTypeBB[pt];
}
inline Bitboard Position::pieces(Color c, PieceType pt1, PieceType pt2) const {
return byColorBB[c] & (byTypeBB[pt1] | byTypeBB[pt2]);
} }
inline int Position::piece_count(Color c, PieceType pt) const { inline int Position::piece_count(Color c, PieceType pt) const {
@@ -330,16 +305,28 @@ inline Square Position::king_square(Color c) const {
return pieceList[c][KING][0]; return pieceList[c][KING][0];
} }
inline bool Position::can_castle(CastleRight f) const { inline int Position::can_castle(CastleRight f) const {
return st->castleRights & f; return st->castleRights & f;
} }
inline bool Position::can_castle(Color c) const { inline int Position::can_castle(Color c) const {
return st->castleRights & ((WHITE_OO | WHITE_OOO) << c); return st->castleRights & ((WHITE_OO | WHITE_OOO) << (2 * c));
} }
inline Square Position::castle_rook_square(CastleRight f) const { inline bool Position::castle_impeded(Color c, CastlingSide s) const {
return castleRookSquare[f]; return byTypeBB[ALL_PIECES] & castlePath[c][s];
}
inline Square Position::castle_rook_square(Color c, CastlingSide s) const {
return castleRookSquare[c][s];
}
template<PieceType Pt>
inline Bitboard Position::attacks_from(Square s) const {
return Pt == BISHOP || Pt == ROOK ? attacks_bb<Pt>(s, pieces())
: Pt == QUEEN ? attacks_from<ROOK>(s) | attacks_from<BISHOP>(s)
: StepAttacksBB[Pt][s];
} }
template<> template<>
@@ -347,32 +334,12 @@ inline Bitboard Position::attacks_from<PAWN>(Square s, Color c) const {
return StepAttacksBB[make_piece(c, PAWN)][s]; return StepAttacksBB[make_piece(c, PAWN)][s];
} }
template<PieceType Piece> // Knight and King and white pawns
inline Bitboard Position::attacks_from(Square s) const {
return StepAttacksBB[Piece][s];
}
template<>
inline Bitboard Position::attacks_from<BISHOP>(Square s) const {
return bishop_attacks_bb(s, occupied_squares());
}
template<>
inline Bitboard Position::attacks_from<ROOK>(Square s) const {
return rook_attacks_bb(s, occupied_squares());
}
template<>
inline Bitboard Position::attacks_from<QUEEN>(Square s) const {
return attacks_from<ROOK>(s) | attacks_from<BISHOP>(s);
}
inline Bitboard Position::attacks_from(Piece p, Square s) const { inline Bitboard Position::attacks_from(Piece p, Square s) const {
return attacks_from(p, s, occupied_squares()); return attacks_from(p, s, byTypeBB[ALL_PIECES]);
} }
inline Bitboard Position::attackers_to(Square s) const { inline Bitboard Position::attackers_to(Square s) const {
return attackers_to(s, occupied_squares()); return attackers_to(s, byTypeBB[ALL_PIECES]);
} }
inline Bitboard Position::checkers() const { inline Bitboard Position::checkers() const {
@@ -392,7 +359,7 @@ inline Bitboard Position::pinned_pieces() const {
} }
inline bool Position::pawn_is_passed(Color c, Square s) const { inline bool Position::pawn_is_passed(Color c, Square s) const {
return !(pieces(PAWN, ~c) & passed_pawn_mask(c, s)); return !(pieces(~c, PAWN) & passed_pawn_mask(c, s));
} }
inline Key Position::key() const { inline Key Position::key() const {
@@ -411,16 +378,12 @@ inline Key Position::material_key() const {
return st->materialKey; return st->materialKey;
} }
inline Score Position::pst(Piece p, Square s) const { inline Score Position::psq_delta(Piece p, Square from, Square to) const {
return pieceSquareTable[p][s]; return pieceSquareTable[p][to] - pieceSquareTable[p][from];
} }
inline Score Position::pst_delta(Piece piece, Square from, Square to) const { inline Score Position::psq_score() const {
return pieceSquareTable[piece][to] - pieceSquareTable[piece][from]; return st->psqScore;
}
inline Score Position::value() const {
return st->value;
} }
inline Value Position::non_pawn_material(Color c) const { inline Value Position::non_pawn_material(Color c) const {
@@ -429,7 +392,7 @@ inline Value Position::non_pawn_material(Color c) const {
inline bool Position::is_passed_pawn_push(Move m) const { inline bool Position::is_passed_pawn_push(Move m) const {
return board[from_sq(m)] == make_piece(sideToMove, PAWN) return type_of(piece_moved(m)) == PAWN
&& pawn_is_passed(sideToMove, to_sq(m)); && pawn_is_passed(sideToMove, to_sq(m));
} }
@@ -437,15 +400,21 @@ inline int Position::startpos_ply_counter() const {
return startPosPly + st->pliesFromNull; // HACK return startPosPly + st->pliesFromNull; // HACK
} }
inline bool Position::opposite_colored_bishops() const { inline bool Position::opposite_bishops() const {
return pieceCount[WHITE][BISHOP] == 1 return pieceCount[WHITE][BISHOP] == 1
&& pieceCount[BLACK][BISHOP] == 1 && pieceCount[BLACK][BISHOP] == 1
&& opposite_colors(pieceList[WHITE][BISHOP][0], pieceList[BLACK][BISHOP][0]); && opposite_colors(pieceList[WHITE][BISHOP][0], pieceList[BLACK][BISHOP][0]);
} }
inline bool Position::has_pawn_on_7th(Color c) const { inline bool Position::bishop_pair(Color c) const {
return pieces(PAWN, c) & rank_bb(relative_rank(c, RANK_7));
return pieceCount[c][BISHOP] >= 2
&& opposite_colors(pieceList[c][BISHOP][0], pieceList[c][BISHOP][1]);
}
inline bool Position::pawn_on_7th(Color c) const {
return pieces(c, PAWN) & rank_bb(relative_rank(c, RANK_7));
} }
inline bool Position::is_chess960() const { inline bool Position::is_chess960() const {
@@ -455,22 +424,22 @@ inline bool Position::is_chess960() const {
inline bool Position::is_capture_or_promotion(Move m) const { inline bool Position::is_capture_or_promotion(Move m) const {
assert(is_ok(m)); assert(is_ok(m));
return is_special(m) ? !is_castle(m) : !square_is_empty(to_sq(m)); return is_special(m) ? !is_castle(m) : !is_empty(to_sq(m));
} }
inline bool Position::is_capture(Move m) const { inline bool Position::is_capture(Move m) const {
// Note that castle is coded as "king captures the rook" // Note that castle is coded as "king captures the rook"
assert(is_ok(m)); assert(is_ok(m));
return (!square_is_empty(to_sq(m)) && !is_castle(m)) || is_enpassant(m); return (!is_empty(to_sq(m)) && !is_castle(m)) || is_enpassant(m);
} }
inline PieceType Position::captured_piece_type() const { inline PieceType Position::captured_piece_type() const {
return st->capturedType; return st->capturedType;
} }
inline int Position::thread() const { inline Thread* Position::this_thread() const {
return threadID; return thisThread;
} }
#endif // !defined(POSITION_H_INCLUDED) #endif // !defined(POSITION_H_INCLUDED)

File diff suppressed because it is too large Load Diff

View File

@@ -23,6 +23,7 @@
#include <cstring> #include <cstring>
#include <vector> #include <vector>
#include "misc.h"
#include "types.h" #include "types.h"
class Position; class Position;
@@ -39,7 +40,6 @@ struct Stack {
int ply; int ply;
Move currentMove; Move currentMove;
Move excludedMove; Move excludedMove;
Move bestMove;
Move killers[2]; Move killers[2];
Depth reduction; Depth reduction;
Value eval; Value eval;
@@ -78,9 +78,9 @@ struct RootMove {
struct LimitsType { struct LimitsType {
LimitsType() { memset(this, 0, sizeof(LimitsType)); } LimitsType() { memset(this, 0, sizeof(LimitsType)); }
bool use_time_management() const { return !(maxTime | maxDepth | maxNodes | infinite); } bool use_time_management() const { return !(movetime | depth | nodes | infinite); }
int time, increment, movesToGo, maxTime, maxDepth, maxNodes, infinite, ponder; int time[2], inc[2], movestogo, depth, nodes, movetime, infinite, ponder;
}; };
@@ -95,6 +95,7 @@ extern volatile SignalsType Signals;
extern LimitsType Limits; extern LimitsType Limits;
extern std::vector<RootMove> RootMoves; extern std::vector<RootMove> RootMoves;
extern Position RootPosition; extern Position RootPosition;
extern Time SearchTime;
extern void init(); extern void init();
extern int64_t perft(Position& pos, Depth depth); extern int64_t perft(Position& pos, Depth depth);

View File

@@ -17,6 +17,7 @@
along with this program. If not, see <http://www.gnu.org/licenses/>. along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include <cassert>
#include <iostream> #include <iostream>
#include "movegen.h" #include "movegen.h"
@@ -31,225 +32,261 @@ ThreadsManager Threads; // Global object
namespace { extern "C" { namespace { extern "C" {
// start_routine() is the C function which is called when a new thread // start_routine() is the C function which is called when a new thread
// is launched. It simply calls idle_loop() of the supplied thread. The first // is launched. It is a wrapper to member function pointed by start_fn.
// and last thread are special. First one is the main search thread while the
// last one mimics a timer, they run in main_loop() and timer_loop().
#if defined(_MSC_VER) long start_routine(Thread* th) { (th->*(th->start_fn))(); return 0; }
DWORD WINAPI start_routine(LPVOID thread) {
#else
void* start_routine(void* thread) {
#endif
Thread* th = (Thread*)thread;
if (th->threadID == 0)
th->main_loop();
else if (th->threadID == MAX_THREADS)
th->timer_loop();
else
th->idle_loop(NULL);
return 0;
}
} } } }
// wake_up() wakes up the thread, normally at the beginning of the search or, // Thread c'tor starts a newly-created thread of execution that will call
// if "sleeping threads" is used, when there is some work to do. // the idle loop function pointed by start_fn going immediately to sleep.
void Thread::wake_up() { Thread::Thread(Fn fn) {
lock_grab(&sleepLock); is_searching = do_exit = false;
cond_signal(&sleepCond); maxPly = splitPointsCnt = 0;
lock_release(&sleepLock); curSplitPoint = NULL;
start_fn = fn;
idx = Threads.size();
do_sleep = (fn != &Thread::main_loop); // Avoid a race with start_searching()
lock_init(sleepLock);
cond_init(sleepCond);
for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
lock_init(splitPoints[j].lock);
if (!thread_create(handle, start_routine, this))
{
std::cerr << "Failed to create thread number " << idx << std::endl;
::exit(EXIT_FAILURE);
}
} }
// cutoff_occurred() checks whether a beta cutoff has occurred in the current // Thread d'tor waits for thread termination before to return.
// active split point, or in some ancestor of the split point.
Thread::~Thread() {
assert(do_sleep);
do_exit = true; // Search must be already finished
wake_up();
thread_join(handle); // Wait for thread termination
lock_destroy(sleepLock);
cond_destroy(sleepCond);
for (int j = 0; j < MAX_SPLITPOINTS_PER_THREAD; j++)
lock_destroy(splitPoints[j].lock);
}
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
// then calls check_time(). If maxPly is 0 thread sleeps until is woken up.
extern void check_time();
void Thread::timer_loop() {
while (!do_exit)
{
lock_grab(sleepLock);
timed_wait(sleepCond, sleepLock, maxPly ? maxPly : INT_MAX);
lock_release(sleepLock);
check_time();
}
}
// Thread::main_loop() is where the main thread is parked waiting to be started
// when there is a new search. Main thread will launch all the slave threads.
void Thread::main_loop() {
while (true)
{
lock_grab(sleepLock);
do_sleep = true; // Always return to sleep after a search
is_searching = false;
while (do_sleep && !do_exit)
{
cond_signal(Threads.sleepCond); // Wake up UI thread if needed
cond_wait(sleepCond, sleepLock);
}
lock_release(sleepLock);
if (do_exit)
return;
is_searching = true;
Search::think();
}
}
// Thread::wake_up() wakes up the thread, normally at the beginning of the search
// or, if "sleeping threads" is used at split time.
void Thread::wake_up() {
lock_grab(sleepLock);
cond_signal(sleepCond);
lock_release(sleepLock);
}
// Thread::wait_for_stop_or_ponderhit() is called when the maximum depth is
// reached while the program is pondering. The point is to work around a wrinkle
// in the UCI protocol: When pondering, the engine is not allowed to give a
// "bestmove" before the GUI sends it a "stop" or "ponderhit" command. We simply
// wait here until one of these commands (that raise StopRequest) is sent and
// then return, after which the bestmove and pondermove will be printed.
void Thread::wait_for_stop_or_ponderhit() {
Signals.stopOnPonderhit = true;
lock_grab(sleepLock);
while (!Signals.stop) cond_wait(sleepCond, sleepLock);
lock_release(sleepLock);
}
// Thread::cutoff_occurred() checks whether a beta cutoff has occurred in the
// current active split point, or in some ancestor of the split point.
bool Thread::cutoff_occurred() const { bool Thread::cutoff_occurred() const {
for (SplitPoint* sp = splitPoint; sp; sp = sp->parent) for (SplitPoint* sp = curSplitPoint; sp; sp = sp->parent)
if (sp->is_betaCutoff) if (sp->cutoff)
return true; return true;
return false; return false;
} }
// is_available_to() checks whether the thread is available to help the thread with // Thread::is_available_to() checks whether the thread is available to help the
// threadID "master" at a split point. An obvious requirement is that thread must be // thread 'master' at a split point. An obvious requirement is that thread must
// idle. With more than two threads, this is not by itself sufficient: If the thread // be idle. With more than two threads, this is not sufficient: If the thread is
// is the master of some active split point, it is only available as a slave to the // the master of some active split point, it is only available as a slave to the
// threads which are busy searching the split point at the top of "slave"'s split // slaves which are busy searching the split point at the top of slaves split
// point stack (the "helpful master concept" in YBWC terminology). // point stack (the "helpful master concept" in YBWC terminology).
bool Thread::is_available_to(int master) const { bool Thread::is_available_to(Thread* master) const {
if (is_searching) if (is_searching)
return false; return false;
// Make a local copy to be sure doesn't become zero under our feet while // Make a local copy to be sure doesn't become zero under our feet while
// testing next condition and so leading to an out of bound access. // testing next condition and so leading to an out of bound access.
int localActiveSplitPoints = activeSplitPoints; int spCnt = splitPointsCnt;
// No active split points means that the thread is available as a slave for any // No active split points means that the thread is available as a slave for any
// other thread otherwise apply the "helpful master" concept if possible. // other thread otherwise apply the "helpful master" concept if possible.
if ( !localActiveSplitPoints return !spCnt || (splitPoints[spCnt - 1].slavesMask & (1ULL << master->idx));
|| splitPoints[localActiveSplitPoints - 1].is_slave[master])
return true;
return false;
} }
// read_uci_options() updates number of active threads and other parameters // init() is called at startup. Initializes lock and condition variable and
// according to the UCI options values. It is called before to start a new search. // launches requested threads sending them immediately to sleep. We cannot use
// a c'tor becuase Threads is a static object and we need a fully initialized
// engine at this point due to allocation of endgames in Thread c'tor.
void ThreadsManager::init() {
cond_init(sleepCond);
lock_init(splitLock);
timer = new Thread(&Thread::timer_loop);
threads.push_back(new Thread(&Thread::main_loop));
read_uci_options();
}
// d'tor cleanly terminates the threads when the program exits.
ThreadsManager::~ThreadsManager() {
for (int i = 0; i < size(); i++)
delete threads[i];
delete timer;
lock_destroy(splitLock);
cond_destroy(sleepCond);
}
// read_uci_options() updates internal threads parameters from the corresponding
// UCI options and creates/destroys threads to match the requested number. Thread
// objects are dynamically allocated to avoid creating in advance all possible
// threads, with included pawns and material tables, if only few are used.
void ThreadsManager::read_uci_options() { void ThreadsManager::read_uci_options() {
maxThreadsPerSplitPoint = Options["Max Threads per Split Point"]; maxThreadsPerSplitPoint = Options["Max Threads per Split Point"];
minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY; minimumSplitDepth = Options["Min Split Depth"] * ONE_PLY;
useSleepingThreads = Options["Use Sleeping Threads"]; useSleepingThreads = Options["Use Sleeping Threads"];
int requested = Options["Threads"];
set_size(Options["Threads"]); assert(requested > 0);
}
while (size() < requested)
threads.push_back(new Thread(&Thread::idle_loop));
// set_size() changes the number of active threads and raises do_sleep flag for while (size() > requested)
// all the unused threads that will go immediately to sleep.
void ThreadsManager::set_size(int cnt) {
assert(cnt > 0 && cnt <= MAX_THREADS);
activeThreads = cnt;
for (int i = 1; i < MAX_THREADS; i++) // Ignore main thread
if (i < activeThreads)
{
// Dynamically allocate pawn and material hash tables according to the
// number of active threads. This avoids preallocating memory for all
// possible threads if only few are used.
threads[i].pawnTable.init();
threads[i].materialTable.init();
threads[i].do_sleep = false;
}
else
threads[i].do_sleep = true;
}
// init() is called during startup. Initializes locks and condition variables
// and launches all threads sending them immediately to sleep.
void ThreadsManager::init() {
// Initialize sleep condition and lock used by thread manager
cond_init(&sleepCond);
lock_init(&threadsLock);
// Initialize thread's sleep conditions and split point locks
for (int i = 0; i <= MAX_THREADS; i++)
{ {
lock_init(&threads[i].sleepLock); delete threads.back();
cond_init(&threads[i].sleepCond); threads.pop_back();
for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
lock_init(&(threads[i].splitPoints[j].lock));
}
// Allocate main thread tables to call evaluate() also when not searching
threads[0].pawnTable.init();
threads[0].materialTable.init();
// Create and launch all the threads, threads will go immediately to sleep
for (int i = 0; i <= MAX_THREADS; i++)
{
threads[i].is_searching = false;
threads[i].do_sleep = (i != 0); // Avoid a race with start_thinking()
threads[i].threadID = i;
#if defined(_MSC_VER)
threads[i].handle = CreateThread(NULL, 0, start_routine, &threads[i], 0, NULL);
bool ok = (threads[i].handle != NULL);
#else
bool ok = !pthread_create(&threads[i].handle, NULL, start_routine, &threads[i]);
#endif
if (!ok)
{
std::cerr << "Failed to create thread number " << i << std::endl;
::exit(EXIT_FAILURE);
}
} }
} }
// exit() is called to cleanly terminate the threads when the program finishes // wake_up() is called before a new search to start the threads that are waiting
// on the sleep condition and to reset maxPly. When useSleepingThreads is set
// threads will be woken up at split time.
void ThreadsManager::exit() { void ThreadsManager::wake_up() const {
for (int i = 0; i <= MAX_THREADS; i++) for (int i = 0; i < size(); i++)
{ {
threads[i].do_terminate = true; // Search must be already finished threads[i]->maxPly = 0;
threads[i].wake_up(); threads[i]->do_sleep = false;
// Wait for thread termination if (!useSleepingThreads)
#if defined(_MSC_VER) threads[i]->wake_up();
WaitForSingleObject(threads[i].handle, INFINITE);
CloseHandle(threads[i].handle);
#else
pthread_join(threads[i].handle, NULL);
#endif
// Now we can safely destroy associated locks and wait conditions
lock_destroy(&threads[i].sleepLock);
cond_destroy(&threads[i].sleepCond);
for (int j = 0; j < MAX_ACTIVE_SPLIT_POINTS; j++)
lock_destroy(&(threads[i].splitPoints[j].lock));
} }
}
lock_destroy(&threadsLock);
cond_destroy(&sleepCond); // sleep() is called after the search finishes to ask all the threads but the
// main one to go waiting on a sleep condition.
void ThreadsManager::sleep() const {
for (int i = 1; i < size(); i++) // Main thread will go to sleep by itself
threads[i]->do_sleep = true; // to avoid a race with start_searching()
} }
// available_slave_exists() tries to find an idle thread which is available as // available_slave_exists() tries to find an idle thread which is available as
// a slave for the thread with threadID 'master'. // a slave for the thread 'master'.
bool ThreadsManager::available_slave_exists(int master) const { bool ThreadsManager::available_slave_exists(Thread* master) const {
assert(master >= 0 && master < activeThreads); for (int i = 0; i < size(); i++)
if (threads[i]->is_available_to(master))
for (int i = 0; i < activeThreads; i++)
if (threads[i].is_available_to(master))
return true; return true;
return false; return false;
} }
// split_point_finished() checks if all the slave threads of a given split
// point have finished searching.
bool ThreadsManager::split_point_finished(SplitPoint* sp) const {
for (int i = 0; i < activeThreads; i++)
if (sp->is_slave[i])
return false;
return true;
}
// split() does the actual work of distributing the work at a node between // split() does the actual work of distributing the work at a node between
// several available threads. If it does not succeed in splitting the node // several available threads. If it does not succeed in splitting the node
// (because no idle threads are available, or because we have no unused split // (because no idle threads are available, or because we have no unused split
@@ -261,32 +298,29 @@ bool ThreadsManager::split_point_finished(SplitPoint* sp) const {
template <bool Fake> template <bool Fake>
Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta, Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
Value bestValue, Depth depth, Move threatMove, Value bestValue, Move* bestMove, Depth depth,
int moveCount, MovePicker* mp, int nodeType) { Move threatMove, int moveCount, MovePicker* mp, int nodeType) {
assert(pos.pos_is_ok()); assert(pos.pos_is_ok());
assert(bestValue > -VALUE_INFINITE); assert(bestValue > -VALUE_INFINITE);
assert(bestValue <= alpha); assert(bestValue <= alpha);
assert(alpha < beta); assert(alpha < beta);
assert(beta <= VALUE_INFINITE); assert(beta <= VALUE_INFINITE);
assert(depth > DEPTH_ZERO); assert(depth > DEPTH_ZERO);
assert(pos.thread() >= 0 && pos.thread() < activeThreads);
assert(activeThreads > 1);
int i, master = pos.thread(); Thread* master = pos.this_thread();
Thread& masterThread = threads[master];
// If we already have too many active split points, don't split if (master->splitPointsCnt >= MAX_SPLITPOINTS_PER_THREAD)
if (masterThread.activeSplitPoints >= MAX_ACTIVE_SPLIT_POINTS)
return bestValue; return bestValue;
// Pick the next available split point from the split point stack // Pick the next available split point from the split point stack
SplitPoint* sp = &masterThread.splitPoints[masterThread.activeSplitPoints]; SplitPoint* sp = &master->splitPoints[master->splitPointsCnt];
// Initialize the split point sp->parent = master->curSplitPoint;
sp->parent = masterThread.splitPoint;
sp->master = master; sp->master = master;
sp->is_betaCutoff = false; sp->cutoff = false;
sp->slavesMask = 1ULL << master->idx;
sp->depth = depth; sp->depth = depth;
sp->bestMove = *bestMove;
sp->threatMove = threatMove; sp->threatMove = threatMove;
sp->alpha = alpha; sp->alpha = alpha;
sp->beta = beta; sp->beta = beta;
@@ -298,88 +332,71 @@ Value ThreadsManager::split(Position& pos, Stack* ss, Value alpha, Value beta,
sp->nodes = 0; sp->nodes = 0;
sp->ss = ss; sp->ss = ss;
for (i = 0; i < activeThreads; i++) assert(master->is_searching);
sp->is_slave[i] = false;
// If we are here it means we are not available master->curSplitPoint = sp;
assert(masterThread.is_searching); int slavesCnt = 0;
int workersCnt = 1; // At least the master is included
// Try to allocate available threads and ask them to start searching setting // Try to allocate available threads and ask them to start searching setting
// is_searching flag. This must be done under lock protection to avoid concurrent // is_searching flag. This must be done under lock protection to avoid concurrent
// allocation of the same slave by another master. // allocation of the same slave by another master.
lock_grab(&threadsLock); lock_grab(sp->lock);
lock_grab(splitLock);
for (i = 0; !Fake && i < activeThreads && workersCnt < maxThreadsPerSplitPoint; i++) for (int i = 0; i < size() && !Fake; ++i)
if (threads[i].is_available_to(master)) if (threads[i]->is_available_to(master))
{ {
workersCnt++; sp->slavesMask |= 1ULL << i;
sp->is_slave[i] = true; threads[i]->curSplitPoint = sp;
threads[i].splitPoint = sp; threads[i]->is_searching = true; // Slave leaves idle_loop()
// This makes the slave to exit from idle_loop()
threads[i].is_searching = true;
if (useSleepingThreads) if (useSleepingThreads)
threads[i].wake_up(); threads[i]->wake_up();
if (++slavesCnt + 1 >= maxThreadsPerSplitPoint) // Master is always included
break;
} }
lock_release(&threadsLock); master->splitPointsCnt++;
// We failed to allocate even one slave, return lock_release(splitLock);
if (!Fake && workersCnt == 1) lock_release(sp->lock);
return bestValue;
masterThread.splitPoint = sp;
masterThread.activeSplitPoints++;
// Everything is set up. The master thread enters the idle loop, from which // Everything is set up. The master thread enters the idle loop, from which
// it will instantly launch a search, because its is_searching flag is set. // it will instantly launch a search, because its is_searching flag is set.
// We pass the split point as a parameter to the idle loop, which means that // We pass the split point as a parameter to the idle loop, which means that
// the thread will return from the idle loop when all slaves have finished // the thread will return from the idle loop when all slaves have finished
// their work at this split point. // their work at this split point.
masterThread.idle_loop(sp); if (slavesCnt || Fake)
{
master->idle_loop(sp);
// In helpful master concept a master can help only a sub-tree of its split // In helpful master concept a master can help only a sub-tree of its split
// point, and because here is all finished is not possible master is booked. // point, and because here is all finished is not possible master is booked.
assert(!masterThread.is_searching); assert(!master->is_searching);
}
// We have returned from the idle loop, which means that all threads are // We have returned from the idle loop, which means that all threads are
// finished. Note that changing state and decreasing activeSplitPoints is done // finished. Note that setting is_searching and decreasing splitPointsCnt is
// under lock protection to avoid a race with Thread::is_available_to(). // done under lock protection to avoid a race with Thread::is_available_to().
lock_grab(&threadsLock); lock_grab(sp->lock); // To protect sp->nodes
lock_grab(splitLock);
masterThread.is_searching = true; master->is_searching = true;
masterThread.activeSplitPoints--; master->splitPointsCnt--;
master->curSplitPoint = sp->parent;
lock_release(&threadsLock);
masterThread.splitPoint = sp->parent;
pos.set_nodes_searched(pos.nodes_searched() + sp->nodes); pos.set_nodes_searched(pos.nodes_searched() + sp->nodes);
*bestMove = sp->bestMove;
lock_release(splitLock);
lock_release(sp->lock);
return sp->bestValue; return sp->bestValue;
} }
// Explicit template instantiations // Explicit template instantiations
template Value ThreadsManager::split<false>(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int); template Value ThreadsManager::split<false>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
template Value ThreadsManager::split<true>(Position&, Stack*, Value, Value, Value, Depth, Move, int, MovePicker*, int); template Value ThreadsManager::split<true>(Position&, Stack*, Value, Value, Value, Move*, Depth, Move, int, MovePicker*, int);
// Thread::timer_loop() is where the timer thread waits maxPly milliseconds and
// then calls do_timer_event(). If maxPly is 0 thread sleeps until is woken up.
extern void check_time();
void Thread::timer_loop() {
while (!do_terminate)
{
lock_grab(&sleepLock);
timed_wait(&sleepCond, &sleepLock, maxPly ? maxPly : INT_MAX);
lock_release(&sleepLock);
check_time();
}
}
// ThreadsManager::set_timer() is used to set the timer to trigger after msec // ThreadsManager::set_timer() is used to set the timer to trigger after msec
@@ -387,124 +404,46 @@ void Thread::timer_loop() {
void ThreadsManager::set_timer(int msec) { void ThreadsManager::set_timer(int msec) {
Thread& timer = threads[MAX_THREADS]; lock_grab(timer->sleepLock);
timer->maxPly = msec;
lock_grab(&timer.sleepLock); cond_signal(timer->sleepCond); // Wake up and restart the timer
timer.maxPly = msec; lock_release(timer->sleepLock);
cond_signal(&timer.sleepCond); // Wake up and restart the timer
lock_release(&timer.sleepLock);
} }
// Thread::main_loop() is where the main thread is parked waiting to be started // ThreadsManager::wait_for_search_finished() waits for main thread to go to
// when there is a new search. Main thread will launch all the slave threads. // sleep, this means search is finished. Then returns.
void Thread::main_loop() { void ThreadsManager::wait_for_search_finished() {
while (true) Thread* t = main_thread();
{ lock_grab(t->sleepLock);
lock_grab(&sleepLock); cond_signal(t->sleepCond); // In case is waiting for stop or ponderhit
while (!t->do_sleep) cond_wait(sleepCond, t->sleepLock);
do_sleep = true; // Always return to sleep after a search lock_release(t->sleepLock);
is_searching = false;
while (do_sleep && !do_terminate)
{
cond_signal(&Threads.sleepCond); // Wake up UI thread if needed
cond_wait(&sleepCond, &sleepLock);
}
is_searching = true;
lock_release(&sleepLock);
if (do_terminate)
return;
Search::think();
}
} }
// ThreadsManager::start_thinking() is used by UI thread to wake up the main // ThreadsManager::start_searching() wakes up the main thread sleeping in
// thread parked in main_loop() and starting a new search. If asyncMode is true // main_loop() so to start a new search, then returns immediately.
// then function returns immediately, otherwise caller is blocked waiting for
// the search to finish.
void ThreadsManager::start_thinking(const Position& pos, const LimitsType& limits, void ThreadsManager::start_searching(const Position& pos, const LimitsType& limits,
const std::set<Move>& searchMoves, bool async) { const std::vector<Move>& searchMoves) {
Thread& main = threads[0]; wait_for_search_finished();
lock_grab(&main.sleepLock); SearchTime.restart(); // As early as possible
// Wait main thread has finished before to launch a new search
while (!main.do_sleep)
cond_wait(&sleepCond, &main.sleepLock);
// Copy input arguments to initialize the search
RootPosition.copy(pos, 0);
Limits = limits;
RootMoves.clear();
// Populate RootMoves with all the legal moves (default) or, if a searchMoves
// set is given, with the subset of legal moves to search.
for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
if (searchMoves.empty() || searchMoves.count(ml.move()))
RootMoves.push_back(RootMove(ml.move()));
// Reset signals before to start the new search
Signals.stopOnPonderhit = Signals.firstRootMove = false; Signals.stopOnPonderhit = Signals.firstRootMove = false;
Signals.stop = Signals.failedLowAtRoot = false; Signals.stop = Signals.failedLowAtRoot = false;
main.do_sleep = false; RootPosition = pos;
cond_signal(&main.sleepCond); // Wake up main thread and start searching Limits = limits;
RootMoves.clear();
if (!async) for (MoveList<MV_LEGAL> ml(pos); !ml.end(); ++ml)
while (!main.do_sleep) if (searchMoves.empty() || std::count(searchMoves.begin(), searchMoves.end(), ml.move()))
cond_wait(&sleepCond, &main.sleepLock); RootMoves.push_back(RootMove(ml.move()));
lock_release(&main.sleepLock); main_thread()->do_sleep = false;
} main_thread()->wake_up();
// ThreadsManager::stop_thinking() is used by UI thread to raise a stop request
// and to wait for the main thread finishing the search. Needed to wait exiting
// and terminate the threads after a 'quit' command.
void ThreadsManager::stop_thinking() {
Thread& main = threads[0];
Search::Signals.stop = true;
lock_grab(&main.sleepLock);
cond_signal(&main.sleepCond); // In case is waiting for stop or ponderhit
while (!main.do_sleep)
cond_wait(&sleepCond, &main.sleepLock);
lock_release(&main.sleepLock);
}
// ThreadsManager::wait_for_stop_or_ponderhit() is called when the maximum depth
// is reached while the program is pondering. The point is to work around a wrinkle
// in the UCI protocol: When pondering, the engine is not allowed to give a
// "bestmove" before the GUI sends it a "stop" or "ponderhit" command. We simply
// wait here until one of these commands (that raise StopRequest) is sent and
// then return, after which the bestmove and pondermove will be printed.
void ThreadsManager::wait_for_stop_or_ponderhit() {
Signals.stopOnPonderhit = true;
Thread& main = threads[0];
lock_grab(&main.sleepLock);
while (!Signals.stop)
cond_wait(&main.sleepCond, &main.sleepLock);
lock_release(&main.sleepLock);
} }

View File

@@ -20,10 +20,8 @@
#if !defined(THREAD_H_INCLUDED) #if !defined(THREAD_H_INCLUDED)
#define THREAD_H_INCLUDED #define THREAD_H_INCLUDED
#include <cstring> #include <vector>
#include <set>
#include "lock.h"
#include "material.h" #include "material.h"
#include "movepick.h" #include "movepick.h"
#include "pawns.h" #include "pawns.h"
@@ -31,32 +29,34 @@
#include "search.h" #include "search.h"
const int MAX_THREADS = 32; const int MAX_THREADS = 32;
const int MAX_ACTIVE_SPLIT_POINTS = 8; const int MAX_SPLITPOINTS_PER_THREAD = 8;
class Thread;
struct SplitPoint { struct SplitPoint {
// Const data after splitPoint has been setup // Const data after split point has been setup
SplitPoint* parent;
const Position* pos; const Position* pos;
const Search::Stack* ss;
Depth depth; Depth depth;
Value beta; Value beta;
int nodeType; int nodeType;
int ply; Thread* master;
int master;
Move threatMove; Move threatMove;
// Const pointers to shared data // Const pointers to shared data
MovePicker* mp; MovePicker* mp;
Search::Stack* ss; SplitPoint* parent;
// Shared data // Shared data
Lock lock; Lock lock;
volatile uint64_t slavesMask;
volatile int64_t nodes; volatile int64_t nodes;
volatile Value alpha; volatile Value alpha;
volatile Value bestValue; volatile Value bestValue;
volatile Move bestMove;
volatile int moveCount; volatile int moveCount;
volatile bool is_betaCutoff; volatile bool cutoff;
volatile bool is_slave[MAX_THREADS];
}; };
@@ -65,33 +65,40 @@ struct SplitPoint {
/// tables so that once we get a pointer to an entry its life time is unlimited /// tables so that once we get a pointer to an entry its life time is unlimited
/// and we don't have to care about someone changing the entry under our feet. /// and we don't have to care about someone changing the entry under our feet.
struct Thread { class Thread {
Thread(const Thread&); // Only declared to disable the default ones
Thread& operator=(const Thread&); // that are not suitable in this case.
typedef void (Thread::* Fn) ();
public:
Thread(Fn fn);
~Thread();
void wake_up(); void wake_up();
bool cutoff_occurred() const; bool cutoff_occurred() const;
bool is_available_to(int master) const; bool is_available_to(Thread* master) const;
void idle_loop(SplitPoint* sp); void idle_loop(SplitPoint* sp_master);
void idle_loop() { idle_loop(NULL); } // Hack to allow storing in start_fn
void main_loop(); void main_loop();
void timer_loop(); void timer_loop();
void wait_for_stop_or_ponderhit();
SplitPoint splitPoints[MAX_ACTIVE_SPLIT_POINTS]; SplitPoint splitPoints[MAX_SPLITPOINTS_PER_THREAD];
MaterialInfoTable materialTable; MaterialTable materialTable;
PawnInfoTable pawnTable; PawnTable pawnTable;
int threadID; int idx;
int maxPly; int maxPly;
Lock sleepLock; Lock sleepLock;
WaitCondition sleepCond; WaitCondition sleepCond;
SplitPoint* volatile splitPoint; NativeHandle handle;
volatile int activeSplitPoints; Fn start_fn;
SplitPoint* volatile curSplitPoint;
volatile int splitPointsCnt;
volatile bool is_searching; volatile bool is_searching;
volatile bool do_sleep; volatile bool do_sleep;
volatile bool do_terminate; volatile bool do_exit;
#if defined(_MSC_VER)
HANDLE handle;
#else
pthread_t handle;
#endif
}; };
@@ -105,37 +112,37 @@ class ThreadsManager {
static storage duration are automatically set to zero before enter main() static storage duration are automatically set to zero before enter main()
*/ */
public: public:
Thread& operator[](int threadID) { return threads[threadID]; } void init(); // No c'tor becuase Threads is static and we need engine initialized
void init(); ~ThreadsManager();
void exit();
Thread& operator[](int id) { return *threads[id]; }
bool use_sleeping_threads() const { return useSleepingThreads; } bool use_sleeping_threads() const { return useSleepingThreads; }
int min_split_depth() const { return minimumSplitDepth; } int min_split_depth() const { return minimumSplitDepth; }
int size() const { return activeThreads; } int size() const { return (int)threads.size(); }
Thread* main_thread() { return threads[0]; }
void set_size(int cnt); void wake_up() const;
void sleep() const;
void read_uci_options(); void read_uci_options();
bool available_slave_exists(int master) const; bool available_slave_exists(Thread* master) const;
bool split_point_finished(SplitPoint* sp) const;
void set_timer(int msec); void set_timer(int msec);
void wait_for_stop_or_ponderhit(); void wait_for_search_finished();
void stop_thinking(); void start_searching(const Position& pos, const Search::LimitsType& limits,
void start_thinking(const Position& pos, const Search::LimitsType& limits, const std::vector<Move>& searchMoves);
const std::set<Move>& = std::set<Move>(), bool async = false);
template <bool Fake> template <bool Fake>
Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Value split(Position& pos, Search::Stack* ss, Value alpha, Value beta, Value bestValue, Move* bestMove,
Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType); Depth depth, Move threatMove, int moveCount, MovePicker* mp, int nodeType);
private: private:
friend struct Thread; friend class Thread;
Thread threads[MAX_THREADS + 1]; // Last one is used as a timer std::vector<Thread*> threads;
Lock threadsLock; Thread* timer;
Lock splitLock;
WaitCondition sleepCond;
Depth minimumSplitDepth; Depth minimumSplitDepth;
int maxThreadsPerSplitPoint; int maxThreadsPerSplitPoint;
int activeThreads;
bool useSleepingThreads; bool useSleepingThreads;
WaitCondition sleepCond;
}; };
extern ThreadsManager Threads; extern ThreadsManager Threads;

View File

@@ -20,7 +20,6 @@
#include <cmath> #include <cmath>
#include <algorithm> #include <algorithm>
#include "misc.h"
#include "search.h" #include "search.h"
#include "timeman.h" #include "timeman.h"
#include "ucioption.h" #include "ucioption.h"
@@ -73,7 +72,7 @@ namespace {
enum TimeType { OptimumTime, MaxTime }; enum TimeType { OptimumTime, MaxTime };
template<TimeType> template<TimeType>
int remaining(int myTime, int movesToGo, int fullMoveNumber); int remaining(int myTime, int movesToGo, int fullMoveNumber, int slowMover);
} }
@@ -84,7 +83,7 @@ void TimeManager::pv_instability(int curChanges, int prevChanges) {
} }
void TimeManager::init(const Search::LimitsType& limits, int currentPly) void TimeManager::init(const Search::LimitsType& limits, int currentPly, Color us)
{ {
/* We support four different kind of time controls: /* We support four different kind of time controls:
@@ -108,25 +107,26 @@ void TimeManager::init(const Search::LimitsType& limits, int currentPly)
int emergencyBaseTime = Options["Emergency Base Time"]; int emergencyBaseTime = Options["Emergency Base Time"];
int emergencyMoveTime = Options["Emergency Move Time"]; int emergencyMoveTime = Options["Emergency Move Time"];
int minThinkingTime = Options["Minimum Thinking Time"]; int minThinkingTime = Options["Minimum Thinking Time"];
int slowMover = Options["Slow Mover"];
// Initialize to maximum values but unstablePVExtraTime that is reset // Initialize to maximum values but unstablePVExtraTime that is reset
unstablePVExtraTime = 0; unstablePVExtraTime = 0;
optimumSearchTime = maximumSearchTime = limits.time; optimumSearchTime = maximumSearchTime = limits.time[us];
// We calculate optimum time usage for different hypothetic "moves to go"-values and choose the // 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. // minimum of calculated search time values. Usually the greatest hypMTG gives the minimum values.
for (hypMTG = 1; hypMTG <= (limits.movesToGo ? std::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 // Calculate thinking time for hypothetic "moves to go"-value
hypMyTime = limits.time hypMyTime = limits.time[us]
+ limits.increment * (hypMTG - 1) + limits.inc[us] * (hypMTG - 1)
- emergencyBaseTime - emergencyBaseTime
- emergencyMoveTime * std::min(hypMTG, emergencyMoveHorizon); - emergencyMoveTime * std::min(hypMTG, emergencyMoveHorizon);
hypMyTime = std::max(hypMyTime, 0); hypMyTime = std::max(hypMyTime, 0);
t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, currentPly); t1 = minThinkingTime + remaining<OptimumTime>(hypMyTime, hypMTG, currentPly, slowMover);
t2 = minThinkingTime + remaining<MaxTime>(hypMyTime, hypMTG, currentPly); t2 = minThinkingTime + remaining<MaxTime>(hypMyTime, hypMTG, currentPly, slowMover);
optimumSearchTime = std::min(optimumSearchTime, t1); optimumSearchTime = std::min(optimumSearchTime, t1);
maximumSearchTime = std::min(maximumSearchTime, t2); maximumSearchTime = std::min(maximumSearchTime, t2);
@@ -143,12 +143,12 @@ void TimeManager::init(const Search::LimitsType& limits, int currentPly)
namespace { namespace {
template<TimeType T> template<TimeType T>
int remaining(int myTime, int movesToGo, int currentPly) int remaining(int myTime, int movesToGo, int currentPly, int slowMover)
{ {
const float TMaxRatio = (T == OptimumTime ? 1 : MaxRatio); const float TMaxRatio = (T == OptimumTime ? 1 : MaxRatio);
const float TStealRatio = (T == OptimumTime ? 0 : StealRatio); const float TStealRatio = (T == OptimumTime ? 0 : StealRatio);
int thisMoveImportance = move_importance(currentPly); int thisMoveImportance = move_importance(currentPly) * slowMover / 100;
int otherMovesImportance = 0; int otherMovesImportance = 0;
for (int i = 1; i < movesToGo; i++) for (int i = 1; i < movesToGo; i++)

View File

@@ -25,7 +25,7 @@
class TimeManager { class TimeManager {
public: public:
void init(const Search::LimitsType& limits, int currentPly); void init(const Search::LimitsType& limits, int currentPly, Color us);
void pv_instability(int curChanges, int prevChanges); void pv_instability(int curChanges, int prevChanges);
int available_time() const { return optimumSearchTime + unstablePVExtraTime; } int available_time() const { return optimumSearchTime + unstablePVExtraTime; }
int maximum_time() const { return maximumSearchTime; } int maximum_time() const { return maximumSearchTime; }

View File

@@ -84,7 +84,7 @@ void TranspositionTable::clear() {
/// more valuable than a TTEntry t2 if t1 is from the current search and t2 is from /// more valuable than a TTEntry t2 if t1 is from the current search and t2 is from
/// a previous search, or if the depth of t1 is bigger than the depth of t2. /// a previous search, or if the depth of t1 is bigger than the depth of t2.
void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d, Move m, Value statV, Value kingD) { void TranspositionTable::store(const Key posKey, Value v, Bound t, Depth d, Move m, Value statV, Value kingD) {
int c1, c2, c3; int c1, c2, c3;
TTEntry *tte, *replace; TTEntry *tte, *replace;
@@ -106,7 +106,7 @@ void TranspositionTable::store(const Key posKey, Value v, ValueType t, Depth d,
// Implement replace strategy // Implement replace strategy
c1 = (replace->generation() == generation ? 2 : 0); c1 = (replace->generation() == generation ? 2 : 0);
c2 = (tte->generation() == generation || tte->type() == VALUE_TYPE_EXACT ? -2 : 0); c2 = (tte->generation() == generation || tte->type() == BOUND_EXACT ? -2 : 0);
c3 = (tte->depth() < replace->depth() ? 1 : 0); c3 = (tte->depth() < replace->depth() ? 1 : 0);
if (c1 + c2 + c3 > 0) if (c1 + c2 + c3 > 0)

View File

@@ -20,12 +20,9 @@
#if !defined(TT_H_INCLUDED) #if !defined(TT_H_INCLUDED)
#define TT_H_INCLUDED #define TT_H_INCLUDED
#include <iostream>
#include "misc.h" #include "misc.h"
#include "types.h" #include "types.h"
/// The TTEntry is the class of transposition table entries /// The TTEntry is the class of transposition table entries
/// ///
/// A TTEntry needs 128 bits to be stored /// A TTEntry needs 128 bits to be stored
@@ -47,11 +44,11 @@
class TTEntry { class TTEntry {
public: public:
void save(uint32_t k, Value v, ValueType t, Depth d, Move m, int g, Value statV, Value statM) { void save(uint32_t k, Value v, Bound b, Depth d, Move m, int g, Value statV, Value statM) {
key32 = (uint32_t)k; key32 = (uint32_t)k;
move16 = (uint16_t)m; move16 = (uint16_t)m;
valueType = (uint8_t)t; bound = (uint8_t)b;
generation8 = (uint8_t)g; generation8 = (uint8_t)g;
value16 = (int16_t)v; value16 = (int16_t)v;
depth16 = (int16_t)d; depth16 = (int16_t)d;
@@ -64,7 +61,7 @@ public:
Depth depth() const { return (Depth)depth16; } Depth depth() const { return (Depth)depth16; }
Move move() const { return (Move)move16; } Move move() const { return (Move)move16; }
Value value() const { return (Value)value16; } Value value() const { return (Value)value16; }
ValueType type() const { return (ValueType)valueType; } Bound type() const { return (Bound)bound; }
int generation() const { return (int)generation8; } int generation() const { return (int)generation8; }
Value static_value() const { return (Value)staticValue; } Value static_value() const { return (Value)staticValue; }
Value static_value_margin() const { return (Value)staticMargin; } Value static_value_margin() const { return (Value)staticMargin; }
@@ -72,7 +69,7 @@ public:
private: private:
uint32_t key32; uint32_t key32;
uint16_t move16; uint16_t move16;
uint8_t valueType, generation8; uint8_t bound, generation8;
int16_t value16, depth16, staticValue, staticMargin; int16_t value16, depth16, staticValue, staticMargin;
}; };
@@ -103,7 +100,7 @@ public:
~TranspositionTable(); ~TranspositionTable();
void set_size(size_t mbSize); void set_size(size_t mbSize);
void clear(); void clear();
void store(const Key posKey, Value v, ValueType type, Depth d, Move m, Value statV, Value kingD); void store(const Key posKey, Value v, Bound type, Depth d, Move m, Value statV, Value kingD);
TTEntry* probe(const Key posKey) const; TTEntry* probe(const Key posKey) const;
void new_search(); void new_search();
TTEntry* first_entry(const Key posKey) const; TTEntry* first_entry(const Key posKey) const;
@@ -136,38 +133,4 @@ inline void TranspositionTable::refresh(const TTEntry* tte) const {
const_cast<TTEntry*>(tte)->set_generation(generation); const_cast<TTEntry*>(tte)->set_generation(generation);
} }
/// A simple fixed size hash table used to store pawns and material
/// configurations. It is basically just an array of Entry objects.
/// Without cluster concept or overwrite policy.
template<class Entry, int HashSize>
struct SimpleHash {
typedef SimpleHash<Entry, HashSize> Base;
void init() {
if (entries)
return;
entries = new (std::nothrow) Entry[HashSize];
if (!entries)
{
std::cerr << "Failed to allocate " << HashSize * sizeof(Entry)
<< " bytes for hash table." << std::endl;
exit(EXIT_FAILURE);
}
memset(entries, 0, HashSize * sizeof(Entry));
}
virtual ~SimpleHash() { delete [] entries; }
Entry* probe(Key key) const { return entries + ((uint32_t)key & (HashSize - 1)); }
void prefetch(Key key) const { ::prefetch((char*)probe(key)); }
protected:
Entry* entries;
};
#endif // !defined(TT_H_INCLUDED) #endif // !defined(TT_H_INCLUDED)

View File

@@ -35,30 +35,11 @@
/// | only in 64-bit mode. For compiling requires hardware with /// | only in 64-bit mode. For compiling requires hardware with
/// | popcnt support. /// | popcnt support.
#include <cctype>
#include <climits> #include <climits>
#include <cstdlib> #include <cstdlib>
#include <ctype.h>
#if defined(_MSC_VER) #include "platform.h"
// Disable some silly and noisy warning from MSVC compiler
#pragma warning(disable: 4127) // Conditional expression is constant
#pragma warning(disable: 4146) // Unary minus operator applied to unsigned type
#pragma warning(disable: 4800) // Forcing value to bool 'true' or 'false'
// MSVC does not support <inttypes.h>
typedef signed __int8 int8_t;
typedef unsigned __int8 uint8_t;
typedef signed __int16 int16_t;
typedef unsigned __int16 uint16_t;
typedef signed __int32 int32_t;
typedef unsigned __int32 uint32_t;
typedef signed __int64 int64_t;
typedef unsigned __int64 uint64_t;
#else
# include <inttypes.h>
#endif
#if defined(_WIN64) #if defined(_WIN64)
# include <intrin.h> // MSVC popcnt and bsfq instrinsics # include <intrin.h> // MSVC popcnt and bsfq instrinsics
@@ -99,7 +80,7 @@ const bool Is64Bit = false;
typedef uint64_t Key; typedef uint64_t Key;
typedef uint64_t Bitboard; typedef uint64_t Bitboard;
const int MAX_MOVES = 256; const int MAX_MOVES = 192;
const int MAX_PLY = 100; const int MAX_PLY = 100;
const int MAX_PLY_PLUS_2 = MAX_PLY + 2; const int MAX_PLY_PLUS_2 = MAX_PLY + 2;
@@ -147,15 +128,20 @@ inline bool operator<(const MoveStack& f, const MoveStack& s) {
return f.score < s.score; return f.score < s.score;
} }
enum CastleRight { enum CastleRight { // Defined as in PolyGlot book hash key
CASTLES_NONE = 0, CASTLES_NONE = 0,
WHITE_OO = 1, WHITE_OO = 1,
BLACK_OO = 2, WHITE_OOO = 2,
WHITE_OOO = 4, BLACK_OO = 4,
BLACK_OOO = 8, BLACK_OOO = 8,
ALL_CASTLES = 15 ALL_CASTLES = 15
}; };
enum CastlingSide {
KING_SIDE,
QUEEN_SIDE
};
enum ScaleFactor { enum ScaleFactor {
SCALE_FACTOR_DRAW = 0, SCALE_FACTOR_DRAW = 0,
SCALE_FACTOR_NORMAL = 64, SCALE_FACTOR_NORMAL = 64,
@@ -163,11 +149,11 @@ enum ScaleFactor {
SCALE_FACTOR_NONE = 255 SCALE_FACTOR_NONE = 255
}; };
enum ValueType { enum Bound {
VALUE_TYPE_NONE = 0, BOUND_NONE = 0,
VALUE_TYPE_UPPER = 1, BOUND_UPPER = 1,
VALUE_TYPE_LOWER = 2, BOUND_LOWER = 2,
VALUE_TYPE_EXACT = VALUE_TYPE_UPPER | VALUE_TYPE_LOWER BOUND_EXACT = BOUND_UPPER | BOUND_LOWER
}; };
enum Value { enum Value {
@@ -186,7 +172,7 @@ enum Value {
}; };
enum PieceType { enum PieceType {
NO_PIECE_TYPE = 0, NO_PIECE_TYPE = 0, ALL_PIECES = 0,
PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6 PAWN = 1, KNIGHT = 2, BISHOP = 3, ROOK = 4, QUEEN = 5, KING = 6
}; };
@@ -207,7 +193,7 @@ enum Depth {
DEPTH_ZERO = 0 * ONE_PLY, DEPTH_ZERO = 0 * ONE_PLY,
DEPTH_QS_CHECKS = -1 * ONE_PLY, DEPTH_QS_CHECKS = -1 * ONE_PLY,
DEPTH_QS_NO_CHECKS = -2 * ONE_PLY, DEPTH_QS_NO_CHECKS = -2 * ONE_PLY,
DEPTH_QS_RECAPTURES = -4 * ONE_PLY, DEPTH_QS_RECAPTURES = -5 * ONE_PLY,
DEPTH_NONE = -127 * ONE_PLY DEPTH_NONE = -127 * ONE_PLY
}; };
@@ -317,21 +303,27 @@ inline Score operator/(Score s, int i) {
return make_score(mg_value(s) / i, eg_value(s) / i); return make_score(mg_value(s) / i, eg_value(s) / i);
} }
/// Weight score v by score w trying to prevent overflow
inline Score apply_weight(Score v, Score w) {
return make_score((int(mg_value(v)) * mg_value(w)) / 0x100,
(int(eg_value(v)) * eg_value(w)) / 0x100);
}
#undef ENABLE_OPERATORS_ON #undef ENABLE_OPERATORS_ON
#undef ENABLE_SAFE_OPERATORS_ON #undef ENABLE_SAFE_OPERATORS_ON
const Value PawnValueMidgame = Value(0x0C6); const Value PawnValueMidgame = Value(198);
const Value PawnValueEndgame = Value(0x102); const Value PawnValueEndgame = Value(258);
const Value KnightValueMidgame = Value(0x331); const Value KnightValueMidgame = Value(817);
const Value KnightValueEndgame = Value(0x34E); const Value KnightValueEndgame = Value(846);
const Value BishopValueMidgame = Value(0x344); const Value BishopValueMidgame = Value(836);
const Value BishopValueEndgame = Value(0x359); const Value BishopValueEndgame = Value(857);
const Value RookValueMidgame = Value(0x4F6); const Value RookValueMidgame = Value(1270);
const Value RookValueEndgame = Value(0x4FE); const Value RookValueEndgame = Value(1278);
const Value QueenValueMidgame = Value(0x9D9); const Value QueenValueMidgame = Value(2521);
const Value QueenValueEndgame = Value(0x9FE); const Value QueenValueEndgame = Value(2558);
extern const Value PieceValueMidgame[17]; extern const Value PieceValueMidgame[17]; // Indexed by Piece or PieceType
extern const Value PieceValueEndgame[17]; extern const Value PieceValueEndgame[17];
extern int SquareDistance[64][64]; extern int SquareDistance[64][64];
@@ -340,7 +332,7 @@ inline Color operator~(Color c) {
} }
inline Square operator~(Square s) { inline Square operator~(Square s) {
return Square(s ^ 56); return Square(s ^ 56); // Vertical flip SQ_A1 -> SQ_A8
} }
inline Value mate_in(int ply) { inline Value mate_in(int ply) {
@@ -355,6 +347,10 @@ inline Piece make_piece(Color c, PieceType pt) {
return Piece((c << 3) | pt); return Piece((c << 3) | pt);
} }
inline CastleRight make_castle_right(Color c, CastlingSide s) {
return CastleRight(WHITE_OO << ((s == QUEEN_SIDE) + 2 * c));
}
inline PieceType type_of(Piece p) { inline PieceType type_of(Piece p) {
return PieceType(p & 7); return PieceType(p & 7);
} }
@@ -367,7 +363,7 @@ inline Square make_square(File f, Rank r) {
return Square((r << 3) | f); return Square((r << 3) | f);
} }
inline bool square_is_ok(Square s) { inline bool is_ok(Square s) {
return s >= SQ_A1 && s <= SQ_H8; return s >= SQ_A1 && s <= SQ_H8;
} }
@@ -380,7 +376,7 @@ inline Rank rank_of(Square s) {
} }
inline Square mirror(Square s) { inline Square mirror(Square s) {
return Square(s ^ 7); return Square(s ^ 7); // Horizontal flip SQ_A1 -> SQ_H1
} }
inline Square relative_square(Color c, Square s) { inline Square relative_square(Color c, Square s) {
@@ -396,7 +392,7 @@ inline Rank relative_rank(Color c, Square s) {
} }
inline bool opposite_colors(Square s1, Square s2) { inline bool opposite_colors(Square s1, Square s2) {
int s = s1 ^ s2; int s = int(s1) ^ int(s2);
return ((s >> 3) ^ s) & 1; return ((s >> 3) ^ s) & 1;
} }
@@ -452,7 +448,7 @@ inline int is_castle(Move m) {
return (m & (3 << 14)) == (3 << 14); return (m & (3 << 14)) == (3 << 14);
} }
inline PieceType promotion_piece_type(Move m) { inline PieceType promotion_type(Move m) {
return PieceType(((m >> 12) & 3) + 2); return PieceType(((m >> 12) & 3) + 2);
} }
@@ -486,23 +482,18 @@ inline const std::string square_to_string(Square s) {
/// Our insertion sort implementation, works with pointers and iterators and is /// Our insertion sort implementation, works with pointers and iterators and is
/// guaranteed to be stable, as is needed. /// guaranteed to be stable, as is needed.
template<typename T, typename K> template<typename T, typename K>
void sort(K firstMove, K lastMove) void sort(K first, K last)
{ {
T value; T tmp;
K cur, p, d; K p, q;
if (firstMove != lastMove) for (p = first + 1; p < last; p++)
for (cur = firstMove + 1; cur != lastMove; cur++) {
{ tmp = *p;
p = d = cur; for (q = p; q != first && *(q-1) < tmp; --q)
value = *p--; *q = *(q-1);
if (*p < value) *q = tmp;
{ }
do *d = *p;
while (--d != firstMove && *--p < value);
*d = value;
}
}
} }
#endif // !defined(TYPES_H_INCLUDED) #endif // !defined(TYPES_H_INCLUDED)

View File

@@ -20,10 +20,8 @@
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector>
#include "evaluate.h" #include "evaluate.h"
#include "misc.h"
#include "position.h" #include "position.h"
#include "search.h" #include "search.h"
#include "thread.h" #include "thread.h"
@@ -31,6 +29,8 @@
using namespace std; using namespace std;
extern void benchmark(const Position& pos, istream& is);
namespace { namespace {
// FEN string of the initial position, normal chess // FEN string of the initial position, normal chess
@@ -44,7 +44,6 @@ namespace {
void set_option(istringstream& up); void set_option(istringstream& up);
void set_position(Position& pos, istringstream& up); void set_position(Position& pos, istringstream& up);
void go(Position& pos, istringstream& up); void go(Position& pos, istringstream& up);
void perft(Position& pos, istringstream& up);
} }
@@ -53,14 +52,17 @@ namespace {
/// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI /// that we exit gracefully if the GUI dies unexpectedly. In addition to the UCI
/// commands, the function also supports a few debug commands. /// commands, the function also supports a few debug commands.
void uci_loop() { void uci_loop(const string& args) {
Position pos(StartFEN, false, 0); // The root position Position pos(StartFEN, false, Threads.main_thread()); // The root position
string cmd, token; string cmd, token;
while (token != "quit") while (token != "quit")
{ {
if (!getline(cin, cmd)) // Block here waiting for input if (!args.empty())
cmd = args;
else if (!getline(cin, cmd)) // Block here waiting for input
cmd = "quit"; cmd = "quit";
istringstream is(cmd); istringstream is(cmd);
@@ -68,7 +70,10 @@ void uci_loop() {
is >> skipws >> token; is >> skipws >> token;
if (token == "quit" || token == "stop") if (token == "quit" || token == "stop")
Threads.stop_thinking(); {
Search::Signals.stop = true;
Threads.wait_for_search_finished(); // Cannot quit while threads are running
}
else if (token == "ponderhit") else if (token == "ponderhit")
{ {
@@ -78,14 +83,17 @@ void uci_loop() {
Search::Limits.ponder = false; Search::Limits.ponder = false;
if (Search::Signals.stopOnPonderhit) if (Search::Signals.stopOnPonderhit)
Threads.stop_thinking(); {
Search::Signals.stop = true;
Threads.main_thread()->wake_up(); // Could be sleeping
}
} }
else if (token == "go") else if (token == "go")
go(pos, is); go(pos, is);
else if (token == "ucinewgame") else if (token == "ucinewgame")
pos.from_fen(StartFEN, false); { /* Avoid returning "Unknown command" */ }
else if (token == "isready") else if (token == "isready")
cout << "readyok" << endl; cout << "readyok" << endl;
@@ -96,20 +104,17 @@ void uci_loop() {
else if (token == "setoption") else if (token == "setoption")
set_option(is); set_option(is);
else if (token == "perft")
perft(pos, is);
else if (token == "d") else if (token == "d")
pos.print(); pos.print();
else if (token == "flip") else if (token == "flip")
pos.flip_me(); pos.flip();
else if (token == "eval") else if (token == "eval")
{ cout << Eval::trace(pos) << endl;
read_evaluation_uci_options(pos.side_to_move());
cout << trace_evaluate(pos) << endl; else if (token == "bench")
} benchmark(pos, is);
else if (token == "key") else if (token == "key")
cout << "key: " << hex << pos.key() cout << "key: " << hex << pos.key()
@@ -120,8 +125,25 @@ void uci_loop() {
cout << "id name " << engine_info(true) cout << "id name " << engine_info(true)
<< "\n" << Options << "\n" << Options
<< "\nuciok" << endl; << "\nuciok" << endl;
else if (token == "perft" && (is >> token)) // Read depth
{
stringstream ss;
ss << Options["Hash"] << " "
<< Options["Threads"] << " " << token << " current perft";
benchmark(pos, ss);
}
else else
cout << "Unknown command: " << cmd << endl; cout << "Unknown command: " << cmd << endl;
if (!args.empty()) // Command line arguments have one-shot behaviour
{
Threads.wait_for_search_finished();
break;
}
} }
} }
@@ -151,7 +173,7 @@ namespace {
else else
return; return;
pos.from_fen(fen, Options["UCI_Chess960"]); pos.from_fen(fen, Options["UCI_Chess960"], Threads.main_thread());
// Parse move list (if any) // Parse move list (if any)
while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE) while (is >> token && (m = move_from_uci(pos, token)) != MOVE_NONE)
@@ -182,81 +204,50 @@ namespace {
while (is >> token) while (is >> token)
value += string(" ", !value.empty()) + token; value += string(" ", !value.empty()) + token;
if (!Options.count(name)) if (Options.count(name))
cout << "No such option: " << name << endl;
else if (value.empty()) // UCI buttons don't have a value
Options[name] = true;
else
Options[name] = value; Options[name] = value;
else
cout << "No such option: " << name << endl;
} }
// go() is called when engine receives the "go" UCI command. The function sets // go() is called when engine receives the "go" UCI command. The function sets
// the thinking time and other parameters from the input string, and then starts // the thinking time and other parameters from the input string, and then starts
// the main searching thread. // the search.
void go(Position& pos, istringstream& is) { void go(Position& pos, istringstream& is) {
string token;
Search::LimitsType limits; Search::LimitsType limits;
std::set<Move> searchMoves; vector<Move> searchMoves;
int time[] = { 0, 0 }, inc[] = { 0, 0 }; string token;
while (is >> token) while (is >> token)
{ {
if (token == "infinite") if (token == "wtime")
is >> limits.time[WHITE];
else if (token == "btime")
is >> limits.time[BLACK];
else if (token == "winc")
is >> limits.inc[WHITE];
else if (token == "binc")
is >> limits.inc[BLACK];
else if (token == "movestogo")
is >> limits.movestogo;
else if (token == "depth")
is >> limits.depth;
else if (token == "nodes")
is >> limits.nodes;
else if (token == "movetime")
is >> limits.movetime;
else if (token == "infinite")
limits.infinite = true; limits.infinite = true;
else if (token == "ponder") else if (token == "ponder")
limits.ponder = true; limits.ponder = true;
else if (token == "wtime")
is >> time[WHITE];
else if (token == "btime")
is >> time[BLACK];
else if (token == "winc")
is >> inc[WHITE];
else if (token == "binc")
is >> inc[BLACK];
else if (token == "movestogo")
is >> limits.movesToGo;
else if (token == "depth")
is >> limits.maxDepth;
else if (token == "nodes")
is >> limits.maxNodes;
else if (token == "movetime")
is >> limits.maxTime;
else if (token == "searchmoves") else if (token == "searchmoves")
while (is >> token) while (is >> token)
searchMoves.insert(move_from_uci(pos, token)); searchMoves.push_back(move_from_uci(pos, token));
} }
limits.time = time[pos.side_to_move()]; Threads.start_searching(pos, limits, searchMoves);
limits.increment = inc[pos.side_to_move()];
Threads.start_thinking(pos, limits, searchMoves, true);
}
// perft() is called when engine receives the "perft" command. The function
// calls perft() with the required search depth then prints counted leaf nodes
// and elapsed time.
void perft(Position& pos, istringstream& is) {
int depth, time;
if (!(is >> depth))
return;
time = system_time();
int64_t n = Search::perft(pos, depth * ONE_PLY);
time = system_time() - time;
std::cout << "\nNodes " << n
<< "\nTime (ms) " << time
<< "\nNodes/second " << int(n / (time / 1000.0)) << std::endl;
} }
} }

View File

@@ -20,21 +20,32 @@
#include <algorithm> #include <algorithm>
#include <sstream> #include <sstream>
#include "evaluate.h"
#include "misc.h" #include "misc.h"
#include "thread.h" #include "thread.h"
#include "tt.h"
#include "ucioption.h" #include "ucioption.h"
using std::string; using std::string;
using namespace std;
OptionsMap Options; // Global object OptionsMap Options; // Global object
namespace {
/// 'On change' actions, triggered by an option's value change
void on_logger(const UCIOption& opt) { start_logger(opt); }
void on_eval(const UCIOption&) { Eval::init(); }
void on_threads(const UCIOption&) { Threads.read_uci_options(); }
void on_hash_size(const UCIOption& opt) { TT.set_size(opt); }
void on_clear_hash(const UCIOption&) { TT.clear(); }
/// Our case insensitive less() function as required by UCI protocol /// Our case insensitive less() function as required by UCI protocol
static bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); } bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); }
}
bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const { bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const {
return lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less); return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less);
} }
@@ -48,38 +59,41 @@ OptionsMap::OptionsMap() {
int msd = cpus < 8 ? 4 : 7; int msd = cpus < 8 ? 4 : 7;
OptionsMap& o = *this; OptionsMap& o = *this;
o["Use Debug Log"] = UCIOption(false, on_logger);
o["Use Search Log"] = UCIOption(false); o["Use Search Log"] = UCIOption(false);
o["Search Log Filename"] = UCIOption("SearchLog.txt"); o["Search Log Filename"] = UCIOption("SearchLog.txt");
o["Book File"] = UCIOption("book.bin"); o["Book File"] = UCIOption("book.bin");
o["Best Book Move"] = UCIOption(false); o["Best Book Move"] = UCIOption(false);
o["Mobility (Middle Game)"] = UCIOption(100, 0, 200); o["Mobility (Middle Game)"] = UCIOption(100, 0, 200, on_eval);
o["Mobility (Endgame)"] = UCIOption(100, 0, 200); o["Mobility (Endgame)"] = UCIOption(100, 0, 200, on_eval);
o["Passed Pawns (Middle Game)"] = UCIOption(100, 0, 200); o["Passed Pawns (Middle Game)"] = UCIOption(100, 0, 200, on_eval);
o["Passed Pawns (Endgame)"] = UCIOption(100, 0, 200); o["Passed Pawns (Endgame)"] = UCIOption(100, 0, 200, on_eval);
o["Space"] = UCIOption(100, 0, 200); o["Space"] = UCIOption(100, 0, 200, on_eval);
o["Aggressiveness"] = UCIOption(100, 0, 200); o["Aggressiveness"] = UCIOption(100, 0, 200, on_eval);
o["Cowardice"] = UCIOption(100, 0, 200); o["Cowardice"] = UCIOption(100, 0, 200, on_eval);
o["Min Split Depth"] = UCIOption(msd, 4, 7); o["Min Split Depth"] = UCIOption(msd, 4, 7, on_threads);
o["Max Threads per Split Point"] = UCIOption(5, 4, 8); o["Max Threads per Split Point"] = UCIOption(5, 4, 8, on_threads);
o["Threads"] = UCIOption(cpus, 1, MAX_THREADS); o["Threads"] = UCIOption(cpus, 1, MAX_THREADS, on_threads);
o["Use Sleeping Threads"] = UCIOption(false); o["Use Sleeping Threads"] = UCIOption(true, on_threads);
o["Hash"] = UCIOption(32, 4, 8192); o["Hash"] = UCIOption(32, 4, 8192, on_hash_size);
o["Clear Hash"] = UCIOption(false, "button"); o["Clear Hash"] = UCIOption(on_clear_hash);
o["Ponder"] = UCIOption(true); o["Ponder"] = UCIOption(true);
o["OwnBook"] = UCIOption(true); o["OwnBook"] = UCIOption(false);
o["MultiPV"] = UCIOption(1, 1, 500); o["MultiPV"] = UCIOption(1, 1, 500);
o["Skill Level"] = UCIOption(20, 0, 20); o["Skill Level"] = UCIOption(20, 0, 20);
o["Emergency Move Horizon"] = UCIOption(40, 0, 50); o["Emergency Move Horizon"] = UCIOption(40, 0, 50);
o["Emergency Base Time"] = UCIOption(200, 0, 30000); o["Emergency Base Time"] = UCIOption(200, 0, 30000);
o["Emergency Move Time"] = UCIOption(70, 0, 5000); o["Emergency Move Time"] = UCIOption(70, 0, 5000);
o["Minimum Thinking Time"] = UCIOption(20, 0, 5000); o["Minimum Thinking Time"] = UCIOption(20, 0, 5000);
o["Slow Mover"] = UCIOption(100, 10, 1000);
o["UCI_Chess960"] = UCIOption(false); o["UCI_Chess960"] = UCIOption(false);
o["UCI_AnalyseMode"] = UCIOption(false); o["UCI_AnalyseMode"] = UCIOption(false, on_eval);
} }
/// operator<<() is used to output all the UCI options in chronological insertion /// operator<<() is used to output all the UCI options in chronological insertion
/// order (the idx field) and in the format defined by the UCI protocol. /// order (the idx field) and in the format defined by the UCI protocol.
std::ostream& operator<<(std::ostream& os, const OptionsMap& om) { std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
for (size_t idx = 0; idx < om.size(); idx++) for (size_t idx = 0; idx < om.size(); idx++)
@@ -103,26 +117,35 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) {
/// UCIOption class c'tors /// UCIOption class c'tors
UCIOption::UCIOption(const char* v) : type("string"), min(0), max(0), idx(Options.size()) UCIOption::UCIOption(const char* v, Fn* f) : type("string"), min(0), max(0), idx(Options.size()), on_change(f)
{ defaultValue = currentValue = v; } { defaultValue = currentValue = v; }
UCIOption::UCIOption(bool v, string t) : type(t), min(0), max(0), idx(Options.size()) UCIOption::UCIOption(bool v, Fn* f) : type("check"), min(0), max(0), idx(Options.size()), on_change(f)
{ defaultValue = currentValue = (v ? "true" : "false"); } { defaultValue = currentValue = (v ? "true" : "false"); }
UCIOption::UCIOption(int v, int minv, int maxv) : type("spin"), min(minv), max(maxv), idx(Options.size()) UCIOption::UCIOption(Fn* f) : type("button"), min(0), max(0), idx(Options.size()), on_change(f)
{}
UCIOption::UCIOption(int v, int minv, int maxv, Fn* f) : type("spin"), min(minv), max(maxv), idx(Options.size()), on_change(f)
{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); } { std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); }
/// UCIOption::operator=() updates currentValue. Normally it's up to the GUI to /// UCIOption::operator=() updates currentValue. Normally it's up to the GUI to
/// check for option's limits, but we could receive the new value directly from /// check for option's limits, but we could receive the new value directly from
/// the user by teminal window, so let's check the bounds anyway. /// the user by console window, so let's check the bounds anyway.
void UCIOption::operator=(const string& v) { void UCIOption::operator=(const string& v) {
assert(!type.empty()); assert(!type.empty());
if ( !v.empty() if ( (type == "button" || !v.empty())
&& (type == "check" || type == "button") == (v == "true" || v == "false") && (type != "check" || (v == "true" || v == "false"))
&& (type != "spin" || (atoi(v.c_str()) >= min && atoi(v.c_str()) <= max))) && (type != "spin" || (atoi(v.c_str()) >= min && atoi(v.c_str()) <= max)))
currentValue = v; {
if (type != "button")
currentValue = v;
if (on_change)
(*on_change)(*this);
}
} }

View File

@@ -29,17 +29,19 @@ struct OptionsMap;
/// UCIOption class implements an option as defined by UCI protocol /// UCIOption class implements an option as defined by UCI protocol
class UCIOption { class UCIOption {
typedef void (Fn)(const UCIOption&);
public: public:
UCIOption() {} // Required by std::map::operator[] UCIOption(Fn* = NULL);
UCIOption(const char* v); UCIOption(bool v, Fn* = NULL);
UCIOption(bool v, std::string type = "check"); UCIOption(const char* v, Fn* = NULL);
UCIOption(int v, int min, int max); UCIOption(int v, int min, int max, Fn* = NULL);
void operator=(const std::string& v); void operator=(const std::string& v);
void operator=(bool v) { *this = std::string(v ? "true" : "false"); }
operator int() const { operator int() const {
assert(type == "check" || type == "button" || type == "spin"); assert(type == "check" || type == "spin");
return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true"); return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true");
} }
@@ -54,6 +56,7 @@ private:
std::string defaultValue, currentValue, type; std::string defaultValue, currentValue, type;
int min, max; int min, max;
size_t idx; size_t idx;
Fn* on_change;
}; };