1 /* util.cpp
2
3 GNU Chess engine
4
5 Copyright (C) 2001-2011 Free Software Foundation, Inc.
6
7 This program is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21
22 // util.cpp
23
24 // includes
25
26 #include <cctype>
27 #include <cerrno>
28 #include <cmath>
29 #include <cstdarg>
30 #include <cstdio>
31 #include <cstdlib>
32 #include <cstring>
33 #include <ctime>
34
35 #include "posix.h"
36 #include "util.h"
37
38 namespace engine {
39
40 // Streams used for coommunication with the the adapter
41
42 extern FILE *pipefd_a2e_0_stream;
43 extern FILE *pipefd_e2a_1_stream;
44
45 // functions
46
47 // util_init()
48
util_init()49 void util_init() {
50
51 setvbuf(pipefd_a2e_0_stream,NULL,_IONBF,0);
52 setvbuf(pipefd_e2a_1_stream,NULL,_IONBF,0); // _IOLBF breaks on Windows!
53 }
54
55 // my_random_init()
56
my_random_init()57 void my_random_init() {
58
59 srand(time(NULL));
60 }
61
62 // my_random()
63
my_random(int n)64 int my_random(int n) {
65
66 double r;
67
68 ASSERT(n>0);
69
70 r = double(rand()) / (double(RAND_MAX) + 1.0);
71
72 return int(floor(r*double(n)));
73 }
74
75 // my_atoll()
76
my_atoll(const char string[])77 sint64 my_atoll(const char string[]) {
78
79 sint64 n;
80
81 sscanf(string,S64_FORMAT,&n);
82
83 return n;
84 }
85
86 // my_round()
87
my_round(double x)88 int my_round(double x) {
89
90 return int(floor(x+0.5));
91 }
92
93 // my_malloc()
94
my_malloc(int size)95 void * my_malloc(int size) {
96
97 void * address;
98
99 ASSERT(size>0);
100
101 address = malloc(size);
102 if (address == NULL) my_fatal("my_malloc(): malloc(): %s\n",strerror(errno));
103
104 return address;
105 }
106
107 // my_free()
108
my_free(void * address)109 void my_free(void * address) {
110
111 ASSERT(address!=NULL);
112
113 free(address);
114 }
115
116 // my_fatal()
117
my_fatal(const char format[],...)118 void my_fatal(const char format[], ...) {
119
120 va_list ap;
121
122 ASSERT(format!=NULL);
123
124 va_start(ap,format);
125 vfprintf(stderr,format,ap);
126 va_end(ap);
127
128 exit(EXIT_FAILURE);
129 // abort();
130 }
131
132 // my_file_read_line()
133
my_file_read_line(FILE * file,char string[],int size)134 bool my_file_read_line(FILE * file, char string[], int size) {
135
136 char * ptr;
137
138 ASSERT(file!=NULL);
139 ASSERT(string!=NULL);
140 ASSERT(size>0);
141
142 if (fgets(string,size,file) == NULL) {
143 if (feof(file)) {
144 return false;
145 } else { // error
146 my_fatal("my_file_read_line(): fgets(): %s\n",strerror(errno));
147 }
148 }
149
150 // suppress '\n'
151
152 ptr = strchr(string,'\n');
153 if (ptr != NULL) *ptr = '\0';
154
155 return true;
156 }
157
158 // my_string_empty()
159
my_string_empty(const char string[])160 bool my_string_empty(const char string[]) {
161
162 return string == NULL || string[0] == '\0';
163 }
164
165 // my_string_equal()
166
my_string_equal(const char string_1[],const char string_2[])167 bool my_string_equal(const char string_1[], const char string_2[]) {
168
169 int c1, c2;
170
171 ASSERT(string_1!=NULL);
172 ASSERT(string_2!=NULL);
173
174 while (true) {
175
176 c1 = *string_1++;
177 c2 = *string_2++;
178
179 if (tolower(c1) != tolower(c2)) return false;
180 if (c1 == '\0') return true;
181 }
182
183 return false;
184 }
185
186 // my_strdup()
187
my_strdup(const char string[])188 char * my_strdup(const char string[]) {
189
190 char * address;
191
192 ASSERT(string!=NULL);
193
194 // strdup() is not ANSI C
195
196 address = (char *) my_malloc(strlen(string)+1);
197 strcpy(address,string);
198
199 return address;
200 }
201
202 // my_string_clear()
203
my_string_clear(const char ** variable)204 void my_string_clear(const char * * variable) {
205
206 ASSERT(variable!=NULL);
207
208 if (*variable != NULL) {
209 my_free((void*)(*variable));
210 *variable = NULL;
211 }
212 }
213
214 // my_string_set()
215
my_string_set(const char ** variable,const char string[])216 void my_string_set(const char * * variable, const char string[]) {
217
218 ASSERT(variable!=NULL);
219 ASSERT(string!=NULL);
220
221 if (*variable != NULL) my_free((void*)(*variable));
222 *variable = my_strdup(string);
223 }
224
225 // my_timer_reset()
226
my_timer_reset(my_timer_t * timer)227 void my_timer_reset(my_timer_t * timer) {
228
229 ASSERT(timer!=NULL);
230
231 timer->start_real = 0.0;
232 timer->start_cpu = 0.0;
233 timer->elapsed_real = 0.0;
234 timer->elapsed_cpu = 0.0;
235 timer->running = false;
236 }
237
238 // my_timer_start()
239
my_timer_start(my_timer_t * timer)240 void my_timer_start(my_timer_t * timer) {
241
242 ASSERT(timer!=NULL);
243
244 ASSERT(timer->start_real==0.0);
245 ASSERT(timer->start_cpu==0.0);
246 ASSERT(!timer->running);
247
248 timer->running = true;
249 timer->start_real = now_real();
250 timer->start_cpu = now_cpu();
251 }
252
253 // my_timer_stop()
254
my_timer_stop(my_timer_t * timer)255 void my_timer_stop(my_timer_t * timer) {
256
257 ASSERT(timer!=NULL);
258
259 ASSERT(timer->running);
260
261 timer->elapsed_real += now_real() - timer->start_real;
262 timer->elapsed_cpu += now_cpu() - timer->start_cpu;
263 timer->start_real = 0.0;
264 timer->start_cpu = 0.0;
265 timer->running = false;
266 }
267
268 // my_timer_elapsed_real()
269
my_timer_elapsed_real(const my_timer_t * timer)270 double my_timer_elapsed_real(const my_timer_t * timer) {
271
272 double elapsed;
273
274 ASSERT(timer!=NULL);
275
276 elapsed = timer->elapsed_real;
277 if (timer->running) elapsed += now_real() - timer->start_real;
278
279 if (elapsed < 0.0) elapsed = 0.0;
280
281 return elapsed;
282 }
283
284 // my_timer_elapsed_cpu()
285
my_timer_elapsed_cpu(const my_timer_t * timer)286 double my_timer_elapsed_cpu(const my_timer_t * timer) {
287
288 double elapsed;
289
290 ASSERT(timer!=NULL);
291
292 elapsed = timer->elapsed_cpu;
293 if (timer->running) elapsed += now_cpu() - timer->start_cpu;
294
295 if (elapsed < 0.0) elapsed = 0.0;
296
297 return elapsed;
298 }
299
300 // my_timer_cpu_usage()
301
my_timer_cpu_usage(const my_timer_t * timer)302 double my_timer_cpu_usage(const my_timer_t * timer) {
303
304 double real, cpu;
305 double usage;
306
307 ASSERT(timer!=NULL);
308
309 real = my_timer_elapsed_real(timer);
310 cpu = my_timer_elapsed_cpu(timer);
311
312 if (real <= 0.0 || cpu <= 0.0) return 0.0;
313
314 usage = cpu / real;
315 if (usage >= 1.0) usage = 1.0;
316
317 return usage;
318 }
319
320 } // namespace engine
321
322 // end of util.cpp
323
324