2 Stockfish, a UCI chess playing engine derived from Glaurung 2.1
3 Copyright (C) 2004-2008 Tord Romstad (Glaurung author)
4 Copyright (C) 2008-2010 Marco Costalba, Joona Kiiski, Tord Romstad
6 Stockfish is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 Stockfish is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #if !defined(_MSC_VER)
22 # include <sys/time.h>
23 # include <sys/types.h>
26 # include <sys/pstat.h>
31 #define _CRT_SECURE_NO_DEPRECATE
33 #include <sys/timeb.h>
37 #if !defined(NO_PREFETCH)
38 # include <xmmintrin.h>
53 /// Version number. If EngineVersion is left empty, then AppTag plus
54 /// current date (in the format YYMMDD) is used as a version number.
56 static const string AppName = "Stockfish";
57 static const string EngineVersion = "";
58 static const string AppTag = "";
61 /// engine_name() returns the full name of the current Stockfish version.
62 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when
63 /// the program was compiled) or "Stockfish <version number>", depending
64 /// on whether the constant EngineVersion is empty.
66 const string engine_name() {
68 const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
69 const string cpu64(CpuIs64Bit ? " 64bit" : "");
71 if (!EngineVersion.empty())
72 return AppName + " " + EngineVersion + cpu64;
74 stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
75 string month, day, year;
77 date >> month >> day >> year;
79 s << setfill('0') << AppName + " " + AppTag + " "
80 << year.substr(2, 2) << setw(2)
81 << (1 + months.find(month) / 4) << setw(2)
88 /// Our brave developers! Required by UCI
90 const string engine_authors() {
92 return "Tord Romstad, Marco Costalba and Joona Kiiski";
96 /// Debug stuff. Helper functions used mainly for debugging purposes
98 static uint64_t dbg_hit_cnt0;
99 static uint64_t dbg_hit_cnt1;
100 static uint64_t dbg_mean_cnt0;
101 static uint64_t dbg_mean_cnt1;
103 void dbg_print_hit_rate() {
106 cerr << "Total " << dbg_hit_cnt0 << " Hit " << dbg_hit_cnt1
107 << " hit rate (%) " << 100 * dbg_hit_cnt1 / dbg_hit_cnt0 << endl;
110 void dbg_print_mean() {
113 cerr << "Total " << dbg_mean_cnt0 << " Mean "
114 << (float)dbg_mean_cnt1 / dbg_mean_cnt0 << endl;
117 void dbg_mean_of(int v) {
123 void dbg_hit_on(bool b) {
130 void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
131 void dbg_before() { dbg_hit_on(false); }
132 void dbg_after() { dbg_hit_on(true); dbg_hit_cnt0--; }
135 /// get_system_time() returns the current system time, measured in milliseconds
137 int get_system_time() {
139 #if defined(_MSC_VER)
142 return int(t.time * 1000 + t.millitm);
145 gettimeofday(&t, NULL);
146 return t.tv_sec * 1000 + t.tv_usec / 1000;
151 /// cpu_count() tries to detect the number of CPU cores
155 #if defined(_MSC_VER)
158 return Min(s.dwNumberOfProcessors, MAX_THREADS);
161 # if defined(_SC_NPROCESSORS_ONLN)
162 return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
163 # elif defined(__hpux)
164 struct pst_dynamic psd;
165 if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
167 return Min(psd.psd_proc_cnt, MAX_THREADS);
176 /// Check for console input. Original code from Beowulf, Olithink and Greko
180 int input_available() {
183 struct timeval timeout;
186 FD_SET(fileno(stdin), &readfds);
187 timeout.tv_sec = 0; // Set to timeout immediately
189 select(16, &readfds, 0, 0, &timeout);
191 return (FD_ISSET(fileno(stdin), &readfds));
196 int input_available() {
198 static HANDLE inh = NULL;
199 static bool usePipe = false;
200 INPUT_RECORD rec[256];
201 DWORD nchars, recCnt;
205 inh = GetStdHandle(STD_INPUT_HANDLE);
206 if (GetConsoleMode(inh, &nchars))
208 SetConsoleMode(inh, nchars & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
209 FlushConsoleInputBuffer(inh);
214 // When using Standard C input functions, also check if there
215 // is anything in the buffer. After a call to such functions,
216 // the input waiting in the pipe will be copied to the buffer,
217 // and the call to PeekNamedPipe can indicate no input available.
218 // Setting stdin to unbuffered was not enough. [from Greko]
222 // When running under a GUI the input commands are sent to us
223 // directly over the internal pipe. If PeekNamedPipe() returns 0
224 // then something went wrong. Probably the parent program exited.
225 // Returning 1 will make the next call to the input function
226 // return EOF, where this should be catched then.
228 return PeekNamedPipe(inh, NULL, 0, NULL, &nchars, NULL) ? nchars : 1;
230 // Count the number of unread input records, including keyboard,
231 // mouse, and window-resizing input records.
232 GetNumberOfConsoleInputEvents(inh, &nchars);
234 // Read data from console without removing it from the buffer
235 if (nchars <= 0 || !PeekConsoleInput(inh, rec, Min(nchars, 256), &recCnt))
238 // Search for at least one keyboard event
239 for (DWORD i = 0; i < recCnt; i++)
240 if (rec[i].EventType == KEY_EVENT)
249 /// prefetch() preloads the given address in L1/L2 cache. This is a non
250 /// blocking function and do not stalls the CPU waiting for data to be
251 /// loaded from memory, that can be quite slow.
252 #if defined(NO_PREFETCH)
254 void prefetch(char*) {}
258 void prefetch(char* addr) {
260 #if defined(__INTEL_COMPILER) || defined(__ICL)
261 // This hack prevents prefetches to be optimized away by
262 // Intel compiler. Both MSVC and gcc seems not affected.
266 _mm_prefetch(addr, _MM_HINT_T2);
267 _mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead