datapath: compat: Downstream the reciprocal_div.{c,h}.
[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 1
3
4 #include <linux/types.h>
5
6 /*
7  * This algorithm is based on the paper "Division by Invariant
8  * Integers Using Multiplication" by Torbjörn Granlund and Peter
9  * L. Montgomery.
10  *
11  * The assembler implementation from Agner Fog, which this code is
12  * based on, can be found here:
13  * http://www.agner.org/optimize/asmlib.zip
14  *
15  * This optimization for A/B is helpful if the divisor B is mostly
16  * runtime invariant. The reciprocal of B is calculated in the
17  * slow-path with reciprocal_value(). The fast-path can then just use
18  * a much faster multiplication operation with a variable dividend A
19  * to calculate the division A/B.
20  */
21
22 #define reciprocal_value rpl_reciprocal_value
23 struct reciprocal_value {
24         u32 m;
25         u8 sh1, sh2;
26 };
27
28 struct reciprocal_value reciprocal_value(u32 d);
29
30 #define reciprocal_divide rpl_reciprocal_divide
31 static inline u32 reciprocal_divide(u32 a, struct reciprocal_value R)
32 {
33         u32 t = (u32)(((u64)a * R.m) >> 32);
34         return (t + ((a - t) >> R.sh1)) >> R.sh2;
35 }
36
37 #endif /* _LINUX_RECIPROCAL_DIV_WRAPPER_H */