return SignedDirection(SignedDirectionTable[s1][s2]);
}
+inline int direction_is_diagonal(Square s1, Square s2) {
+ return DirectionTable[s1][s2] & 2;
+}
+
+inline bool direction_is_straight(Square s1, Square s2) {
+ return DirectionTable[s1][s2] < 2;
+}
////
//// Prototypes
# include <unistd.h>
#else
+/*
+ (c) Copyright 1992 Eric Backus
+ This software may be used freely so long as this copyright notice is
+ left intact. There is no warrantee on this software.
+*/
# include <windows.h>
# include <time.h>
# include "dos.h"
-int gettimeofday(struct timeval * tp, struct timezone * tzp);
+static int gettimeofday(struct timeval* tp, struct timezone*)
+{
+ SYSTEMTIME systime;
+
+ if (tp)
+ {
+ struct tm tmrec;
+ time_t theTime = time(NULL);
+
+ tmrec = *localtime(&theTime);
+ tp->tv_sec = mktime(&tmrec);
+ GetLocalTime(&systime); /* system time */
+
+ tp->tv_usec = systime.wMilliseconds * 1000;
+ }
+ return 0;
+}
#endif
#include "misc.h"
+using namespace std;
+
+/// Version number. If this is left empty, the current date (in the format
+/// YYMMDD) is used as a version number.
+
+static const string EngineVersion = "";
+static const string AppName = "Stockfish";
+static const string AppTag = "";
+
////
//// Variables
////
-static const std::string AppName = "Stockfish";
-static const std::string AppTag = "";
-
long dbg_cnt0 = 0;
long dbg_cnt1 = 0;
void dbg_print_hit_rate() {
- std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
- << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
- << std::endl;
+ cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
+ << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
}
void dbg_print_mean() {
- std::cout << "Total " << dbg_cnt0 << " Mean "
- << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
+ cout << "Total " << dbg_cnt0 << " Mean "
+ << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
}
-void dbg_print_hit_rate(std::ofstream& logFile) {
+void dbg_print_hit_rate(ofstream& logFile) {
logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
- << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
- << std::endl;
+ << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
}
-void dbg_print_mean(std::ofstream& logFile) {
+void dbg_print_mean(ofstream& logFile) {
logFile << "Total " << dbg_cnt0 << " Mean "
- << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
+ << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
}
/// engine_name() returns the full name of the current Stockfish version.
/// program was compiled) or "Stockfish <version number>", depending on whether
/// the constant EngineVersion (defined in misc.h) is empty.
-const std::string engine_name() {
+const string engine_name() {
- if (EngineVersion.empty())
- {
- std::string date(__DATE__); // From compiler, format is "Sep 21 2008"
- std::string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
+ if (!EngineVersion.empty())
+ return "Stockfish " + EngineVersion;
- size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
+ string date(__DATE__); // From compiler, format is "Sep 21 2008"
+ string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
- std::stringstream s;
- std::string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
+ size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
- std::string name = AppName + " " + AppTag + " ";
+ stringstream s;
+ string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
- s << name << date.substr(date.length() - 2) << std::setfill('0')
- << std::setw(2) << mon << std::setw(2) << day;
+ string name = AppName + " " + AppTag + " ";
- return s.str();
- } else
- return "Stockfish " + EngineVersion;
+ s << name << date.substr(date.length() - 2) << setfill('0')
+ << setw(2) << mon << setw(2) << day;
+
+ return s.str();
}
#include <string>
-////
-//// Constants
-////
-
-
-/// Version number. If this is left empty, the current date (in the format
-/// YYMMDD) is used as a version number.
-
-const std::string EngineVersion = "";
-
-
////
//// Macros
////
//// Includes
////
-#include <cstring>
+#include <string>
#include "piece.h"
+using namespace std;
////
//// Functions
/// Translating piece types to/from English piece letters
-static const char PieceChars[] = " pnbrqk";
+static const string PieceChars(" pnbrqk PNBRQK");
char piece_type_to_char(PieceType pt, bool upcase) {
- return char(upcase? toupper(PieceChars[pt]) : PieceChars[pt]);
+
+ return PieceChars[pt + upcase * 7];
}
PieceType piece_type_from_char(char c) {
- const char* ch = strchr(PieceChars, tolower(c));
- return ch? PieceType(ch - PieceChars) : NO_PIECE_TYPE;
+
+ size_t idx = PieceChars.find(c);
+
+ return idx != string::npos ? PieceType(idx % 7) : NO_PIECE_TYPE;
}
case BISHOP:
return (dcCandidates && bit_is_set(dcCandidates, from))
- || ( direction_between_squares(ksq, to) != DIR_NONE
- && bit_is_set(piece_attacks<BISHOP>(ksq), to));
+ || (direction_is_diagonal(ksq, to) && bit_is_set(piece_attacks<BISHOP>(ksq), to));
case ROOK:
return (dcCandidates && bit_is_set(dcCandidates, from))
- || ( direction_between_squares(ksq, to) != DIR_NONE
- && bit_is_set(piece_attacks<ROOK>(ksq), to));
+ || (direction_is_straight(ksq, to) && bit_is_set(piece_attacks<ROOK>(ksq), to));
case QUEEN:
// Discovered checks are impossible!
assert(!bit_is_set(dcCandidates, from));
- return ( direction_between_squares(ksq, to) != DIR_NONE
- && bit_is_set(piece_attacks<QUEEN>(ksq), to));
+ return ( (direction_is_straight(ksq, to) && bit_is_set(piece_attacks<ROOK>(ksq), to))
+ || (direction_is_diagonal(ksq, to) && bit_is_set(piece_attacks<BISHOP>(ksq), to)));
case KING:
// Discovered check?
}
-/// Position::move_is_capture() tests whether a move from the current
-/// position is a capture. Move must not be MOVE_NONE.
-
-bool Position::move_is_capture(Move m) const {
-
- assert(m != MOVE_NONE);
-
- return ( !square_is_empty(move_to(m))
- && (color_of_piece_on(move_to(m)) != color_of_piece_on(move_from(m)))
- )
- || move_is_ep(m);
-}
-
-
/// Position::update_checkers() udpates chekers info given the move. It is called
/// in do_move() and is faster then find_checkers().
return pawns(c) & relative_rank_bb(c, RANK_7);
}
+inline bool Position::move_is_capture(Move m) const {
+
+ // Move must not be MOVE_NONE !
+
+ return ( !square_is_empty(move_to(m))
+ && (color_of_piece_on(move_to(m)) != color_of_piece_on(move_from(m)))
+ )
+ || move_is_ep(m);
+}
#endif // !defined(POSITION_H_INCLUDED)
Depth ThreatDepth;
// Depth limit for selective search
- Depth SelectiveDepth;
+ const Depth SelectiveDepth = 7*OnePly;
// Use internal iterative deepening?
const bool UseIIDAtPVNodes = true;
////
// The main transposition table
-TranspositionTable TT = TranspositionTable(TTDefaultSize);
+TranspositionTable TT;
// Number of active threads:
LMRPVMoves = get_option_value_int("Full Depth Moves (PV nodes)") + 1;
LMRNonPVMoves = get_option_value_int("Full Depth Moves (non-PV nodes)") + 1;
ThreatDepth = get_option_value_int("Threat Depth") * OnePly;
- SelectiveDepth = get_option_value_int("Selective Plies") * OnePly;
Chess960 = get_option_value_bool("UCI_Chess960");
ShowCurrentLine = get_option_value_bool("UCI_ShowCurrLine");
return bestValue;
if (bestValue <= oldAlpha)
- TT.store(pos, value_to_tt(bestValue, ply), depth, MOVE_NONE, VALUE_TYPE_UPPER);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
else if (bestValue >= beta)
{
update_history(pos, m, depth, movesSearched, moveCount);
update_killers(m, ss[ply]);
}
- TT.store(pos, value_to_tt(bestValue, ply), depth, m, VALUE_TYPE_LOWER);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, m);
}
else
- TT.store(pos, value_to_tt(bestValue, ply), depth, ss[ply].pv[ply], VALUE_TYPE_EXACT);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_EXACT, depth, ss[ply].pv[ply]);
return bestValue;
}
continue;
// Value based pruning
- if (depth < 7 * OnePly && approximateEval < beta)
+ if (approximateEval < beta)
{
if (futilityValue == VALUE_NONE)
futilityValue = evaluate(pos, ei, threadID)
return bestValue;
if (bestValue < beta)
- TT.store(pos, value_to_tt(bestValue, ply), depth, MOVE_NONE, VALUE_TYPE_UPPER);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, depth, MOVE_NONE);
else
{
BetaCounter.add(pos.side_to_move(), depth, threadID);
update_history(pos, m, depth, movesSearched, moveCount);
update_killers(m, ss[ply]);
}
- TT.store(pos, value_to_tt(bestValue, ply), depth, m, VALUE_TYPE_LOWER);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, depth, m);
}
assert(bestValue > -VALUE_INFINITE && bestValue < VALUE_INFINITE);
{
// Store the score to avoid a future costly evaluation() call
if (!isCheck && !tte && ei.futilityMargin == 0)
- TT.store(pos, value_to_tt(bestValue, ply), Depth(-127*OnePly), MOVE_NONE, VALUE_TYPE_EVAL);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_EVAL, Depth(-127*OnePly), MOVE_NONE);
return bestValue;
}
{
Depth d = (depth == Depth(0) ? Depth(0) : Depth(-1));
if (bestValue < beta)
- TT.store(pos, value_to_tt(bestValue, ply), d, MOVE_NONE, VALUE_TYPE_UPPER);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_UPPER, d, MOVE_NONE);
else
- TT.store(pos, value_to_tt(bestValue, ply), d, m, VALUE_TYPE_LOWER);
+ TT.store(pos, value_to_tt(bestValue, ply), VALUE_TYPE_LOWER, d, m);
}
// Update killers only for good check moves
+++ /dev/null
-/*
- (c) Copyright 1992 Eric Backus
-
- This software may be used freely so long as this copyright notice is
- left intact. There is no warrantee on this software.
-*/
-
-#include <windows.h>
-#include <time.h>
-#include "dos.h"
-
-int gettimeofday(struct timeval* tp, struct timezone*)
-{
- SYSTEMTIME systime;
-
- if (tp)
- {
- struct tm tmrec;
- time_t theTime = time(NULL);
-
- tmrec = *localtime(&theTime);
- tp->tv_sec = mktime(&tmrec);
- GetLocalTime(&systime); /* system time */
-
- tp->tv_usec = systime.wMilliseconds * 1000;
- }
- return 0;
-}
//// Functions
////
-/// Constructor
+TranspositionTable::TranspositionTable() {
-TranspositionTable::TranspositionTable(unsigned mbSize) {
-
- size = 0;
- generation = 0;
- writes = 0;
+ size = writes = 0;
entries = 0;
- set_size(mbSize);
+ generation = 0;
}
-
-/// Destructor
-
TranspositionTable::~TranspositionTable() {
delete [] entries;
void TranspositionTable::set_size(unsigned mbSize) {
- assert(mbSize >= 4 && mbSize <= 1024);
+ assert(mbSize >= 4 && mbSize <= 4096);
unsigned newSize = 1024;
// We store a cluster of 4 TTEntry for each position and newSize is
// the maximum number of storable positions
- for ( ; newSize * 4 * (sizeof(TTEntry)) <= (mbSize << 20); newSize *= 2);
- newSize /= 2;
+ while ((2 * newSize) * 4 * (sizeof(TTEntry)) <= (mbSize << 20))
+ newSize *= 2;
+
if (newSize != size)
{
- size = newSize;
- delete [] entries;
- entries = new TTEntry[size * 4];
- if (!entries)
- {
- std::cerr << "Failed to allocate " << mbSize
- << " MB for transposition table."
- << std::endl;
- exit(EXIT_FAILURE);
- }
- clear();
+ size = newSize;
+ delete [] entries;
+ entries = new TTEntry[size * 4];
+ if (!entries)
+ {
+ std::cerr << "Failed to allocate " << mbSize
+ << " MB for transposition table." << std::endl;
+ exit(EXIT_FAILURE);
+ }
+ clear();
}
}
/// TranspositionTable::clear overwrites the entire transposition table
-/// with zeroes. It is called whenever the table is resized, or when the
+/// with zeroes. It is called whenever the table is resized, or when the
/// user asks the program to clear the table (from the UCI interface).
/// Perhaps we should also clear it when the "ucinewgame" command is recieved?
/// TranspositionTable::store writes a new entry containing a position,
/// a value, a value type, a search depth, and a best move to the
-/// transposition table. The transposition table is organized in clusters
-/// of four TTEntry objects, and when a new entry is written, it replaces
-/// the least valuable of the four entries in a cluster. A TTEntry t1 is
+/// transposition table. Transposition table is organized in clusters of
+/// four TTEntry objects, and when a new entry is written, it replaces
+/// the least valuable of the four entries in a cluster. A TTEntry t1 is
/// considered to be 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 TTEntry of type VALUE_TYPE_EVAL
/// never replaces another entry for the same position.
-void TranspositionTable::store(const Position &pos, Value v, Depth d,
- Move m, ValueType type) {
+void TranspositionTable::store(const Position& p, Value v, ValueType t, Depth d, Move m) {
+
TTEntry *tte, *replace;
- tte = replace = first_entry(pos);
+ tte = replace = first_entry(p);
for (int i = 0; i < 4; i++, tte++)
{
- if (!tte->key() || tte->key() == pos.get_key()) // empty or overwrite old
- {
- // Do not overwrite position entry when VALUE_TYPE_EVAL
- if ( tte->key()
- && type == VALUE_TYPE_EVAL)
- return;
-
- if (m == MOVE_NONE)
- m = tte->move();
-
- *tte = TTEntry(pos.get_key(), v, type, d, m, generation);
- return;
- }
- else if (i == 0) // replace would be a no-op in this common case
- continue;
-
- int c1 = (replace->generation() == generation ? 2 : 0);
- int c2 = (tte->generation() == generation ? -2 : 0);
- int c3 = (tte->depth() < replace->depth() ? 1 : 0);
-
- if (c1 + c2 + c3 > 0)
- replace = tte;
+ if (!tte->key() || tte->key() == p.get_key()) // empty or overwrite old
+ {
+ // Do not overwrite when new type is VALUE_TYPE_EVAL
+ if (tte->key() && t == VALUE_TYPE_EVAL)
+ return;
+
+ if (m == MOVE_NONE)
+ m = tte->move();
+
+ *tte = TTEntry(p.get_key(), v, t, d, m, generation);
+ return;
+ }
+ else if (i == 0) // replace would be a no-op in this common case
+ continue;
+
+ int c1 = (replace->generation() == generation ? 2 : 0);
+ int c2 = (tte->generation() == generation ? -2 : 0);
+ int c3 = (tte->depth() < replace->depth() ? 1 : 0);
+
+ if (c1 + c2 + c3 > 0)
+ replace = tte;
}
- *replace = TTEntry(pos.get_key(), v, type, d, m, generation);
+ *replace = TTEntry(p.get_key(), v, t, d, m, generation);
writes++;
}
/// transposition table. Returns a pointer to the TTEntry or NULL
/// if position is not found.
-TTEntry* TranspositionTable::retrieve(const Position &pos) const {
+TTEntry* TranspositionTable::retrieve(const Position& pos) const {
TTEntry *tte = first_entry(pos);
for (int i = 0; i < 4; i++, tte++)
- {
if (tte->key() == pos.get_key())
return tte;
- }
+
return NULL;
}
/// TranspositionTable::first_entry returns a pointer to the first
/// entry of a cluster given a position.
-inline TTEntry* TranspositionTable::first_entry(const Position &pos) const {
+inline TTEntry* TranspositionTable::first_entry(const Position& pos) const {
return entries + (int(pos.get_key() & (size - 1)) << 2);
}
/// TranspositionTable::new_search() is called at the beginning of every new
-/// search. It increments the "generation" variable, which is used to
+/// search. It increments the "generation" variable, which is used to
/// distinguish transposition table entries from previous searches from
/// entries from the current search.
/// TranspositionTable::insert_pv() is called at the end of a search
-/// iteration, and inserts the PV back into the PV. This makes sure the
-/// old PV moves are searched first, even if the old TT entries have been
-/// overwritten.
+/// iteration, and inserts the PV back into the PV. This makes sure
+/// the old PV moves are searched first, even if the old TT entries
+/// have been overwritten.
-void TranspositionTable::insert_pv(const Position &pos, Move pv[]) {
+void TranspositionTable::insert_pv(const Position& pos, Move pv[]) {
StateInfo st;
Position p(pos);
for (int i = 0; pv[i] != MOVE_NONE; i++)
{
- store(p, VALUE_NONE, Depth(-127*OnePly), pv[i], VALUE_TYPE_NONE);
- p.do_move(pv[i], st);
+ store(p, VALUE_NONE, VALUE_TYPE_NONE, Depth(-127*OnePly), pv[i]);
+ p.do_move(pv[i], st);
}
}
/// entries which have received at least one write during the current search.
/// It is used to display the "info hashfull ..." information in UCI.
-int TranspositionTable::full() {
+int TranspositionTable::full() const {
double N = double(size) * 4.0;
return int(1000 * (1 - exp(writes * log(1.0 - 1.0/N))));
}
-
-
-/// Constructors
-
-TTEntry::TTEntry() {
-}
-
-TTEntry::TTEntry(Key k, Value v, ValueType t, Depth d, Move m,
- int generation) :
- key_ (k), data((m & 0x1FFFF) | (t << 20) | (generation << 23)),
- value_(int16_t(v)), depth_(int16_t(d)) {}
class TTEntry {
public:
- TTEntry();
- TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation);
+ TTEntry() {}
+ TTEntry(Key k, Value v, ValueType t, Depth d, Move m, int generation)
+ : key_ (k), data((m & 0x1FFFF) | (t << 20) | (generation << 23)),
+ value_(int16_t(v)), depth_(int16_t(d)) {}
+
Key key() const { return key_; }
Depth depth() const { return Depth(depth_); }
Move move() const { return Move(data & 0x1FFFF); }
class TranspositionTable {
public:
- TranspositionTable(unsigned mbSize);
+ TranspositionTable();
~TranspositionTable();
void set_size(unsigned mbSize);
void clear();
- void store(const Position &pos, Value v, Depth d, Move m, ValueType type);
- TTEntry* retrieve(const Position &pos) const;
+ void store(const Position& pos, Value v, ValueType type, Depth d, Move m);
+ TTEntry* retrieve(const Position& pos) const;
void new_search();
- void insert_pv(const Position &pos, Move pv[]);
- int full();
+ void insert_pv(const Position& pos, Move pv[]);
+ int full() const;
private:
- inline TTEntry* first_entry(const Position &pos) const;
+ inline TTEntry* first_entry(const Position& pos) const;
- unsigned size;
- int writes;
+ unsigned size, writes;
TTEntry* entries;
uint8_t generation;
};
-
-////
-//// Constants and variables
-////
-
-// Default transposition table size, in megabytes:
-const int TTDefaultSize = 32;
-
-
#endif // !defined(TT_H_INCLUDED)
o["Full Depth Moves (PV nodes)"] = Option(14, 1, 100);
o["Full Depth Moves (non-PV nodes)"] = Option(3, 1, 100);
o["Threat Depth"] = Option(5, 0, 100);
- o["Selective Plies"] = Option(7, 0, 10);
o["Futility Pruning (Main Search)"] = Option(true);
o["Futility Pruning (Quiescence Search)"] = Option(true);
o["LSN filtering"] = Option(true);