2 * Copyright © 2014 - 2015 Collabora, Ltd.
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:
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.
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
26 #ifndef TIMESPEC_UTIL_H
27 #define TIMESPEC_UTIL_H
34 #define NSEC_PER_SEC 1000000000
38 * \param r[out] result: a - b
39 * \param a[in] operand
40 * \param b[in] operand
43 timespec_sub(struct timespec *r,
44 const struct timespec *a, const struct timespec *b)
46 r->tv_sec = a->tv_sec - b->tv_sec;
47 r->tv_nsec = a->tv_nsec - b->tv_nsec;
50 r->tv_nsec += NSEC_PER_SEC;
54 /* Add a nanosecond value to a timespec
56 * \param r[out] result: a + b
57 * \param a[in] base operand as timespec
58 * \param b[in] operand in nanoseconds
61 timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
63 r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
64 r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);
66 if (r->tv_nsec >= NSEC_PER_SEC) {
68 r->tv_nsec -= NSEC_PER_SEC;
69 } else if (r->tv_nsec < 0) {
71 r->tv_nsec += NSEC_PER_SEC;
75 /* Add a millisecond value to a timespec
77 * \param r[out] result: a + b
78 * \param a[in] base operand as timespec
79 * \param b[in] operand in milliseconds
82 timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
84 return timespec_add_nsec(r, a, b * 1000000);
87 /* Convert timespec to nanoseconds
93 timespec_to_nsec(const struct timespec *a)
95 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
98 /* Subtract timespecs and return result in nanoseconds
100 * \param a[in] operand
101 * \param b[in] operand
102 * \return to_nanoseconds(a - b)
104 static inline int64_t
105 timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b)
108 timespec_sub(&r, a, b);
109 return timespec_to_nsec(&r);
112 /* Convert timespec to milliseconds
115 * \return milliseconds
117 * Rounding to integer milliseconds happens always down (floor()).
119 static inline int64_t
120 timespec_to_msec(const struct timespec *a)
122 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
125 /* Subtract timespecs and return result in milliseconds
127 * \param a[in] operand
128 * \param b[in] operand
129 * \return to_milliseconds(a - b)
131 static inline int64_t
132 timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
134 return timespec_sub_to_nsec(a, b) / 1000000;
137 /* Convert timespec to microseconds
140 * \return microseconds
142 * Rounding to integer microseconds happens always down (floor()).
144 static inline int64_t
145 timespec_to_usec(const struct timespec *a)
147 return (int64_t)a->tv_sec * 1000000 + a->tv_nsec / 1000;
150 /* Convert timespec to protocol data
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
157 * The input timespec must be normalized (the nanoseconds part should
158 * be less than 1 second) and non-negative.
161 timespec_to_proto(const struct timespec *a, uint32_t *tv_sec_hi,
162 uint32_t *tv_sec_lo, uint32_t *tv_nsec)
164 assert(a->tv_sec >= 0);
165 assert(a->tv_nsec >= 0 && a->tv_nsec < NSEC_PER_SEC);
167 uint64_t sec64 = a->tv_sec;
169 *tv_sec_hi = sec64 >> 32;
170 *tv_sec_lo = sec64 & 0xffffffff;
171 *tv_nsec = a->tv_nsec;
174 /* Convert nanoseconds to timespec
177 * \param b nanoseconds
180 timespec_from_nsec(struct timespec *a, int64_t b)
182 a->tv_sec = b / NSEC_PER_SEC;
183 a->tv_nsec = b % NSEC_PER_SEC;
186 /* Convert microseconds to timespec
189 * \param b microseconds
192 timespec_from_usec(struct timespec *a, int64_t b)
194 timespec_from_nsec(a, b * 1000);
197 /* Convert milliseconds to timespec
200 * \param b milliseconds
203 timespec_from_msec(struct timespec *a, int64_t b)
205 timespec_from_nsec(a, b * 1000000);
208 /* Convert protocol data to timespec
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
216 timespec_from_proto(struct timespec *a, uint32_t tv_sec_hi,
217 uint32_t tv_sec_lo, uint32_t tv_nsec)
219 a->tv_sec = ((uint64_t)tv_sec_hi << 32) + tv_sec_lo;
220 a->tv_nsec = tv_nsec;
223 /* Check if a timespec is zero
226 * \return whether the timespec is zero
229 timespec_is_zero(const struct timespec *a)
231 return a->tv_sec == 0 && a->tv_nsec == 0;
234 /* Check if two timespecs are equal
236 * \param a[in] timespec to check
237 * \param b[in] timespec to check
238 * \return whether timespecs a and b are equal
241 timespec_eq(const struct timespec *a, const struct timespec *b)
243 return a->tv_sec == b->tv_sec &&
244 a->tv_nsec == b->tv_nsec;
247 /* Convert milli-Hertz to nanoseconds
249 * \param mhz frequency in mHz, not zero
250 * \return period in nanoseconds
252 static inline int64_t
253 millihz_to_nsec(uint32_t mhz)
256 return 1000000000000LL / mhz;
259 #endif /* TIMESPEC_UTIL_H */