From 3259da07aef6ca7d9ce395503d512c53ea3316d7 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Sat, 9 Feb 2019 19:18:53 +0100 Subject: [SHARED] More timing helpers --- src/shared/timing.h | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/shared/timing.h b/src/shared/timing.h index dfff5f8..f3d8802 100644 --- a/src/shared/timing.h +++ b/src/shared/timing.h @@ -66,6 +66,12 @@ static inline void timing_gets(ticks* retval, int32_t addSeconds) retval->tv_sec += addSeconds; } +static inline void timing_addSeconds(ticks* retval, ticks* base, int32_t addSeconds) +{ + retval->tv_sec = base->tv_sec + addSeconds; + retval->tv_nsec = base->tv_nsec; +} + /** * Check whether given timeout is reached. * Might trigger up to one second early. @@ -135,5 +141,22 @@ static inline uint64_t timing_diffMs(const ticks *start, const ticks *end) return diff; } +/** + * Get difference between two ticks, rounded down to microseconds. + * Same as above; passing arguments in reverse will always return 0. + */ +static inline uint64_t timing_diffUs(const ticks *start, const ticks *end) +{ + if ( end->tv_sec < start->tv_sec ) return 0; + uint64_t diff = (uint64_t)( end->tv_sec - start->tv_sec ) * 1000000; + if ( start->tv_nsec >= end->tv_nsec ) { + if ( diff == 0 ) return 0; + diff -= ( start->tv_nsec - end->tv_nsec ) / 1000; + } else { + diff += ( end->tv_nsec - start->tv_nsec ) / 1000; + } + return diff; +} + #endif -- cgit v1.2.3-55-g7522