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