Pekka Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 1 | /* |
| 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 Paalanen | d7894d0 | 2015-07-03 15:08:53 +0300 | [diff] [blame] | 29 | #include <stdint.h> |
| 30 | #include <assert.h> |
Yann E. MORIN | fa41bdf | 2017-10-01 14:31:10 +0200 | [diff] [blame] | 31 | #include <time.h> |
Alexandros Frantzis | e2a5f9e | 2017-11-27 10:54:49 +0200 | [diff] [blame^] | 32 | #include <stdbool.h> |
Pekka Paalanen | d7894d0 | 2015-07-03 15:08:53 +0300 | [diff] [blame] | 33 | |
Pekka Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 34 | #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 | */ |
| 42 | static inline void |
| 43 | timespec_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 Stone | 5ae7e84 | 2017-03-01 11:34:00 +0000 | [diff] [blame] | 54 | /* 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 | */ |
| 60 | static inline void |
| 61 | timespec_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 Stone | 61f6d7d | 2017-03-01 11:34:01 +0000 | [diff] [blame] | 75 | /* 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 | */ |
| 81 | static inline void |
| 82 | timespec_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 Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 87 | /* Convert timespec to nanoseconds |
| 88 | * |
| 89 | * \param a timespec |
| 90 | * \return nanoseconds |
| 91 | */ |
| 92 | static inline int64_t |
| 93 | timespec_to_nsec(const struct timespec *a) |
| 94 | { |
| 95 | return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec; |
| 96 | } |
| 97 | |
Daniel Stone | 839b635 | 2017-03-01 11:34:03 +0000 | [diff] [blame] | 98 | /* 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 | */ |
| 104 | static inline int64_t |
| 105 | timespec_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 Stone | 37ad7e3 | 2017-03-01 11:34:02 +0000 | [diff] [blame] | 112 | /* Convert timespec to milliseconds |
| 113 | * |
| 114 | * \param a timespec |
| 115 | * \return milliseconds |
| 116 | * |
| 117 | * Rounding to integer milliseconds happens always down (floor()). |
| 118 | */ |
| 119 | static inline int64_t |
| 120 | timespec_to_msec(const struct timespec *a) |
| 121 | { |
| 122 | return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; |
| 123 | } |
| 124 | |
Daniel Stone | 839b635 | 2017-03-01 11:34:03 +0000 | [diff] [blame] | 125 | /* 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 | */ |
| 131 | static inline int64_t |
| 132 | timespec_sub_to_msec(const struct timespec *a, const struct timespec *b) |
| 133 | { |
| 134 | return timespec_sub_to_nsec(a, b) / 1000000; |
| 135 | } |
| 136 | |
Alexandros Frantzis | e2a5f9e | 2017-11-27 10:54:49 +0200 | [diff] [blame^] | 137 | /* Check if a timespec is zero |
| 138 | * |
| 139 | * \param a timespec |
| 140 | * \return whether the timespec is zero |
| 141 | */ |
| 142 | static inline bool |
| 143 | timespec_is_zero(const struct timespec *a) |
| 144 | { |
| 145 | return a->tv_sec == 0 && a->tv_nsec == 0; |
| 146 | } |
| 147 | |
Pekka Paalanen | d7894d0 | 2015-07-03 15:08:53 +0300 | [diff] [blame] | 148 | /* Convert milli-Hertz to nanoseconds |
| 149 | * |
| 150 | * \param mhz frequency in mHz, not zero |
| 151 | * \return period in nanoseconds |
| 152 | */ |
| 153 | static inline int64_t |
| 154 | millihz_to_nsec(uint32_t mhz) |
| 155 | { |
| 156 | assert(mhz > 0); |
| 157 | return 1000000000000LL / mhz; |
| 158 | } |
Pekka Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 159 | |
| 160 | #endif /* TIMESPEC_UTIL_H */ |