d2013064dc69719cf7111f8c045c02580e51006e
[muen/linux.git] / include / asm-generic / div64.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _ASM_GENERIC_DIV64_H
3 #define _ASM_GENERIC_DIV64_H
4 /*
5  * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
6  * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
7  *
8  * Optimization for constant divisors on 32-bit machines:
9  * Copyright (C) 2006-2015 Nicolas Pitre
10  *
11  * The semantics of do_div() are:
12  *
13  * uint32_t do_div(uint64_t *n, uint32_t base)
14  * {
15  *      uint32_t remainder = *n % base;
16  *      *n = *n / base;
17  *      return remainder;
18  * }
19  *
20  * NOTE: macro parameter n is evaluated multiple times,
21  *       beware of side effects!
22  */
23
24 #include <linux/types.h>
25 #include <linux/compiler.h>
26
27 #if BITS_PER_LONG == 64
28
29 # define do_div(n,base) ({                                      \
30         uint32_t __base = (base);                               \
31         uint32_t __rem;                                         \
32         __rem = ((uint64_t)(n)) % __base;                       \
33         (n) = ((uint64_t)(n)) / __base;                         \
34         __rem;                                                  \
35  })
36
37 #elif BITS_PER_LONG == 32
38
39 #include <linux/log2.h>
40
41 /*
42  * If the divisor happens to be constant, we determine the appropriate
43  * inverse at compile time to turn the division into a few inline
44  * multiplications which ought to be much faster. And yet only if compiling
45  * with a sufficiently recent gcc version to perform proper 64-bit constant
46  * propagation.
47  *
48  * (It is unfortunate that gcc doesn't perform all this internally.)
49  */
50
51 #ifndef __div64_const32_is_OK
52 #define __div64_const32_is_OK (__GNUC__ >= 4)
53 #endif
54
55 #define __div64_const32(n, ___b)                                        \
56 ({                                                                      \
57         /*                                                              \
58          * Multiplication by reciprocal of b: n / b = n * (p / b) / p   \
59          *                                                              \
60          * We rely on the fact that most of this code gets optimized    \
61          * away at compile time due to constant propagation and only    \
62          * a few multiplication instructions should remain.             \
63          * Hence this monstrous macro (static inline doesn't always     \
64          * do the trick here).                                          \
65          */                                                             \
66         uint64_t ___res, ___x, ___t, ___m, ___n = (n);                  \
67         uint32_t ___p, ___bias;                                         \
68                                                                         \
69         /* determine MSB of b */                                        \
70         ___p = 1 << ilog2(___b);                                        \
71                                                                         \
72         /* compute m = ((p << 64) + b - 1) / b */                       \
73         ___m = (~0ULL / ___b) * ___p;                                   \
74         ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b;        \
75                                                                         \
76         /* one less than the dividend with highest result */            \
77         ___x = ~0ULL / ___b * ___b - 1;                                 \
78                                                                         \
79         /* test our ___m with res = m * x / (p << 64) */                \
80         ___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32;     \
81         ___t = ___res += (___m & 0xffffffff) * (___x >> 32);            \
82         ___res += (___x & 0xffffffff) * (___m >> 32);                   \
83         ___t = (___res < ___t) ? (1ULL << 32) : 0;                      \
84         ___res = (___res >> 32) + ___t;                                 \
85         ___res += (___m >> 32) * (___x >> 32);                          \
86         ___res /= ___p;                                                 \
87                                                                         \
88         /* Now sanitize and optimize what we've got. */                 \
89         if (~0ULL % (___b / (___b & -___b)) == 0) {                     \
90                 /* special case, can be simplified to ... */            \
91                 ___n /= (___b & -___b);                                 \
92                 ___m = ~0ULL / (___b / (___b & -___b));                 \
93                 ___p = 1;                                               \
94                 ___bias = 1;                                            \
95         } else if (___res != ___x / ___b) {                             \
96                 /*                                                      \
97                  * We can't get away without a bias to compensate       \
98                  * for bit truncation errors.  To avoid it we'd need an \
99                  * additional bit to represent m which would overflow   \
100                  * a 64-bit variable.                                   \
101                  *                                                      \
102                  * Instead we do m = p / b and n / b = (n * m + m) / p. \
103                  */                                                     \
104                 ___bias = 1;                                            \
105                 /* Compute m = (p << 64) / b */                         \
106                 ___m = (~0ULL / ___b) * ___p;                           \
107                 ___m += ((~0ULL % ___b + 1) * ___p) / ___b;             \
108         } else {                                                        \
109                 /*                                                      \
110                  * Reduce m / p, and try to clear bit 31 of m when      \
111                  * possible, otherwise that'll need extra overflow      \
112                  * handling later.                                      \
113                  */                                                     \
114                 uint32_t ___bits = -(___m & -___m);                     \
115                 ___bits |= ___m >> 32;                                  \
116                 ___bits = (~___bits) << 1;                              \
117                 /*                                                      \
118                  * If ___bits == 0 then setting bit 31 is  unavoidable. \
119                  * Simply apply the maximum possible reduction in that  \
120                  * case. Otherwise the MSB of ___bits indicates the     \
121                  * best reduction we should apply.                      \
122                  */                                                     \
123                 if (!___bits) {                                         \
124                         ___p /= (___m & -___m);                         \
125                         ___m /= (___m & -___m);                         \
126                 } else {                                                \
127                         ___p >>= ilog2(___bits);                        \
128                         ___m >>= ilog2(___bits);                        \
129                 }                                                       \
130                 /* No bias needed. */                                   \
131                 ___bias = 0;                                            \
132         }                                                               \
133                                                                         \
134         /*                                                              \
135          * Now we have a combination of 2 conditions:                   \
136          *                                                              \
137          * 1) whether or not we need to apply a bias, and               \
138          *                                                              \
139          * 2) whether or not there might be an overflow in the cross    \
140          *    product determined by (___m & ((1 << 63) | (1 << 31))).   \
141          *                                                              \
142          * Select the best way to do (m_bias + m * n) / (1 << 64).      \
143          * From now on there will be actual runtime code generated.     \
144          */                                                             \
145         ___res = __arch_xprod_64(___m, ___n, ___bias);                  \
146                                                                         \
147         ___res /= ___p;                                                 \
148 })
149
150 #ifndef __arch_xprod_64
151 /*
152  * Default C implementation for __arch_xprod_64()
153  *
154  * Prototype: uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
155  * Semantic:  retval = ((bias ? m : 0) + m * n) >> 64
156  *
157  * The product is a 128-bit value, scaled down to 64 bits.
158  * Assuming constant propagation to optimize away unused conditional code.
159  * Architectures may provide their own optimized assembly implementation.
160  */
161 static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
162 {
163         uint32_t m_lo = m;
164         uint32_t m_hi = m >> 32;
165         uint32_t n_lo = n;
166         uint32_t n_hi = n >> 32;
167         uint64_t res, tmp;
168
169         if (!bias) {
170                 res = ((uint64_t)m_lo * n_lo) >> 32;
171         } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
172                 /* there can't be any overflow here */
173                 res = (m + (uint64_t)m_lo * n_lo) >> 32;
174         } else {
175                 res = m + (uint64_t)m_lo * n_lo;
176                 tmp = (res < m) ? (1ULL << 32) : 0;
177                 res = (res >> 32) + tmp;
178         }
179
180         if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
181                 /* there can't be any overflow here */
182                 res += (uint64_t)m_lo * n_hi;
183                 res += (uint64_t)m_hi * n_lo;
184                 res >>= 32;
185         } else {
186                 tmp = res += (uint64_t)m_lo * n_hi;
187                 res += (uint64_t)m_hi * n_lo;
188                 tmp = (res < tmp) ? (1ULL << 32) : 0;
189                 res = (res >> 32) + tmp;
190         }
191
192         res += (uint64_t)m_hi * n_hi;
193
194         return res;
195 }
196 #endif
197
198 #ifndef __div64_32
199 extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
200 #endif
201
202 /* The unnecessary pointer compare is there
203  * to check for type safety (n must be 64bit)
204  */
205 # define do_div(n,base) ({                              \
206         uint32_t __base = (base);                       \
207         uint32_t __rem;                                 \
208         (void)(((typeof((n)) *)0) == ((uint64_t *)0));  \
209         if (__builtin_constant_p(__base) &&             \
210             is_power_of_2(__base)) {                    \
211                 __rem = (n) & (__base - 1);             \
212                 (n) >>= ilog2(__base);                  \
213         } else if (__div64_const32_is_OK &&             \
214                    __builtin_constant_p(__base) &&      \
215                    __base != 0) {                       \
216                 uint32_t __res_lo, __n_lo = (n);        \
217                 (n) = __div64_const32(n, __base);       \
218                 /* the remainder can be computed with 32-bit regs */ \
219                 __res_lo = (n);                         \
220                 __rem = __n_lo - __res_lo * __base;     \
221         } else if (likely(((n) >> 32) == 0)) {          \
222                 __rem = (uint32_t)(n) % __base;         \
223                 (n) = (uint32_t)(n) / __base;           \
224         } else                                          \
225                 __rem = __div64_32(&(n), __base);       \
226         __rem;                                          \
227  })
228
229 #else /* BITS_PER_LONG == ?? */
230
231 # error do_div() does not yet support the C64
232
233 #endif /* BITS_PER_LONG */
234
235 #endif /* _ASM_GENERIC_DIV64_H */