]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Lower SingularExtensionDepth to 7 plies for non-pv
[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-2010 Marco Costalba, Joona Kiiski, Tord Romstad
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 #  if defined(__hpux)
31 #     include <sys/pstat.h>
32 #  endif
33
34 #else
35
36 #define _CRT_SECURE_NO_DEPRECATE
37 #include <windows.h>
38 #include <sys/timeb.h>
39
40 #endif
41
42 #if !defined(NO_PREFETCH)
43 #  include <xmmintrin.h>
44 #endif
45
46 #include <cassert>
47 #include <cstdio>
48 #include <iomanip>
49 #include <iostream>
50 #include <sstream>
51
52 #include "bitcount.h"
53 #include "misc.h"
54 #include "thread.h"
55
56 using namespace std;
57
58 /// Version number. If this is left empty, the current date (in the format
59 /// YYMMDD) is used as a version number.
60
61 static const string EngineVersion = "";
62 static const string AppName = "Stockfish";
63 static const string AppTag  = "";
64
65
66 ////
67 //// Variables
68 ////
69
70 bool Chess960;
71
72 uint64_t dbg_cnt0 = 0;
73 uint64_t dbg_cnt1 = 0;
74
75 bool dbg_show_mean = false;
76 bool dbg_show_hit_rate = false;
77
78
79 ////
80 //// Functions
81 ////
82
83 void dbg_hit_on(bool b) {
84
85     assert(!dbg_show_mean);
86     dbg_show_hit_rate = true;
87     dbg_cnt0++;
88     if (b)
89         dbg_cnt1++;
90 }
91
92 void dbg_hit_on_c(bool c, bool b) {
93
94     if (c)
95         dbg_hit_on(b);
96 }
97
98 void dbg_before() {
99
100     assert(!dbg_show_mean);
101     dbg_show_hit_rate = true;
102     dbg_cnt0++;
103 }
104
105 void dbg_after() {
106
107     assert(!dbg_show_mean);
108     dbg_show_hit_rate = true;
109     dbg_cnt1++;
110 }
111
112 void dbg_mean_of(int v) {
113
114     assert(!dbg_show_hit_rate);
115     dbg_show_mean = true;
116     dbg_cnt0++;
117     dbg_cnt1 += v;
118 }
119
120 void dbg_print_hit_rate() {
121
122   cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
123        << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
124 }
125
126 void dbg_print_mean() {
127
128   cout << "Total " << dbg_cnt0 << " Mean "
129        << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
130 }
131
132 void dbg_print_hit_rate(ofstream& logFile) {
133
134   logFile << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
135           << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
136 }
137
138 void dbg_print_mean(ofstream& logFile) {
139
140   logFile << "Total " << dbg_cnt0 << " Mean "
141           << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
142 }
143
144 /// engine_name() returns the full name of the current Stockfish version.
145 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
146 /// program was compiled) or "Stockfish <version number>", depending on whether
147 /// the constant EngineVersion (defined in misc.h) is empty.
148
149 const string engine_name() {
150
151   const string cpu64(CpuHas64BitPath ? " 64bit" : "");
152
153   if (!EngineVersion.empty())
154       return AppName + " " + EngineVersion + cpu64;
155
156   string date(__DATE__); // From compiler, format is "Sep 21 2008"
157   string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
158
159   size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
160
161   stringstream s;
162   string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
163
164   string name = AppName + " " + AppTag + " ";
165
166   s << name << date.substr(date.length() - 2) << setfill('0')
167     << setw(2) << mon << setw(2) << day << cpu64;
168
169   return s.str();
170 }
171
172
173 /// get_system_time() returns the current system time, measured in
174 /// milliseconds.
175
176 int get_system_time() {
177
178 #if defined(_MSC_VER)
179     struct _timeb t;
180     _ftime(&t);
181     return int(t.time*1000 + t.millitm);
182 #else
183     struct timeval t;
184     gettimeofday(&t, NULL);
185     return t.tv_sec*1000 + t.tv_usec/1000;
186 #endif
187 }
188
189
190 /// cpu_count() tries to detect the number of CPU cores.
191
192 #if !defined(_MSC_VER)
193
194 #  if defined(_SC_NPROCESSORS_ONLN)
195 int cpu_count() {
196   return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
197 }
198 #  elif defined(__hpux)
199 int cpu_count() {
200   struct pst_dynamic psd;
201   if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
202       return 1;
203
204   return Min(psd.psd_proc_cnt, MAX_THREADS);
205 }
206 #  else
207 int cpu_count() {
208   return 1;
209 }
210 #  endif
211
212 #else
213
214 int cpu_count() {
215   SYSTEM_INFO s;
216   GetSystemInfo(&s);
217   return Min(s.dwNumberOfProcessors, MAX_THREADS);
218 }
219
220 #endif
221
222
223 /*
224   From Beowulf, from Olithink
225 */
226 #ifndef _WIN32
227 /* Non-windows version */
228 int Bioskey()
229 {
230   fd_set          readfds;
231   struct timeval  timeout;
232
233   FD_ZERO(&readfds);
234   FD_SET(fileno(stdin), &readfds);
235   /* Set to timeout immediately */
236   timeout.tv_sec = 0;
237   timeout.tv_usec = 0;
238   select(16, &readfds, 0, 0, &timeout);
239
240   return (FD_ISSET(fileno(stdin), &readfds));
241 }
242
243 #else
244 /* Windows-version */
245 #include <windows.h>
246 #include <conio.h>
247 int Bioskey()
248 {
249     static int      init = 0,
250                     pipe;
251     static HANDLE   inh;
252     DWORD           dw;
253     /* If we're running under XBoard then we can't use _kbhit() as the input
254      * commands are sent to us directly over the internal pipe */
255
256 #if defined(FILE_CNT)
257     if (stdin->_cnt > 0)
258         return stdin->_cnt;
259 #endif
260     if (!init) {
261         init = 1;
262         inh = GetStdHandle(STD_INPUT_HANDLE);
263         pipe = !GetConsoleMode(inh, &dw);
264         if (!pipe) {
265             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
266             FlushConsoleInputBuffer(inh);
267         }
268     }
269     if (pipe) {
270         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
271             return 1;
272         return dw;
273     } else {
274         // Count the number of unread input records, including keyboard,
275         // mouse, and window-resizing input records.
276         GetNumberOfConsoleInputEvents(inh, &dw);
277         if (dw <= 0)
278             return 0;
279
280         // Read data from console without removing it from the buffer
281         INPUT_RECORD rec[256];
282         DWORD recCnt;
283         if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
284             return 0;
285
286         // Search for at least one keyboard event
287         for (DWORD i = 0; i < recCnt; i++)
288             if (rec[i].EventType == KEY_EVENT)
289                 return 1;
290
291         return 0;
292     }
293 }
294
295 #endif
296
297 /// prefetch() preloads the given address in L1/L2 cache. This is a non
298 /// blocking function and do not stalls the CPU waiting for data to be
299 /// loaded from RAM, that can be very slow.
300 #if defined(NO_PREFETCH)
301 void prefetch(char*) {}
302 #else
303
304 void prefetch(char* addr) {
305
306 #if defined(__INTEL_COMPILER) || defined(__ICL)
307    // This hack prevents prefetches to be optimized away by
308    // Intel compiler. Both MSVC and gcc seems not affected.
309    __asm__ ("");
310 #endif
311
312   _mm_prefetch(addr, _MM_HINT_T2);
313   _mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead
314 }
315
316 #endif
317