From 893e4fc46d31d1cc51c1e39481f5659a40152233 Mon Sep 17 00:00:00 2001 From: Sebastian Wick Date: Wed, 7 Feb 2024 18:53:16 +0100 Subject: [PATCH] timespec: Implement saturating timespec substraction Signed-off-by: Sebastian Wick --- src/timespec-util.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/timespec-util.h b/src/timespec-util.h index d37568d2..d3c53bd7 100644 --- a/src/timespec-util.h +++ b/src/timespec-util.h @@ -290,4 +290,22 @@ timespec_add(struct timespec *r, } } +/** + * Saturating timespec subtraction + * + * \param r[out] result: max(a - b, 0) + * \param a[in] operand + * \param b[in] operand + */ +static inline void +timespec_sub_saturate(struct timespec *r, + const struct timespec *a, const struct timespec *b) +{ + timespec_sub(r, a, b); + if (r->tv_sec < 0) { + r->tv_sec = 0; + r->tv_nsec = 0; + } +} + #endif /* TIMESPEC_UTIL_H */