33#if defined(__unix__) || defined(__APPLE__)
37#include <sys/resource.h>
44#include <sys/sysinfo.h>
59#define S_ISREG(x) (x & _S_IFREG)
62#define S_ISFIFO(x) (x & _S_IFIFO)
67#if defined(__unix__) || defined(__APPLE__)
79 clock_gettime(CLOCK_MONOTONIC, &tv);
80 return tv.tv_sec * 1000ULL + tv.tv_nsec / 1000000ULL;
83 gettimeofday(&tv, NULL);
84 return tv.tv_sec * 1000 + tv.tv_usec / 1000;
97 getrusage(RUSAGE_SELF, &u);
98 return 1000 * u.ru_utime.tv_sec + u.ru_utime.tv_usec / 1000;
101#elif defined (_WIN32)
105 return GetTickCount();
110 return GetTickCount();
135 const char *space = justified ?
" " :
"";
138 if (t < 0) {sign = -1; t = -t;}
else sign = +1;
139 d = t / 86400000; t -= d * 86400000LL;
140 h = t / 3600000; t -= h * 3600000;
141 m = t / 60000; t -= m * 60000;
145 if (d) fprintf(f,
"%2d:%02d:%02d:%02d.%03d", sign*d, h, m, s, c);
146 else if (h) fprintf(f,
"%s%2d:%02d:%02d.%03d", space, sign*h, m, s, c);
147 else fprintf(f,
"%s%s%2d:%02d.%03d", space, space, sign*m, s, c);
161 while (isspace(c = getc(f)));
163 n = 0;
while (isdigit(c = getc(f))) n = n * 10 + (c -
'0');
166 n = 0;
while (isdigit(c = getc(f))) n = n * 10 + (c -
'0');
169 n = 0;
while (isdigit(c = getc(f))) n = n * 10 + (c -
'0');
172 n = 0;
while (isdigit(c = getc(f))) n = n * 10 + (c -
'0');
178 if (c !=
'.')
return t;
179 n = 0;
while (isdigit(c = getc(f))) n = n * 10 + (c -
'0');
180 while (n > 1000) n /= 10;
191 time_t t = time(NULL);
195 fprintf(f,
"[%4d/%2d/%2d %2d:%2d:%2d] ", tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
205#if defined(__unix__) || defined(__APPLE__)
207 ts.tv_sec = t / 1000;
208 ts.tv_nsec = (t % 1000) * 1000000;
209 nanosleep(&ts, NULL);
224 static const char *multiple =
"EPTGMk mµnpfa";
227 if (fabs(v) < 1e-24) {
230 u = floor(log10(fabs(v)) / 3.0);
231 if (u > 6) u = 6;
else if (u < -6) u = -6;
235 if (fabs(v) - floor(fabs(v)) < 0.01) sprintf(f,
" %5.1f %c%s", v, multiple[6 - u], unit);
236 else if (fabs(v + 0.05) < 10.0) sprintf(f,
" %5.3f %c%s", v, multiple[6 - u], unit);
237 else if (fabs(v + 0.5) < 100.0) sprintf(f,
" %5.2f %c%s", v, multiple[6 - u], unit);
238 else sprintf(f,
" %5.1f %c%s", v, multiple[6 - u], unit);
271 line = (
char*) malloc(n);
272 if (line == NULL)
fatal_error(
"Allocation error\n");
274 while ((c = getc(f)) != EOF && c !=
'\n') {
275 line[i++] = (char) c;
278 line = (
char*) realloc(line, n);
279 if (line == NULL)
fatal_error(
"Allocation error\n");
284 if (i == 0 && c == EOF) {
304 const int n = strlen(s) + 1;
305 d = (
char*) malloc(n);
306 if (d) memcpy(d, s, n);
341 if (*s ==
'.' && isdigit(s[1])) {
344 t = (t + n + x) * 1000;
389 if (tolower(s[0]) ==
'p' && s[1] ==
'@') s += 2;
390 else if (s[0] ==
'@') ++s;
392 if (tolower(s[0]) ==
'p' && (tolower(s[1]) ==
'a' || tolower(s[1]) ==
's')) x =
PASS;
393 else if (s[0] ==
'@' && s[1] ==
'@') x =
PASS;
395 int c = tolower(s[0]) -
'a';
397 if (0 <= c && c <= 7 && 0 <= r && r <= 7) x = r * 8 + c;
416 while (*s && isspace(*s++)) ;
418 while (*s && !isspace(*s++)) ;
434 if (strcmp(s,
"false") == 0
435 || strcmp(s,
"off") == 0
436 || strcmp(s,
"no") == 0
437 || strcmp(s,
"0") == 0)
return false;
439 if (strcmp(s,
"true") == 0
440 || strcmp(s,
"on") == 0
441 || strcmp(s,
"yes") == 0
442 || strcmp(s,
"1") == 0)
return true;
459 char *end = (
char*) s;
462 if (s) n = strtol(s, &end, 10);
471 }
else if (n > INT_MAX) {
490 char *end = (
char*) s;
493 if (s) x = strtod(s, &end);
517 while (*
string && isspace(*
string)) ++string;
519 return (
char*) string;
531 while (*
string && isspace(*
string)) ++string;
532 while (*
string && !isspace(*
string)) ++string;
533 while (*
string && isspace(*
string)) ++string;
535 return (
char*) string;
548 while (*
string && *
string != c) ++string;
550 return (
char*) string;
562char*
parse_word(
const char *
string,
char *word,
unsigned int n)
566 while (*
string && !isspace(*
string) && n--) *word++ = *
string++;
569 return (
char*) string;
582char*
parse_field(
const char *
string,
char *word,
unsigned int n,
char separator)
586 while (*
string && *
string != separator && n--) *word++ = *
string++;
587 if (*
string == separator) ++string;
590 return (
char*) string;
604char*
parse_line(
const char *
string,
char *line,
unsigned int n)
606 const char *s = string;
608 while (*s && *s !=
'\n' && *s !=
'\r' && n--) *line++ = *s++;
609 if (*s ==
'\0') s = string;
611 while (*s && *s !=
'\n' && *s !=
'\r' ) ++s;
612 while (*s ==
'\r' || *s ==
'\n') ++s;
646 return (
char*) string;
665 while ((next =
parse_move(
string, board, move)) !=
string || move->
x ==
PASS) {
671 return (
char *) string;
689 for (i =
A1; i <=
H8; ++i) {
690 if (*s ==
'\0')
return (
char*) string;
691 switch (tolower(*s)) {
705 if (!isspace(*s))
return (
char*) string;
714 switch (tolower(*s)) {
719 return (
char*) s + 1;
724 return (
char*) s + 1;
731 return (
char*) string;
749 if (errno != EINVAL) *result = r;
751 return (
char*) string;
768 if (s) n = strtol(s, &end, 10);
777 }
else if (n > INT_MAX) {
786 return (
char*) string;
803 if (s) d = strtod(s, &end);
814 return (
char*) string;
846 if (*s ==
'.' && isdigit(s[1])) {
848 assert(0.0 <= x && x < 1.0);
850 *t = (*t + n + x) * 1000;
851 if (errno != EINVAL && errno != ERANGE)
string = s;
854 return (
char*) string;
867char*
parse_command(
const char *
string,
char *cmd,
char *param,
const unsigned int size)
871 if (strcmp(cmd,
"set") == 0) {
879 return (
char*) string;
892 for (c = path; *c; ++c) ;
893 for (;c >= path && *c !=
'/'; --c) ;
895 while (path <= c) *dir++ = *path++;
909 while (*base) *file++ = *base++;
910 while (*ext) *file++ = *ext++;
924#if defined(__unix__) || (defined(_WIN32) && defined(USE_PTHREAD)) || defined(__APPLE__)
925 pthread_create(thread, NULL, function, data);
928 *thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) function, data, 0, &
id);
942#if defined(__unix__) || (defined(_WIN32) && defined(USE_PTHREAD)) || defined(__APPLE__)
943 pthread_join(thread, NULL);
945 WaitForSingleObject(thread, INFINITE);
956#if defined(__unix__) || (defined(_WIN32) && defined(USE_PTHREAD)) || defined(__APPLE__)
957 return pthread_self();
959 return GetCurrentThread();
969#if defined(__linux__) && !defined(ANDROID)
974 pthread_setaffinity_np(thread,
sizeof (cpu_set_t), &
cpu);
975#elif defined(_WIN32) && !defined(USE_PTHREAD)
976 SetThreadIdealProcessor(thread, i);
978 (void) thread; (void) i;
996 sprintf(file,
"/sys/devices/system/cpu/cpu%d", n);
997 f = fopen(file,
"r");
1004#elif defined(_SC_NPROCESSORS_ONLN)
1006 n = sysconf(_SC_NPROCESSORS_ONLN);
1008#elif defined(_WIN32)
1012 GetSystemInfo(&
info);
1013 n =
info.dwNumberOfProcessors;
1015#elif defined(__APPLE__)
1021 mib[1] = HW_AVAILCPU;
1022 sysctl(mib, 2, &n, &len, NULL, 0);
1025 sysctl( mib, 2, &n, &len, NULL, 0 );
1045 const unsigned long long MASK48 = 0xFFFFFFFFFFFFull;
1046 const unsigned long long A = 0x5DEECE66Dull;
1047 const unsigned long long B = 0xBull;
1048 register unsigned long long r;
1050 random->
x = ((A * random->
x + B) & MASK48);
1051 r = random->
x >> 16;
1052 random->
x = ((A * random->
x + B) & MASK48);
1053 return (r << 32) | (random->
x >> 16);
1064 const unsigned long long MASK48 = 0xFFFFFFFFFFFFull;
1065 random->
x = (seed & MASK48);
#define x_to_bit(x)
Definition bit.h:43
void board_update(Board *board, const Move *move)
Update a board.
Definition board.c:469
void board_swap_players(Board *board)
Swap players.
Definition board.c:137
void board_init(Board *board)
Set a board to the starting position.
Definition board.c:280
bool board_is_pass(const Board *board)
Check if current player should pass.
Definition board.c:1191
void board_check(const Board *board)
Check board consistency.
Definition board.c:291
bool board_is_occupied(const Board *board, const int x)
Check if a square is occupied.
Definition board.c:1180
unsigned long long(* flip[BOARD_SIZE+2])(const unsigned long long, const unsigned long long)
Definition flip_bitscan.c:1912
@ PASS
Definition const.h:37
@ NOMOVE
Definition const.h:37
@ A1
Definition const.h:29
@ H8
Definition const.h:36
@ WHITE
Definition const.h:43
@ BLACK
Definition const.h:42
#define MAX_THREADS
Definition const.h:15
void line_push(Line *line, const int x)
Add a move to the sequence.
Definition move.c:561
const Move MOVE_INIT
Definition move.c:25
unsigned long long player
Definition board.h:27
unsigned long long opponent
Definition board.h:27
unsigned long long flipped
Definition move.h:21
int x
Definition move.h:22
unsigned long long x
Definition util.h:88
void time_print(long long t, bool justified, FILE *f)
Print time as "D:HH:MM:SS.CC".
Definition util.c:131
char * parse_move(const char *string, const Board *board, Move *move)
Parse a move.
Definition util.c:627
long long time_read(FILE *f)
read time as "D:HH:MM:SS.C".
Definition util.c:156
void time_stamp(FILE *f)
Print local time.
Definition util.c:189
void string_to_uppercase(char *s)
Change all char of a string to uppercase.
Definition util.c:369
char * string_duplicate(const char *s)
Duplicate a string.
Definition util.c:299
char * file_add_ext(const char *base, const char *ext, char *file)
Add an extension to a string.
Definition util.c:907
void print_scientific(double v, const char *unit, FILE *f)
Print a value with a unit.
Definition util.c:250
bool string_to_boolean(const char *s)
Convert a string into a boolean.
Definition util.c:432
char * parse_int(const char *string, int *result)
Parse an integer.
Definition util.c:761
void thread_create2(Thread *thread, void *(*function)(void *), void *data)
Create a thread.
Definition util.c:922
char * parse_skip_word(const char *string)
Skip word.
Definition util.c:528
void thread_join(Thread thread)
Join a thread.
Definition util.c:940
long long(* time_clock)(void)
Time clock.
Definition util.c:122
int string_to_coordinate(const char *s)
Convert the two first chars of a string into a coordinate.
Definition util.c:384
void string_to_lowercase(char *s)
Change all char of a string to lowercase.
Definition util.c:355
char * parse_field(const char *string, char *word, unsigned int n, char separator)
Parse a field.
Definition util.c:582
char * parse_time(const char *string, long long *t)
parse time as "D:HH:MM:SS.C".
Definition util.c:824
char * parse_real(const char *string, double *result)
Parse a real number (as a double floating point).
Definition util.c:796
char * string_read_line(FILE *f)
Read a line.
Definition util.c:265
void thread_set_cpu(Thread thread, int i)
Choose a single core or cpu to run on, under linux systems, to avoid context changes.
Definition util.c:967
int string_to_int(const char *s, const int default_value)
Convert a string into an integer.
Definition util.c:457
char * parse_command(const char *string, char *cmd, char *param, const unsigned int size)
Parse a command.
Definition util.c:867
int get_cpu_number(void)
Get the number of cpus or cores on the machine.
Definition util.c:987
char * parse_skip_spaces(const char *string)
Skip spaces.
Definition util.c:514
unsigned long long random_get(Random *random)
Pseudo-random number generator.
Definition util.c:1043
double string_to_real(const char *s, const double default_value)
Convert a string into a real number.
Definition util.c:488
void random_seed(Random *random, const unsigned long long seed)
Pseudo-random number seed.
Definition util.c:1062
char * parse_word(const char *string, char *word, unsigned int n)
Parse a word.
Definition util.c:562
char * parse_line(const char *string, char *line, unsigned int n)
Parse a line.
Definition util.c:604
char * format_scientific(double v, const char *unit, char *f)
Format a value with a unit.
Definition util.c:222
char * string_to_word(char *s)
remove spaces from a string.
Definition util.c:411
void relax(int t)
sleep for t ms.
Definition util.c:203
long long string_to_time(const char *string)
Read time as "D:HH:MM:SS.C".
Definition util.c:320
void path_get_dir(const char *path, char *dir)
Extract the directory of a file path.
Definition util.c:888
char * parse_boolean(const char *string, bool *result)
Parse a boolean.
Definition util.c:741
char * parse_find(const char *string, const int c)
Find a char.
Definition util.c:545
char * parse_game(const char *string, const Board *board_init, Line *line)
Parse a sequence of moves.
Definition util.c:657
Thread thread_self(void)
Current thread.
Definition util.c:954
char * parse_board(const char *string, Board *board, int *player)
Parse a board.
Definition util.c:682
Miscellaneous utilities header.
long long real_clock(void)
long long cpu_clock(void)
#define fatal_error(...)
Display an error message as "FATAL_ERROR : file name : function name : line number : ....
Definition util.h:349
#define info(...)
Display a message.
Definition util.h:382