1 /*--------------------------------------------------------------------------
2  Portable clock functions.
3 --------------------------------------------------------------------------*/
4 #ifndef eclock_h
5 #define eclock_h
6 
7 //#include "dprint.h"
8 #include <time.h>
9 #include <sys/time.h>
10 #include <unistd.h>
11 
12 /*--------------------------------------------------------------------------
13  Get current time, in clock ticks.
14  Return type is int.
15 --------------------------------------------------------------------------*/
16 #define eclock_HERTZ 10000
17 #define eclock_TICKSPERUSEC 100
18 
19 extern struct timeval eclock_timeval;
20 #define eclock() (gettimeofday(&eclock_timeval, NULL), \
21 	((int)((eclock_timeval.tv_sec * eclock_HERTZ) + (eclock_timeval.tv_usec / eclock_TICKSPERUSEC))))
22 
23 /*--------------------------------------------------------------------------
24  Get number of clock ticks per second, in hertz.  This evaluates to a
25  system call, so call it just once at the start of the program.
26  Return type is int.
27 --------------------------------------------------------------------------*/
28 #define eclock_hertz() eclock_HERTZ
29 
30 /*--------------------------------------------------------------------------
31  Safe way to compare to eclock() values to see which is after the other.
32  Assumes the largest interval to be compared is one half the maximum
33  representable in a long.
34 
35  Returns true if time a is after time b.
36 --------------------------------------------------------------------------*/
37 //#define eclock_after(a, b) (((long)((a)-(b))) > 0)
eclock_after(clock_t a,clock_t b)38 inline bool eclock_after(clock_t a, clock_t b)
39 {
40 	long diff = (long) (a - b);
41 	//DPRINT(("eclock_after(%d, %d): diff %d\n", a, b, diff));
42 	return (diff > 0);
43 }
44 
45 /*--------------------------------------------------------------------------
46  Safe way to compare to eclock() values to see which is after the other.
47  Assumes the largest interval to be compared is one half the maximum
48  representable in a long.
49 
50  Returns true if time a is before time b.
51 --------------------------------------------------------------------------*/
52 //#define eclock_before(a, b) (((long)((a)-(b))) < 0)
eclock_before(clock_t a,clock_t b)53 inline bool eclock_before(clock_t a, clock_t b)
54 {
55 	long diff = (long) (a - b);
56 	//DPRINT(("eclock_before(%d, %d): diff %d\n", a, b, diff));
57 	return (diff < 0);
58 }
59 
60 #endif
61