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