ref: 8e6bb43568906262359ace3692983d8587641275
parent: ffac9231da5c1b585b6d4947918d8e20ce841f0a
author: zamfofex <zamfofex@twdb.moe>
date: Tue Dec 31 08:59:04 EST 2024
make dependency on 'clock_gettime' optional
--- a/README.md
+++ b/README.md
@@ -188,9 +188,16 @@
porting moonfish
---
-The only piece of functionality the moonfish depends on that is not specified entirely in C89 is `clock_gettime`.
+Porting moonfish to a different platform should be a matter of simply providing a “mostly C89‐compliant”. Of course, moonfish doesn’t make use of *all* C89 features, so it is not necessary to have features that it doesn’t use. [Compiling on 9front](#compiling-on-9front), for example, works through NPE, which provides something close enough to C89 for moonfish to work.
-Porting moonfish to a different platform should be a matter of simply providing a “mostly C89‐compliant” environment alongside `clock_gettime`. Of course, moonfish doesn’t make use of *all* C89 features, so it is not necessary to have features that it doesn’t use. [Compiling on 9front](#compiling-on-9front), for example, works through NPE, which provides something close enough to C89 for moonfish to work.
+The only pieces of functionality that moonfish optionally depends on that is not specified entirely in C89 are `clock_gettime` and threads. For threads, either pthreads or C11 `<threads.h>` may be used. For `clock_gettime`, it is possible to use `time` instead. These can be configured with the following compile-time macros.
+
+- default (without explicit options): use `clock_gettime` and C11 `<threads.h>`
+- `-Dmoonfish_pthreads`: use pthreads instead of C11 `<threads.h>`
+- `-Dmoonfish_no_threads`: disable threads altogether
+- `-Dmoonfish_no_clock`: use `time` instead of `clock_gettime` (note: this is highly discouraged because 1 second time granularity is very detrimental in fast games, besides unfortunate reliance on wall-clock time.)
+
+Thus, moonfish can be compiled within a strict C89 implementation by setting the `moonfish_no_threads` and `moonfish_no_clock` macros during compile time.
license
---
--- a/search.c
+++ b/search.c
@@ -18,7 +18,7 @@
#ifdef _WIN32
-static long int moonfish_clock(void)
+static unsigned long int moonfish_clock(void)
{return GetTickCount();
}
@@ -25,20 +25,34 @@
#else
-static long int moonfish_clock(void)
+#ifdef moonfish_no_clock
+
+static unsigned long int moonfish_clock(void)
{+ time_t t;
+ if (time(&t) < 0) {+ perror("time");+ exit(1);
+ }
+ return t * 1000;
+}
+
+#else
+
+static unsigned long int moonfish_clock(void)
+{struct timespec ts;
-
if (clock_gettime(CLOCK_MONOTONIC, &ts)) { perror("clock_gettime");exit(1);
}
-
return ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
}
#endif
+#endif
+
struct moonfish_node {struct moonfish_node *parent;
struct moonfish_node *children;
@@ -58,7 +72,7 @@
struct moonfish_data {struct moonfish_root *root;
- long int time, time0;
+ unsigned long int time, time0;
long int node_count;
};
@@ -346,9 +360,10 @@
#endif
time = LONG_MAX;
+ if (options->our_time >= 0) time = options->our_time / 16;
if (options->max_time >= 0 && time > options->max_time) time = options->max_time;
- if (options->our_time >= 0 && time > options->our_time / 16) time = options->our_time / 16;
time -= time / 32 + 125;
+ if (time < 0) time = 0;
data.root = root;
data.time = time;
--
⑨