63char*
parse_word(
const char*,
char*,
unsigned int);
64char*
parse_field(
const char*,
char*,
unsigned int,
char);
65char*
parse_line(
const char*,
char*,
unsigned int);
72char*
parse_command(
const char*,
char*,
char*,
const unsigned int);
99#define MAX(a, b) ((a) > (b) ? (a) : (b))
102#define MIN(a, b) ((a) < (b) ? (a) : (b))
105#define BOUND(var, min, max, name) do {\
106 if (var < min && min <= max) {\
107 if (name) fprintf(stderr, "\nWARNING: %s = %lld is out of range. Set to %lld\n", name, (long long)var, (long long)min);\
109 } else if (var > max) {\
110 if (name) fprintf(stderr, "\nWARNING: %s = %lld is out of range. Set to %lld\n", name, (long long)var, (long long)max);\
119#if defined(__unix__) || (defined(_WIN32) && defined(USE_PTHREAD)) || defined(__APPLE__)
124typedef pthread_t Thread;
127typedef pthread_mutex_t Lock;
130typedef pthread_cond_t Condition;
133#define thread_detach(thread) pthread_detach(thread)
135#if DEBUG && defined(__unix__) && !defined(__APPLE__)
137#define lock(c) if (pthread_mutex_lock(&(c)->lock)) { \
142#define unlock(c) if (pthread_mutex_unlock(&(c)->lock)) { \
147#define lock_init(c) do {\
148 pthread_mutexattr_t attr;\
149 pthread_mutexattr_init(&attr);\
150 pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ERRORCHECK_NP);\
151 pthread_mutex_init(&(c)->lock, &attr);\
155#define lock_free(c) if (pthread_mutex_destroy(&(c)->lock)) { \
156 error("lock_free"); \
162#define lock(c) pthread_mutex_lock(&(c)->lock)
165#define unlock(c) pthread_mutex_unlock(&(c)->lock)
168#define lock_init(c) pthread_mutex_init(&(c)->lock, NULL)
171#define lock_free(c) pthread_mutex_destroy(&(c)->lock)
177#include <libkern/OSAtomic.h>
180typedef OSSpinLock SpinLock;
183#define spin_lock(c) OSSpinLockLock(&(c)->spin)
186#define spin_unlock(c) OSSpinLockUnlock(&(c)->spin)
189#define spin_init(c) do {(c)->spin = OS_SPINLOCK_INIT;} while (0)
195#elif defined(__USE_XOPEN2K)
198typedef pthread_spinlock_t SpinLock;
201#define spin_lock(c) pthread_spin_lock(&(c)->spin)
204#define spin_unlock(c) pthread_spin_unlock(&(c)->spin)
207#define spin_init(c) pthread_spin_init(&(c)->spin, PTHREAD_PROCESS_PRIVATE)
210#define spin_free(c) pthread_spin_destroy(&(c)->spin)
215typedef pthread_mutex_t SpinLock;
218#define spin_lock(c) pthread_mutex_lock(&(c)->spin)
221#define spin_unlock(c) pthread_mutex_unlock(&(c)->spin)
224#define spin_init(c) pthread_mutex_init(&(c)->spin, NULL)
227#define spin_free(c) pthread_mutex_destroy(&(c)->spin)
232#define condition_init(c) pthread_cond_init(&(c)->cond, NULL)
235#define condition_wait(c) pthread_cond_wait(&(c)->cond, &(c)->lock)
238#define condition_signal(c) pthread_cond_signal(&(c)->cond)
241#define condition_broadcast(c) pthread_cond_broadcast(&(c)->cond)
244#define condition_free(c) pthread_cond_destroy(&(c)->cond)
251typedef HANDLE Thread;
254typedef CRITICAL_SECTION Lock;
257typedef CRITICAL_SECTION SpinLock;
265void InitializeConditionVariable(CONDITION_VARIABLE*);
266void WakeConditionVariable(CONDITION_VARIABLE*);
267void WakeAllConditionVariable(CONDITION_VARIABLE*);
268BOOL SleepConditionVariableCS(CONDITION_VARIABLE*, CRITICAL_SECTION*, DWORD);
273typedef CONDITION_VARIABLE Condition;
276#define condition_init(c) InitializeConditionVariable(&(c)->cond)
279#define condition_wait(c) SleepConditionVariableCS(&(c)->cond, &(c)->lock, INFINITE)
282#define condition_signal(c) WakeConditionVariable(&(c)->cond)
285#define condition_broadcast(c) WakeAllConditionVariable(&(c)->cond)
288#define condition_free(c)
293#define thread_detach(thread) CloseHandle(thread)
296#define lock(c) EnterCriticalSection(&(c)->lock)
299#define unlock(c) LeaveCriticalSection(&(c)->lock)
302#define lock_init(c) InitializeCriticalSection(&(c)->lock)
305#define lock_free(c) DeleteCriticalSection(&(c)->lock)
308#define spin_lock(c) EnterCriticalSection(&(c)->spin)
311#define spin_unlock(c) LeaveCriticalSection(&(c)->spin)
314#define spin_init(c) InitializeCriticalSection(&(c)->spin)
317#define spin_free(c) DeleteCriticalSection(&(c)->spin)
328static inline void atomic_add(
volatile unsigned long long *value,
long long i)
330#if defined(USE_GAS_X64)
331 __asm__ __volatile__(
"lock xaddq %1, %0":
"=m"(*value) :
"r"(i),
"m" (*value));
332#elif defined(USE_MSVC_X64)
333 _InterlockedAdd64(value, i);
350#define fatal_error(...) \
352 fprintf(stderr, "\nFATAL ERROR: %s : %s : %d : ", __FILE__, __func__, __LINE__); \
353 if (errno) fprintf(stderr, "\terror #%d : %s", errno, strerror(errno)); \
354 fputc('\n', stderr); \
355 fprintf(stderr, __VA_ARGS__); \
364 fprintf(stderr, "\nERROR: %s : %s : %d :", __FILE__, __func__, __LINE__); \
365 if (errno) fprintf(stderr, " error #%d : %s", errno, strerror(errno)); \
366 fputc('\n', stderr); \
367 fprintf(stderr, __VA_ARGS__); \
376 fprintf(stderr, "\nWARNING: "); \
377 fprintf(stderr, __VA_ARGS__); \
383#define info(...) if (options.info) { \
384 fprintf(stderr, __VA_ARGS__); \
390#define cassio_debug(...) if (options.debug_cassio) { \
392 printf(__VA_ARGS__); \
393 log_print(engine_log, "DEBUG: "); \
394 log_print(engine_log, __VA_ARGS__);\
402#define trace(...) do {\
403 fprintf(stderr, "trace %s : %s : %d : ", __FILE__, __func__, __LINE__); \
404 fprintf(stderr, __VA_ARGS__); \
412 fprintf(stderr, "\nDEBUG : "); \
413 fprintf(stderr, __VA_ARGS__); \
430#define log_open(l, file) if ((l)->f == NULL && file != NULL) { \
432 (l)->f = fopen(file, "w"); \
436#define log_close(l) if ((l)->f) { \
443#define log_print(l, ...) if ((l)->f) { \
444 fprintf((l)->f, __VA_ARGS__); \
449#define log_is_open(l) ((l)->f != NULL)
452#define log_receive(l, title, ...) if ((l)->f) { \
453 fprintf((l)->f, "%s", title); \
454 time_stamp((l)->f); \
455 fprintf((l)->f, " <== "); \
456 fprintf((l)->f, __VA_ARGS__); \
461#define log_send(l, title, ...) if ((l)->f) { \
462 fprintf((l)->f, "%s", title); \
463 time_stamp((l)->f); \
464 fprintf((l)->f, " ==> "); \
465 fprintf((l)->f, __VA_ARGS__); \
LogFile.
Definition util.h:424
Lock lock
Definition util.h:426
FILE * f
Definition util.h:425
unsigned long long x
Definition util.h:89
char * parse_boolean(const char *, bool *)
Parse a boolean.
Definition util.c:807
char * file_add_ext(const char *, const char *, char *)
Add an extension to a string.
Definition util.c:973
char * parse_move(const char *, const struct Board *, struct Move *)
char * parse_int(const char *, int *)
Parse an integer.
Definition util.c:827
char * parse_command(const char *, char *, char *, const unsigned int)
Parse a command.
Definition util.c:933
char * string_copy_line(FILE *)
void time_print(long long, bool, FILE *)
Print time as "D:HH:MM:SS.CC".
Definition util.c:197
long long real_clock(void)
char * parse_skip_spaces(const char *)
Skip spaces.
Definition util.c:580
long long time_read(FILE *)
read time as "D:HH:MM:SS.C".
Definition util.c:222
long long cpu_clock(void)
int string_to_int(const char *, const int)
Convert a string into an integer.
Definition util.c:523
void time_stamp(FILE *)
Print local time.
Definition util.c:255
long long(* time_clock)(void)
Time clock.
Definition util.c:188
FILE * file_open(const char *, const char *)
Open a file from a UTF-8 path on Windows.
Definition util.c:78
void random_seed(Random *, const unsigned long long)
Pseudo-random number seed.
Definition util.c:1128
char * parse_real(const char *, double *)
Parse a real number (as a double floating point).
Definition util.c:862
int string_to_coordinate(const char *)
Convert the two first chars of a string into a coordinate.
Definition util.c:450
void print_scientific(double, const char *, FILE *)
Print a value with a unit.
Definition util.c:316
char * parse_line(const char *, char *, unsigned int)
Parse a line.
Definition util.c:670
bool is_stdin_keyboard(void)
void relax(int)
sleep for t ms.
Definition util.c:269
char * parse_skip_word(const char *)
Skip word.
Definition util.c:594
char * parse_game(const char *, const struct Board *, struct Line *)
char * string_read_line(FILE *)
Read a line.
Definition util.c:331
void thread_create2(Thread *, void *(*f)(void *), void *)
Create a thread.
Definition util.c:988
static void atomic_add(volatile unsigned long long *value, long long i)
Definition util.h:328
char * parse_board(const char *, struct Board *, int *)
Parse a board.
Definition util.c:748
int get_cpu_number(void)
Get the number of cpus or cores on the machine.
Definition util.c:1053
void path_get_dir(const char *, char *)
Extract the directory of a file path.
Definition util.c:954
void thread_join(Thread)
Join a thread.
Definition util.c:1006
void string_to_uppercase(char *)
Change all char of a string to uppercase.
Definition util.c:435
char * string_to_word(char *)
remove spaces from a string.
Definition util.c:477
char * parse_field(const char *, char *, unsigned int, char)
Parse a field.
Definition util.c:648
double string_to_real(const char *, const double)
Convert a string into a real number.
Definition util.c:554
unsigned long long random_get(Random *)
Pseudo-random number generator.
Definition util.c:1109
char * string_duplicate(const char *)
Duplicate a string.
Definition util.c:365
char * parse_find(const char *, const int)
Find a char.
Definition util.c:611
long long string_to_time(const char *)
Read time as "D:HH:MM:SS.C".
Definition util.c:386
char * format_scientific(double, const char *, char *)
Format a value with a unit.
Definition util.c:288
char * parse_word(const char *, char *, unsigned int)
Parse a word.
Definition util.c:628
bool string_to_boolean(const char *)
Convert a string into a boolean.
Definition util.c:498
void string_to_lowercase(char *)
Change all char of a string to lowercase.
Definition util.c:421
void thread_set_cpu(Thread, int)
Choose a single core or cpu to run on, under linux systems, to avoid context changes.
Definition util.c:1033
Thread thread_self(void)
Current thread.
Definition util.c:1020