1 /*
2 * Copyright © 2014 - 2015 Collabora, Ltd.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial
14 * portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 #ifndef TIMESPEC_UTIL_H
27 #define TIMESPEC_UTIL_H
28
29 #include <stdint.h>
30 #include <assert.h>
31 #include <time.h>
32 #include <stdbool.h>
33
34 #define NSEC_PER_SEC 1000000000
35
36 /* Subtract timespecs
37 *
38 * \param r[out] result: a - b
39 * \param a[in] operand
40 * \param b[in] operand
41 */
42 static inline void
timespec_sub(struct timespec * r,const struct timespec * a,const struct timespec * b)43 timespec_sub(struct timespec *r,
44 const struct timespec *a, const struct timespec *b)
45 {
46 r->tv_sec = a->tv_sec - b->tv_sec;
47 r->tv_nsec = a->tv_nsec - b->tv_nsec;
48 if (r->tv_nsec < 0) {
49 r->tv_sec--;
50 r->tv_nsec += NSEC_PER_SEC;
51 }
52 }
53
54 /* Add a nanosecond value to a timespec
55 *
56 * \param r[out] result: a + b
57 * \param a[in] base operand as timespec
58 * \param b[in] operand in nanoseconds
59 */
60 static inline void
timespec_add_nsec(struct timespec * r,const struct timespec * a,int64_t b)61 timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
62 {
63 r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
64 r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);
65
66 if (r->tv_nsec >= NSEC_PER_SEC) {
67 r->tv_sec++;
68 r->tv_nsec -= NSEC_PER_SEC;
69 } else if (r->tv_nsec < 0) {
70 r->tv_sec--;
71 r->tv_nsec += NSEC_PER_SEC;
72 }
73 }
74
75 /* Add a millisecond value to a timespec
76 *
77 * \param r[out] result: a + b
78 * \param a[in] base operand as timespec
79 * \param b[in] operand in milliseconds
80 */
81 static inline void
timespec_add_msec(struct timespec * r,const struct timespec * a,int64_t b)82 timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
83 {
84 timespec_add_nsec(r, a, b * 1000000);
85 }
86
87 /* Convert timespec to nanoseconds
88 *
89 * \param a timespec
90 * \return nanoseconds
91 */
92 static inline int64_t
timespec_to_nsec(const struct timespec * a)93 timespec_to_nsec(const struct timespec *a)
94 {
95 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
96 }
97
98 /* Subtract timespecs and return result in nanoseconds
99 *
100 * \param a[in] operand
101 * \param b[in] operand
102 * \return to_nanoseconds(a - b)
103 */
104 static inline int64_t
timespec_sub_to_nsec(const struct timespec * a,const struct timespec * b)105 timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b)
106 {
107 struct timespec r;
108 timespec_sub(&r, a, b);
109 return timespec_to_nsec(&r);
110 }
111
112 /* Convert timespec to milliseconds
113 *
114 * \param a timespec
115 * \return milliseconds
116 *
117 * Rounding to integer milliseconds happens always down (floor()).
118 */
119 static inline int64_t
timespec_to_msec(const struct timespec * a)120 timespec_to_msec(const struct timespec *a)
121 {
122 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
123 }
124
125 /* Subtract timespecs and return result in milliseconds
126 *
127 * \param a[in] operand
128 * \param b[in] operand
129 * \return to_milliseconds(a - b)
130 */
131 static inline int64_t
timespec_sub_to_msec(const struct timespec * a,const struct timespec * b)132 timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
133 {
134 return timespec_sub_to_nsec(a, b) / 1000000;
135 }
136
137 /* Convert timespec to microseconds
138 *
139 * \param a timespec
140 * \return microseconds
141 *
142 * Rounding to integer microseconds happens always down (floor()).
143 */
144 static inline int64_t
timespec_to_usec(const struct timespec * a)145 timespec_to_usec(const struct timespec *a)
146 {
147 return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000;
148 }
149
150 /* Convert timespec to protocol data
151 *
152 * \param a timespec
153 * \param tv_sec_hi[out] the high bytes of the seconds part
154 * \param tv_sec_lo[out] the low bytes of the seconds part
155 * \param tv_nsec[out] the nanoseconds part
156 *
157 * The input timespec must be normalized (the nanoseconds part should
158 * be less than 1 second) and non-negative.
159 */
160 static inline void
timespec_to_proto(const struct timespec * a,uint32_t * tv_sec_hi,uint32_t * tv_sec_lo,uint32_t * tv_nsec)161 timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi,
162 uint32_t *tv_sec_lo, uint32_t *tv_nsec)
163 {
164 assert(a->tv_sec >= 0);
165 assert(a->tv_nsec >= 0 && a->tv_nsec < NSEC_PER_SEC);
166
167 uint64_t sec64 = a->tv_sec;
168
169 *tv_sec_hi = sec64 >> 32;
170 *tv_sec_lo = sec64 & 0xffffffff;
171 *tv_nsec = a->tv_nsec;
172 }
173
174 /* Convert nanoseconds to timespec
175 *
176 * \param a timespec
177 * \param b nanoseconds
178 */
179 static inline void
timespec_from_nsec(struct timespec * a,int64_t b)180 timespec_from_nsec(struct timespec *a, int64_t b)
181 {
182 a->tv_sec = b / NSEC_PER_SEC;
183 a->tv_nsec = b % NSEC_PER_SEC;
184 }
185
186 /* Convert microseconds to timespec
187 *
188 * \param a timespec
189 * \param b microseconds
190 */
191 static inline void
timespec_from_usec(struct timespec * a,int64_t b)192 timespec_from_usec(struct timespec *a, int64_t b)
193 {
194 timespec_from_nsec(a, b * 1000);
195 }
196
197 /* Convert milliseconds to timespec
198 *
199 * \param a timespec
200 * \param b milliseconds
201 */
202 static inline void
timespec_from_msec(struct timespec * a,int64_t b)203 timespec_from_msec(struct timespec *a, int64_t b)
204 {
205 timespec_from_nsec(a, b * 1000000);
206 }
207
208 /* Convert protocol data to timespec
209 *
210 * \param a[out] timespec
211 * \param tv_sec_hi the high bytes of seconds part
212 * \param tv_sec_lo the low bytes of seconds part
213 * \param tv_nsec the nanoseconds part
214 */
215 static inline void
timespec_from_proto(struct timespec * a,uint32_t tv_sec_hi,uint32_t tv_sec_lo,uint32_t tv_nsec)216 timespec_from_proto(struct timespec *a, uint32_t tv_sec_hi,
217 uint32_t tv_sec_lo, uint32_t tv_nsec)
218 {
219 a->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
220 a->tv_nsec = tv_nsec;
221 }
222
223 /* Check if a timespec is zero
224 *
225 * \param a timespec
226 * \return whether the timespec is zero
227 */
228 static inline bool
timespec_is_zero(const struct timespec * a)229 timespec_is_zero(const struct timespec *a)
230 {
231 return a->tv_sec == 0 && a->tv_nsec == 0;
232 }
233
234 /* Check if two timespecs are equal
235 *
236 * \param a[in] timespec to check
237 * \param b[in] timespec to check
238 * \return whether timespecs a and b are equal
239 */
240 static inline bool
timespec_eq(const struct timespec * a,const struct timespec * b)241 timespec_eq(const struct timespec *a, const struct timespec *b)
242 {
243 return a->tv_sec == b->tv_sec &&
244 a->tv_nsec == b->tv_nsec;
245 }
246
247 /* Convert milli-Hertz to nanoseconds
248 *
249 * \param mhz frequency in mHz, not zero
250 * \return period in nanoseconds
251 */
252 static inline int64_t
millihz_to_nsec(uint32_t mhz)253 millihz_to_nsec(uint32_t mhz)
254 {
255 assert(mhz > 0);
256 return 1000000000000LL / mhz;
257 }
258
259 #endif /* TIMESPEC_UTIL_H */
260