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> |
| 31 | |
Pekka Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 32 | #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 | */ |
| 40 | static inline void |
| 41 | timespec_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 Stone | 5ae7e84 | 2017-03-01 11:34:00 +0000 | [diff] [blame] | 52 | /* 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 | */ |
| 58 | static inline void |
| 59 | timespec_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 Stone | 61f6d7d | 2017-03-01 11:34:01 +0000 | [diff] [blame] | 73 | /* 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 | */ |
| 79 | static inline void |
| 80 | timespec_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 Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 85 | /* Convert timespec to nanoseconds |
| 86 | * |
| 87 | * \param a timespec |
| 88 | * \return nanoseconds |
| 89 | */ |
| 90 | static inline int64_t |
| 91 | timespec_to_nsec(const struct timespec *a) |
| 92 | { |
| 93 | return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec; |
| 94 | } |
| 95 | |
Daniel Stone | 839b635 | 2017-03-01 11:34:03 +0000 | [diff] [blame] | 96 | /* 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 | */ |
| 102 | static inline int64_t |
| 103 | timespec_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 Stone | 37ad7e3 | 2017-03-01 11:34:02 +0000 | [diff] [blame] | 110 | /* Convert timespec to milliseconds |
| 111 | * |
| 112 | * \param a timespec |
| 113 | * \return milliseconds |
| 114 | * |
| 115 | * Rounding to integer milliseconds happens always down (floor()). |
| 116 | */ |
| 117 | static inline int64_t |
| 118 | timespec_to_msec(const struct timespec *a) |
| 119 | { |
| 120 | return (int64_t)a->tv_sec * 1000 + a->tv_nsec / 1000000; |
| 121 | } |
| 122 | |
Daniel Stone | 839b635 | 2017-03-01 11:34:03 +0000 | [diff] [blame] | 123 | /* 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 | */ |
| 129 | static inline int64_t |
| 130 | timespec_sub_to_msec(const struct timespec *a, const struct timespec *b) |
| 131 | { |
| 132 | return timespec_sub_to_nsec(a, b) / 1000000; |
| 133 | } |
| 134 | |
Pekka Paalanen | d7894d0 | 2015-07-03 15:08:53 +0300 | [diff] [blame] | 135 | /* Convert milli-Hertz to nanoseconds |
| 136 | * |
| 137 | * \param mhz frequency in mHz, not zero |
| 138 | * \return period in nanoseconds |
| 139 | */ |
| 140 | static inline int64_t |
| 141 | millihz_to_nsec(uint32_t mhz) |
| 142 | { |
| 143 | assert(mhz > 0); |
| 144 | return 1000000000000LL / mhz; |
| 145 | } |
Pekka Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 146 | |
| 147 | #endif /* TIMESPEC_UTIL_H */ |