UCT virtual_loss: Revert default change
[pachi.git] / util.h
blob188f3e4d953b45959e96ad21bf841cb5097630af
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((LONG volatile *) (ap), (b));
13 #define __sync_fetch_and_sub(ap, b) InterlockedExchangeAdd((LONG volatile *) (ap), -(b));
15 /* MinGW gcc, no function prototype for built-in function stpcpy() */
16 char *stpcpy (char *dest, const char *src);
18 #include <ctype.h>
19 static inline const char *
20 strcasestr(const char *haystack, const char *needle)
22 for (const char *p = haystack; *p; p++) {
23 for (int ni = 0; needle[ni]; ni++) {
24 if (!p[ni])
25 return NULL;
26 if (toupper(p[ni]) != toupper(needle[ni]))
27 goto more_hay;
29 return p;
30 more_hay:;
32 return NULL;
34 #endif
36 /* Misc. definitions. */
38 /* Use make DOUBLE=1 in large configurations with counts > 1M
39 * where 24 bits of floating_t mantissa become insufficient. */
40 #ifdef DOUBLE
41 # define floating_t double
42 # define PRIfloating "%lf"
43 #else
44 # define floating_t float
45 # define PRIfloating "%f"
46 #endif
48 #define likely(x) __builtin_expect(!!(x), 1)
49 #define unlikely(x) __builtin_expect((x), 0)
51 static inline void *
52 checked_malloc(size_t size, char *filename, unsigned int line, const char *func)
54 void *p = malloc(size);
55 if (!p) {
56 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY malloc(%zu)\n",
57 filename, line, func, size);
58 exit(1);
60 return p;
63 static inline void *
64 checked_calloc(size_t nmemb, size_t size, char *filename, unsigned int line, const char *func)
66 void *p = calloc(nmemb, size);
67 if (!p) {
68 fprintf(stderr, "%s:%u: %s: OUT OF MEMORY calloc(%zu, %zu)\n",
69 filename, line, func, nmemb, size);
70 exit(1);
72 return p;
75 #define malloc2(size) checked_malloc((size), __FILE__, __LINE__, __func__)
76 #define calloc2(nmemb, size) checked_calloc((nmemb), (size), __FILE__, __LINE__, __func__)
78 #endif