]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Retire move_from_san()
[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 static uint64_t dbg_cnt0 = 0;
71 static uint64_t dbg_cnt1 = 0;
72
73 bool dbg_show_mean = false;
74 bool dbg_show_hit_rate = false;
75
76
77 ////
78 //// Functions
79 ////
80
81 void dbg_hit_on(bool b) {
82
83     assert(!dbg_show_mean);
84     dbg_show_hit_rate = true;
85     dbg_cnt0++;
86     if (b)
87         dbg_cnt1++;
88 }
89
90 void dbg_hit_on_c(bool c, bool b) {
91
92     if (c)
93         dbg_hit_on(b);
94 }
95
96 void dbg_before() {
97
98     assert(!dbg_show_mean);
99     dbg_show_hit_rate = true;
100     dbg_cnt0++;
101 }
102
103 void dbg_after() {
104
105     assert(!dbg_show_mean);
106     dbg_show_hit_rate = true;
107     dbg_cnt1++;
108 }
109
110 void dbg_mean_of(int v) {
111
112     assert(!dbg_show_hit_rate);
113     dbg_show_mean = true;
114     dbg_cnt0++;
115     dbg_cnt1 += v;
116 }
117
118 void dbg_print_hit_rate() {
119
120   cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
121        << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1) << endl;
122 }
123
124 void dbg_print_mean() {
125
126   cout << "Total " << dbg_cnt0 << " Mean "
127        << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << endl;
128 }
129
130
131 /// engine_name() returns the full name of the current Stockfish version.
132 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when
133 /// the program was compiled) or "Stockfish <version number>", depending
134 /// on whether the constant EngineVersion (defined in misc.h) is empty.
135
136 const string engine_name() {
137
138   const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
139   const string cpu64(CpuIs64Bit ? " 64bit" : "");
140
141   if (!EngineVersion.empty())
142       return AppName + " " + EngineVersion + cpu64;
143
144   stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
145   string month, day, year;
146
147   date >> month >> day >> year;
148
149   s << setfill('0') << AppName + " " + AppTag + " "
150     << year.substr(2, 2) << setw(2)
151     << (1 + months.find(month) / 4) << setw(2)
152     << day << cpu64;
153
154   return s.str();
155 }
156
157
158 /// get_system_time() returns the current system time, measured in
159 /// milliseconds.
160
161 int get_system_time() {
162
163 #if defined(_MSC_VER)
164     struct _timeb t;
165     _ftime(&t);
166     return int(t.time*1000 + t.millitm);
167 #else
168     struct timeval t;
169     gettimeofday(&t, NULL);
170     return t.tv_sec*1000 + t.tv_usec/1000;
171 #endif
172 }
173
174
175 /// cpu_count() tries to detect the number of CPU cores.
176
177 #if !defined(_MSC_VER)
178
179 #  if defined(_SC_NPROCESSORS_ONLN)
180 int cpu_count() {
181   return Min(sysconf(_SC_NPROCESSORS_ONLN), MAX_THREADS);
182 }
183 #  elif defined(__hpux)
184 int cpu_count() {
185   struct pst_dynamic psd;
186   if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) == -1)
187       return 1;
188
189   return Min(psd.psd_proc_cnt, MAX_THREADS);
190 }
191 #  else
192 int cpu_count() {
193   return 1;
194 }
195 #  endif
196
197 #else
198
199 int cpu_count() {
200   SYSTEM_INFO s;
201   GetSystemInfo(&s);
202   return Min(s.dwNumberOfProcessors, MAX_THREADS);
203 }
204
205 #endif
206
207
208 /// Check for console input. Original code from Beowulf, Olithink and Greko
209
210 #ifndef _WIN32
211
212 int data_available()
213 {
214   fd_set readfds;
215   struct timeval  timeout;
216
217   FD_ZERO(&readfds);
218   FD_SET(fileno(stdin), &readfds);
219   timeout.tv_sec = 0; // Set to timeout immediately
220   timeout.tv_usec = 0;
221   select(16, &readfds, 0, 0, &timeout);
222
223   return (FD_ISSET(fileno(stdin), &readfds));
224 }
225
226 #else
227
228 int input_available()
229 {
230     static HANDLE inh = NULL;
231     static bool usePipe = false;
232     INPUT_RECORD rec[256];
233     DWORD nchars, recCnt;
234
235     if (!inh)
236     {
237         inh = GetStdHandle(STD_INPUT_HANDLE);
238         if (GetConsoleMode(inh, &nchars))
239         {
240             SetConsoleMode(inh, nchars & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
241             FlushConsoleInputBuffer(inh);
242         } else
243             usePipe = true;
244     }
245
246     // When using Standard C input functions, also check if there
247     // is anything in the buffer. After a call to such functions,
248     // the input waiting in the pipe will be copied to the buffer,
249     // and the call to PeekNamedPipe can indicate no input available.
250     // Setting stdin to unbuffered was not enough. [from Greko]
251     if (stdin->_cnt > 0)
252         return 1;
253
254     // When running under a GUI the input commands are sent to us
255     // directly over the internal pipe. If PeekNamedPipe() returns 0
256     // then something went wrong. Probably the parent program exited.
257     // Returning 1 will make the next call to the input function
258     // return EOF, where this should be catched then.
259     if (usePipe)
260         return PeekNamedPipe(inh, NULL, 0, NULL, &nchars, NULL) ? nchars : 1;
261
262     // Count the number of unread input records, including keyboard,
263     // mouse, and window-resizing input records.
264     GetNumberOfConsoleInputEvents(inh, &nchars);
265
266     // Read data from console without removing it from the buffer
267     if (nchars <= 0 || !PeekConsoleInput(inh, rec, Min(nchars, 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
279
280
281 /// prefetch() preloads the given address in L1/L2 cache. This is a non
282 /// blocking function and do not stalls the CPU waiting for data to be
283 /// loaded from RAM, that can be very slow.
284 #if defined(NO_PREFETCH)
285 void prefetch(char*) {}
286 #else
287
288 void prefetch(char* addr) {
289
290 #if defined(__INTEL_COMPILER) || defined(__ICL)
291    // This hack prevents prefetches to be optimized away by
292    // Intel compiler. Both MSVC and gcc seems not affected.
293    __asm__ ("");
294 #endif
295
296   _mm_prefetch(addr, _MM_HINT_T2);
297   _mm_prefetch(addr+64, _MM_HINT_T2); // 64 bytes ahead
298 }
299
300 #endif
301