blob: 576b3e8f08e7e77af109c454385c8d27500a2a06 [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 Stone839b6352017-03-01 11:34:03 +000096/* Subtract timespecs and return result in nanoseconds
97 *
98 * \param a[in] operand
99 * \param b[in] operand
100 * \return to_nanoseconds(a - b)
101 */
102static inline int64_t
103timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b)
104{
105 struct timespec r;
106 timespec_sub(&r, a, b);
107 return timespec_to_nsec(&r);
108}
109
Daniel Stone37ad7e32017-03-01 11:34:02 +0000110/* Convert timespec to milliseconds
111 *
112 * \param a timespec
113 * \return milliseconds
114 *
115 * Rounding to integer milliseconds happens always down (floor()).
116 */
117static inline int64_t
118timespec_to_msec(const struct timespec *a)
119{
120 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
121}
122
Daniel Stone839b6352017-03-01 11:34:03 +0000123/* Subtract timespecs and return result in milliseconds
124 *
125 * \param a[in] operand
126 * \param b[in] operand
127 * \return to_milliseconds(a - b)
128 */
129static inline int64_t
130timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
131{
132 return timespec_sub_to_nsec(a, b) / 1000000;
133}
134
Pekka Paalanend7894d02015-07-03 15:08:53 +0300135/* Convert milli-Hertz to nanoseconds
136 *
137 * \param mhz frequency in mHz, not zero
138 * \return period in nanoseconds
139 */
140static inline int64_t
141millihz_to_nsec(uint32_t mhz)
142{
143 assert(mhz > 0);
144 return 1000000000000LL / mhz;
145}
Pekka Paalanenaa21f622015-07-03 15:44:50 +0300146
147#endif /* TIMESPEC_UTIL_H */