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