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