2 Glaurung, a UCI chess playing engine.
3 Copyright (C) 2004-2008 Tord Romstad
5 Glaurung is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, either version 3 of the License, or
8 (at your option) any later version.
10 Glaurung is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #if !defined(_MSC_VER)
26 # include <sys/time.h>
27 # include <sys/types.h>
35 int gettimeofday(struct timeval * tp, struct timezone * tzp);
59 void dbg_print_hit_rate() {
61 std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
62 << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
66 /// engine_name() returns the full name of the current Glaurung version.
67 /// This will be either "Glaurung YYMMDD" (where YYMMDD is the date when the
68 /// program was compiled) or "Glaurung <version number>", depending on whether
69 /// the constant EngineVersion (defined in misc.h) is empty.
71 const std::string engine_name() {
73 if (EngineVersion.empty())
75 std::string date(__DATE__); // From compiler, format is "Sep 21 2008"
76 std::string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
78 size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
81 s << "Glaurung clone " << date.substr(date.length() - 2) << std::setfill('0')
82 << std::setw(2) << mon << date.substr(4, 2);
86 return "Glaurung clone " + EngineVersion;
90 /// get_system_time() returns the current system time, measured in
93 int get_system_time() {
95 gettimeofday(&t, NULL);
96 return t.tv_sec*1000 + t.tv_usec/1000;
100 /// cpu_count() tries to detect the number of CPU cores.
102 #if !defined(_MSC_VER)
104 # if defined(_SC_NPROCESSORS_ONLN)
106 return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
119 return Min(s.dwNumberOfProcessors, 8);
126 From Beowulf, from Olithink
129 /* Non-windows version */
133 struct timeval timeout;
136 FD_SET(fileno(stdin), &readfds);
137 /* Set to timeout immediately */
140 select(16, &readfds, 0, 0, &timeout);
142 return (FD_ISSET(fileno(stdin), &readfds));
146 /* Windows-version */
155 /* If we're running under XBoard then we can't use _kbhit() as the input
156 * commands are sent to us directly over the internal pipe */
158 #if defined(FILE_CNT)
164 inh = GetStdHandle(STD_INPUT_HANDLE);
165 pipe = !GetConsoleMode(inh, &dw);
167 SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
168 FlushConsoleInputBuffer(inh);
172 if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
176 // Count the number of unread input records, including keyboard,
177 // mouse, and window-resizing input records.
178 GetNumberOfConsoleInputEvents(inh, &dw);
182 // Read data from console without removing it from the buffer
183 INPUT_RECORD rec[256];
185 if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
188 // Search for at least one keyboard event
189 for (DWORD i = 0; i < recCnt; i++)
190 if (rec[i].EventType == KEY_EVENT)