Also some assorted comments fixes and other trivia.
No functional change.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
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",
void benchmark(int argc, char* argv[]) {
- std::vector<Move> searchMoves(1, MOVE_NONE);
+ vector<Move> searchMoves(1, MOVE_NONE);
vector<string> fenList;
Search::LimitsType limits;
int64_t totalNodes;
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());
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;
#include <iostream>
#include "book.h"
+#include "misc.h"
#include "movegen.h"
using namespace std;
#include <fstream>
#include <string>
-#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;
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#if !defined(_MSC_VER)
+#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 <sys/pstat.h>
# endif
-#else
-
-#define _CRT_SECURE_NO_DEPRECATE
-#define NOMINMAX // disable macros min() and max()
-#include <windows.h>
-#include <sys/timeb.h>
-
#endif
#if !defined(NO_PREFETCH)
/// 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);
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
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(); }
#include <cstring>
#include <string>
-#include "move.h"
#include "movegen.h"
#include "position.h"
+++ /dev/null
-/*
- 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 <http://www.gnu.org/licenses/>.
-*/
-
-#if !defined(MOVE_H_INCLUDED)
-#define MOVE_H_INCLUDED
-
-#include <string>
-
-#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<typename T, typename K>
-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)
template<PieceType Pt>
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<Pt>(from) & pos.empty_squares();
template<PieceType Pt>
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;
template MoveStack* generate<MV_NON_EVASION>(const Position& pos, MoveStack* mlist);
-/// generate_non_capture_checks() generates all pseudo-legal non-captures and knight
+/// generate<MV_NON_CAPTURE_CHECK> 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<MV_NON_CAPTURE_CHECK>(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<MV_EVASION> 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<MV_EVASION>(const Position& pos, MoveStack* mlist) {
#if !defined(MOVEGEN_H_INCLUDED)
#define MOVEGEN_H_INCLUDED
-#include "move.h"
+#include "types.h"
enum MoveType {
MV_CAPTURE,
MV_LEGAL
};
+class Position;
+
template<MoveType>
MoveStack* generate(const Position& pos, MoveStack* mlist);
#define MOVEPICK_H_INCLUDED
#include "history.h"
-#include "move.h"
#include "position.h"
#include "types.h"
#include <cassert>
#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];
};
/// 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;
#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
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)
#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
#include "evaluate.h"
#include "history.h"
#include "misc.h"
-#include "move.h"
#include "movegen.h"
#include "movepick.h"
#include "search.h"
}
-/// 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() {
#include <cstring>
#include <vector>
-#include "move.h"
#include "types.h"
class Position;
extern int64_t perft(Position& pos, Depth depth);
extern void think();
-}
+} // namespace
extern void do_timer_event();
#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; }
#include <iostream>
-#include "move.h"
+#include "misc.h"
#include "types.h"
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;
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,
return c == WHITE ? DELTA_N : DELTA_S;
}
+// An helper insertion sort implementation, works with pointers and iterators
+template<typename T, typename K>
+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)
#include "evaluate.h"
#include "misc.h"
-#include "move.h"
#include "position.h"
#include "search.h"
#include "thread.h"