]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Unify MovePickerExt template parameters
[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 #if !defined(_MSC_VER)
21
22 #  include <sys/time.h>
23 #  include <sys/types.h>
24 #  include <unistd.h>
25 #  if defined(__hpux)
26 #     include <sys/pstat.h>
27 #  endif
28
29 #else
30
31 #define _CRT_SECURE_NO_DEPRECATE
32 #include <windows.h>
33 #include <sys/timeb.h>
34
35 #endif
36
37 #if !defined(NO_PREFETCH)
38 #  include <xmmintrin.h>
39 #endif
40
41 #include <cassert>
42 #include <cstdio>
43 #include <iomanip>
44 #include <iostream>
45 #include <sstream>
46
47 #include "bitcount.h"
48 #include "misc.h"
49 #include "thread.h"
50
51 using namespace std;
52
53 /// Version number. If EngineVersion is left empty, then AppTag plus
54 /// current date (in the format YYMMDD) is used as a version number.
55
56 static const string AppName = "Stockfish";
57 static const string EngineVersion = "";
58 static const string AppTag  = "";
59
60
61 /// engine_name() returns the full name of the current Stockfish version.
62 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when
63 /// the program was compiled) or "Stockfish <version number>", depending
64 /// on whether the constant EngineVersion is empty.
65
66 const string engine_name() {
67
68   const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
69   const string cpu64(CpuIs64Bit ? " 64bit" : "");
70
71   if (!EngineVersion.empty())
72       return AppName + " " + EngineVersion + cpu64;
73
74   stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
75   string month, day, year;
76
77   date >> month >> day >> year;
78
79   s << setfill('0') << AppName + " " + AppTag + " "
80     << year.substr(2, 2) << setw(2)
81     << (1 + months.find(month) / 4) << setw(2)
82     << day << cpu64;
83
84   return s.str();
85 }
86
87
88 /// Our brave developers! Required by UCI
89
90 const string engine_authors() {
91
92   return "Tord Romstad, Marco Costalba and Joona Kiiski";
93 }
94
95
96 /// Debug stuff. Helper functions used mainly for debugging purposes
97
98 static uint64_t dbg_hit_cnt0;
99 static uint64_t dbg_hit_cnt1;
100 static uint64_t dbg_mean_cnt0;
101 static uint64_t dbg_mean_cnt1;
102
103 void dbg_print_hit_rate() {
104
105   if (dbg_hit_cnt0)
106       cerr << "Total " << dbg_hit_cnt0 << " Hit " << dbg_hit_cnt1
107            << " hit rate (%) " << 100 * dbg_hit_cnt1 / dbg_hit_cnt0 << endl;
108 }
109
110 void dbg_print_mean() {
111
112   if (dbg_mean_cnt0)
113       cerr << "Total " << dbg_mean_cnt0 << " Mean "
114            << (float)dbg_mean_cnt1 / dbg_mean_cnt0 << endl;
115 }
116
117 void dbg_mean_of(int v) {
118
119   dbg_mean_cnt0++;
120   dbg_mean_cnt1 += v;
121 }
122
123 void dbg_hit_on(bool b) {
124
125   dbg_hit_cnt0++;
126   if (b)
127       dbg_hit_cnt1++;
128 }
129
130 void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
131 void dbg_before() { dbg_hit_on(false); }
132 void dbg_after()  { dbg_hit_on(true); dbg_hit_cnt0--; }
133
134
135 /// get_system_time() returns the current system time, measured in milliseconds
136
137 int get_system_time() {
138
139 #if defined(_MSC_VER)
140   struct _timeb t;
141   _ftime(&t);
142   return int(t.time * 1000 + t.millitm);
143 #else
144   struct timeval t;
145   gettimeofday(&t, NULL);
146   return t.tv_sec * 1000 + t.tv_usec / 1000;
147 #endif
148 }
149
150
151 /// cpu_count() tries to detect the number of CPU cores
152
153 int cpu_count() {
154
155 #if defined(_MSC_VER)
156   SYSTEM_INFO s;
157   GetSystemInfo(&s);
158   return Min(s.dwNumberOfProcessors, MAX_THREADS);
159 #else
160
161 #  if defined(_SC_NPROCESSORS_ONLN)
162   return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
163 #  elif defined(__hpux)
164   struct pst_dynamic psd;
165   if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
166       return 1;
167   return Min(psd.psd_proc_cnt, MAX_THREADS);
168 #  else
169   return 1;
170 #  endif
171
172 #endif
173 }
174
175
176 /// Check for console input. Original code from Beowulf, Olithink and Greko
177
178 #ifndef _WIN32
179
180 int input_available() {
181
182   fd_set readfds;
183   struct timeval  timeout;
184
185   FD_ZERO(&readfds);
186   FD_SET(fileno(stdin), &readfds);
187   timeout.tv_sec = 0; // Set to timeout immediately
188   timeout.tv_usec = 0;
189   select(16, &readfds, 0, 0, &timeout);
190
191   return (FD_ISSET(fileno(stdin), &readfds));
192 }
193
194 #else
195
196 int input_available() {
197
198   static HANDLE inh = NULL;
199   static bool usePipe = false;
200   INPUT_RECORD rec[256];
201   DWORD nchars, recCnt;
202
203   if (!inh)
204   {
205       inh = GetStdHandle(STD_INPUT_HANDLE);
206       if (GetConsoleMode(inh, &nchars))
207       {
208           SetConsoleMode(inh, nchars & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
209           FlushConsoleInputBuffer(inh);
210       } else
211           usePipe = true;
212   }
213
214   // When using Standard C input functions, also check if there
215   // is anything in the buffer. After a call to such functions,
216   // the input waiting in the pipe will be copied to the buffer,
217   // and the call to PeekNamedPipe can indicate no input available.
218   // Setting stdin to unbuffered was not enough. [from Greko]
219   if (stdin->_cnt > 0)
220       return 1;
221
222   // When running under a GUI the input commands are sent to us
223   // directly over the internal pipe. If PeekNamedPipe() returns 0
224   // then something went wrong. Probably the parent program exited.
225   // Returning 1 will make the next call to the input function
226   // return EOF, where this should be catched then.
227   if (usePipe)
228       return PeekNamedPipe(inh, NULL, 0, NULL, &nchars, NULL) ? nchars : 1;
229
230   // Count the number of unread input records, including keyboard,
231   // mouse, and window-resizing input records.
232   GetNumberOfConsoleInputEvents(inh, &nchars);
233
234   // Read data from console without removing it from the buffer
235   if (nchars <= 0 || !PeekConsoleInput(inh, rec, Min(nchars, 256), &recCnt))
236       return 0;
237
238   // Search for at least one keyboard event
239   for (DWORD i = 0; i < recCnt; i++)
240       if (rec[i].EventType == KEY_EVENT)
241           return 1;
242
243   return 0;
244 }
245
246 #endif
247
248
249 /// prefetch() preloads the given address in L1/L2 cache. This is a non
250 /// blocking function and do not stalls the CPU waiting for data to be
251 /// loaded from memory, that can be quite slow.
252 #if defined(NO_PREFETCH)
253
254 void prefetch(char*) {}
255
256 #else
257
258 void prefetch(char* addr) {
259
260 #if defined(__INTEL_COMPILER) || defined(__ICL)
261    // This hack prevents prefetches to be optimized away by
262    // Intel compiler. Both MSVC and gcc seems not affected.
263    __asm__ ("");
264 #endif
265
266   _mm_prefetch(addr, _MM_HINT_T2);
267   _mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead
268 }
269
270 #endif