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 Marco Costalba
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>
33 (c) Copyright 1992 Eric Backus
35 This software may be used freely so long as this copyright notice is
36 left intact. There is no warrantee on this software.
42 static int gettimeofday(struct timeval* tp, struct timezone*)
49 time_t theTime = time(NULL);
51 tmrec = *localtime(&theTime);
52 tp->tv_sec = mktime(&tmrec);
53 GetLocalTime(&systime); /* system time */
55 tp->tv_usec = systime.wMilliseconds * 1000;
72 /// Version number. If this is left empty, the current date (in the format
73 /// YYMMDD) is used as a version number.
75 static const string EngineVersion = "";
76 static const string AppName = "Stockfish";
77 static const string AppTag = "";
87 bool dbg_show_mean = false;
88 bool dbg_show_hit_rate = false;
95 void dbg_hit_on(bool b) {
97 assert(!dbg_show_mean);
98 dbg_show_hit_rate = true;
104 void dbg_hit_on_c(bool c, bool b) {
112 assert(!dbg_show_mean);
113 dbg_show_hit_rate = true;
119 assert(!dbg_show_mean);
120 dbg_show_hit_rate = true;
124 void dbg_mean_of(int v) {
126 assert(!dbg_show_hit_rate);
127 dbg_show_mean = true;
132 void dbg_print_hit_rate() {
134 cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
135 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
138 void dbg_print_mean() {
140 cout << "Total " << dbg_cnt0 << " Mean "
141 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
144 void dbg_print_hit_rate(ofstream& logFile) {
146 logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
147 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
150 void dbg_print_mean(ofstream& logFile) {
152 logFile << "Total " << dbg_cnt0 << " Mean "
153 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
156 /// engine_name() returns the full name of the current Stockfish version.
157 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
158 /// program was compiled) or "Stockfish <version number>", depending on whether
159 /// the constant EngineVersion (defined in misc.h) is empty.
161 const string engine_name() {
163 if (!EngineVersion.empty())
164 return "Stockfish " + EngineVersion;
166 string date(__DATE__); // From compiler, format is "Sep 21 2008"
167 string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
169 size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
172 string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
174 string name = AppName + " " + AppTag + " ";
176 s << name << date.substr(date.length() - 2) << setfill('0')
177 << setw(2) << mon << setw(2) << day;
183 /// get_system_time() returns the current system time, measured in
186 int get_system_time() {
188 gettimeofday(&t, NULL);
189 return t.tv_sec*1000 + t.tv_usec/1000;
193 /// cpu_count() tries to detect the number of CPU cores.
195 #if !defined(_MSC_VER)
197 # if defined(_SC_NPROCESSORS_ONLN)
199 return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
212 return Min(s.dwNumberOfProcessors, 8);
219 From Beowulf, from Olithink
222 /* Non-windows version */
226 struct timeval timeout;
229 FD_SET(fileno(stdin), &readfds);
230 /* Set to timeout immediately */
233 select(16, &readfds, 0, 0, &timeout);
235 return (FD_ISSET(fileno(stdin), &readfds));
239 /* Windows-version */
248 /* If we're running under XBoard then we can't use _kbhit() as the input
249 * commands are sent to us directly over the internal pipe */
251 #if defined(FILE_CNT)
257 inh = GetStdHandle(STD_INPUT_HANDLE);
258 pipe = !GetConsoleMode(inh, &dw);
260 SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
261 FlushConsoleInputBuffer(inh);
265 if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
269 // Count the number of unread input records, including keyboard,
270 // mouse, and window-resizing input records.
271 GetNumberOfConsoleInputEvents(inh, &dw);
275 // Read data from console without removing it from the buffer
276 INPUT_RECORD rec[256];
278 if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
281 // Search for at least one keyboard event
282 for (DWORD i = 0; i < recCnt; i++)
283 if (rec[i].EventType == KEY_EVENT)