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.
41 static int gettimeofday(struct timeval* tp, struct timezone*)
48 time_t theTime = time(NULL);
50 tmrec = *localtime(&theTime);
51 tp->tv_sec = mktime(&tmrec);
52 GetLocalTime(&systime); /* system time */
54 tp->tv_usec = systime.wMilliseconds * 1000;
71 /// Version number. If this is left empty, the current date (in the format
72 /// YYMMDD) is used as a version number.
74 static const string EngineVersion = "";
75 static const string AppName = "Stockfish";
76 static const string AppTag = "";
86 bool dbg_show_mean = false;
87 bool dbg_show_hit_rate = false;
94 void dbg_hit_on(bool b) {
96 assert(!dbg_show_mean);
97 dbg_show_hit_rate = true;
103 void dbg_hit_on_c(bool c, bool b) {
111 assert(!dbg_show_mean);
112 dbg_show_hit_rate = true;
118 assert(!dbg_show_mean);
119 dbg_show_hit_rate = true;
123 void dbg_mean_of(int v) {
125 assert(!dbg_show_hit_rate);
126 dbg_show_mean = true;
131 void dbg_print_hit_rate() {
133 cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
134 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
137 void dbg_print_mean() {
139 cout << "Total " << dbg_cnt0 << " Mean "
140 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
143 void dbg_print_hit_rate(ofstream& logFile) {
145 logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
146 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
149 void dbg_print_mean(ofstream& logFile) {
151 logFile << "Total " << dbg_cnt0 << " Mean "
152 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
155 /// engine_name() returns the full name of the current Stockfish version.
156 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
157 /// program was compiled) or "Stockfish <version number>", depending on whether
158 /// the constant EngineVersion (defined in misc.h) is empty.
160 const string engine_name() {
162 if (!EngineVersion.empty())
163 return "Stockfish " + EngineVersion;
165 string date(__DATE__); // From compiler, format is "Sep 21 2008"
166 string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
168 size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
171 string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
173 string name = AppName + " " + AppTag + " ";
175 s << name << date.substr(date.length() - 2) << setfill('0')
176 << setw(2) << mon << setw(2) << day;
182 /// get_system_time() returns the current system time, measured in
185 int get_system_time() {
187 gettimeofday(&t, NULL);
188 return t.tv_sec*1000 + t.tv_usec/1000;
192 /// cpu_count() tries to detect the number of CPU cores.
194 #if !defined(_MSC_VER)
196 # if defined(_SC_NPROCESSORS_ONLN)
198 return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
211 return Min(s.dwNumberOfProcessors, 8);
218 From Beowulf, from Olithink
221 /* Non-windows version */
225 struct timeval timeout;
228 FD_SET(fileno(stdin), &readfds);
229 /* Set to timeout immediately */
232 select(16, &readfds, 0, 0, &timeout);
234 return (FD_ISSET(fileno(stdin), &readfds));
238 /* Windows-version */
247 /* If we're running under XBoard then we can't use _kbhit() as the input
248 * commands are sent to us directly over the internal pipe */
250 #if defined(FILE_CNT)
256 inh = GetStdHandle(STD_INPUT_HANDLE);
257 pipe = !GetConsoleMode(inh, &dw);
259 SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
260 FlushConsoleInputBuffer(inh);
264 if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
268 // Count the number of unread input records, including keyboard,
269 // mouse, and window-resizing input records.
270 GetNumberOfConsoleInputEvents(inh, &dw);
274 // Read data from console without removing it from the buffer
275 INPUT_RECORD rec[256];
277 if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
280 // Search for at least one keyboard event
281 for (DWORD i = 0; i < recCnt; i++)
282 if (rec[i].EventType == KEY_EVENT)