shithub: moonfish

Download patch

ref: 9f1006a64b0b1617aa360837c28fdba9077e1852
parent: 89075ea71723a00674b3c74cb42b1a3e7213544a
author: zamfofex <zamfofex@twdb.moe>
date: Wed Nov 15 18:06:58 EST 2023

replace ‘clock()’ with ‘clock_gettime()’ (note: POSIX)

--- a/search.c
+++ b/search.c
@@ -209,36 +209,37 @@
 
 #endif
 
-static clock_t moonfish_clock(struct moonfish *ctx)
+static void moonfish_clock(struct moonfish *ctx, struct timespec *ts)
 {
-	clock_t t;
-	t = clock();
-	if (t < 0)
+	if (clock_gettime(CLOCK_MONOTONIC, ts))
 	{
-		fprintf(stderr, "%s: unknown clock\n", ctx->argv0);
+		perror(ctx->argv0);
 		exit(1);
 	}
-	return t;
 }
 
 int moonfish_best_move(struct moonfish *ctx, struct moonfish_move *best_move, long int our_time, long int their_time)
 {
-	clock_t t, d;
+	long int d, t;
 	int i;
 	int score;
+	struct timespec t0, t1;
 	
-	our_time *= CLOCKS_PER_SEC;
-	their_time *= CLOCKS_PER_SEC;
-	
 	d = our_time - their_time;
 	if (d < 0) d = 0;
 	d += our_time / 16;
-	d /= 1000;
 	
 	i = 3;
-	t = moonfish_clock(ctx);
+	
+	moonfish_clock(ctx, &t0);
 	score = moonfish_best_move_depth(ctx, best_move, i);
-	t = moonfish_clock(ctx) - t + CLOCKS_PER_SEC / 4;
+	moonfish_clock(ctx, &t1);
+	
+	t = 1000;
+	t += t1.tv_sec * 1000;
+	t -= t0.tv_sec * 1000;
+	t += t1.tv_nsec / 1000000;
+	t -= t0.tv_nsec / 1000000;
 	
 	for (;;)
 	{
--