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>
36 int gettimeofday(struct timeval * tp, struct timezone * tzp);
52 static const std::string AppName = "Stockfish";
53 static const std::string AppTag = "";
58 bool dbg_show_mean = false;
59 bool dbg_show_hit_rate = false;
66 void dbg_print_hit_rate() {
68 std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
69 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
73 void dbg_print_mean() {
75 std::cout << "Total " << dbg_cnt0 << " Mean "
76 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
79 void dbg_print_hit_rate(std::ofstream& logFile) {
81 logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
82 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
86 void dbg_print_mean(std::ofstream& logFile) {
88 logFile << "Total " << dbg_cnt0 << " Mean "
89 << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
92 /// engine_name() returns the full name of the current Stockfish version.
93 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
94 /// program was compiled) or "Stockfish <version number>", depending on whether
95 /// the constant EngineVersion (defined in misc.h) is empty.
97 const std::string engine_name() {
99 if (EngineVersion.empty())
101 std::string date(__DATE__); // From compiler, format is "Sep 21 2008"
102 std::string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
104 size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
107 std::string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
109 std::string name = AppName + " " + AppTag + " ";
111 s << name << date.substr(date.length() - 2) << std::setfill('0')
112 << std::setw(2) << mon << std::setw(2) << day;
116 return "Stockfish " + EngineVersion;
120 /// get_system_time() returns the current system time, measured in
123 int get_system_time() {
125 gettimeofday(&t, NULL);
126 return t.tv_sec*1000 + t.tv_usec/1000;
130 /// cpu_count() tries to detect the number of CPU cores.
132 #if !defined(_MSC_VER)
134 # if defined(_SC_NPROCESSORS_ONLN)
136 return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
149 return Min(s.dwNumberOfProcessors, 8);
156 From Beowulf, from Olithink
159 /* Non-windows version */
163 struct timeval timeout;
166 FD_SET(fileno(stdin), &readfds);
167 /* Set to timeout immediately */
170 select(16, &readfds, 0, 0, &timeout);
172 return (FD_ISSET(fileno(stdin), &readfds));
176 /* Windows-version */
185 /* If we're running under XBoard then we can't use _kbhit() as the input
186 * commands are sent to us directly over the internal pipe */
188 #if defined(FILE_CNT)
194 inh = GetStdHandle(STD_INPUT_HANDLE);
195 pipe = !GetConsoleMode(inh, &dw);
197 SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
198 FlushConsoleInputBuffer(inh);
202 if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
206 // Count the number of unread input records, including keyboard,
207 // mouse, and window-resizing input records.
208 GetNumberOfConsoleInputEvents(inh, &dw);
212 // Read data from console without removing it from the buffer
213 INPUT_RECORD rec[256];
215 if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
218 // Search for at least one keyboard event
219 for (DWORD i = 0; i < recCnt; i++)
220 if (rec[i].EventType == KEY_EVENT)