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