From 776cd87e321f403929898ad5167f48040c24460e Mon Sep 17 00:00:00 2001 From: Willy Tarreau Date: Thu, 5 Mar 2009 00:34:01 +0100 Subject: [PATCH] [MINOR] time: add __usec_to_1024th to convert usecs to 1024th of second This function performs a fast conversion from usec to 1024th of a second, and will be useful for many fast sub-second computations. --- include/common/time.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/include/common/time.h b/include/common/time.h index 7dbed6d73..07d3070eb 100644 --- a/include/common/time.h +++ b/include/common/time.h @@ -157,6 +157,18 @@ REGPRM2 static inline struct timeval * __tv_from_ms(struct timeval *tv, unsigned return tv; } +/* Return a number of 1024Hz ticks between 0 and 1023 for input number of + * usecs between 0 and 999999. This function has been optimized to remove + * any divide and multiply, as it is completely optimized away by the compiler + * on CPUs which don't have a fast multiply. Its avg error rate is 305 ppm, + * which is almost twice as low as a direct usec to ms conversion. This version + * also has the benefit of returning 1024 for 1000000. + */ +REGPRM1 static inline unsigned int __usec_to_1024th(unsigned int usec) +{ + return (usec * 1073 + 742516) >> 20; +} + /**** comparison functions and macros ***********************************/