From 81cd417b4584b0e3830940c5cb122c898afde08a Mon Sep 17 00:00:00 2001 From: Marco Costalba Date: Sun, 4 Dec 2011 10:53:40 +0100 Subject: [PATCH] Retire move.h Also some assorted comments fixes and other trivia. No functional change. Signed-off-by: Marco Costalba --- src/benchmark.cpp | 12 ++--- src/book.cpp | 1 + src/book.h | 7 ++- src/misc.cpp | 32 +++++------ src/misc.h | 5 ++ src/move.cpp | 1 - src/move.h | 131 ---------------------------------------------- src/movegen.cpp | 12 ++--- src/movegen.h | 4 +- src/movepick.h | 1 - src/position.h | 11 ++-- src/psqtab.h | 8 +-- src/rkiss.h | 27 +++++----- src/search.cpp | 8 ++- src/search.h | 3 +- src/timeman.h | 4 +- src/tt.h | 2 +- src/types.h | 95 +++++++++++++++++++++++++++++++++ src/uci.cpp | 1 - 19 files changed, 162 insertions(+), 203 deletions(-) delete mode 100644 src/move.h diff --git a/src/benchmark.cpp b/src/benchmark.cpp index ee112804..052176cd 100644 --- a/src/benchmark.cpp +++ b/src/benchmark.cpp @@ -28,7 +28,7 @@ using namespace std; -static const string Defaults[] = { +static const char* 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", @@ -59,7 +59,7 @@ static const string Defaults[] = { void benchmark(int argc, char* argv[]) { - std::vector searchMoves(1, MOVE_NONE); + vector searchMoves(1, MOVE_NONE); vector fenList; Search::LimitsType limits; int64_t totalNodes; @@ -85,7 +85,10 @@ void benchmark(int argc, char* argv[]) { limits.maxDepth = atoi(valStr.c_str()); // Do we need to load positions from a given FEN file? - if (fenFile != "default") + if (fenFile == "default") + for (int i = 0; *Defaults[i]; i++) + fenList.push_back(Defaults[i]); + else { string fen; ifstream f(fenFile.c_str()); @@ -102,9 +105,6 @@ void benchmark(int argc, char* argv[]) { f.close(); } - else // Load default positions - for (int i = 0; !Defaults[i].empty(); i++) - fenList.push_back(Defaults[i]); // Ok, let's start the benchmark ! totalNodes = 0; diff --git a/src/book.cpp b/src/book.cpp index 243583ff..427ae56e 100644 --- a/src/book.cpp +++ b/src/book.cpp @@ -28,6 +28,7 @@ #include #include "book.h" +#include "misc.h" #include "movegen.h" using namespace std; diff --git a/src/book.h b/src/book.h index 0690d32b..f84d9389 100644 --- a/src/book.h +++ b/src/book.h @@ -23,14 +23,13 @@ #include #include -#include "move.h" #include "position.h" #include "rkiss.h" -// A Polyglot book is a series of "entries" of 16 bytes. All integers are -// stored highest byte first (regardless of size). The entries are ordered -// according to key. Lowest key first. +/// A Polyglot book is a series of "entries" of 16 bytes. All integers are +/// stored highest byte first (regardless of size). The entries are ordered +/// according to key. Lowest key first. struct BookEntry { uint64_t key; uint16_t move; diff --git a/src/misc.cpp b/src/misc.cpp index 5e851f12..4d91c0fb 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -17,7 +17,14 @@ along with this program. If not, see . */ -#if !defined(_MSC_VER) +#if defined(_MSC_VER) + +#define _CRT_SECURE_NO_DEPRECATE +#define NOMINMAX // disable macros min() and max() +#include +#include + +#else # include # include @@ -26,13 +33,6 @@ # include # endif -#else - -#define _CRT_SECURE_NO_DEPRECATE -#define NOMINMAX // disable macros min() and max() -#include -#include - #endif #if !defined(NO_PREFETCH) @@ -178,18 +178,13 @@ int cpu_count() { /// timed_wait() waits for msec milliseconds. It is mainly an helper to wrap /// conversion from milliseconds to struct timespec, as used by pthreads. -#if defined(_MSC_VER) - void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) { - cond_timedwait(sleepCond, sleepLock, msec); -} +#if defined(_MSC_VER) + int tm = msec; #else - -void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) { - struct timeval t; - struct timespec abstime; + struct timespec abstime, *tm = &abstime; gettimeofday(&t, NULL); @@ -201,12 +196,11 @@ void timed_wait(WaitCondition* sleepCond, Lock* sleepLock, int msec) { abstime.tv_sec += 1; abstime.tv_nsec -= 1000000000LL; } +#endif - cond_timedwait(sleepCond, sleepLock, &abstime); + cond_timedwait(sleepCond, sleepLock, tm); } -#endif - /// prefetch() preloads the given address in L1/L2 cache. This is a non /// blocking function and do not stalls the CPU waiting for data to be diff --git a/src/misc.h b/src/misc.h index 92b97461..303514f7 100644 --- a/src/misc.h +++ b/src/misc.h @@ -41,6 +41,11 @@ extern void dbg_mean_of(int v); extern void dbg_print_hit_rate(); extern void dbg_print_mean(); +class Position; +extern Move move_from_uci(const Position& pos, const std::string& str); +extern const std::string move_to_uci(Move m, bool chess960); +extern const std::string move_to_san(Position& pos, Move m); + struct Log : public std::ofstream { Log(const std::string& f = "log.txt") : std::ofstream(f.c_str(), std::ios::out | std::ios::app) {} ~Log() { if (is_open()) close(); } diff --git a/src/move.cpp b/src/move.cpp index b019110b..1a312cbc 100644 --- a/src/move.cpp +++ b/src/move.cpp @@ -21,7 +21,6 @@ #include #include -#include "move.h" #include "movegen.h" #include "position.h" diff --git a/src/move.h b/src/move.h deleted file mode 100644 index 99e0c44d..00000000 --- a/src/move.h +++ /dev/null @@ -1,131 +0,0 @@ -/* - Stockfish, a UCI chess playing engine derived from Glaurung 2.1 - Copyright (C) 2004-2008 Tord Romstad (Glaurung author) - Copyright (C) 2008-2010 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 . -*/ - -#if !defined(MOVE_H_INCLUDED) -#define MOVE_H_INCLUDED - -#include - -#include "misc.h" -#include "types.h" - -// Maximum number of allowed moves per position -const int MAX_MOVES = 256; - -/// A move needs 16 bits to be stored -/// -/// bit 0- 5: destination square (from 0 to 63) -/// bit 6-11: origin square (from 0 to 63) -/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2) -/// bit 14-15: special move flag: promotion (1), en passant (2), castle (3) -/// -/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in -/// because in any normal move destination square is always different -/// from origin square while MOVE_NONE and MOVE_NULL have the same -/// origin and destination square, 0 and 1 respectively. - -enum Move { - MOVE_NONE = 0, - MOVE_NULL = 65 -}; - - -struct MoveStack { - Move move; - int score; -}; - -inline bool operator<(const MoveStack& f, const MoveStack& s) { return f.score < s.score; } - -// An helper insertion sort implementation, works with pointers and iterators -template -inline void sort(K firstMove, K lastMove) -{ - T value; - K cur, p, d; - - if (firstMove != lastMove) - for (cur = firstMove + 1; cur != lastMove; cur++) - { - p = d = cur; - value = *p--; - if (*p < value) - { - do *d = *p; - while (--d != firstMove && *--p < value); - *d = value; - } - } -} - -inline Square move_from(Move m) { - return Square((m >> 6) & 0x3F); -} - -inline Square move_to(Move m) { - return Square(m & 0x3F); -} - -inline bool is_special(Move m) { - return m & (3 << 14); -} - -inline bool is_promotion(Move m) { - return (m & (3 << 14)) == (1 << 14); -} - -inline int is_enpassant(Move m) { - return (m & (3 << 14)) == (2 << 14); -} - -inline int is_castle(Move m) { - return (m & (3 << 14)) == (3 << 14); -} - -inline PieceType promotion_piece_type(Move m) { - return PieceType(((m >> 12) & 3) + 2); -} - -inline Move make_move(Square from, Square to) { - return Move(to | (from << 6)); -} - -inline Move make_promotion_move(Square from, Square to, PieceType promotion) { - return Move(to | (from << 6) | (1 << 14) | ((promotion - 2) << 12)) ; -} - -inline Move make_enpassant_move(Square from, Square to) { - return Move(to | (from << 6) | (2 << 14)); -} - -inline Move make_castle_move(Square from, Square to) { - return Move(to | (from << 6) | (3 << 14)); -} - -inline bool is_ok(Move m) { - return move_from(m) != move_to(m); // Catches also MOVE_NONE -} - -class Position; - -extern const std::string move_to_uci(Move m, bool chess960); -extern Move move_from_uci(const Position& pos, const std::string& str); -extern const std::string move_to_san(Position& pos, Move m); - -#endif // !defined(MOVE_H_INCLUDED) diff --git a/src/movegen.cpp b/src/movegen.cpp index ba1cc334..aa5d28d4 100644 --- a/src/movegen.cpp +++ b/src/movegen.cpp @@ -47,8 +47,7 @@ namespace { template inline MoveStack* generate_discovered_checks(const Position& pos, MoveStack* mlist, Square from) { - assert(Pt != QUEEN); - assert(Pt != PAWN); + assert(Pt != QUEEN && Pt != PAWN); Bitboard b = pos.attacks_from(from) & pos.empty_squares(); @@ -62,8 +61,7 @@ namespace { template inline MoveStack* generate_direct_checks(const Position& pos, MoveStack* mlist, Color us, Bitboard dc, Square ksq) { - assert(Pt != KING); - assert(Pt != PAWN); + assert(Pt != KING && Pt != PAWN); Bitboard checkSqs, b; Square from; @@ -198,7 +196,7 @@ template MoveStack* generate(const Position& pos, MoveStack* mli template MoveStack* generate(const Position& pos, MoveStack* mlist); -/// generate_non_capture_checks() generates all pseudo-legal non-captures and knight +/// generate generates all pseudo-legal non-captures and knight /// underpromotions that give check. Returns a pointer to the end of the move list. template<> MoveStack* generate(const Position& pos, MoveStack* mlist) { @@ -238,8 +236,8 @@ MoveStack* generate(const Position& pos, MoveStack* mlist) } -/// generate_evasions() generates all pseudo-legal check evasions when -/// the side to move is in check. Returns a pointer to the end of the move list. +/// generate generates all pseudo-legal check evasions when the side +/// to move is in check. Returns a pointer to the end of the move list. template<> MoveStack* generate(const Position& pos, MoveStack* mlist) { diff --git a/src/movegen.h b/src/movegen.h index a80db334..38351e32 100644 --- a/src/movegen.h +++ b/src/movegen.h @@ -20,7 +20,7 @@ #if !defined(MOVEGEN_H_INCLUDED) #define MOVEGEN_H_INCLUDED -#include "move.h" +#include "types.h" enum MoveType { MV_CAPTURE, @@ -32,6 +32,8 @@ enum MoveType { MV_LEGAL }; +class Position; + template MoveStack* generate(const Position& pos, MoveStack* mlist); diff --git a/src/movepick.h b/src/movepick.h index 3ade58ed..34329582 100644 --- a/src/movepick.h +++ b/src/movepick.h @@ -21,7 +21,6 @@ #define MOVEPICK_H_INCLUDED #include "history.h" -#include "move.h" #include "position.h" #include "types.h" diff --git a/src/position.h b/src/position.h index e4b6bd0f..e1bdf39e 100644 --- a/src/position.h +++ b/src/position.h @@ -23,20 +23,20 @@ #include #include "bitboard.h" -#include "move.h" #include "types.h" /// The checkInfo struct is initialized at c'tor time and keeps info used /// to detect if a move gives check. +class Position; struct CheckInfo { - explicit CheckInfo(const Position&); + explicit CheckInfo(const Position&); - Bitboard dcCandidates; - Bitboard pinned; - Bitboard checkSq[8]; + Bitboard dcCandidates; + Bitboard pinned; + Bitboard checkSq[8]; }; @@ -44,7 +44,6 @@ struct CheckInfo { /// object to its previous state when we retract a move. Whenever a move /// is made on the board (by calling Position::do_move), an StateInfo object /// must be passed as a parameter. -class Position; struct StateInfo { Key pawnKey, materialKey; diff --git a/src/psqtab.h b/src/psqtab.h index cfbf4a4d..30df2b76 100644 --- a/src/psqtab.h +++ b/src/psqtab.h @@ -24,9 +24,11 @@ #define S(mg, eg) make_score(mg, eg) -// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on a -// given square a (midgame, endgame) score pair is assigned. PSQT is defined for -// white side, for black side the tables are symmetric. + +/// PSQT[PieceType][Square] contains Piece-Square scores. For each piece type on +/// a given square a (midgame, endgame) score pair is assigned. PSQT is defined +/// for white side, for black side the tables are symmetric. + static const Score PSQT[][64] = { { }, { // Pawn diff --git a/src/rkiss.h b/src/rkiss.h index 5c01a0e5..d0178495 100644 --- a/src/rkiss.h +++ b/src/rkiss.h @@ -20,19 +20,6 @@ available under 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. - - ** George Marsaglia invented the RNG-Kiss-family in the early 90's. - ** This is a specific version that Heinz van Saanen derived and - ** tested from some public domain code by Bob Jenkins: - ** - ** Quite platform independent - ** Passes ALL dieharder tests! Here *nix sys-rand() e.g. fails miserably:-) - ** ~12 times faster than my *nix sys-rand() - ** ~4 times faster than SSE2-version of Mersenne twister - ** Average cycle length: ~2^126 - ** 64 bit seed - ** Return doubles with a full 53 bit mantissa - ** Thread safe */ #if !defined(RKISS_H_INCLUDED) @@ -40,6 +27,20 @@ #include "types.h" +/// RKISS is our pseudo random number generator (PRNG) used to compute hash keys. +/// George Marsaglia invented the RNG-Kiss-family in the early 90's. This is a +/// specific version that Heinz van Saanen derived from some public domain code +/// by Bob Jenkins. Following the feature list, as tested by Heinz. +/// +/// - Quite platform independent +/// - Passes ALL dieharder tests! Here *nix sys-rand() e.g. fails miserably:-) +/// - ~12 times faster than my *nix sys-rand() +/// - ~4 times faster than SSE2-version of Mersenne twister +/// - Average cycle length: ~2^126 +/// - 64 bit seed +/// - Return doubles with a full 53 bit mantissa +/// - Thread safe + class RKISS { // Keep variables always together diff --git a/src/search.cpp b/src/search.cpp index 51c2a27d..e625405d 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -30,7 +30,6 @@ #include "evaluate.h" #include "history.h" #include "misc.h" -#include "move.h" #include "movegen.h" #include "movepick.h" #include "search.h" @@ -353,10 +352,9 @@ int64_t Search::perft(Position& pos, Depth depth) { } -/// think() is the external interface to Stockfish's search, and is called when -/// the program receives the UCI 'go' command. It initializes various global -/// variables, and calls id_loop(). It returns false when a "quit" command is -/// received during the search. +/// think() is the external interface to Stockfish's search, and is called by the +/// main thread when the program receives the UCI 'go' command. It searches from +/// RootPosition and at the end prints the "bestmove" to output. void Search::think() { diff --git a/src/search.h b/src/search.h index 84704b0d..53e7444c 100644 --- a/src/search.h +++ b/src/search.h @@ -23,7 +23,6 @@ #include #include -#include "move.h" #include "types.h" class Position; @@ -74,7 +73,7 @@ extern void init(); extern int64_t perft(Position& pos, Depth depth); extern void think(); -} +} // namespace extern void do_timer_event(); diff --git a/src/timeman.h b/src/timeman.h index 703dc234..ee5fdcf2 100644 --- a/src/timeman.h +++ b/src/timeman.h @@ -20,11 +20,11 @@ #if !defined(TIMEMAN_H_INCLUDED) #define TIMEMAN_H_INCLUDED -struct SearchLimits; +/// The TimeManager class computes the optimal time to think depending on the +/// maximum available time, the move game number and other parameters. class TimeManager { public: - void init(const Search::LimitsType& limits, int currentPly); void pv_instability(int curChanges, int prevChanges); int available_time() const { return optimumSearchTime + unstablePVExtraTime; } diff --git a/src/tt.h b/src/tt.h index 03147bf1..16e1cd9d 100644 --- a/src/tt.h +++ b/src/tt.h @@ -22,7 +22,7 @@ #include -#include "move.h" +#include "misc.h" #include "types.h" diff --git a/src/types.h b/src/types.h index 7040b5ad..b5898b25 100644 --- a/src/types.h +++ b/src/types.h @@ -155,6 +155,7 @@ const bool CpuIs64Bit = false; typedef uint64_t Key; typedef uint64_t Bitboard; +const int MAX_MOVES = 256; const int PLY_MAX = 100; const int PLY_MAX_PLUS_2 = PLY_MAX + 2; @@ -176,6 +177,31 @@ const Bitboard Rank6BB = Rank1BB << (8 * 5); const Bitboard Rank7BB = Rank1BB << (8 * 6); const Bitboard Rank8BB = Rank1BB << (8 * 7); +/// A move needs 16 bits to be stored +/// +/// bit 0- 5: destination square (from 0 to 63) +/// bit 6-11: origin square (from 0 to 63) +/// bit 12-13: promotion piece type - 2 (from KNIGHT-2 to QUEEN-2) +/// bit 14-15: special move flag: promotion (1), en passant (2), castle (3) +/// +/// Special cases are MOVE_NONE and MOVE_NULL. We can sneak these in because in +/// any normal move destination square is always different from origin square +/// while MOVE_NONE and MOVE_NULL have the same origin and destination square. + +enum Move { + MOVE_NONE = 0, + MOVE_NULL = 65 +}; + +struct MoveStack { + Move move; + int score; +}; + +inline bool operator<(const MoveStack& f, const MoveStack& s) { + return f.score < s.score; +} + enum ValueType { VALUE_TYPE_NONE = 0, VALUE_TYPE_UPPER = 1, @@ -467,4 +493,73 @@ inline Square pawn_push(Color c) { return c == WHITE ? DELTA_N : DELTA_S; } +// An helper insertion sort implementation, works with pointers and iterators +template +inline void sort(K firstMove, K lastMove) +{ + T value; + K cur, p, d; + + if (firstMove != lastMove) + for (cur = firstMove + 1; cur != lastMove; cur++) + { + p = d = cur; + value = *p--; + if (*p < value) + { + do *d = *p; + while (--d != firstMove && *--p < value); + *d = value; + } + } +} + +inline Square move_from(Move m) { + return Square((m >> 6) & 0x3F); +} + +inline Square move_to(Move m) { + return Square(m & 0x3F); +} + +inline bool is_special(Move m) { + return m & (3 << 14); +} + +inline bool is_promotion(Move m) { + return (m & (3 << 14)) == (1 << 14); +} + +inline int is_enpassant(Move m) { + return (m & (3 << 14)) == (2 << 14); +} + +inline int is_castle(Move m) { + return (m & (3 << 14)) == (3 << 14); +} + +inline PieceType promotion_piece_type(Move m) { + return PieceType(((m >> 12) & 3) + 2); +} + +inline Move make_move(Square from, Square to) { + return Move(to | (from << 6)); +} + +inline Move make_promotion_move(Square from, Square to, PieceType promotion) { + return Move(to | (from << 6) | (1 << 14) | ((promotion - 2) << 12)) ; +} + +inline Move make_enpassant_move(Square from, Square to) { + return Move(to | (from << 6) | (2 << 14)); +} + +inline Move make_castle_move(Square from, Square to) { + return Move(to | (from << 6) | (3 << 14)); +} + +inline bool is_ok(Move m) { + return move_from(m) != move_to(m); // Catches also MOVE_NULL and MOVE_NONE +} + #endif // !defined(TYPES_H_INCLUDED) diff --git a/src/uci.cpp b/src/uci.cpp index fb2d3eac..9948eb62 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -24,7 +24,6 @@ #include "evaluate.h" #include "misc.h" -#include "move.h" #include "position.h" #include "search.h" #include "thread.h" -- 2.39.2