DPDK  16.11.11
rte_reciprocal.h
1 /*
2  * Reciprocal divide
3  *
4  * Used with permission from original authors
5  * Hannes Frederic Sowa and Daniel Borkmann
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 #ifndef _RTE_RECIPROCAL_H_
23 #define _RTE_RECIPROCAL_H_
24 
25 #include <stdint.h>
26 
27 struct rte_reciprocal {
28  uint32_t m;
29  uint8_t sh1, sh2;
30 };
31 
32 static inline uint32_t rte_reciprocal_divide(uint32_t a, struct rte_reciprocal R)
33 {
34  uint32_t t = (uint32_t)(((uint64_t)a * R.m) >> 32);
35 
36  return (t + ((a - t) >> R.sh1)) >> R.sh2;
37 }
38 
39 struct rte_reciprocal rte_reciprocal_value(uint32_t d);
40 
41 #endif /* _RTE_RECIPROCAL_H_ */