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