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>
54 /// Version number. If this is left empty, the current date (in the format
55 /// YYMMDD) is used as a version number.
57 static const string EngineVersion = "";
58 static const string AppName = "Stockfish";
59 static const string AppTag = "";
68 uint64_t dbg_cnt0 = 0;
69 uint64_t dbg_cnt1 = 0;
71 bool dbg_show_mean = false;
72 bool dbg_show_hit_rate = false;
79 void dbg_hit_on(bool b) {
81 assert(!dbg_show_mean);
82 dbg_show_hit_rate = true;
88 void dbg_hit_on_c(bool c, bool b) {
96 assert(!dbg_show_mean);
97 dbg_show_hit_rate = true;
103 assert(!dbg_show_mean);
104 dbg_show_hit_rate = true;
108 void dbg_mean_of(int v) {
110 assert(!dbg_show_hit_rate);
111 dbg_show_mean = true;
116 void dbg_print_hit_rate() {
118 cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
119 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
122 void dbg_print_mean() {
124 cout << "Total " << dbg_cnt0 << " Mean "
125 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
128 void dbg_print_hit_rate(ofstream& logFile) {
130 logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
131 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
134 void dbg_print_mean(ofstream& logFile) {
136 logFile << "Total " << dbg_cnt0 << " Mean "
137 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
140 /// engine_name() returns the full name of the current Stockfish version.
141 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
142 /// program was compiled) or "Stockfish <version number>", depending on whether
143 /// the constant EngineVersion (defined in misc.h) is empty.
145 const string engine_name() {
147 const string cpu64(CpuHas64BitPath ? " 64bit" : "");
149 if (!EngineVersion.empty())
150 return AppName+ " " + EngineVersion + cpu64;
152 string date(__DATE__); // From compiler, format is "Sep 21 2008"
153 string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
155 size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
158 string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
160 string name = AppName + " " + AppTag + " ";
162 s << name << date.substr(date.length() - 2) << setfill('0')
163 << setw(2) << mon << setw(2) << day << cpu64;
169 /// get_system_time() returns the current system time, measured in
172 int get_system_time() {
174 #if defined(_MSC_VER)
177 return int(t.time*1000 + t.millitm);
180 gettimeofday(&t, NULL);
181 return t.tv_sec*1000 + t.tv_usec/1000;
186 /// cpu_count() tries to detect the number of CPU cores.
188 #if !defined(_MSC_VER)
190 # if defined(_SC_NPROCESSORS_ONLN)
192 return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
194 # elif defined(__hpux)
196 struct pst_dynamic psd;
197 if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
200 return Min(psd.psd_proc_cnt, MAX_THREADS);
213 return Min(s.dwNumberOfProcessors, MAX_THREADS);
220 From Beowulf, from Olithink
223 /* Non-windows version */
227 struct timeval timeout;
230 FD_SET(fileno(stdin), &readfds);
231 /* Set to timeout immediately */
234 select(16, &readfds, 0, 0, &timeout);
236 return (FD_ISSET(fileno(stdin), &readfds));
240 /* Windows-version */
249 /* If we're running under XBoard then we can't use _kbhit() as the input
250 * commands are sent to us directly over the internal pipe */
252 #if defined(FILE_CNT)
258 inh = GetStdHandle(STD_INPUT_HANDLE);
259 pipe = !GetConsoleMode(inh, &dw);
261 SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
262 FlushConsoleInputBuffer(inh);
266 if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
270 // Count the number of unread input records, including keyboard,
271 // mouse, and window-resizing input records.
272 GetNumberOfConsoleInputEvents(inh, &dw);
276 // Read data from console without removing it from the buffer
277 INPUT_RECORD rec[256];
279 if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
282 // Search for at least one keyboard event
283 for (DWORD i = 0; i < recCnt; i++)
284 if (rec[i].EventType == KEY_EVENT)