]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Improve time managment
[stockfish] / src / misc.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
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.
9
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.
14
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/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #if !defined(_MSC_VER)
25
26 #  include <sys/time.h>
27 #  include <sys/types.h>
28 #  include <unistd.h>
29
30 #else
31
32 #  include <windows.h>
33 #  include <time.h>
34 #  include "dos.h"
35 int gettimeofday(struct timeval * tp, struct timezone * tzp);
36
37 #endif
38
39 #include <cstdio>
40 #include <iomanip>
41 #include <iostream>
42 #include <sstream>
43
44 #include "misc.h"
45
46
47 ////
48 //// Variables
49 ////
50
51 long dbg_cnt0 = 0;
52 long dbg_cnt1 = 0;
53
54
55 ////
56 //// Functions
57 ////
58
59 void dbg_print_hit_rate() {
60
61   std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
62             << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
63             << std::endl;
64 }
65
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.
70
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"
75     };
76     const char *dateString = __DATE__;
77     std::stringstream s;
78     int month = 0, day = 0;
79
80     for(int i = 0; i < 12; i++)
81       if(strncmp(dateString, monthNames[i], 3) == 0)
82         month = i + 1;
83     day = atoi(dateString+4);
84
85     s << "Glaurung " << (dateString+9) << std::setfill('0') << std::setw(2)
86       << month << std::setfill('0') << std::setw(2) << day;
87
88     return s.str();
89   }
90   else
91     return "Glaurung " + EngineVersion;
92 }
93
94
95 /// get_system_time() returns the current system time, measured in
96 /// milliseconds.
97
98 int get_system_time() {
99   struct timeval t;
100   gettimeofday(&t, NULL);
101   return t.tv_sec*1000 + t.tv_usec/1000;
102 }
103
104
105 /// cpu_count() tries to detect the number of CPU cores.
106
107 #if !defined(_MSC_VER)
108
109 #  if defined(_SC_NPROCESSORS_ONLN)
110 int cpu_count() {
111   return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
112 }
113 #  else
114 int cpu_count() {
115   return 1;
116 }
117 #  endif
118
119 #else
120
121 int cpu_count() {
122   SYSTEM_INFO s;
123   GetSystemInfo(&s);
124   return Min(s.dwNumberOfProcessors, 8);
125 }
126
127 #endif
128
129
130 /*
131   From Beowulf, from Olithink
132 */
133 #ifndef _WIN32
134 /* Non-windows version */
135 int Bioskey()
136 {
137   fd_set          readfds;
138   struct timeval  timeout;
139
140   FD_ZERO(&readfds);
141   FD_SET(fileno(stdin), &readfds);
142   /* Set to timeout immediately */
143   timeout.tv_sec = 0;
144   timeout.tv_usec = 0;
145   select(16, &readfds, 0, 0, &timeout);
146
147   return (FD_ISSET(fileno(stdin), &readfds));
148 }
149
150 #else
151 /* Windows-version */
152 #include <windows.h>
153 #include <conio.h>
154 int Bioskey()
155 {
156     static int      init = 0,
157                     pipe;
158     static HANDLE   inh;
159     DWORD           dw;
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 */
162
163 #if defined(FILE_CNT)
164     if (stdin->_cnt > 0)
165         return stdin->_cnt;
166 #endif
167     if (!init) {
168         init = 1;
169         inh = GetStdHandle(STD_INPUT_HANDLE);
170         pipe = !GetConsoleMode(inh, &dw);
171         if (!pipe) {
172             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
173             FlushConsoleInputBuffer(inh);
174         }
175     }
176     if (pipe) {
177         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
178             return 1;
179         return dw;
180     } else {
181         GetNumberOfConsoleInputEvents(inh, &dw);
182         return dw <= 1 ? 0 : dw;
183     }
184 }
185 #endif