]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Prefer template to name decoration
[stockfish] / src / misc.cpp
1 /*
2   Glaurung, a UCI chess playing engine.
3   Copyright (C) 2004-2008 Tord Romstad
4
5   Glaurung is free software: you can redistribute it and/or modify
6   it under the terms of the GNU General Public License as published by
7   the Free Software Foundation, either version 3 of the License, or
8   (at your option) any later version.
9
10   Glaurung is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   GNU General Public License for more details.
14
15   You should have received a copy of the GNU General Public License
16   along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 */
18
19
20 ////
21 //// Includes
22 ////
23
24 #if !defined(_MSC_VER)
25
26 #  include <sys/time.h>
27 #  include <sys/types.h>
28 #  include <unistd.h>
29
30 #else
31
32 #  include <windows.h>
33 #  include <time.h>
34 #  include "dos.h"
35 int gettimeofday(struct timeval * tp, struct timezone * tzp);
36
37 #endif
38
39 #include <cstdio>
40 #include <iomanip>
41 #include <iostream>
42 #include <sstream>
43
44 #include "misc.h"
45
46
47 ////
48 //// Variables
49 ////
50
51 long dbg_cnt0 = 0;
52 long dbg_cnt1 = 0;
53
54 bool dbg_show_mean = false;
55 bool dbg_show_hit_rate = false;
56
57
58 ////
59 //// Functions
60 ////
61
62 void dbg_print_hit_rate() {
63
64   std::cout << "Total " << dbg_cnt0 << " Hit " << dbg_cnt1
65             << " hit rate (%) " << (dbg_cnt1*100)/(dbg_cnt0 ? dbg_cnt0 : 1)
66             << std::endl;
67 }
68
69 void dbg_print_mean() {
70
71   std::cout << "Total " << dbg_cnt0 << " Mean "
72             << (float)dbg_cnt1 / (dbg_cnt0 ? dbg_cnt0 : 1) << std::endl;
73 }
74
75 /// engine_name() returns the full name of the current Stockfish version.
76 /// This will be either "Stockfish YYMMDD" (where YYMMDD is the date when the
77 /// program was compiled) or "Stockfish <version number>", depending on whether
78 /// the constant EngineVersion (defined in misc.h) is empty.
79
80 const std::string engine_name() {
81
82   if (EngineVersion.empty())
83   {
84       std::string date(__DATE__); // From compiler, format is "Sep 21 2008"
85       std::string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
86
87       size_t mon = 1 + months.find(date.substr(0, 3)) / 4;
88
89       std::stringstream s;
90       std::string day = (date[4] == ' ' ? date.substr(5, 1) : date.substr(4, 2));
91
92       s << "Stockfish " << date.substr(date.length() - 2) << std::setfill('0')
93         << std::setw(2) << mon << std::setw(2) << day;
94
95       return s.str();
96   } else
97       return "Stockfish " + EngineVersion;
98 }
99
100
101 /// get_system_time() returns the current system time, measured in
102 /// milliseconds.
103
104 int get_system_time() {
105   struct timeval t;
106   gettimeofday(&t, NULL);
107   return t.tv_sec*1000 + t.tv_usec/1000;
108 }
109
110
111 /// cpu_count() tries to detect the number of CPU cores.
112
113 #if !defined(_MSC_VER)
114
115 #  if defined(_SC_NPROCESSORS_ONLN)
116 int cpu_count() {
117   return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
118 }
119 #  else
120 int cpu_count() {
121   return 1;
122 }
123 #  endif
124
125 #else
126
127 int cpu_count() {
128   SYSTEM_INFO s;
129   GetSystemInfo(&s);
130   return Min(s.dwNumberOfProcessors, 8);
131 }
132
133 #endif
134
135
136 /*
137   From Beowulf, from Olithink
138 */
139 #ifndef _WIN32
140 /* Non-windows version */
141 int Bioskey()
142 {
143   fd_set          readfds;
144   struct timeval  timeout;
145
146   FD_ZERO(&readfds);
147   FD_SET(fileno(stdin), &readfds);
148   /* Set to timeout immediately */
149   timeout.tv_sec = 0;
150   timeout.tv_usec = 0;
151   select(16, &readfds, 0, 0, &timeout);
152
153   return (FD_ISSET(fileno(stdin), &readfds));
154 }
155
156 #else
157 /* Windows-version */
158 #include <windows.h>
159 #include <conio.h>
160 int Bioskey()
161 {
162     static int      init = 0,
163                     pipe;
164     static HANDLE   inh;
165     DWORD           dw;
166     /* If we're running under XBoard then we can't use _kbhit() as the input
167      * commands are sent to us directly over the internal pipe */
168
169 #if defined(FILE_CNT)
170     if (stdin->_cnt > 0)
171         return stdin->_cnt;
172 #endif
173     if (!init) {
174         init = 1;
175         inh = GetStdHandle(STD_INPUT_HANDLE);
176         pipe = !GetConsoleMode(inh, &dw);
177         if (!pipe) {
178             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
179             FlushConsoleInputBuffer(inh);
180         }
181     }
182     if (pipe) {
183         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
184             return 1;
185         return dw;
186     } else {
187         // Count the number of unread input records, including keyboard,
188         // mouse, and window-resizing input records.
189         GetNumberOfConsoleInputEvents(inh, &dw);
190         if (dw <= 0)
191             return 0;
192
193         // Read data from console without removing it from the buffer
194         INPUT_RECORD rec[256];
195         DWORD recCnt;
196         if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
197             return 0;
198
199         // Search for at least one keyboard event
200         for (DWORD i = 0; i < recCnt; i++)
201             if (rec[i].EventType == KEY_EVENT)
202                 return 1;
203
204         return 0;
205     }
206 }
207 #endif