xref: /linux-6.15/include/linux/reciprocal_div.h (revision 06ae4826)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
26a2d7a95SEric Dumazet #ifndef _LINUX_RECIPROCAL_DIV_H
36a2d7a95SEric Dumazet #define _LINUX_RECIPROCAL_DIV_H
46a2d7a95SEric Dumazet 
56a2d7a95SEric Dumazet #include <linux/types.h>
66a2d7a95SEric Dumazet 
76a2d7a95SEric Dumazet /*
8809fa972SHannes Frederic Sowa  * This algorithm is based on the paper "Division by Invariant
9809fa972SHannes Frederic Sowa  * Integers Using Multiplication" by Torbjörn Granlund and Peter
10809fa972SHannes Frederic Sowa  * L. Montgomery.
116a2d7a95SEric Dumazet  *
12809fa972SHannes Frederic Sowa  * The assembler implementation from Agner Fog, which this code is
13809fa972SHannes Frederic Sowa  * based on, can be found here:
14809fa972SHannes Frederic Sowa  * http://www.agner.org/optimize/asmlib.zip
156a2d7a95SEric Dumazet  *
16809fa972SHannes Frederic Sowa  * This optimization for A/B is helpful if the divisor B is mostly
17809fa972SHannes Frederic Sowa  * runtime invariant. The reciprocal of B is calculated in the
18809fa972SHannes Frederic Sowa  * slow-path with reciprocal_value(). The fast-path can then just use
19809fa972SHannes Frederic Sowa  * a much faster multiplication operation with a variable dividend A
20809fa972SHannes Frederic Sowa  * to calculate the division A/B.
216a2d7a95SEric Dumazet  */
226a2d7a95SEric Dumazet 
23809fa972SHannes Frederic Sowa struct reciprocal_value {
24809fa972SHannes Frederic Sowa 	u32 m;
25809fa972SHannes Frederic Sowa 	u8 sh1, sh2;
26809fa972SHannes Frederic Sowa };
276a2d7a95SEric Dumazet 
28*06ae4826SJiong Wang /* "reciprocal_value" and "reciprocal_divide" together implement the basic
29*06ae4826SJiong Wang  * version of the algorithm described in Figure 4.1 of the paper.
30*06ae4826SJiong Wang  */
31809fa972SHannes Frederic Sowa struct reciprocal_value reciprocal_value(u32 d);
326a2d7a95SEric Dumazet 
reciprocal_divide(u32 a,struct reciprocal_value R)33809fa972SHannes Frederic Sowa static inline u32 reciprocal_divide(u32 a, struct reciprocal_value R)
346a2d7a95SEric Dumazet {
35809fa972SHannes Frederic Sowa 	u32 t = (u32)(((u64)a * R.m) >> 32);
36809fa972SHannes Frederic Sowa 	return (t + ((a - t) >> R.sh1)) >> R.sh2;
376a2d7a95SEric Dumazet }
38809fa972SHannes Frederic Sowa 
39*06ae4826SJiong Wang struct reciprocal_value_adv {
40*06ae4826SJiong Wang 	u32 m;
41*06ae4826SJiong Wang 	u8 sh, exp;
42*06ae4826SJiong Wang 	bool is_wide_m;
43*06ae4826SJiong Wang };
44*06ae4826SJiong Wang 
45*06ae4826SJiong Wang /* "reciprocal_value_adv" implements the advanced version of the algorithm
46*06ae4826SJiong Wang  * described in Figure 4.2 of the paper except when "divisor > (1U << 31)" whose
47*06ae4826SJiong Wang  * ceil(log2(d)) result will be 32 which then requires u128 divide on host. The
48*06ae4826SJiong Wang  * exception case could be easily handled before calling "reciprocal_value_adv".
49*06ae4826SJiong Wang  *
50*06ae4826SJiong Wang  * The advanced version requires more complex calculation to get the reciprocal
51*06ae4826SJiong Wang  * multiplier and other control variables, but then could reduce the required
52*06ae4826SJiong Wang  * emulation operations.
53*06ae4826SJiong Wang  *
54*06ae4826SJiong Wang  * It makes no sense to use this advanced version for host divide emulation,
55*06ae4826SJiong Wang  * those extra complexities for calculating multiplier etc could completely
56*06ae4826SJiong Wang  * waive our saving on emulation operations.
57*06ae4826SJiong Wang  *
58*06ae4826SJiong Wang  * However, it makes sense to use it for JIT divide code generation for which
59*06ae4826SJiong Wang  * we are willing to trade performance of JITed code with that of host. As shown
60*06ae4826SJiong Wang  * by the following pseudo code, the required emulation operations could go down
61*06ae4826SJiong Wang  * from 6 (the basic version) to 3 or 4.
62*06ae4826SJiong Wang  *
63*06ae4826SJiong Wang  * To use the result of "reciprocal_value_adv", suppose we want to calculate
64*06ae4826SJiong Wang  * n/d, the pseudo C code will be:
65*06ae4826SJiong Wang  *
66*06ae4826SJiong Wang  *   struct reciprocal_value_adv rvalue;
67*06ae4826SJiong Wang  *   u8 pre_shift, exp;
68*06ae4826SJiong Wang  *
69*06ae4826SJiong Wang  *   // handle exception case.
70*06ae4826SJiong Wang  *   if (d >= (1U << 31)) {
71*06ae4826SJiong Wang  *     result = n >= d;
72*06ae4826SJiong Wang  *     return;
73*06ae4826SJiong Wang  *   }
74*06ae4826SJiong Wang  *
75*06ae4826SJiong Wang  *   rvalue = reciprocal_value_adv(d, 32)
76*06ae4826SJiong Wang  *   exp = rvalue.exp;
77*06ae4826SJiong Wang  *   if (rvalue.is_wide_m && !(d & 1)) {
78*06ae4826SJiong Wang  *     // floor(log2(d & (2^32 -d)))
79*06ae4826SJiong Wang  *     pre_shift = fls(d & -d) - 1;
80*06ae4826SJiong Wang  *     rvalue = reciprocal_value_adv(d >> pre_shift, 32 - pre_shift);
81*06ae4826SJiong Wang  *   } else {
82*06ae4826SJiong Wang  *     pre_shift = 0;
83*06ae4826SJiong Wang  *   }
84*06ae4826SJiong Wang  *
85*06ae4826SJiong Wang  *   // code generation starts.
86*06ae4826SJiong Wang  *   if (imm == 1U << exp) {
87*06ae4826SJiong Wang  *     result = n >> exp;
88*06ae4826SJiong Wang  *   } else if (rvalue.is_wide_m) {
89*06ae4826SJiong Wang  *     // pre_shift must be zero when reached here.
90*06ae4826SJiong Wang  *     t = (n * rvalue.m) >> 32;
91*06ae4826SJiong Wang  *     result = n - t;
92*06ae4826SJiong Wang  *     result >>= 1;
93*06ae4826SJiong Wang  *     result += t;
94*06ae4826SJiong Wang  *     result >>= rvalue.sh - 1;
95*06ae4826SJiong Wang  *   } else {
96*06ae4826SJiong Wang  *     if (pre_shift)
97*06ae4826SJiong Wang  *       result = n >> pre_shift;
98*06ae4826SJiong Wang  *     result = ((u64)result * rvalue.m) >> 32;
99*06ae4826SJiong Wang  *     result >>= rvalue.sh;
100*06ae4826SJiong Wang  *   }
101*06ae4826SJiong Wang  */
102*06ae4826SJiong Wang struct reciprocal_value_adv reciprocal_value_adv(u32 d, u8 prec);
103*06ae4826SJiong Wang 
104809fa972SHannes Frederic Sowa #endif /* _LINUX_RECIPROCAL_DIV_H */
105