blob: 34a120ae659e163355af0c0110818927d2832024 [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>
Pekka Paalanend7894d02015-07-03 15:08:53 +030032
Pekka Paalanenaa21f622015-07-03 15:44:50 +030033#define NSEC_PER_SEC 1000000000
34
35/* Subtract timespecs
36 *
37 * \param r[out] result: a - b
38 * \param a[in] operand
39 * \param b[in] operand
40 */
41static inline void
42timespec_sub(struct timespec *r,
43 const struct timespec *a, const struct timespec *b)
44{
45 r->tv_sec = a->tv_sec - b->tv_sec;
46 r->tv_nsec = a->tv_nsec - b->tv_nsec;
47 if (r->tv_nsec < 0) {
48 r->tv_sec--;
49 r->tv_nsec += NSEC_PER_SEC;
50 }
51}
52
Daniel Stone5ae7e842017-03-01 11:34:00 +000053/* Add a nanosecond value to a timespec
54 *
55 * \param r[out] result: a + b
56 * \param a[in] base operand as timespec
57 * \param b[in] operand in nanoseconds
58 */
59static inline void
60timespec_add_nsec(struct timespec *r, const struct timespec *a, int64_t b)
61{
62 r->tv_sec = a->tv_sec + (b / NSEC_PER_SEC);
63 r->tv_nsec = a->tv_nsec + (b % NSEC_PER_SEC);
64
65 if (r->tv_nsec >= NSEC_PER_SEC) {
66 r->tv_sec++;
67 r->tv_nsec -= NSEC_PER_SEC;
68 } else if (r->tv_nsec < 0) {
69 r->tv_sec--;
70 r->tv_nsec += NSEC_PER_SEC;
71 }
72}
73
Daniel Stone61f6d7d2017-03-01 11:34:01 +000074/* Add a millisecond value to a timespec
75 *
76 * \param r[out] result: a + b
77 * \param a[in] base operand as timespec
78 * \param b[in] operand in milliseconds
79 */
80static inline void
81timespec_add_msec(struct timespec *r, const struct timespec *a, int64_t b)
82{
83 return timespec_add_nsec(r, a, b * 1000000);
84}
85
Pekka Paalanenaa21f622015-07-03 15:44:50 +030086/* Convert timespec to nanoseconds
87 *
88 * \param a timespec
89 * \return nanoseconds
90 */
91static inline int64_t
92timespec_to_nsec(const struct timespec *a)
93{
94 return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec;
95}
96
Daniel Stone839b6352017-03-01 11:34:03 +000097/* Subtract timespecs and return result in nanoseconds
98 *
99 * \param a[in] operand
100 * \param b[in] operand
101 * \return to_nanoseconds(a - b)
102 */
103static inline int64_t
104timespec_sub_to_nsec(const struct timespec *a, const struct timespec *b)
105{
106 struct timespec r;
107 timespec_sub(&r, a, b);
108 return timespec_to_nsec(&r);
109}
110
Daniel Stone37ad7e32017-03-01 11:34:02 +0000111/* Convert timespec to milliseconds
112 *
113 * \param a timespec
114 * \return milliseconds
115 *
116 * Rounding to integer milliseconds happens always down (floor()).
117 */
118static inline int64_t
119timespec_to_msec(const struct timespec *a)
120{
121 return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000;
122}
123
Daniel Stone839b6352017-03-01 11:34:03 +0000124/* Subtract timespecs and return result in milliseconds
125 *
126 * \param a[in] operand
127 * \param b[in] operand
128 * \return to_milliseconds(a - b)
129 */
130static inline int64_t
131timespec_sub_to_msec(const struct timespec *a, const struct timespec *b)
132{
133 return timespec_sub_to_nsec(a, b) / 1000000;
134}
135
Pekka Paalanend7894d02015-07-03 15:08:53 +0300136/* Convert milli-Hertz to nanoseconds
137 *
138 * \param mhz frequency in mHz, not zero
139 * \return period in nanoseconds
140 */
141static inline int64_t
142millihz_to_nsec(uint32_t mhz)
143{
144 assert(mhz > 0);
145 return 1000000000000LL / mhz;
146}
Pekka Paalanenaa21f622015-07-03 15:44:50 +0300147
148#endif /* TIMESPEC_UTIL_H */