f618bddc6400d629d458afaa2f2ebcb03b0ca900
[sliver-openvswitch.git] / datapath / linux / compat / include / linux / reciprocal_div.h
1 #ifndef __LINUX_RECIPROCAL_DIV_WRAPPER_H
2 #define __LINUX_RECIPROCAL_DIV_WRAPPER_H
3
4 #include <linux/version.h>
5
6 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
7 #include_next <linux/reciprocal_div.h>
8 #else
9
10 #include <linux/types.h>
11
12 /*
13  * This file describes reciprocical division.
14  *
15  * This optimizes the (A/B) problem, when A and B are two u32
16  * and B is a known value (but not known at compile time)
17  *
18  * The math principle used is :
19  *   Let RECIPROCAL_VALUE(B) be (((1LL << 32) + (B - 1))/ B)
20  *   Then A / B = (u32)(((u64)(A) * (R)) >> 32)
21  *
22  * This replaces a divide by a multiply (and a shift), and
23  * is generally less expensive in CPU cycles.
24  */
25
26 /*
27  * Computes the reciprocal value (R) for the value B of the divisor.
28  * Should not be called before each reciprocal_divide(),
29  * or else the performance is slower than a normal divide.
30  */
31 extern u32 reciprocal_value(u32 B);
32
33
34 static inline u32 reciprocal_divide(u32 A, u32 R)
35 {
36         return (u32)(((u64)A * R) >> 32);
37 }
38
39 #endif /* Linux kernel < 2.6.20 */
40 #endif /* __LINUX_RECIPROCAL_DIV_WRAPPER_H */