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() {
72 if(EngineVersion == "") {
73 static const char monthNames[12][4] = {
74 "Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"
76 const char *dateString = __DATE__;
78 int month = 0, day = 0;
80 for(int i = 0; i < 12; i++)
81 if(strncmp(dateString, monthNames[i], 3) == 0)
83 day = atoi(dateString+4);
85 s << "Glaurung " << (dateString+9) << std::setfill('0') << std::setw(2)
86 << month << std::setfill('0') << std::setw(2) << day;
91 return "Glaurung " + EngineVersion;
95 /// get_system_time() returns the current system time, measured in
98 int get_system_time() {
100 gettimeofday(&t, NULL);
101 return t.tv_sec*1000 + t.tv_usec/1000;
105 /// cpu_count() tries to detect the number of CPU cores.
107 #if !defined(_MSC_VER)
109 # if defined(_SC_NPROCESSORS_ONLN)
111 return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
124 return Min(s.dwNumberOfProcessors, 8);
131 From Beowulf, from Olithink
134 /* Non-windows version */
138 struct timeval timeout;
141 FD_SET(fileno(stdin), &readfds);
142 /* Set to timeout immediately */
145 select(16, &readfds, 0, 0, &timeout);
147 return (FD_ISSET(fileno(stdin), &readfds));
151 /* Windows-version */
160 /* If we're running under XBoard then we can't use _kbhit() as the input
161 * commands are sent to us directly over the internal pipe */
163 #if defined(FILE_CNT)
169 inh = GetStdHandle(STD_INPUT_HANDLE);
170 pipe = !GetConsoleMode(inh, &dw);
172 SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
173 FlushConsoleInputBuffer(inh);
177 if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
181 // Count the number of unread input records, including keyboard,
182 // mouse, and window-resizing input records.
183 GetNumberOfConsoleInputEvents(inh, &dw);
187 // Read data from console without removing it from the buffer
188 INPUT_RECORD rec[256];
190 if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
193 // Search for at least one keyboard event
194 for (DWORD i = 0; i < recCnt; i++)
195 if (rec[i].EventType == KEY_EVENT)