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