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
     1.1 --- a/conf.mk	Wed Apr 09 18:11:51 2014 +0200
     1.2 +++ b/conf.mk	Thu Apr 10 11:44:55 2014 +0200
     1.3 @@ -35,7 +35,7 @@
     1.4  CFLAGS     = -O2 -std=gnu99
     1.5  CFLAGS_D   = -g -std=gnu99 -Wall -pedantic
     1.6  LD         = gcc
     1.7 -LDFLAGS    = -lncurses -lrt
     1.8 +LDFLAGS    = -lncurses
     1.9  ARFLAGS    = -r
    1.10  MKDIRFLAGS = -p
    1.11  RMFLAGS    = -f -R
     2.1 --- a/src/chess/rules.c	Wed Apr 09 18:11:51 2014 +0200
     2.2 +++ b/src/chess/rules.c	Thu Apr 10 11:44:55 2014 +0200
     2.3 @@ -31,6 +31,7 @@
     2.4  #include "chess.h"
     2.5  #include <string.h>
     2.6  #include <stdlib.h>
     2.7 +#include <sys/time.h>
     2.8  
     2.9  void gamestate_cleanup(GameState *gamestate) {
    2.10      MoveList *elem;
    2.11 @@ -47,26 +48,26 @@
    2.12      elem->next = NULL;
    2.13      elem->move = *move;
    2.14      
    2.15 -    clock_gettime(CLOCK_REALTIME, &(elem->move.timestamp));
    2.16 +    gettimeofday(&(elem->move.timestamp), NULL);
    2.17      
    2.18      if (gamestate->lastmove) {
    2.19 -        struct timespec *lasttstamp = &(gamestate->lastmove->move.timestamp);
    2.20 +        struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp);
    2.21          time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec;
    2.22 -        long int nanos;
    2.23 -        if (elem->move.timestamp.tv_nsec < lasttstamp->tv_nsec) {
    2.24 -            nanos = 1e9L-(lasttstamp->tv_nsec - elem->move.timestamp.tv_nsec);
    2.25 +        suseconds_t micros;
    2.26 +        if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) {
    2.27 +            micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec);
    2.28              sec--;
    2.29          } else {
    2.30 -            nanos = elem->move.timestamp.tv_nsec - lasttstamp->tv_nsec;
    2.31 +            micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec;
    2.32          }
    2.33          
    2.34          elem->move.movetime.tv_sec = sec;
    2.35 -        elem->move.movetime.tv_nsec = nanos;
    2.36 +        elem->move.movetime.tv_usec = micros;
    2.37          
    2.38          gamestate->lastmove->next = elem;
    2.39          gamestate->lastmove = elem;
    2.40      } else {
    2.41 -        elem->move.movetime.tv_nsec = 0;
    2.42 +        elem->move.movetime.tv_usec = 0;
    2.43          elem->move.movetime.tv_sec = 0;
    2.44          gamestate->movelist = gamestate->lastmove = elem;
    2.45      }
    2.46 @@ -550,7 +551,7 @@
    2.47      
    2.48      if (gamestate->movelist) {
    2.49          uint16_t time = gameinfo->time;
    2.50 -        long int nanos = 0;
    2.51 +        suseconds_t micros = 0;
    2.52          
    2.53          MoveList *movelist = color == WHITE ?
    2.54              gamestate->movelist : gamestate->movelist->next;
    2.55 @@ -558,13 +559,13 @@
    2.56          while (movelist) {
    2.57              time += gameinfo->addtime;
    2.58              
    2.59 -            struct timespec *movetime = &(movelist->move.movetime);
    2.60 +            struct timeval *movetime = &(movelist->move.movetime);
    2.61              if (movetime->tv_sec >= time) {
    2.62                  return 0;
    2.63              }
    2.64              
    2.65              time -= movetime->tv_sec;
    2.66 -            nanos += movetime->tv_nsec;
    2.67 +            micros += movetime->tv_usec;
    2.68              
    2.69              movelist = movelist->next ? movelist->next->next : NULL;
    2.70          }
    2.71 @@ -572,10 +573,10 @@
    2.72          time_t sec;
    2.73          movelist = gamestate->lastmove;
    2.74          if ((movelist->move.piece & COLOR_MASK) != color) {
    2.75 -            struct timespec *lastmovetstamp = &(movelist->move.timestamp);
    2.76 -            struct timespec currenttstamp;
    2.77 -            clock_gettime(CLOCK_REALTIME, &currenttstamp);
    2.78 -            nanos += currenttstamp.tv_nsec - lastmovetstamp->tv_nsec;
    2.79 +            struct timeval *lastmovetstamp = &(movelist->move.timestamp);
    2.80 +            struct timeval currenttstamp;
    2.81 +            gettimeofday(&currenttstamp, NULL);
    2.82 +            micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec;
    2.83              sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec;
    2.84              if (sec >= time) {
    2.85                  return 0;
    2.86 @@ -584,7 +585,7 @@
    2.87              time -= sec;
    2.88          }
    2.89          
    2.90 -        sec = nanos / 1e9L;
    2.91 +        sec = micros / 1e6L;
    2.92          
    2.93          if (sec >= time) {
    2.94              return 0;
     3.1 --- a/src/chess/rules.h	Wed Apr 09 18:11:51 2014 +0200
     3.2 +++ b/src/chess/rules.h	Thu Apr 10 11:44:55 2014 +0200
     3.3 @@ -31,7 +31,7 @@
     3.4  #define	RULES_H
     3.5  
     3.6  #include <stdint.h>
     3.7 -#include <time.h>
     3.8 +#include <sys/time.h>
     3.9  
    3.10  #define VALID_MOVE_SYNTAX   0
    3.11  #define INVALID_MOVE_SYNTAX 1
    3.12 @@ -69,7 +69,6 @@
    3.13  
    3.14  typedef uint8_t Board[8][8];
    3.15  
    3.16 -
    3.17  typedef struct {
    3.18      uint8_t piece;
    3.19      uint8_t fromfile;
    3.20 @@ -77,8 +76,8 @@
    3.21      uint8_t tofile;
    3.22      uint8_t torow;
    3.23      uint8_t promotion;
    3.24 -    struct timespec timestamp;
    3.25 -    struct timespec movetime;
    3.26 +    struct timeval timestamp;
    3.27 +    struct timeval movetime;
    3.28      _Bool check;
    3.29      _Bool capture;
    3.30  } Move;

mercurial