blob: ca0156afc71e0c8db8d4f8575c79981d48a53eeb [file] [log] [blame]
Pekka Paalanenaa21f622015-07-03 15:44:50 +03001/*
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
Pekka Paalanend7894d02015-07-03 15:08:53 +030029#include <stdint.h>
30#include <assert.h>
Yann E. MORINfa41bdf2017-10-01 14:31:10 +020031#include <time.h>
Alexandros Frantzise2a5f9e2017-11-27 10:54:49 +020032#include <stdbool.h>
Pekka Paalanend7894d02015-07-03 15:08:53 +030033
Pekka Paalanenaa21f622015-07-03 15:44:50 +030034#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 */
42static inline void
43timespec_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
Daniel Stone5ae7e842017-03-01 11:34:00 +000054/* 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 */
60static inline void
61timespec_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
Daniel Stone61f6d7d2017-03-01 11:34:01 +000075/* 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 */
81static inline void
82timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
83{
84 return timespec_add_nsec(r, a, b * 1000000);
85}
86
Pekka Paalanenaa21f622015-07-03 15:44:50 +030087/* Convert timespec to nanoseconds
88 *
89 * \param a timespec
90 * \return nanoseconds
91 */
92static inline int64_t
93timespec_to_nsec(const struct timespec *a)
94{
95 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
96}
97
Daniel Stone839b6352017-03-01 11:34:03 +000098/* 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 */
104static inline int64_t
105timespec_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
Daniel Stone37ad7e32017-03-01 11:34:02 +0000112/* Convert timespec to milliseconds
113 *
114 * \param a timespec
115 * \return milliseconds
116 *
117 * Rounding to integer milliseconds happens always down (floor()).
118 */
119static inline int64_t
120timespec_to_msec(const struct timespec *a)
121{
122 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
123}
124
Daniel Stone839b6352017-03-01 11:34:03 +0000125/* 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 */
131static inline int64_t
132timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
133{
134 return timespec_sub_to_nsec(a, b) / 1000000;
135}
136
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +0200137/* Convert timespec to microseconds
138 *
139 * \param a timespec
140 * \return microseconds
141 *
142 * Rounding to integer microseconds happens always down (floor()).
143 */
144static inline int64_t
145timespec_to_usec(const struct timespec *a)
146{
147 return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000;
148}
149
Alexandros Frantzis10d708d2017-12-13 13:27:54 +0200150/* 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 */
160static inline void
161timespec_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
Alexandros Frantzis6c2752a2017-11-16 18:20:51 +0200174/* Convert nanoseconds to timespec
175 *
176 * \param a timespec
177 * \param b nanoseconds
178 */
179static inline void
180timespec_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 */
191static inline void
192timespec_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 */
202static inline void
203timespec_from_msec(struct timespec *a, int64_t b)
204{
205 timespec_from_nsec(a, b * 1000000);
206}
207
Alexandros Frantzis787fa612017-12-13 13:27:53 +0200208/* 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 */
215static inline void
216timespec_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
Alexandros Frantzise2a5f9e2017-11-27 10:54:49 +0200223/* Check if a timespec is zero
224 *
225 * \param a timespec
226 * \return whether the timespec is zero
227 */
228static inline bool
229timespec_is_zero(const struct timespec *a)
230{
231 return a->tv_sec == 0 && a->tv_nsec == 0;
232}
233
Alexandros Frantzis7a93bb22018-02-16 18:44:14 +0200234/* 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 */
240static inline bool
241timespec_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
Pekka Paalanend7894d02015-07-03 15:08:53 +0300247/* Convert milli-Hertz to nanoseconds
248 *
249 * \param mhz frequency in mHz, not zero
250 * \return period in nanoseconds
251 */
252static inline int64_t
253millihz_to_nsec(uint32_t mhz)
254{
255 assert(mhz > 0);
256 return 1000000000000LL / mhz;
257}
Pekka Paalanenaa21f622015-07-03 15:44:50 +0300258
259#endif /* TIMESPEC_UTIL_H */