blob: 7260dc8b05b42f3630d48a0b7700143fd380f522 [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 Frantzise2a5f9e2017-11-27 10:54:49 +0200137/* Check if a timespec is zero
138 *
139 * \param a timespec
140 * \return whether the timespec is zero
141 */
142static inline bool
143timespec_is_zero(const struct timespec *a)
144{
145 return a->tv_sec == 0 && a->tv_nsec == 0;
146}
147
Pekka Paalanend7894d02015-07-03 15:08:53 +0300148/* Convert milli-Hertz to nanoseconds
149 *
150 * \param mhz frequency in mHz, not zero
151 * \return period in nanoseconds
152 */
153static inline int64_t
154millihz_to_nsec(uint32_t mhz)
155{
156 assert(mhz > 0);
157 return 1000000000000LL / mhz;
158}
Pekka Paalanenaa21f622015-07-03 15:44:50 +0300159
160#endif /* TIMESPEC_UTIL_H */