GTP play: Add support for a trailing engine-specific argument
[pachi/t.git] / util.h
blob2711c6d8abd05bcaec78a2c9555a0fce164526a3
1 #ifndef PACHI_UTIL_H
2 #define PACHI_UTIL_H
4 #include <stdlib.h>
6 /* Portability definitions. */
8 #ifdef _WIN32
9 #include <windows.h>
11 #define sleep(seconds) Sleep((seconds) * 1000)
12 #define __sync_fetch_and_add(ap, b) InterlockedExchangeAdd((unsigned long *) (ap), (b));
13 #define __sync_fetch_and_sub(ap, b) InterlockedExchangeAdd((unsigned long *) (ap), -(b));
15 #include <ctype.h>
16 static inline const char *
17 strcasestr(const char *haystack, const char *needle)
19 for (const char *p = haystack; *p; p++) {
20 for (int ni = 0; needle[ni]; ni++) {
21 if (!p[ni])
22 return NULL;
23 if (toupper(p[ni]) != toupper(needle[ni]))
24 goto more_hay;
26 return p;
27 more_hay:;
29 return NULL;
31 #endif
33 /* Misc. definitions. */
35 /* Use make DOUBLE=1 in large configurations with counts > 1M
36 * where 24 bits of floating_t mantissa become insufficient. */
37 #ifdef DOUBLE
38 # define floating_t double
39 # define PRIfloating "%lf"
40 #else
41 # define floating_t float
42 # define PRIfloating "%f"
43 #endif
45 #define likely(x) __builtin_expect(!!(x), 1)
46 #define unlikely(x) __builtin_expect((x), 0)
48 static inline void *
49 checked_malloc(size_t size, char *filename, unsigned int line, const char *func)
51 void *p = malloc(size);
52 if (!p) {
53 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY malloc(%zu)\n",
54 filename, line, func, size);
55 exit(1);
57 return p;
60 static inline void *
61 checked_calloc(size_t nmemb, size_t size, char *filename, unsigned int line, const char *func)
63 void *p = calloc(nmemb, size);
64 if (!p) {
65 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY calloc(%zu, %zu)\n",
66 filename, line, func, nmemb, size);
67 exit(1);
69 return p;
72 #define malloc2(size) checked_malloc((size), __FILE__, __LINE__, __func__)
73 #define calloc2(nmemb, size) checked_calloc((nmemb), (size), __FILE__, __LINE__, __func__)
75 #endif