From: Marco Costalba Date: Sat, 31 Jan 2015 12:04:15 +0000 (+0100) Subject: Sync with master X-Git-Url: https://git.sesse.net/?p=stockfish;a=commitdiff_plain;h=60c121f3b1ee7d5ced3435cc1718e4e6e6fd8383;hp=a3b4e9e23ca7f8949336014468b872e57da85762 Sync with master bench: 7374604 --- diff --git a/src/Makefile b/src/Makefile index 53bbc1f4..769b9d34 100644 --- a/src/Makefile +++ b/src/Makefile @@ -140,7 +140,7 @@ endif ### 3.1 Selecting compiler (default = gcc) -CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti $(EXTRACXXFLAGS) +CXXFLAGS += -Wall -Wcast-qual -fno-exceptions -fno-rtti -std=c++11 $(EXTRACXXFLAGS) LDFLAGS += $(EXTRALDFLAGS) ifeq ($(COMP),) @@ -150,7 +150,8 @@ endif ifeq ($(COMP),gcc) comp=gcc CXX=g++ - CXXFLAGS += -ansi -pedantic -Wno-long-long -Wextra -Wshadow + CXXFLAGS += -pedantic -Wno-long-long -Wextra -Wshadow + LDFLAGS += -Wl,--no-as-needed endif ifeq ($(COMP),mingw) diff --git a/src/benchmark.cpp b/src/benchmark.cpp index 605c95ad..cf0a315c 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -17,7 +17,6 @@ along with this program. If not, see . */ -#include #include #include #include @@ -34,7 +33,7 @@ using namespace std; namespace { -const char* Defaults[] = { +const vector Defaults = { "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 10", "8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 11", @@ -108,19 +107,19 @@ void benchmark(const Position& current, istream& is) { TT.clear(); if (limitType == "time") - limits.movetime = atoi(limit.c_str()); // movetime is in ms + limits.movetime = stoi(limit); // movetime is in ms else if (limitType == "nodes") - limits.nodes = atoi(limit.c_str()); + limits.nodes = stoi(limit); else if (limitType == "mate") - limits.mate = atoi(limit.c_str()); + limits.mate = stoi(limit); else - limits.depth = atoi(limit.c_str()); + limits.depth = stoi(limit); if (fenFile == "default") - fens.assign(Defaults, Defaults + 37); + fens = Defaults; else if (fenFile == "current") fens.push_back(current.fen()); @@ -128,7 +127,7 @@ void benchmark(const Position& current, istream& is) { else { string fen; - ifstream file(fenFile.c_str()); + ifstream file(fenFile); if (!file.is_open()) { @@ -164,7 +163,7 @@ void benchmark(const Position& current, istream& is) { } } - elapsed = std::max(Time::now() - elapsed, Time::point(1)); // Avoid a 'divide by zero' + elapsed = Time::now() - elapsed + 1; // Ensure positivity to avoid a 'divide by zero' dbg_print(); // Just before to exit diff --git a/src/bitbase.cpp b/src/bitbase.cpp index a018d3cd..ebf3c59a 100644 --- a/src/bitbase.cpp +++ b/src/bitbase.cpp @@ -17,7 +17,9 @@ along with this program. If not, see . */ +#include #include +#include #include #include "bitboard.h" @@ -54,17 +56,17 @@ namespace { inline Result& operator|=(Result& r, Result v) { return r = Result(r | v); } struct KPKPosition { - - KPKPosition(unsigned idx); + KPKPosition() = default; + explicit KPKPosition(unsigned idx); operator Result() const { return result; } Result classify(const std::vector& db) { return us == WHITE ? classify(db) : classify(db); } - private: template Result classify(const std::vector& db); + unsigned id; Color us; - Square bksq, wksq, psq; + Square ksq[COLOR_NB], psq; Result result; }; @@ -82,24 +84,20 @@ bool Bitbases::probe(Square wksq, Square wpsq, Square bksq, Color us) { void Bitbases::init() { - unsigned idx, repeat = 1; - std::vector db; - db.reserve(MAX_INDEX); + std::vector db(MAX_INDEX); // Initialize db with known win / draw positions - for (idx = 0; idx < MAX_INDEX; ++idx) - db.push_back(KPKPosition(idx)); + std::generate(db.begin(), db.end(), [](){ static unsigned id; return KPKPosition(id++); }); // Iterate through the positions until none of the unknown positions can be // changed to either wins or draws (15 cycles needed). - while (repeat) - for (repeat = idx = 0; idx < MAX_INDEX; ++idx) - repeat |= (db[idx] == UNKNOWN && db[idx].classify(db) != UNKNOWN); + while (std::accumulate(db.begin(), db.end(), false, [&](bool repeat, KPKPosition& pos) + { return (pos == UNKNOWN && pos.classify(db) != UNKNOWN) || repeat; })){} // Map 32 results into one KPKBitbase[] entry - for (idx = 0; idx < MAX_INDEX; ++idx) - if (db[idx] == WIN) - KPKBitbase[idx / 32] |= 1 << (idx & 0x1F); + for (auto& pos : db) + if (pos == WIN) + KPKBitbase[pos.id / 32] |= 1 << (pos.id & 0x1F); } @@ -107,69 +105,74 @@ namespace { KPKPosition::KPKPosition(unsigned idx) { - wksq = Square((idx >> 0) & 0x3F); - bksq = Square((idx >> 6) & 0x3F); - us = Color ((idx >> 12) & 0x01); - psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7)); - result = UNKNOWN; + id = idx; + ksq[WHITE] = Square((idx >> 0) & 0x3F); + ksq[BLACK] = Square((idx >> 6) & 0x3F); + us = Color ((idx >> 12) & 0x01); + psq = make_square(File((idx >> 13) & 0x3), RANK_7 - Rank((idx >> 15) & 0x7)); // Check if two pieces are on the same square or if a king can be captured - if ( distance(wksq, bksq) <= 1 - || wksq == psq - || bksq == psq - || (us == WHITE && (StepAttacksBB[PAWN][psq] & bksq))) + if ( distance(ksq[WHITE], ksq[BLACK]) <= 1 + || ksq[WHITE] == psq + || ksq[BLACK] == psq + || (us == WHITE && (StepAttacksBB[PAWN][psq] & ksq[BLACK]))) result = INVALID; - else if (us == WHITE) - { - // Immediate win if a pawn can be promoted without getting captured - if ( rank_of(psq) == RANK_7 - && wksq != psq + DELTA_N - && ( distance(bksq, psq + DELTA_N) > 1 - ||(StepAttacksBB[KING][wksq] & (psq + DELTA_N)))) - result = WIN; - } + // Immediate win if a pawn can be promoted without getting captured + else if ( us == WHITE + && rank_of(psq) == RANK_7 + && ksq[us] != psq + DELTA_N + && ( distance(ksq[~us], psq + DELTA_N) > 1 + || (StepAttacksBB[KING][ksq[us]] & (psq + DELTA_N)))) + result = WIN; + // Immediate draw if it is a stalemate or a king captures undefended pawn - else if ( !(StepAttacksBB[KING][bksq] & ~(StepAttacksBB[KING][wksq] | StepAttacksBB[PAWN][psq])) - || (StepAttacksBB[KING][bksq] & psq & ~StepAttacksBB[KING][wksq])) + else if ( us == BLACK + && ( !(StepAttacksBB[KING][ksq[us]] & ~(StepAttacksBB[KING][ksq[~us]] | StepAttacksBB[PAWN][psq])) + || (StepAttacksBB[KING][ksq[us]] & psq & ~StepAttacksBB[KING][ksq[~us]]))) result = DRAW; + + // Position will be classified later + else + result = UNKNOWN; } template Result KPKPosition::classify(const std::vector& db) { - // White to Move: If one move leads to a position classified as WIN, the result + // White to move: If one move leads to a position classified as WIN, the result // of the current position is WIN. If all moves lead to positions classified // as DRAW, the current position is classified as DRAW, otherwise the current // position is classified as UNKNOWN. // - // Black to Move: If one move leads to a position classified as DRAW, the result + // Black to move: If one move leads to a position classified as DRAW, the result // of the current position is DRAW. If all moves lead to positions classified // as WIN, the position is classified as WIN, otherwise the current position is // classified as UNKNOWN. - const Color Them = (Us == WHITE ? BLACK : WHITE); + const Color Them = (Us == WHITE ? BLACK : WHITE); + const Result Good = (Us == WHITE ? WIN : DRAW); + const Result Bad = (Us == WHITE ? DRAW : WIN); Result r = INVALID; - Bitboard b = StepAttacksBB[KING][Us == WHITE ? wksq : bksq]; + Bitboard b = StepAttacksBB[KING][ksq[Us]]; while (b) - r |= Us == WHITE ? db[index(Them, bksq, pop_lsb(&b), psq)] - : db[index(Them, pop_lsb(&b), wksq, psq)]; + r |= Us == WHITE ? db[index(Them, ksq[Them] , pop_lsb(&b), psq)] + : db[index(Them, pop_lsb(&b), ksq[Them] , psq)]; - if (Us == WHITE && rank_of(psq) < RANK_7) + if (Us == WHITE) { - Square s = psq + DELTA_N; - r |= db[index(BLACK, bksq, wksq, s)]; // Single push + if (rank_of(psq) < RANK_7) // Single push + r |= db[index(Them, ksq[Them], ksq[Us], psq + DELTA_N)]; - if (rank_of(psq) == RANK_2 && s != wksq && s != bksq) - r |= db[index(BLACK, bksq, wksq, s + DELTA_N)]; // Double push + if ( rank_of(psq) == RANK_2 // Double push + && psq + DELTA_N != ksq[Us] + && psq + DELTA_N != ksq[Them]) + r |= db[index(Them, ksq[Them], ksq[Us], psq + DELTA_N + DELTA_N)]; } - if (Us == WHITE) - return result = r & WIN ? WIN : r & UNKNOWN ? UNKNOWN : DRAW; - else - return result = r & DRAW ? DRAW : r & UNKNOWN ? UNKNOWN : WIN; + return result = r & Good ? Good : r & UNKNOWN ? UNKNOWN : Bad; } } // namespace diff --git a/src/endgame.cpp b/src/endgame.cpp index 2c87b2a1..836621d2 100644 --- a/src/endgame.cpp +++ b/src/endgame.cpp @@ -96,12 +96,9 @@ namespace { string fen = sides[0] + char(8 - sides[0].length() + '0') + "/8/8/8/8/8/8/" + sides[1] + char(8 - sides[1].length() + '0') + " w - - 0 10"; - return Position(fen, false, NULL).material_key(); + return Position(fen, false, nullptr).material_key(); } - template - void delete_endgame(const typename M::value_type& p) { delete p.second; } - } // namespace @@ -128,17 +125,11 @@ Endgames::Endgames() { add("KRPPKRP"); } -Endgames::~Endgames() { - - for_each(m1.begin(), m1.end(), delete_endgame); - for_each(m2.begin(), m2.end(), delete_endgame); -} -template +template void Endgames::add(const string& code) { - - map((Endgame*)0)[key(code, WHITE)] = new Endgame(WHITE); - map((Endgame*)0)[key(code, BLACK)] = new Endgame(BLACK); + map()[key(code, WHITE)] = std::unique_ptr>(new Endgame(WHITE)); + map()[key(code, BLACK)] = std::unique_ptr>(new Endgame(BLACK)); } diff --git a/src/endgame.h b/src/endgame.h index d7a7681a..cd05f0cd 100644 --- a/src/endgame.h +++ b/src/endgame.h @@ -21,7 +21,10 @@ #define ENDGAME_H_INCLUDED #include +#include #include +#include +#include #include "position.h" #include "types.h" @@ -63,11 +66,9 @@ enum EndgameType { /// Endgame functions can be of two types depending on whether they return a -/// Value or a ScaleFactor. Type eg_fun::type returns either ScaleFactor -/// or Value depending on whether the template parameter is 0 or 1. - -template struct eg_fun { typedef Value type; }; -template<> struct eg_fun<1> { typedef ScaleFactor type; }; +/// Value or a ScaleFactor. +template using +eg_type = typename std::conditional<(E < SCALING_FUNCTIONS), Value, ScaleFactor>::type; /// Base and derived templates for endgame evaluation and scaling functions @@ -81,7 +82,7 @@ struct EndgameBase { }; -template SCALING_FUNCTIONS)>::type> +template> struct Endgame : public EndgameBase { explicit Endgame(Color c) : strongSide(c), weakSide(~c) {} @@ -99,23 +100,24 @@ private: class Endgames { - typedef std::map::type>*> M1; - typedef std::map::type>*> M2; + template using Map = std::map>>; - M1 m1; - M2 m2; + template> + void add(const std::string& code); - M1& map(M1::mapped_type) { return m1; } - M2& map(M2::mapped_type) { return m2; } + template + Map& map() { + return std::get::value>(maps); + } - template void add(const std::string& code); + std::pair, Map> maps; public: Endgames(); - ~Endgames(); - template T probe(Key key, T& eg) { - return eg = map(eg).count(key) ? map(eg)[key] : NULL; + template + EndgameBase* probe(Key key) { + return map().count(key) ? map()[key].get() : nullptr; } }; diff --git a/src/material.cpp b/src/material.cpp index 094a13df..e2294212 100644 --- a/src/material.cpp +++ b/src/material.cpp @@ -139,7 +139,7 @@ Entry* probe(const Position& pos) { // Let's look if we have a specialized evaluation function for this particular // material configuration. Firstly we look for a fixed configuration one, then // for a generic one if the previous search failed. - if (pos.this_thread()->endgames.probe(key, e->evaluationFunction)) + if ((e->evaluationFunction = pos.this_thread()->endgames.probe(key)) != nullptr) return e; if (is_KXK(pos)) @@ -158,7 +158,7 @@ Entry* probe(const Position& pos) { // configuration. Is there a suitable specialized scaling function? EndgameBase* sf; - if (pos.this_thread()->endgames.probe(key, sf)) + if ((sf = pos.this_thread()->endgames.probe(key)) != nullptr) { e->scalingFunction[sf->strong_side()] = sf; // Only strong color assigned return e; diff --git a/src/material.h b/src/material.h index 0119caab..67d555a4 100644 --- a/src/material.h +++ b/src/material.h @@ -40,7 +40,7 @@ struct Entry { Score imbalance() const { return make_score(value, value); } Phase game_phase() const { return gamePhase; } - bool specialized_eval_exists() const { return evaluationFunction != NULL; } + bool specialized_eval_exists() const { return evaluationFunction != nullptr; } Value evaluate(const Position& pos) const { return (*evaluationFunction)(pos); } // scale_factor takes a position and a color as input and returns a scale factor diff --git a/src/misc.cpp b/src/misc.cpp index 656b3155..2f99668d 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -17,6 +17,7 @@ along with this program. If not, see . */ +#include #include #include #include @@ -26,6 +27,7 @@ #include "thread.h" using namespace std; +using namespace std::chrono; namespace { @@ -123,6 +125,13 @@ const string engine_info(bool to_uci) { } +/// Convert system time to milliseconds. That's all we need. + +Time::point Time::now() { + return duration_cast(steady_clock::now().time_since_epoch()).count(); +} + + /// Debug functions used mainly to collect run-time statistics void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; } @@ -146,7 +155,7 @@ void dbg_print() { std::ostream& operator<<(std::ostream& os, SyncCout sc) { - static Mutex m; + static std::mutex m; if (sc == IO_LOCK) m.lock(); @@ -162,25 +171,6 @@ std::ostream& operator<<(std::ostream& os, SyncCout sc) { void start_logger(bool b) { Logger::start(b); } -/// timed_wait() waits for msec milliseconds. It is mainly a helper to wrap -/// the conversion from milliseconds to struct timespec, as used by pthreads. - -void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) { - -#ifdef _WIN32 - int tm = msec; -#else - timespec ts, *tm = &ts; - uint64_t ms = Time::now() + msec; - - ts.tv_sec = ms / 1000; - ts.tv_nsec = (ms % 1000) * 1000000LL; -#endif - - cond_timedwait(sleepCond, sleepLock, tm); -} - - /// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking /// function that doesn't stall the CPU waiting for data to be loaded from memory, /// which can be quite slow. diff --git a/src/misc.h b/src/misc.h index 965a0ee8..e3fb6435 100644 --- a/src/misc.h +++ b/src/misc.h @@ -28,7 +28,6 @@ #include "types.h" const std::string engine_info(bool to_uci = false); -void timed_wait(WaitCondition&, Lock&, int); void prefetch(char* addr); void start_logger(bool b); @@ -40,17 +39,16 @@ void dbg_print(); namespace Time { typedef int64_t point; - inline point now() { return system_time_to_msec(); } + point now(); } template struct HashTable { - HashTable() : table(Size, Entry()) {} Entry* operator[](Key key) { return &table[(uint32_t)key & (Size - 1)]; } private: - std::vector table; + std::vector table = std::vector(Size); }; diff --git a/src/movegen.cpp b/src/movegen.cpp index bfc8858a..cf65cd62 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -256,7 +256,7 @@ namespace { template FORCE_INLINE ExtMove* generate_all(const Position& pos, ExtMove* moveList, Bitboard target, - const CheckInfo* ci = NULL) { + const CheckInfo* ci = nullptr) { const bool Checks = Type == QUIET_CHECKS; diff --git a/src/movegen.h b/src/movegen.h index 62a121d7..6f1e24a4 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -50,18 +50,17 @@ ExtMove* generate(const Position& pos, ExtMove* moveList); template struct MoveList { - explicit MoveList(const Position& pos) : cur(moveList), last(generate(pos, moveList)) { last->move = MOVE_NONE; } - void operator++() { ++cur; } - Move operator*() const { return cur->move; } + explicit MoveList(const Position& pos) : last(generate(pos, moveList)) {} + const ExtMove* begin() const { return moveList; } + const ExtMove* end() const { return last; } size_t size() const { return last - moveList; } bool contains(Move m) const { - for (const ExtMove* it(moveList); it != last; ++it) if (it->move == m) return true; + for (const ExtMove& ms : *this) if (ms.move == m) return true; return false; } private: - ExtMove moveList[MAX_MOVES]; - ExtMove *cur, *last; + ExtMove moveList[MAX_MOVES], *last; }; #endif // #ifndef MOVEGEN_H_INCLUDED diff --git a/src/movepick.cpp b/src/movepick.cpp index edee8179..b1fa91d0 100644 --- a/src/movepick.cpp +++ b/src/movepick.cpp @@ -49,10 +49,6 @@ namespace { } } - // Unary predicate used by std::partition to split positive values from remaining - // ones so as to sort the two sets separately, with the second sort delayed. - inline bool has_positive_value(const ExtMove& move) { return move.value > VALUE_ZERO; } - // Picks the best move in the range (begin, end) and moves it to the front. // It's faster than sorting all the moves in advance when there are few // moves e.g. possible captures. @@ -247,7 +243,7 @@ void MovePicker::generate_next_stage() { case QUIETS_1_S1: endQuiets = end = generate(pos, moves); score(); - end = std::partition(cur, end, has_positive_value); + end = std::partition(cur, end, [](const ExtMove& m) { return m.value > VALUE_ZERO; }); insertion_sort(cur, end); return; diff --git a/src/movepick.h b/src/movepick.h index a7b25a92..9482a89c 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -80,10 +80,10 @@ typedef Stats > MovesStats; /// to get a cut-off first. class MovePicker { - - MovePicker& operator=(const MovePicker&); // Silence a warning under MSVC - public: + MovePicker(const MovePicker&) = delete; + MovePicker& operator=(const MovePicker&) = delete; + MovePicker(const Position&, Move, Depth, const HistoryStats&, Square); MovePicker(const Position&, Move, const HistoryStats&, PieceType); MovePicker(const Position&, Move, Depth, const HistoryStats&, Move*, Move*, Search::Stack*); diff --git a/src/platform.h b/src/platform.h deleted file mode 100644 index 47c0b117..00000000 --- a/src/platform.h +++ /dev/null @@ -1,116 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2008 Tord Romstad (Glaurung author) - Copyright (C) 2008-2015 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 . -*/ - -#ifndef PLATFORM_H_INCLUDED -#define PLATFORM_H_INCLUDED - -#ifdef _MSC_VER - -// Disable some silly and noisy warnings 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 -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 -#endif - -#ifndef _WIN32 // Linux - Unix - -# include - -inline int64_t system_time_to_msec() { - timeval t; - gettimeofday(&t, NULL); - return t.tv_sec * 1000LL + t.tv_usec / 1000; -} - -# include -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 - -inline int64_t system_time_to_msec() { - _timeb t; - _ftime(&t); - return t.time * 1000LL + t.millitm; -} - -#ifndef NOMINMAX -# define NOMINMAX // disable macros min() and max() -#endif - -#define WIN32_LEAN_AND_MEAN -#include -#undef WIN32_LEAN_AND_MEAN -#undef NOMINMAX - -// We use critical sections on Windows to support Windows XP and older versions. -// Unfortunately, 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; - -// On Windows 95 and 98 parameter lpThreadId may not be null -inline DWORD* dwWin9xKludge() { static DWORD dw; return &dw; } - -# 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,dwWin9xKludge())) -# define thread_join(x) { WaitForSingleObject(x, INFINITE); CloseHandle(x); } - -#endif - -#endif // #ifndef PLATFORM_H_INCLUDED diff --git a/src/position.cpp b/src/position.cpp index 994e1c76..c0c26f03 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -1147,10 +1147,6 @@ bool Position::is_draw() const { /// Position::flip() flips position with the white and black sides reversed. This /// is only useful for debugging e.g. for finding evaluation symmetry bugs. -static char toggle_case(char c) { - return char(islower(c) ? toupper(c) : tolower(c)); -} - void Position::flip() { string f, token; @@ -1168,7 +1164,8 @@ void Position::flip() { ss >> token; // Castling availability f += token + " "; - std::transform(f.begin(), f.end(), f.begin(), toggle_case); + std::transform(f.begin(), f.end(), f.begin(), + [](char c) { return char(islower(c) ? toupper(c) : tolower(c)); }); ss >> token; // En passant square f += (token == "-" ? token : token.replace(1, 1, token[1] == '3' ? "6" : "3")); diff --git a/src/position.h b/src/position.h index 447872ab..0bb7b0b9 100644 --- a/src/position.h +++ b/src/position.h @@ -82,12 +82,11 @@ class Position { friend std::ostream& operator<<(std::ostream&, const Position&); - Position(const Position&); // Disable the default copy constructor - public: static void init(); - Position() {} // To define the global object RootPos + Position() = default; // To define the global object RootPos + Position(const Position&) = delete; Position(const Position& pos, Thread* th) { *this = pos; thisThread = th; } Position(const std::string& f, bool c960, Thread* th) { set(f, c960, th); } Position& operator=(const Position&); // To assign RootPos from UCI @@ -175,7 +174,7 @@ public: Value non_pawn_material(Color c) const; // Position consistency check, for debugging - bool pos_is_ok(int* step = NULL) const; + bool pos_is_ok(int* step = nullptr) const; void flip(); private: diff --git a/src/search.cpp b/src/search.cpp index eea52984..c1884691 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -66,22 +66,29 @@ namespace { // Different node types, used as template parameter enum NodeType { Root, PV, NonPV }; - // Dynamic razoring margin based on depth + // Razoring and futility margin based on depth inline Value razor_margin(Depth d) { return Value(512 + 32 * d); } + inline Value futility_margin(Depth d) { return Value(200 * d); } - // Futility lookup tables (initialized at startup) and their access functions - int FutilityMoveCounts[2][16]; // [improving][depth] + // Futility and reductions lookup tables, initialized at startup + int FutilityMoveCounts[2][16]; // [improving][depth] + Depth Reductions[2][2][64][64]; // [pv][improving][depth][moveNumber] - inline Value futility_margin(Depth d) { - return Value(200 * d); + template inline Depth reduction(bool i, Depth d, int mn) { + return Reductions[PvNode][i][std::min(d, 63 * ONE_PLY)][std::min(mn, 63)]; } - // Reduction lookup tables (initialized at startup) and their access function - int8_t Reductions[2][2][64][64]; // [pv][improving][depth][moveNumber] + // Skill struct is used to implement strength limiting + struct Skill { + Skill(int l) : level(l) {} + bool enabled() const { return level < 20; } + bool time_to_pick(Depth depth) const { return depth / ONE_PLY == 1 + level; } + Move best_move(size_t multiPV) { return best ? best : pick_best(multiPV); } + Move pick_best(size_t multiPV); - template inline Depth reduction(bool i, Depth d, int mn) { - return (Depth) Reductions[PvNode][i][std::min(int(d), 63)][std::min(mn, 63)]; - } + int level; + Move best = MOVE_NONE; + }; size_t PVIdx; TimeManager TimeMgr; @@ -102,26 +109,6 @@ namespace { Value value_from_tt(Value v, int ply); void update_pv(Move* pv, Move move, Move* childPv); void update_stats(const Position& pos, Stack* ss, Move move, Depth depth, Move* quiets, int quietsCnt); - string uci_pv(const Position& pos, Depth depth, Value alpha, Value beta); - - struct Skill { - Skill(int l, size_t rootSize) : level(l), - candidates(l < 20 ? std::min(4, (int)rootSize) : 0), - best(MOVE_NONE) {} - ~Skill() { - if (candidates) // Swap best PV line with the sub-optimal one - std::swap(RootMoves[0], *std::find(RootMoves.begin(), - RootMoves.end(), best ? best : pick_move())); - } - - size_t candidates_size() const { return candidates; } - bool time_to_pick(Depth depth) const { return depth / ONE_PLY == 1 + level; } - Move pick_move(); - - int level; - size_t candidates; - Move best; - }; } // namespace @@ -130,25 +117,23 @@ namespace { void Search::init() { - // Init reductions array - for (int d = 1; d < 64; ++d) - for (int mc = 1; mc < 64; ++mc) - { - double pvRed = 0.00 + log(double(d)) * log(double(mc)) / 3.00; - double nonPVRed = 0.33 + log(double(d)) * log(double(mc)) / 2.25; + const double K[][2] = {{ 0.83, 2.25 }, { 0.50, 3.00 }}; - Reductions[1][1][d][mc] = int8_t( pvRed >= 1.0 ? pvRed + 0.5: 0); - Reductions[0][1][d][mc] = int8_t(nonPVRed >= 1.0 ? nonPVRed + 0.5: 0); + for (int pv = 0; pv <= 1; ++pv) + for (int imp = 0; imp <= 1; ++imp) + for (int d = 1; d < 64; ++d) + for (int mc = 1; mc < 64; ++mc) + { + double r = K[pv][0] + log(d) * log(mc) / K[pv][1]; - Reductions[1][0][d][mc] = Reductions[1][1][d][mc]; - Reductions[0][0][d][mc] = Reductions[0][1][d][mc]; + if (r >= 1.5) + Reductions[pv][imp][d][mc] = int(r) * ONE_PLY; - // Increase reduction when eval is not improving - if (Reductions[0][0][d][mc] >= 2) - Reductions[0][0][d][mc] += 1; - } + // Increase reduction when eval is not improving + if (!pv && !imp && Reductions[pv][imp][d][mc] >= 2 * ONE_PLY) + Reductions[pv][imp][d][mc] += ONE_PLY; + } - // Init futility move count array for (int d = 0; d < 16; ++d) { FutilityMoveCounts[0][d] = int(2.4 + 0.773 * pow(d + 0.00, 1.8)); @@ -167,19 +152,19 @@ uint64_t Search::perft(Position& pos, Depth depth) { CheckInfo ci(pos); const bool leaf = (depth == 2 * ONE_PLY); - for (MoveList it(pos); *it; ++it) + for (const ExtMove& ms : MoveList(pos)) { if (Root && depth <= ONE_PLY) cnt = 1, nodes++; else { - pos.do_move(*it, st, ci, pos.gives_check(*it, ci)); + pos.do_move(ms.move, st, ci, pos.gives_check(ms.move, ci)); cnt = leaf ? MoveList(pos).size() : perft(pos, depth - ONE_PLY); nodes += cnt; - pos.undo_move(*it); + pos.undo_move(ms.move); } if (Root) - sync_cout << UCI::move(*it, pos.is_chess960()) << ": " << cnt << sync_endl; + sync_cout << UCI::move(ms.move, pos.is_chess960()) << ": " << cnt << sync_endl; } return nodes; } @@ -252,8 +237,8 @@ void Search::think() { } } - for (size_t i = 0; i < Threads.size(); ++i) - Threads[i]->maxPly = 0; + for (Thread* th : Threads) + th->maxPly = 0; Threads.timer->run = true; Threads.timer->notify_one(); // Wake up the recurring timer @@ -309,11 +294,14 @@ namespace { Followupmoves.clear(); size_t multiPV = Options["MultiPV"]; - Skill skill(Options["Skill Level"], RootMoves.size()); + Skill skill(Options["Skill Level"]); - // Do we have to play with skill handicap? In this case enable MultiPV search - // that we will use behind the scenes to retrieve a set of possible moves. - multiPV = std::max(multiPV, skill.candidates_size()); + // When playing with strength handicap enable MultiPV search that we will + // use behind the scenes to retrieve a set of possible moves. + if (skill.enabled()) + multiPV = std::max(multiPV, (size_t)4); + + multiPV = std::min(multiPV, RootMoves.size()); // Iterative deepening loop until requested to stop or target depth reached while (++depth < DEPTH_MAX && !Signals.stop && (!Limits.depth || depth <= Limits.depth)) @@ -323,11 +311,11 @@ namespace { // Save the last iteration's scores before first PV line is searched and // all the move scores except the (new) PV are set to -VALUE_INFINITE. - for (size_t i = 0; i < RootMoves.size(); ++i) - RootMoves[i].previousScore = RootMoves[i].score; + for (RootMove& rm : RootMoves) + rm.previousScore = rm.score; // MultiPV loop. We perform a full root search for each PV line - for (PVIdx = 0; PVIdx < std::min(multiPV, RootMoves.size()) && !Signals.stop; ++PVIdx) + for (PVIdx = 0; PVIdx < multiPV && !Signals.stop; ++PVIdx) { // Reset aspiration window starting size if (depth >= 5 * ONE_PLY) @@ -368,7 +356,7 @@ namespace { if ( multiPV == 1 && (bestValue <= alpha || bestValue >= beta) && Time::now() - SearchTime > 3000) - sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl; + sync_cout << UCI::pv(pos, depth, alpha, beta) << sync_endl; // In case of failing low/high increase aspiration window and // re-search, otherwise exit the loop. @@ -400,14 +388,13 @@ namespace { sync_cout << "info nodes " << RootPos.nodes_searched() << " time " << Time::now() - SearchTime << sync_endl; - else if ( PVIdx + 1 == std::min(multiPV, RootMoves.size()) - || Time::now() - SearchTime > 3000) - sync_cout << uci_pv(pos, depth, alpha, beta) << sync_endl; + else if (PVIdx + 1 == multiPV || Time::now() - SearchTime > 3000) + sync_cout << UCI::pv(pos, depth, alpha, beta) << sync_endl; } - // If skill levels are enabled and time is up, pick a sub-optimal best move - if (skill.candidates_size() && skill.time_to_pick(depth)) - skill.pick_move(); + // If skill level is enabled and time is up, pick a sub-optimal best move + if (skill.enabled() && skill.time_to_pick(depth)) + skill.pick_best(multiPV); // Have we found a "mate in x"? if ( Limits.mate @@ -436,6 +423,11 @@ namespace { } } } + + // If skill level is enabled, swap best PV line with the sub-optimal one + if (skill.enabled()) + std::swap(RootMoves[0], *std::find(RootMoves.begin(), + RootMoves.end(), skill.best_move(multiPV))); } @@ -477,7 +469,7 @@ namespace { splitPoint = ss->splitPoint; bestMove = splitPoint->bestMove; bestValue = splitPoint->bestValue; - tte = NULL; + tte = nullptr; ttHit = false; ttMove = excludedMove = MOVE_NONE; ttValue = VALUE_NONE; @@ -540,7 +532,7 @@ namespace { // If ttMove is quiet, update killers, history, counter move and followup move on TT hit if (ttValue >= beta && ttMove && !pos.capture_or_promotion(ttMove) && !inCheck) - update_stats(pos, ss, ttMove, depth, NULL, 0); + update_stats(pos, ss, ttMove, depth, nullptr, 0); return ttValue; } @@ -789,7 +781,7 @@ moves_loop: // When in check and at SpNode search starts from here } if (PvNode) - (ss+1)->pv = NULL; + (ss+1)->pv = nullptr; extension = DEPTH_ZERO; captureOrPromotion = pos.capture_or_promotion(move); @@ -1352,6 +1344,7 @@ moves_loop: // When in check and at SpNode search starts from here // played quiet moves. Value bonus = Value((depth / ONE_PLY) * (depth / ONE_PLY)); History.update(pos.moved_piece(move), to_sq(move), bonus); + for (int i = 0; i < quietsCnt; ++i) { Move m = quiets[i]; @@ -1372,24 +1365,23 @@ moves_loop: // When in check and at SpNode search starts from here } - // When playing with a strength handicap, choose best move among the first 'candidates' - // RootMoves using a statistical rule dependent on 'level'. Idea by Heinz van Saanen. + // When playing with strength handicap, choose best move among a set of RootMoves + // using a statistical rule dependent on 'level'. Idea by Heinz van Saanen. - Move Skill::pick_move() { + Move Skill::pick_best(size_t multiPV) { // PRNG sequence should be non-deterministic, so we seed it with the time at init static PRNG rng(Time::now()); // RootMoves are already sorted by score in descending order - int variance = std::min(RootMoves[0].score - RootMoves[candidates - 1].score, PawnValueMg); + int variance = std::min(RootMoves[0].score - RootMoves[multiPV - 1].score, PawnValueMg); int weakness = 120 - 2 * level; int maxScore = -VALUE_INFINITE; - best = MOVE_NONE; // Choose best move. For each move score we add two terms both dependent on - // weakness. One deterministic and bigger for weaker moves, and one random, + // weakness. One deterministic and bigger for weaker levels, and one random, // then we choose the move with the resulting highest score. - for (size_t i = 0; i < candidates; ++i) + for (size_t i = 0; i < multiPV; ++i) { int score = RootMoves[i].score; @@ -1406,64 +1398,64 @@ moves_loop: // When in check and at SpNode search starts from here return best; } +} // namespace - // uci_pv() formats PV information according to the UCI protocol. UCI - // requires that all (if any) unsearched PV lines are sent using a previous - // search score. - string uci_pv(const Position& pos, Depth depth, Value alpha, Value beta) { +/// UCI::pv() formats PV information according to the UCI protocol. UCI requires +/// that all (if any) unsearched PV lines are sent using a previous search score. - std::stringstream ss; - Time::point elapsed = Time::now() - SearchTime + 1; - size_t uciPVSize = std::min((size_t)Options["MultiPV"], RootMoves.size()); - int selDepth = 0; +string UCI::pv(const Position& pos, Depth depth, Value alpha, Value beta) { - for (size_t i = 0; i < Threads.size(); ++i) - if (Threads[i]->maxPly > selDepth) - selDepth = Threads[i]->maxPly; + std::stringstream ss; + Time::point elapsed = Time::now() - SearchTime + 1; + size_t multiPV = std::min((size_t)Options["MultiPV"], RootMoves.size()); + int selDepth = 0; - for (size_t i = 0; i < uciPVSize; ++i) - { - bool updated = (i <= PVIdx); + for (Thread* th : Threads) + if (th->maxPly > selDepth) + selDepth = th->maxPly; - if (depth == ONE_PLY && !updated) - continue; + for (size_t i = 0; i < multiPV; ++i) + { + bool updated = (i <= PVIdx); - Depth d = updated ? depth : depth - ONE_PLY; - Value v = updated ? RootMoves[i].score : RootMoves[i].previousScore; + if (depth == ONE_PLY && !updated) + continue; - bool tb = TB::RootInTB && abs(v) < VALUE_MATE - MAX_PLY; - v = tb ? TB::Score : v; + Depth d = updated ? depth : depth - ONE_PLY; + Value v = updated ? RootMoves[i].score : RootMoves[i].previousScore; - if (ss.rdbuf()->in_avail()) // Not at first line - ss << "\n"; + bool tb = TB::RootInTB && abs(v) < VALUE_MATE - MAX_PLY; + v = tb ? TB::Score : v; - ss << "info depth " << d / ONE_PLY - << " seldepth " << selDepth - << " multipv " << i + 1 - << " score " << UCI::value(v); + if (ss.rdbuf()->in_avail()) // Not at first line + ss << "\n"; - if (!tb && i == PVIdx) - ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : ""); + ss << "info" + << " depth " << d / ONE_PLY + << " seldepth " << selDepth + << " multipv " << i + 1 + << " score " << UCI::value(v); - ss << " nodes " << pos.nodes_searched() - << " nps " << pos.nodes_searched() * 1000 / elapsed; + if (!tb && i == PVIdx) + ss << (v >= beta ? " lowerbound" : v <= alpha ? " upperbound" : ""); - if (elapsed > 1000) // Earlier makes little sense - ss << " hashfull " << TT.hashfull(); + ss << " nodes " << pos.nodes_searched() + << " nps " << pos.nodes_searched() * 1000 / elapsed; - ss << " tbhits " << TB::Hits - << " time " << elapsed - << " pv"; + if (elapsed > 1000) // Earlier makes little sense + ss << " hashfull " << TT.hashfull(); - for (size_t j = 0; j < RootMoves[i].pv.size(); ++j) - ss << " " << UCI::move(RootMoves[i].pv[j], pos.is_chess960()); - } + ss << " tbhits " << TB::Hits + << " time " << elapsed + << " pv"; - return ss.str(); + for (Move m : RootMoves[i].pv) + ss << " " << UCI::move(m, pos.is_chess960()); } -} // namespace + return ss.str(); +} /// RootMove::insert_pv_in_tt() is called at the end of a search iteration, and @@ -1473,22 +1465,22 @@ moves_loop: // When in check and at SpNode search starts from here void RootMove::insert_pv_in_tt(Position& pos) { StateInfo state[MAX_PLY], *st = state; - size_t idx = 0; + bool ttHit; - for ( ; idx < pv.size(); ++idx) + for (Move m : pv) { - bool ttHit; - TTEntry* tte = TT.probe(pos.key(), ttHit); + assert(MoveList(pos).contains(m)); - if (!ttHit || tte->move() != pv[idx]) // Don't overwrite correct entries - tte->save(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, pv[idx], VALUE_NONE, TT.generation()); + TTEntry* tte = TT.probe(pos.key(), ttHit); - assert(MoveList(pos).contains(pv[idx])); + if (!ttHit || tte->move() != m) // Don't overwrite correct entries + tte->save(pos.key(), VALUE_NONE, BOUND_NONE, DEPTH_NONE, m, VALUE_NONE, TT.generation()); - pos.do_move(pv[idx], *st++); + pos.do_move(m, *st++); } - while (idx) pos.undo_move(pv[--idx]); + for (size_t i = pv.size(); i > 0; ) + pos.undo_move(pv[--i]); } @@ -1497,22 +1489,25 @@ void RootMove::insert_pv_in_tt(Position& pos) { /// root. We try hard to have a ponder move to return to the GUI, otherwise in case of /// 'ponder on' we have nothing to think on. -Move RootMove::extract_ponder_from_tt(Position& pos) +bool RootMove::extract_ponder_from_tt(Position& pos) { StateInfo st; - bool found; + bool ttHit; assert(pv.size() == 1); pos.do_move(pv[0], st); - TTEntry* tte = TT.probe(pos.key(), found); - Move m = found ? tte->move() : MOVE_NONE; - if (!MoveList(pos).contains(m)) - m = MOVE_NONE; - + TTEntry* tte = TT.probe(pos.key(), ttHit); pos.undo_move(pv[0]); - pv.push_back(m); - return m; + + if (ttHit) + { + Move m = tte->move(); // Local copy to be SMP safe + if (MoveList(pos).contains(m)) + return pv.push_back(m), true; + } + + return false; } @@ -1522,7 +1517,7 @@ void Thread::idle_loop() { // Pointer 'this_sp' is not null only if we are called from split(), and not // at the thread creation. This means we are the split point's master. - SplitPoint* this_sp = splitPointsSize ? activeSplitPoint : NULL; + SplitPoint* this_sp = splitPointsSize ? activeSplitPoint : nullptr; assert(!this_sp || (this_sp->masterThread == this && searching)); @@ -1546,7 +1541,7 @@ void Thread::idle_loop() { sp->mutex.lock(); - assert(activePosition == NULL); + assert(activePosition == nullptr); activePosition = &pos; @@ -1565,7 +1560,7 @@ void Thread::idle_loop() { assert(searching); searching = false; - activePosition = NULL; + activePosition = nullptr; sp->slavesMask.reset(idx); sp->allSlavesSearching = false; sp->nodes += pos.nodes_searched(); @@ -1590,7 +1585,7 @@ void Thread::idle_loop() { for (size_t i = 0; i < Threads.size(); ++i) { const int size = Threads[i]->splitPointsSize; // Local copy - sp = size ? &Threads[i]->splitPoints[size - 1] : NULL; + sp = size ? &Threads[i]->splitPoints[size - 1] : nullptr; if ( sp && sp->allSlavesSearching @@ -1617,22 +1612,19 @@ void Thread::idle_loop() { } // Grab the lock to avoid races with Thread::notify_one() - mutex.lock(); + std::unique_lock lk(mutex); // If we are master and all slaves have finished then exit idle_loop if (this_sp && this_sp->slavesMask.none()) { assert(!searching); - mutex.unlock(); break; } // If we are not searching, wait for a condition to be signaled instead of // wasting CPU time polling for work. if (!searching && !exit) - sleepCondition.wait(mutex); - - mutex.unlock(); + sleepCondition.wait(lk); } } @@ -1677,10 +1669,10 @@ void check_time() { // Loop across all split points and sum accumulated SplitPoint nodes plus // all the currently active positions nodes. - for (size_t i = 0; i < Threads.size(); ++i) - for (int j = 0; j < Threads[i]->splitPointsSize; ++j) + for (Thread* th : Threads) + for (int i = 0; i < th->splitPointsSize; ++i) { - SplitPoint& sp = Threads[i]->splitPoints[j]; + SplitPoint& sp = th->splitPoints[i]; sp.mutex.lock(); diff --git a/src/search.h b/src/search.h index 5a0ad5df..32cd1bed 100644 --- a/src/search.h +++ b/src/search.h @@ -60,7 +60,7 @@ struct RootMove { bool operator<(const RootMove& m) const { return score > m.score; } // Ascending sort bool operator==(const Move& m) const { return pv[0] == m; } void insert_pv_in_tt(Position& pos); - Move extract_ponder_from_tt(Position& pos); + bool extract_ponder_from_tt(Position& pos); Value score; Value previousScore; @@ -96,7 +96,7 @@ struct SignalsType { bool stop, stopOnPonderhit, firstRootMove, failedLowAtRoot; }; -typedef std::auto_ptr > StateStackPtr; +typedef std::unique_ptr> StateStackPtr; extern volatile SignalsType Signals; extern LimitsType Limits; diff --git a/src/syzygy/tbprobe.cpp b/src/syzygy/tbprobe.cpp index 0abd2b2e..126fe91c 100644 --- a/src/syzygy/tbprobe.cpp +++ b/src/syzygy/tbprobe.cpp @@ -7,6 +7,8 @@ this code to other chess engines. */ +#define NOMINMAX + #include #include "../position.h" diff --git a/src/thread.cpp b/src/thread.cpp index b8571dcd..c232b849 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -33,19 +33,13 @@ extern void check_time(); namespace { - // start_routine() is the C function which is called when a new thread - // is launched. It is a wrapper to the virtual function idle_loop(). - - extern "C" { long start_routine(ThreadBase* th) { th->idle_loop(); return 0; } } - - // Helpers to launch a thread after creation and joining before delete. Must be // outside Thread c'tor and d'tor because the object must be fully initialized // when start_routine (and hence virtual idle_loop) is called and when joining. template T* new_thread() { T* th = new T(); - thread_create(th->handle, start_routine, th); // Will go to sleep + th->nativeThread = std::thread(&ThreadBase::idle_loop, th); // Will go to sleep return th; } @@ -56,7 +50,7 @@ namespace { th->mutex.unlock(); th->notify_one(); - thread_join(th->handle); // Wait for thread termination + th->nativeThread.join(); // Wait for thread termination delete th; } @@ -67,9 +61,8 @@ namespace { void ThreadBase::notify_one() { - mutex.lock(); + std::unique_lock(this->mutex); sleepCondition.notify_one(); - mutex.unlock(); } @@ -77,9 +70,8 @@ void ThreadBase::notify_one() { void ThreadBase::wait_for(volatile const bool& condition) { - mutex.lock(); - while (!condition) sleepCondition.wait(mutex); - mutex.unlock(); + std::unique_lock lk(mutex); + sleepCondition.wait(lk, [&]{ return condition; }); } @@ -90,8 +82,8 @@ Thread::Thread() /* : splitPoints() */ { // Initialization of non POD broken in searching = false; maxPly = splitPointsSize = 0; - activeSplitPoint = NULL; - activePosition = NULL; + activeSplitPoint = nullptr; + activePosition = nullptr; idx = Threads.size(); // Starts from 0 } @@ -178,11 +170,11 @@ void Thread::split(Position& pos, Stack* ss, Value alpha, Value beta, Value* bes sp.allSlavesSearching = true; // Must be set under lock protection ++splitPointsSize; activeSplitPoint = &sp; - activePosition = NULL; + activePosition = nullptr; Thread* slave; - while ((slave = Threads.available_slave(this)) != NULL) + while ((slave = Threads.available_slave(this)) != nullptr) { sp.slavesMask.set(slave->idx); slave->activeSplitPoint = &sp; @@ -231,12 +223,12 @@ void TimerThread::idle_loop() { while (!exit) { - mutex.lock(); + std::unique_lock lk(mutex); if (!exit) - sleepCondition.wait_for(mutex, run ? Resolution : INT_MAX); + sleepCondition.wait_for(lk, std::chrono::milliseconds(run ? Resolution : INT_MAX)); - mutex.unlock(); + lk.unlock(); if (run) check_time(); @@ -251,17 +243,17 @@ void MainThread::idle_loop() { while (!exit) { - mutex.lock(); + std::unique_lock lk(mutex); thinking = false; while (!thinking && !exit) { Threads.sleepCondition.notify_one(); // Wake up the UI thread if needed - sleepCondition.wait(mutex); + sleepCondition.wait(lk); } - mutex.unlock(); + lk.unlock(); if (!exit) { @@ -297,8 +289,8 @@ void ThreadPool::exit() { delete_thread(timer); // As first because check_time() accesses threads data - for (iterator it = begin(); it != end(); ++it) - delete_thread(*it); + for (Thread* th : *this) + delete_thread(th); } @@ -335,11 +327,11 @@ void ThreadPool::read_uci_options() { Thread* ThreadPool::available_slave(const Thread* master) const { - for (const_iterator it = begin(); it != end(); ++it) - if ((*it)->available_to(master)) - return *it; + for (Thread* th : *this) + if (th->available_to(master)) + return th; - return NULL; + return nullptr; } @@ -347,10 +339,8 @@ Thread* ThreadPool::available_slave(const Thread* master) const { void ThreadPool::wait_for_think_finished() { - MainThread* th = main(); - th->mutex.lock(); - while (th->thinking) sleepCondition.wait(th->mutex); - th->mutex.unlock(); + std::unique_lock lk(main()->mutex); + sleepCondition.wait(lk, [&]{ return !main()->thinking; }); } @@ -371,14 +361,14 @@ void ThreadPool::start_thinking(const Position& pos, const LimitsType& limits, Limits = limits; if (states.get()) // If we don't set a new position, preserve current state { - SetupStates = states; // Ownership transfer here + SetupStates = std::move(states); // Ownership transfer here assert(!states.get()); } - for (MoveList it(pos); *it; ++it) + for (const ExtMove& ms : MoveList(pos)) if ( limits.searchmoves.empty() - || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), *it)) - RootMoves.push_back(RootMove(*it)); + || std::count(limits.searchmoves.begin(), limits.searchmoves.end(), ms.move)) + RootMoves.push_back(RootMove(ms.move)); main()->thinking = true; main()->notify_one(); // Starts main thread diff --git a/src/thread.h b/src/thread.h index 9750ed7b..3f902dc1 100644 --- a/src/thread.h +++ b/src/thread.h @@ -21,6 +21,9 @@ #define THREAD_H_INCLUDED #include +#include +#include +#include #include #include "material.h" @@ -34,35 +37,6 @@ struct Thread; const int MAX_THREADS = 128; const int MAX_SPLITPOINTS_PER_THREAD = 8; -/// Mutex and ConditionVariable struct are wrappers of the low level locking -/// machinery and are modeled after the corresponding C++11 classes. - -struct Mutex { - Mutex() { lock_init(l); } - ~Mutex() { lock_destroy(l); } - - void lock() { lock_grab(l); } - void unlock() { lock_release(l); } - -private: - friend struct ConditionVariable; - - Lock l; -}; - -struct ConditionVariable { - ConditionVariable() { cond_init(c); } - ~ConditionVariable() { cond_destroy(c); } - - void wait(Mutex& m) { cond_wait(c, m.l); } - void wait_for(Mutex& m, int ms) { timed_wait(c, m.l, ms); } - void notify_one() { cond_signal(c); } - -private: - WaitCondition c; -}; - - /// SplitPoint struct stores information shared by the threads searching in /// parallel below the same split point. It is populated at splitting time. @@ -82,7 +56,7 @@ struct SplitPoint { SplitPoint* parentSplitPoint; // Shared variable data - Mutex mutex; + std::mutex mutex; std::bitset slavesMask; volatile bool allSlavesSearching; volatile uint64_t nodes; @@ -99,16 +73,15 @@ struct SplitPoint { struct ThreadBase { - ThreadBase() : handle(NativeHandle()), exit(false) {} - virtual ~ThreadBase() {} + virtual ~ThreadBase() = default; virtual void idle_loop() = 0; void notify_one(); void wait_for(volatile const bool& b); - Mutex mutex; - ConditionVariable sleepCondition; - NativeHandle handle; - volatile bool exit; + std::thread nativeThread; + std::mutex mutex; + std::condition_variable sleepCondition; + volatile bool exit = false; }; @@ -144,19 +117,17 @@ struct Thread : public ThreadBase { /// special threads: the main one and the recurring timer. struct MainThread : public Thread { - MainThread() : thinking(true) {} // Avoid a race with start_thinking() virtual void idle_loop(); - volatile bool thinking; + volatile bool thinking = true; // Avoid a race with start_thinking() }; struct TimerThread : public ThreadBase { static const int Resolution = 5; // Millisec between two check_time() calls - TimerThread() : run(false) {} virtual void idle_loop(); - bool run; + bool run = false; }; @@ -176,8 +147,8 @@ struct ThreadPool : public std::vector { void start_thinking(const Position&, const Search::LimitsType&, Search::StateStackPtr&); Depth minimumSplitDepth; - Mutex mutex; - ConditionVariable sleepCondition; + std::mutex mutex; + std::condition_variable sleepCondition; TimerThread* timer; }; diff --git a/src/tt.cpp b/src/tt.cpp index 19de55ae..d0e2d729 100644 --- a/src/tt.cpp +++ b/src/tt.cpp @@ -32,8 +32,6 @@ TranspositionTable TT; // Our global transposition table void TranspositionTable::resize(size_t mbSize) { - assert(sizeof(Cluster) == CacheLineSize / 2); - size_t newClusterCount = size_t(1) << msb((mbSize * 1024 * 1024) / sizeof(Cluster)); if (newClusterCount == clusterCount) diff --git a/src/tt.h b/src/tt.h index 7343c525..32b4036e 100644 --- a/src/tt.h +++ b/src/tt.h @@ -81,6 +81,8 @@ class TranspositionTable { char padding[2]; // Align to the cache line size }; + static_assert(sizeof(Cluster) == CacheLineSize / 2, "Cluster size incorrect"); + public: ~TranspositionTable() { free(mem); } void new_search() { generation8 += 4; } // Lower 2 bits are used by Bound diff --git a/src/types.h b/src/types.h index 7c5776ab..23e69d2e 100644 --- a/src/types.h +++ b/src/types.h @@ -33,13 +33,22 @@ /// /// -DUSE_POPCNT | Add runtime support for use of popcnt asm-instruction. Works /// | only in 64-bit mode and requires hardware with popcnt support. +/// +/// -DUSE_PEXT | Add runtime support for use of pext asm-instruction. Works +/// | only in 64-bit mode and requires hardware with pext support. #include #include #include +#include #include -#include "platform.h" +#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' +#endif /// Predefined macros hell: /// @@ -171,7 +180,7 @@ enum Bound { BOUND_EXACT = BOUND_UPPER | BOUND_LOWER }; -enum Value { +enum Value : int { VALUE_ZERO = 0, VALUE_DRAW = 0, VALUE_KNOWN_WIN = 10000, @@ -182,9 +191,6 @@ enum Value { VALUE_MATE_IN_MAX_PLY = VALUE_MATE - 2 * MAX_PLY, VALUE_MATED_IN_MAX_PLY = -VALUE_MATE + 2 * MAX_PLY, - VALUE_ENSURE_INTEGER_SIZE_P = INT_MAX, - VALUE_ENSURE_INTEGER_SIZE_N = INT_MIN, - PawnValueMg = 198, PawnValueEg = 258, KnightValueMg = 817, KnightValueEg = 846, BishopValueMg = 836, BishopValueEg = 857, @@ -255,16 +261,10 @@ enum Rank { }; -/// Score enum stores a middlegame and an endgame value in a single integer. -/// The least significant 16 bits are used to store the endgame value and -/// the upper 16 bits are used to store the middlegame value. The compiler -/// is free to choose the enum type as long as it can store the data, so we -/// ensure that Score is an integer type by assigning some big int values. -enum Score { - SCORE_ZERO, - SCORE_ENSURE_INTEGER_SIZE_P = INT_MAX, - SCORE_ENSURE_INTEGER_SIZE_N = INT_MIN -}; +/// Score enum stores a middlegame and an endgame value in a single integer +/// (enum). The least significant 16 bits are used to store the endgame value +/// and the upper 16 bits are used to store the middlegame value. +enum Score : int { SCORE_ZERO }; inline Score make_score(int mg, int eg) { return Score((mg << 16) + eg); diff --git a/src/uci.cpp b/src/uci.cpp index 1b91b451..f05a114e 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -232,9 +232,7 @@ string UCI::value(Value v) { /// UCI::square() converts a Square to a string in algebraic notation (g1, a7, etc.) std::string UCI::square(Square s) { - - char sq[] = { char('a' + file_of(s)), char('1' + rank_of(s)), 0 }; // NULL terminated - return sq; + return std::string{ char('a' + file_of(s)), char('1' + rank_of(s)) }; } @@ -274,9 +272,9 @@ Move UCI::to_move(const Position& pos, string& str) { if (str.length() == 5) // Junior could send promotion piece in uppercase str[4] = char(tolower(str[4])); - for (MoveList it(pos); *it; ++it) - if (str == UCI::move(*it, pos.is_chess960())) - return *it; + for (const ExtMove& ms : MoveList(pos)) + if (str == UCI::move(ms.move, pos.is_chess960())) + return ms.move; return MOVE_NONE; } diff --git a/src/uci.h b/src/uci.h index b928e8ae..76e6cda6 100644 --- a/src/uci.h +++ b/src/uci.h @@ -45,10 +45,10 @@ class Option { typedef void (*OnChange)(const Option&); public: - Option(OnChange = NULL); - Option(bool v, OnChange = NULL); - Option(const char* v, OnChange = NULL); - Option(int v, int min, int max, OnChange = NULL); + Option(OnChange = nullptr); + Option(bool v, OnChange = nullptr); + Option(const char* v, OnChange = nullptr); + Option(int v, int min, int max, OnChange = nullptr); Option& operator=(const std::string&); void operator<<(const Option&); @@ -69,6 +69,7 @@ void loop(int argc, char* argv[]); std::string value(Value v); std::string square(Square s); std::string move(Move m, bool chess960); +std::string pv(const Position& pos, Depth depth, Value alpha, Value beta); Move to_move(const Position& pos, std::string& str); } // namespace UCI diff --git a/src/ucioption.cpp b/src/ucioption.cpp index 1778c718..c7823764 100644 --- a/src/ucioption.cpp +++ b/src/ucioption.cpp @@ -19,7 +19,6 @@ #include #include -#include #include #include "misc.h" @@ -43,10 +42,10 @@ void on_tb_path(const Option& o) { Tablebases::init(o); } /// Our case insensitive less() function as required by UCI protocol -bool ci_less(char c1, char c2) { return tolower(c1) < tolower(c2); } - bool CaseInsensitiveLess::operator() (const string& s1, const string& s2) const { - return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), ci_less); + + return std::lexicographical_compare(s1.begin(), s1.end(), s2.begin(), s2.end(), + [](char c1, char c2) { return tolower(c1) < tolower(c2); }); } @@ -82,11 +81,11 @@ void init(OptionsMap& o) { std::ostream& operator<<(std::ostream& os, const OptionsMap& om) { for (size_t idx = 0; idx < om.size(); ++idx) - for (OptionsMap::const_iterator it = om.begin(); it != om.end(); ++it) - if (it->second.idx == idx) + for (auto& it : om) + if (it.second.idx == idx) { - const Option& o = it->second; - os << "\noption name " << it->first << " type " << o.type; + const Option& o = it.second; + os << "\noption name " << it.first << " type " << o.type; if (o.type != "button") os << " default " << o.defaultValue; @@ -96,6 +95,7 @@ std::ostream& operator<<(std::ostream& os, const OptionsMap& om) { break; } + return os; } @@ -112,12 +112,11 @@ Option::Option(OnChange f) : type("button"), min(0), max(0), on_change(f) {} Option::Option(int v, int minv, int maxv, OnChange f) : type("spin"), min(minv), max(maxv), on_change(f) -{ std::ostringstream ss; ss << v; defaultValue = currentValue = ss.str(); } - +{ defaultValue = currentValue = std::to_string(v); } Option::operator int() const { assert(type == "check" || type == "spin"); - return (type == "spin" ? atoi(currentValue.c_str()) : currentValue == "true"); + return (type == "spin" ? stoi(currentValue) : currentValue == "true"); } Option::operator std::string() const { @@ -147,7 +146,7 @@ Option& Option::operator=(const string& v) { if ( (type != "button" && v.empty()) || (type == "check" && v != "true" && v != "false") - || (type == "spin" && (atoi(v.c_str()) < min || atoi(v.c_str()) > max))) + || (type == "spin" && (stoi(v) < min || stoi(v) > max))) return *this; if (type != "button")