]> git.sesse.net Git - stockfish/blob - src/misc.cpp
Grammar fix in MovePicker::next_move
[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-2014 Marco Costalba, Joona Kiiski, Tord Romstad
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 #include <iomanip>
21 #include <iostream>
22 #include <sstream>
23
24 #include "misc.h"
25 #include "thread.h"
26
27 using namespace std;
28
29 /// Version number. If Version is left empty, then compile date in the format
30 /// DD-MM-YY and show in engine_info.
31 static const string Version = "";
32
33
34 /// engine_info() returns the full name of the current Stockfish version. This
35 /// will be either "Stockfish <Tag> DD-MM-YY" (where DD-MM-YY is the date when
36 /// the program was compiled) or "Stockfish <Version>", depending on whether
37 /// Version is empty.
38
39 const string engine_info(bool to_uci) {
40
41   const string months("Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec");
42   string month, day, year;
43   stringstream s, date(__DATE__); // From compiler, format is "Sep 21 2008"
44
45   s << "Stockfish " << Version << setfill('0');
46
47   if (Version.empty())
48   {
49       date >> month >> day >> year;
50       s << setw(2) << day << setw(2) << (1 + months.find(month) / 4) << year.substr(2);
51   }
52
53   s << (Is64Bit ? " 64" : "")
54     << (HasPopCnt ? " SSE4.2" : "")
55     << (to_uci ? "\nid author ": " by ")
56     << "Tord Romstad, Marco Costalba and Joona Kiiski";
57
58   return s.str();
59 }
60
61
62 /// Debug functions used mainly to collect run-time statistics
63
64 static uint64_t hits[2], means[2];
65
66 void dbg_hit_on(bool b) { ++hits[0]; if (b) ++hits[1]; }
67 void dbg_hit_on_c(bool c, bool b) { if (c) dbg_hit_on(b); }
68 void dbg_mean_of(int v) { ++means[0]; means[1] += v; }
69
70 void dbg_print() {
71
72   if (hits[0])
73       cerr << "Total " << hits[0] << " Hits " << hits[1]
74            << " hit rate (%) " << 100 * hits[1] / hits[0] << endl;
75
76   if (means[0])
77       cerr << "Total " << means[0] << " Mean "
78            << (double)means[1] / means[0] << endl;
79 }
80
81
82 /// Our fancy logging facility. The trick here is to replace cin.rdbuf() and
83 /// cout.rdbuf() with two Tie objects that tie cin and cout to a file stream. We
84 /// can toggle the logging of std::cout and std:cin at runtime whilst preserving
85 /// usual i/o functionality, all without changing a single line of code!
86 /// Idea from http://groups.google.com/group/comp.lang.c++/msg/1d941c0f26ea0d81
87
88 struct Tie: public streambuf { // MSVC requires splitted streambuf for cin and cout
89
90   Tie(streambuf* b, ofstream* f) : buf(b), file(f) {}
91
92   int sync() { return file->rdbuf()->pubsync(), buf->pubsync(); }
93   int overflow(int c) { return log(buf->sputc((char)c), "<< "); }
94   int underflow() { return buf->sgetc(); }
95   int uflow() { return log(buf->sbumpc(), ">> "); }
96
97   streambuf* buf;
98   ofstream* file;
99
100   int log(int c, const char* prefix) {
101
102     static int last = '\n';
103
104     if (last == '\n')
105         file->rdbuf()->sputn(prefix, 3);
106
107     return last = file->rdbuf()->sputc((char)c);
108   }
109 };
110
111 class Logger {
112
113   Logger() : in(cin.rdbuf(), &file), out(cout.rdbuf(), &file) {}
114  ~Logger() { start(false); }
115
116   ofstream file;
117   Tie in, out;
118
119 public:
120   static void start(bool b) {
121
122     static Logger l;
123
124     if (b && !l.file.is_open())
125     {
126         l.file.open("io_log.txt", ifstream::out | ifstream::app);
127         cin.rdbuf(&l.in);
128         cout.rdbuf(&l.out);
129     }
130     else if (!b && l.file.is_open())
131     {
132         cout.rdbuf(l.out.buf);
133         cin.rdbuf(l.in.buf);
134         l.file.close();
135     }
136   }
137 };
138
139
140 /// Used to serialize access to std::cout to avoid multiple threads writing at
141 /// the same time.
142
143 std::ostream& operator<<(std::ostream& os, SyncCout sc) {
144
145   static Mutex m;
146
147   if (sc == io_lock)
148       m.lock();
149
150   if (sc == io_unlock)
151       m.unlock();
152
153   return os;
154 }
155
156
157 /// Trampoline helper to avoid moving Logger to misc.h
158 void start_logger(bool b) { Logger::start(b); }
159
160
161 /// timed_wait() waits for msec milliseconds. It is mainly a helper to wrap
162 /// the conversion from milliseconds to struct timespec, as used by pthreads.
163
164 void timed_wait(WaitCondition& sleepCond, Lock& sleepLock, int msec) {
165
166 #ifdef _WIN32
167   int tm = msec;
168 #else
169   timespec ts, *tm = &ts;
170   uint64_t ms = Time::now() + msec;
171
172   ts.tv_sec = ms / 1000;
173   ts.tv_nsec = (ms % 1000) * 1000000LL;
174 #endif
175
176   cond_timedwait(sleepCond, sleepLock, tm);
177 }
178
179
180 /// prefetch() preloads the given address in L1/L2 cache. This is a non-blocking
181 /// function that doesn't stall the CPU waiting for data to be loaded from memory,
182 /// which can be quite slow.
183 #ifdef NO_PREFETCH
184
185 void prefetch(char*) {}
186
187 #else
188
189 void prefetch(char* addr) {
190
191 #  if defined(__INTEL_COMPILER)
192    // This hack prevents prefetches from being optimized away by
193    // Intel compiler. Both MSVC and gcc seem not be affected by this.
194    __asm__ ("");
195 #  endif
196
197 #  if defined(__INTEL_COMPILER) || defined(_MSC_VER)
198   _mm_prefetch(addr, _MM_HINT_T0);
199 #  else
200   __builtin_prefetch(addr);
201 #  endif
202 }
203
204 #endif