From: Willy Tarreau Date: Wed, 4 Mar 2009 23:34:01 +0000 (+0100) Subject: [MINOR] time: add __usec_to_1024th to convert usecs to 1024th of second X-Git-Tag: v1.3.16-rc1~31 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=776cd87e321f403929898ad5167f48040c24460e;p=thirdparty%2Fhaproxy.git [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. --- diff --git a/include/common/time.h b/include/common/time.h index 7dbed6d732..07d3070eb6 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 ***********************************/