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 | |
| 52 | /* Convert timespec to nanoseconds |
| 53 | * |
| 54 | * \param a timespec |
| 55 | * \return nanoseconds |
| 56 | */ |
| 57 | static inline int64_t |
| 58 | timespec_to_nsec(const struct timespec *a) |
| 59 | { |
| 60 | return (int64_t)a->tv_sec * NSEC_PER_SEC + a->tv_nsec; |
| 61 | } |
| 62 | |
Pekka Paalanen | d7894d0 | 2015-07-03 15:08:53 +0300 | [diff] [blame] | 63 | /* Convert milli-Hertz to nanoseconds |
| 64 | * |
| 65 | * \param mhz frequency in mHz, not zero |
| 66 | * \return period in nanoseconds |
| 67 | */ |
| 68 | static inline int64_t |
| 69 | millihz_to_nsec(uint32_t mhz) |
| 70 | { |
| 71 | assert(mhz > 0); |
| 72 | return 1000000000000LL / mhz; |
| 73 | } |
Pekka Paalanen | aa21f62 | 2015-07-03 15:44:50 +0300 | [diff] [blame] | 74 | |
| 75 | #endif /* TIMESPEC_UTIL_H */ |