blob: 958adb0584326b0b76434f92236cd8cf2e9a1ea2 [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>
31
Pekka Paalanenaa21f622015-07-03 15:44:50 +030032#define NSEC_PER_SEC 1000000000
33
34/* Subtract timespecs
35 *
36 * \param r[out] result: a - b
37 * \param a[in] operand
38 * \param b[in] operand
39 */
40static inline void
41timespec_sub(struct timespec *r,
42 const struct timespec *a, const struct timespec *b)
43{
44 r->tv_sec = a->tv_sec - b->tv_sec;
45 r->tv_nsec = a->tv_nsec - b->tv_nsec;
46 if (r->tv_nsec < 0) {
47 r->tv_sec--;
48 r->tv_nsec += NSEC_PER_SEC;
49 }
50}
51
Daniel Stone5ae7e842017-03-01 11:34:00 +000052/* Add a nanosecond value to a timespec
53 *
54 * \param r[out] result: a + b
55 * \param a[in] base operand as timespec
56 * \param b[in] operand in nanoseconds
57 */
58static inline void
59timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
60{
61 r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
62 r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);
63
64 if (r->tv_nsec >= NSEC_PER_SEC) {
65 r->tv_sec++;
66 r->tv_nsec -= NSEC_PER_SEC;
67 } else if (r->tv_nsec < 0) {
68 r->tv_sec--;
69 r->tv_nsec += NSEC_PER_SEC;
70 }
71}
72
Daniel Stone61f6d7d2017-03-01 11:34:01 +000073/* Add a millisecond value to a timespec
74 *
75 * \param r[out] result: a + b
76 * \param a[in] base operand as timespec
77 * \param b[in] operand in milliseconds
78 */
79static inline void
80timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
81{
82 return timespec_add_nsec(r, a, b * 1000000);
83}
84
Pekka Paalanenaa21f622015-07-03 15:44:50 +030085/* Convert timespec to nanoseconds
86 *
87 * \param a timespec
88 * \return nanoseconds
89 */
90static inline int64_t
91timespec_to_nsec(const struct timespec *a)
92{
93 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
94}
95
Daniel Stone37ad7e32017-03-01 11:34:02 +000096/* Convert timespec to milliseconds
97 *
98 * \param a timespec
99 * \return milliseconds
100 *
101 * Rounding to integer milliseconds happens always down (floor()).
102 */
103static inline int64_t
104timespec_to_msec(const struct timespec *a)
105{
106 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
107}
108
Pekka Paalanend7894d02015-07-03 15:08:53 +0300109/* Convert milli-Hertz to nanoseconds
110 *
111 * \param mhz frequency in mHz, not zero
112 * \return period in nanoseconds
113 */
114static inline int64_t
115millihz_to_nsec(uint32_t mhz)
116{
117 assert(mhz > 0);
118 return 1000000000000LL / mhz;
119}
Pekka Paalanenaa21f622015-07-03 15:44:50 +0300120
121#endif /* TIMESPEC_UTIL_H */