From 9d5de6062ba82a56d25d8a53ccace3afecbf50e5 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Wed, 7 Feb 2024 18:51:39 +0100 Subject: [PATCH] timespec: Pull in timespec_after and timespec_add from mesa Signed-off-by: Sebastian Wick --- src/timespec-util.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/src/timespec-util.h b/src/timespec-util.h index f79969bb..d37568d2 100644 --- a/src/timespec-util.h +++ b/src/timespec-util.h @@ -256,4 +256,38 @@ millihz_to_nsec(uint32_t mhz) return 1000000000000LL / mhz; } +/** + * Checks whether a timespec value is after another + * + * \param a[in] timespec to compare + * \param b[in] timespec to compare + * \return whether a is after b + */ +static inline bool +timespec_after(const struct timespec *a, const struct timespec *b) +{ + return (a->tv_sec == b->tv_sec) ? + (a->tv_nsec > b->tv_nsec) : + (a->tv_sec > b->tv_sec); +} + +/** + * Add timespecs + * + * \param r[out] result: a + b + * \param a[in] operand + * \param b[in] operand + */ +static inline void +timespec_add(struct timespec *r, + const struct timespec *a, const struct timespec *b) +{ + r->tv_sec = a->tv_sec + b->tv_sec; + r->tv_nsec = a->tv_nsec + b->tv_nsec; + if (r->tv_nsec > NSEC_PER_SEC) { + r->tv_sec++; + r->tv_nsec -= NSEC_PER_SEC; + } +} + #endif /* TIMESPEC_UTIL_H */