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