]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Evaluate: weight_option() is static
[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       s << "Glaurung clone " << date.substr(date.length() - 2) << std::setfill('0')
82         << std::setw(2) << mon << date.substr(4, 2);
83
84       return s.str();
85   } else
86       return "Glaurung clone " + EngineVersion;
87 }
88
89
90 /// get_system_time() returns the current system time, measured in
91 /// milliseconds.
92
93 int get_system_time() {
94   struct timeval t;
95   gettimeofday(&t, NULL);
96   return t.tv_sec*1000 + t.tv_usec/1000;
97 }
98
99
100 /// cpu_count() tries to detect the number of CPU cores.
101
102 #if !defined(_MSC_VER)
103
104 #  if defined(_SC_NPROCESSORS_ONLN)
105 int cpu_count() {
106   return Min(sysconf(_SC_NPROCESSORS_ONLN), 8);
107 }
108 #  else
109 int cpu_count() {
110   return 1;
111 }
112 #  endif
113
114 #else
115
116 int cpu_count() {
117   SYSTEM_INFO s;
118   GetSystemInfo(&s);
119   return Min(s.dwNumberOfProcessors, 8);
120 }
121
122 #endif
123
124
125 /*
126   From Beowulf, from Olithink
127 */
128 #ifndef _WIN32
129 /* Non-windows version */
130 int Bioskey()
131 {
132   fd_set          readfds;
133   struct timeval  timeout;
134
135   FD_ZERO(&readfds);
136   FD_SET(fileno(stdin), &readfds);
137   /* Set to timeout immediately */
138   timeout.tv_sec = 0;
139   timeout.tv_usec = 0;
140   select(16, &readfds, 0, 0, &timeout);
141
142   return (FD_ISSET(fileno(stdin), &readfds));
143 }
144
145 #else
146 /* Windows-version */
147 #include <windows.h>
148 #include <conio.h>
149 int Bioskey()
150 {
151     static int      init = 0,
152                     pipe;
153     static HANDLE   inh;
154     DWORD           dw;
155     /* If we're running under XBoard then we can't use _kbhit() as the input
156      * commands are sent to us directly over the internal pipe */
157
158 #if defined(FILE_CNT)
159     if (stdin->_cnt > 0)
160         return stdin->_cnt;
161 #endif
162     if (!init) {
163         init = 1;
164         inh = GetStdHandle(STD_INPUT_HANDLE);
165         pipe = !GetConsoleMode(inh, &dw);
166         if (!pipe) {
167             SetConsoleMode(inh, dw & ~(ENABLE_MOUSE_INPUT | ENABLE_WINDOW_INPUT));
168             FlushConsoleInputBuffer(inh);
169         }
170     }
171     if (pipe) {
172         if (!PeekNamedPipe(inh, NULL, 0, NULL, &dw, NULL))
173             return 1;
174         return dw;
175     } else {
176         // Count the number of unread input records, including keyboard,
177         // mouse, and window-resizing input records.
178         GetNumberOfConsoleInputEvents(inh, &dw);
179         if (dw <= 0)
180             return 0;
181
182         // Read data from console without removing it from the buffer
183         INPUT_RECORD rec[256];
184         DWORD recCnt;
185         if (!PeekConsoleInput(inh, rec, Min(dw, 256), &recCnt))
186             return 0;
187
188         // Search for at least one keyboard event
189         for (DWORD i = 0; i < recCnt; i++)
190             if (rec[i].EventType == KEY_EVENT)
191                 return 1;
192
193         return 0;
194     }
195 }
196 #endif