Fast add nanoseconds to timespec structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static inline uint32_t __iter_div_u64_rem(uint64_t dividend, uint32_t divisor, uint64_t *remainder) | |
{ | |
uint32_t ret = 0; | |
while (dividend >= divisor) { | |
/* The following asm() prevents the compiler from | |
optimising this loop into a modulo operation. */ | |
asm("" : "+rm"(dividend)); | |
dividend -= divisor; | |
ret++; | |
} | |
*remainder = dividend; | |
return ret; | |
} | |
#define NSEC_PER_SEC 1000000000L | |
static inline void timespec_add_ns(struct timespec *a, uint64_t ns) | |
{ | |
a->tv_sec += __iter_div_u64_rem(a->tv_nsec + ns, NSEC_PER_SEC, &ns); | |
a->tv_nsec = ns; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment