1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#ifndef HIGH_RES_TIME_FUNCTIONS_H
#define HIGH_RES_TIME_FUNCTIONS_H
#include <ctime>
#include <sys/time.h>
#include <cmath>
/* Requires the librt and libm libraries */
static const long NSEC_PER_SEC = 1000000000L;
static inline bool timespec_greater(const struct timespec* t1, const struct timespec* t0){
return ((t1->tv_sec > t0->tv_sec) || ((t1->tv_sec == t0->tv_sec) && (t1->tv_nsec > t0->tv_nsec)));
}
static inline bool timespec_greater(const struct timespec t1, const struct timespec t0){
return ((t1.tv_sec > t0.tv_sec) || ((t1.tv_sec == t0.tv_sec) && (t1.tv_nsec > t0.tv_nsec)));
}
static inline bool timespec_less(const struct timespec* t1, const struct timespec* t0){
return ((t1->tv_sec < t0->tv_sec) || ((t1->tv_sec == t0->tv_sec) && (t1->tv_nsec < t0->tv_nsec)));
}
static inline bool timespec_less(const struct timespec t1, const struct timespec t0){
return ((t1.tv_sec < t0.tv_sec) || ((t1.tv_sec == t0.tv_sec) && (t1.tv_nsec < t0.tv_nsec)));
}
static inline bool timespec_equal(const struct timespec* t1, const struct timespec* t0){
return ((t1->tv_sec == t0->tv_sec) && (t1->tv_nsec == t0->tv_nsec));
}
static inline bool timespec_equal(const struct timespec t1, const struct timespec t0){
return ((t1.tv_sec == t0.tv_sec) && (t1.tv_nsec == t0.tv_nsec));
}
static inline void timespec_reset(struct timespec* ret){
ret->tv_sec = 0;
ret->tv_nsec = 0;
}
static inline void set_normalized_timespec(struct timespec *ts, time_t sec, long nsec){
while (nsec > NSEC_PER_SEC){
nsec -= NSEC_PER_SEC;
++sec;
}
while(nsec < 0){
nsec += NSEC_PER_SEC;
--sec;
}
ts->tv_sec = sec;
ts->tv_nsec = nsec;
}
static inline struct timespec convert_to_timespec(const double timeValue){
struct timespec ret;
double seconds = 0;
long nsec = static_cast<long>(modf(timeValue, &seconds) * static_cast<double>(NSEC_PER_SEC));
time_t sec = static_cast<time_t>(seconds);
set_normalized_timespec(&ret, sec, nsec);
return ret;
}
static inline double convert_from_timespec(const timespec actual){
return (static_cast<double>(actual.tv_sec) + (static_cast<double>(actual.tv_nsec) / static_cast<double>(NSEC_PER_SEC)));
}
static inline void timespec_add(struct timespec *ret, const struct timespec* t1, const struct timespec* t0){
time_t sec = t1->tv_sec + t0->tv_sec;
long nsec = t1->tv_nsec + t0->tv_nsec;
set_normalized_timespec(ret, sec, nsec);
}
static inline void timespec_add(struct timespec *ret, const struct timespec t1, const struct timespec t0){
return timespec_add(ret, &t1, &t0);
}
static inline struct timespec timespec_add(const struct timespec t1, const struct timespec t0){
struct timespec ret;
timespec_add(&ret, &t1, &t0);
return ret;
}
static inline struct timespec timespec_add(const struct timespec t1, const double time0){
struct timespec ret;
struct timespec t0;
t0 = convert_to_timespec(time0);
timespec_add(&ret, &t1, &t0);
return ret;
}
static inline void timespec_subtract(struct timespec *ret, const struct timespec* t1, const struct timespec* t0){
time_t sec = t1->tv_sec - t0->tv_sec;
long nsec = t1->tv_nsec - t0->tv_nsec;
set_normalized_timespec(ret, sec, nsec);
}
static inline void timespec_subtract(struct timespec *ret, const struct timespec t1, const struct timespec t0){
return timespec_subtract(ret, &t1, &t0);
}
static inline struct timespec timespec_subtract(const struct timespec t1, const struct timespec t0){
struct timespec ret;
timespec_subtract(&ret, &t1, &t0);
return ret;
}
static inline struct timespec timespec_subtract(const struct timespec t1, const double time0){
struct timespec ret;
struct timespec t0;
t0 = convert_to_timespec(time0);
timespec_subtract(&ret, &t1, &t0);
return ret;
}
static inline double diff_timespec(struct timespec* ret, const struct timespec *t1, const struct timespec* t0){
struct timespec actual;
time_t sec = 0;
long nsec = 0;
if(timespec_greater(t1, t0)){
sec = t1->tv_sec - t0->tv_sec;
nsec = t1->tv_nsec - t0->tv_nsec;
set_normalized_timespec(&actual, sec, nsec);
if(ret != NULL){
ret->tv_sec = actual.tv_sec;
ret->tv_nsec = actual.tv_nsec;
}
return convert_from_timespec(actual);
}
else{
sec = t0->tv_sec - t1->tv_sec;
nsec = t0->tv_nsec - t1->tv_nsec;
// Do nothing with the ret value as the ret value would have to store a negative, which it can't.
set_normalized_timespec(&actual, sec, nsec);
return (-convert_from_timespec(actual));
}
}
static inline double diff_timespec(struct timespec* ret, const struct timespec t1, const struct timespec t0){
return diff_timespec(ret, &t1, &t0);
}
static inline double diff_timespec(const struct timespec t1, const struct timespec t0){
return diff_timespec(NULL, &t1, &t0);
}
static inline double diff_timespec(const struct timespec* t1, const struct timespec* t0){
return diff_timespec(NULL, t1, t0);
}
static inline void get_highres_clock(struct timespec* ret){
if(clock_gettime(CLOCK_REALTIME, ret) != 0){
// Unable to get high resolution time - fail over to low resolution time
timeval lowResTime;
gettimeofday(&lowResTime, NULL);
ret->tv_sec = lowResTime.tv_sec;
ret->tv_nsec = lowResTime.tv_usec*1000;
}
}
static inline struct timespec get_highres_clock(){
struct timespec ret;
get_highres_clock(&ret);
return ret;
}
static inline bool timespec_empty(const struct timespec* ret){
return ( (ret->tv_sec == 0 ) && (ret->tv_nsec == 0) );
}
static inline bool timespec_empty(const struct timespec ret){
return timespec_empty(&ret);
}
#endif /* HIGH_RES_TIME_FUNCTIONS_H */
|