]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Fix a warning un using anonymous structs
[stockfish] / src / misc.cpp
1 /*
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-2009 Marco Costalba
5
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.
10
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.
15
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/>.
18 */
19
20
21 ////
22 //// Includes
23 ////
24
25 #if !defined(_MSC_VER)
26
27 #  include <sys/time.h>
28 #  include <sys/types.h>
29 #  include <unistd.h>
30
31 #else
32 /*
33    (c) Copyright 1992 Eric Backus
34
35    This software may be used freely so long as this copyright notice is
36    left intact. There is no warrantee on this software.
37 */
38 #  include <windows.h>
39 #  include <time.h>
40 #  include "dos.h"
41
42 static int gettimeofday(struct timeval* tp, struct timezone*)
43 {
44     SYSTEMTIME systime;
45
46     if (tp)
47     {
48         struct tm tmrec;
49         time_t theTime = time(NULL);
50
51         tmrec = *localtime(&theTime);
52         tp->tv_sec = mktime(&tmrec);
53         GetLocalTime(&systime); /* system time */
54
55         tp->tv_usec = systime.wMilliseconds * 1000;
56     }
57     return 0;
58 }
59
60 #endif
61
62 #include <cassert>
63 #include <cstdio>
64 #include <iomanip>
65 #include <iostream>
66 #include <sstream>
67
68 #include "misc.h"
69
70 using namespace std;
71
72 /// Version number. If this is left empty, the current date (in the format
73 /// YYMMDD) is used as a version number.
74
75 static const string EngineVersion = "";
76 static const string AppName = "Stockfish";
77 static const string AppTag  = "";
78
79
80 ////
81 //// Variables
82 ////
83
84 bool Chess960;
85
86 uint64_t dbg_cnt0 = 0;
87 uint64_t dbg_cnt1 = 0;
88
89 bool dbg_show_mean = false;
90 bool dbg_show_hit_rate = false;
91
92
93 ////
94 //// Functions
95 ////
96
97 void dbg_hit_on(bool b) {
98
99     assert(!dbg_show_mean);
100     dbg_show_hit_rate = true;
101     dbg_cnt0++;
102     if (b)
103         dbg_cnt1++;
104 }
105
106 void dbg_hit_on_c(bool c, bool b) {
107
108     if (c)
109         dbg_hit_on(b);
110 }
111
112 void dbg_before() {
113
114     assert(!dbg_show_mean);
115     dbg_show_hit_rate = true;
116     dbg_cnt0++;
117 }
118
119 void dbg_after() {
120
121     assert(!dbg_show_mean);
122     dbg_show_hit_rate = true;
123     dbg_cnt1++;
124 }
125
126 void dbg_mean_of(int v) {
127
128     assert(!dbg_show_hit_rate);
129     dbg_show_mean = true;
130     dbg_cnt0++;
131     dbg_cnt1 += v;
132 }
133
134 void dbg_print_hit_rate() {
135
136   cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
137        << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
138 }
139
140 void dbg_print_mean() {
141
142   cout << "Total " << dbg_cnt0 << " Mean "
143        << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
144 }
145
146 void dbg_print_hit_rate(ofstream& logFile) {
147
148   logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
149           << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
150 }
151
152 void dbg_print_mean(ofstream& logFile) {
153
154   logFile << "Total " << dbg_cnt0 << " Mean "
155           << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
156 }
157
158 /// engine_name() returns the full name of the current Stockfish version.
159 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
160 /// program was compiled) or "Stockfish <version number>", depending on whether
161 /// the constant EngineVersion (defined in misc.h) is empty.
162
163 const string engine_name() {
164
165   if (!EngineVersion.empty())
166       return "Stockfish " + EngineVersion;
167
168   string date(__DATE__); // From compiler, format is "Sep 21 2008"
169   string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
170
171   size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
172
173   stringstream s;
174   string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
175
176   string name = AppName + " " + AppTag + " ";
177
178   s << name << date.substr(date.length() - 2) << setfill('0')
179     << setw(2) << mon << setw(2) << day;
180
181   return s.str();
182 }
183
184
185 /// get_system_time() returns the current system time, measured in
186 /// milliseconds.
187
188 int get_system_time() {
189   struct timeval t;
190   gettimeofday(&t, NULL);
191   return t.tv_sec*1000 + t.tv_usec/1000;
192 }
193
194
195 /// cpu_count() tries to detect the number of CPU cores.
196
197 #if !defined(_MSC_VER)
198
199 #  if defined(_SC_NPROCESSORS_ONLN)
200 int cpu_count() {
201   return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
202 }
203 #  else
204 int cpu_count() {
205   return 1;
206 }
207 #  endif
208
209 #else
210
211 int cpu_count() {
212   SYSTEM_INFO s;
213   GetSystemInfo(&s);
214   return Min(s.dwNumberOfProcessors, 8);
215 }
216
217 #endif
218
219
220 /*
221   From Beowulf, from Olithink
222 */
223 #ifndef _WIN32
224 /* Non-windows version */
225 int Bioskey()
226 {
227   fd_set          readfds;
228   struct timeval  timeout;
229
230   FD_ZERO(&readfds);
231   FD_SET(fileno(stdin), &readfds);
232   /* Set to timeout immediately */
233   timeout.tv_sec = 0;
234   timeout.tv_usec = 0;
235   select(16, &readfds, 0, 0, &timeout);
236
237   return (FD_ISSET(fileno(stdin), &readfds));
238 }
239
240 #else
241 /* Windows-version */
242 #include <windows.h>
243 #include <conio.h>
244 int Bioskey()
245 {
246     static int      init = 0,
247                     pipe;
248     static HANDLE   inh;
249     DWORD           dw;
250     /* If we're running under XBoard then we can't use _kbhit() as the input
251      * commands are sent to us directly over the internal pipe */
252
253 #if defined(FILE_CNT)
254     if (stdin->_cnt > 0)
255         return stdin->_cnt;
256 #endif
257     if (!init) {
258         init = 1;
259         inh = GetStdHandle(STD_INPUT_HANDLE);
260         pipe = !GetConsoleMode(inh, &dw);
261         if (!pipe) {
262             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
263             FlushConsoleInputBuffer(inh);
264         }
265     }
266     if (pipe) {
267         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
268             return 1;
269         return dw;
270     } else {
271         // Count the number of unread input records, including keyboard,
272         // mouse, and window-resizing input records.
273         GetNumberOfConsoleInputEvents(inh, &dw);
274         if (dw <= 0)
275             return 0;
276
277         // Read data from console without removing it from the buffer
278         INPUT_RECORD rec[256];
279         DWORD recCnt;
280         if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
281             return 0;
282
283         // Search for at least one keyboard event
284         for (DWORD i = 0; i < recCnt; i++)
285             if (rec[i].EventType == KEY_EVENT)
286                 return 1;
287
288         return 0;
289     }
290 }
291 #endif