]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] time: add __usec_to_1024th to convert usecs to 1024th of second
authorWilly Tarreau <w@1wt.eu>
Wed, 4 Mar 2009 23:34:01 +0000 (00:34 +0100)
committerWilly Tarreau <w@1wt.eu>
Wed, 4 Mar 2009 23:34:01 +0000 (00:34 +0100)
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

index 7dbed6d732d11bc4669382a3e516c9d859d1020c..07d3070eb64989f7435eab7042644bcc5374709d 100644 (file)
@@ -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 ***********************************/