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/>.
25 #if !defined(_MSC_VER)
27 # include <sys/time.h>
28 # include <sys/types.h>
31 # include <sys/pstat.h>
36 #define _CRT_SECURE_NO_DEPRECATE
38 #include <sys/timeb.h>
42 #if !defined(NO_PREFETCH)
43 # include <xmmintrin.h>
58 /// Version number. If this is left empty, the current date (in the format
59 /// YYMMDD) is used as a version number.
61 static const string EngineVersion = "";
62 static const string AppName = "Stockfish";
63 static const string AppTag = "";
70 static uint64_t dbg_cnt0 = 0;
71 static uint64_t dbg_cnt1 = 0;
73 bool dbg_show_mean = false;
74 bool dbg_show_hit_rate = false;
81 void dbg_hit_on(bool b) {
83 assert(!dbg_show_mean);
84 dbg_show_hit_rate = true;
90 void dbg_hit_on_c(bool c, bool b) {
98 assert(!dbg_show_mean);
99 dbg_show_hit_rate = true;
105 assert(!dbg_show_mean);
106 dbg_show_hit_rate = true;
110 void dbg_mean_of(int v) {
112 assert(!dbg_show_hit_rate);
113 dbg_show_mean = true;
118 void dbg_print_hit_rate() {
120 cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
121 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
124 void dbg_print_mean() {
126 cout << "Total " << dbg_cnt0 << " Mean "
127 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
131 /// engine_name() returns the full name of the current Stockfish version.
132 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when
133 /// the program was compiled) or "Stockfish <version number>", depending
134 /// on whether the constant EngineVersion (defined in misc.h) is empty.
136 const string engine_name() {
138 const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
139 const string cpu64(CpuIs64Bit ? " 64bit" : "");
141 if (!EngineVersion.empty())
142 return AppName + " " + EngineVersion + cpu64;
144 stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
145 string month, day, year;
147 date >> month >> day >> year;
149 s << setfill('0') << AppName + " " + AppTag + " "
150 << year.substr(2, 2) << setw(2)
151 << (1 + months.find(month) / 4) << setw(2)
157 const string engine_author() { return "Tord Romstad, Marco Costalba and Joona Kiiski"; }
160 /// get_system_time() returns the current system time, measured in
163 int get_system_time() {
165 #if defined(_MSC_VER)
168 return int(t.time*1000 + t.millitm);
171 gettimeofday(&t, NULL);
172 return t.tv_sec*1000 + t.tv_usec/1000;
177 /// cpu_count() tries to detect the number of CPU cores.
179 #if !defined(_MSC_VER)
181 # if defined(_SC_NPROCESSORS_ONLN)
183 return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
185 # elif defined(__hpux)
187 struct pst_dynamic psd;
188 if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
191 return Min(psd.psd_proc_cnt, MAX_THREADS);
204 return Min(s.dwNumberOfProcessors, MAX_THREADS);
210 /// Check for console input. Original code from Beowulf, Olithink and Greko
214 int input_available()
217 struct timeval timeout;
220 FD_SET(fileno(stdin), &readfds);
221 timeout.tv_sec = 0; // Set to timeout immediately
223 select(16, &readfds, 0, 0, &timeout);
225 return (FD_ISSET(fileno(stdin), &readfds));
230 int input_available()
232 static HANDLE inh = NULL;
233 static bool usePipe = false;
234 INPUT_RECORD rec[256];
235 DWORD nchars, recCnt;
239 inh = GetStdHandle(STD_INPUT_HANDLE);
240 if (GetConsoleMode(inh, &nchars))
242 SetConsoleMode(inh, nchars & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
243 FlushConsoleInputBuffer(inh);
248 // When using Standard C input functions, also check if there
249 // is anything in the buffer. After a call to such functions,
250 // the input waiting in the pipe will be copied to the buffer,
251 // and the call to PeekNamedPipe can indicate no input available.
252 // Setting stdin to unbuffered was not enough. [from Greko]
256 // When running under a GUI the input commands are sent to us
257 // directly over the internal pipe. If PeekNamedPipe() returns 0
258 // then something went wrong. Probably the parent program exited.
259 // Returning 1 will make the next call to the input function
260 // return EOF, where this should be catched then.
262 return PeekNamedPipe(inh, NULL, 0, NULL, &nchars, NULL) ? nchars : 1;
264 // Count the number of unread input records, including keyboard,
265 // mouse, and window-resizing input records.
266 GetNumberOfConsoleInputEvents(inh, &nchars);
268 // Read data from console without removing it from the buffer
269 if (nchars <= 0 || !PeekConsoleInput(inh, rec, Min(nchars, 256), &recCnt))
272 // Search for at least one keyboard event
273 for (DWORD i = 0; i < recCnt; i++)
274 if (rec[i].EventType == KEY_EVENT)
283 /// prefetch() preloads the given address in L1/L2 cache. This is a non
284 /// blocking function and do not stalls the CPU waiting for data to be
285 /// loaded from RAM, that can be very slow.
286 #if defined(NO_PREFETCH)
287 void prefetch(char*) {}
290 void prefetch(char* addr) {
292 #if defined(__INTEL_COMPILER) || defined(__ICL)
293 // This hack prevents prefetches to be optimized away by
294 // Intel compiler. Both MSVC and gcc seems not affected.
298 _mm_prefetch(addr, _MM_HINT_T2);
299 _mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead