reduced awesome great nanosecond precision so we can compile on OS X

Thu, 10 Apr 2014 11:44:55 +0200

author
Mike Becker <universe@uap-core.de>
date
Thu, 10 Apr 2014 11:44:55 +0200
changeset 36
ebe0c961e9a6
parent 35
6c64b7a073af
child 37
bcf624518909

reduced awesome great nanosecond precision so we can compile on OS X

conf.mk file | annotate | diff | comparison | revisions
src/chess/rules.c file | annotate | diff | comparison | revisions
src/chess/rules.h file | annotate | diff | comparison | revisions
--- a/conf.mk	Wed Apr 09 18:11:51 2014 +0200
+++ b/conf.mk	Thu Apr 10 11:44:55 2014 +0200
@@ -35,7 +35,7 @@
 CFLAGS     = -O2 -std=gnu99
 CFLAGS_D   = -g -std=gnu99 -Wall -pedantic
 LD         = gcc
-LDFLAGS    = -lncurses -lrt
+LDFLAGS    = -lncurses
 ARFLAGS    = -r
 MKDIRFLAGS = -p
 RMFLAGS    = -f -R
--- a/src/chess/rules.c	Wed Apr 09 18:11:51 2014 +0200
+++ b/src/chess/rules.c	Thu Apr 10 11:44:55 2014 +0200
@@ -31,6 +31,7 @@
 #include "chess.h"
 #include <string.h>
 #include <stdlib.h>
+#include <sys/time.h>
 
 void gamestate_cleanup(GameState *gamestate) {
     MoveList *elem;
@@ -47,26 +48,26 @@
     elem->next = NULL;
     elem->move = *move;
     
-    clock_gettime(CLOCK_REALTIME, &(elem->move.timestamp));
+    gettimeofday(&(elem->move.timestamp), NULL);
     
     if (gamestate->lastmove) {
-        struct timespec *lasttstamp = &(gamestate->lastmove->move.timestamp);
+        struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
         time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec;
-        long int nanos;
-        if (elem->move.timestamp.tv_nsec < lasttstamp->tv_nsec) {
-            nanos = 1e9L-(lasttstamp->tv_nsec - elem->move.timestamp.tv_nsec);
+        suseconds_t micros;
+        if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) {
+            micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec);
             sec--;
         } else {
-            nanos = elem->move.timestamp.tv_nsec - lasttstamp->tv_nsec;
+            micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec;
         }
         
         elem->move.movetime.tv_sec = sec;
-        elem->move.movetime.tv_nsec = nanos;
+        elem->move.movetime.tv_usec = micros;
         
         gamestate->lastmove->next = elem;
         gamestate->lastmove = elem;
     } else {
-        elem->move.movetime.tv_nsec = 0;
+        elem->move.movetime.tv_usec = 0;
         elem->move.movetime.tv_sec = 0;
         gamestate->movelist = gamestate->lastmove = elem;
     }
@@ -550,7 +551,7 @@
     
     if (gamestate->movelist) {
         uint16_t time = gameinfo->time;
-        long int nanos = 0;
+        suseconds_t micros = 0;
         
         MoveList *movelist = color == WHITE ?
             gamestate->movelist : gamestate->movelist->next;
@@ -558,13 +559,13 @@
         while (movelist) {
             time += gameinfo->addtime;
             
-            struct timespec *movetime = &(movelist->move.movetime);
+            struct timeval *movetime = &(movelist->move.movetime);
             if (movetime->tv_sec >= time) {
                 return 0;
             }
             
             time -= movetime->tv_sec;
-            nanos += movetime->tv_nsec;
+            micros += movetime->tv_usec;
             
             movelist = movelist->next ? movelist->next->next : NULL;
         }
@@ -572,10 +573,10 @@
         time_t sec;
         movelist = gamestate->lastmove;
         if ((movelist->move.piece & COLOR_MASK) != color) {
-            struct timespec *lastmovetstamp = &(movelist->move.timestamp);
-            struct timespec currenttstamp;
-            clock_gettime(CLOCK_REALTIME, &currenttstamp);
-            nanos += currenttstamp.tv_nsec - lastmovetstamp->tv_nsec;
+            struct timeval *lastmovetstamp = &(movelist->move.timestamp);
+            struct timeval currenttstamp;
+            gettimeofday(&currenttstamp, NULL);
+            micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
             sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
             if (sec >= time) {
                 return 0;
@@ -584,7 +585,7 @@
             time -= sec;
         }
         
-        sec = nanos / 1e9L;
+        sec = micros / 1e6L;
         
         if (sec >= time) {
             return 0;
--- a/src/chess/rules.h	Wed Apr 09 18:11:51 2014 +0200
+++ b/src/chess/rules.h	Thu Apr 10 11:44:55 2014 +0200
@@ -31,7 +31,7 @@
 #define	RULES_H
 
 #include <stdint.h>
-#include <time.h>
+#include <sys/time.h>
 
 #define VALID_MOVE_SYNTAX   0
 #define INVALID_MOVE_SYNTAX 1
@@ -69,7 +69,6 @@
 
 typedef uint8_t Board[8][8];
 
-
 typedef struct {
     uint8_t piece;
     uint8_t fromfile;
@@ -77,8 +76,8 @@
     uint8_t tofile;
     uint8_t torow;
     uint8_t promotion;
-    struct timespec timestamp;
-    struct timespec movetime;
+    struct timeval timestamp;
+    struct timeval movetime;
     _Bool check;
     _Bool capture;
 } Move;

mercurial