1 #ifndef _ASM_GENERIC_DIV64_H
2 #define _ASM_GENERIC_DIV64_H
4 * Copyright (C) 2003 Bernardo Innocenti <bernie@develer.com>
5 * Based on former asm-ppc/div64.h and asm-m68knommu/div64.h
7 * Optimization for constant divisors on 32-bit machines:
8 * Copyright (C) 2006-2015 Nicolas Pitre
10 * The semantics of do_div() are:
12 * u32 do_div(u64 *n, u32 base)
14 * u32 remainder = *n % base;
19 * NOTE: macro parameter n is evaluated multiple times,
20 * beware of side effects!
23 #include <linux/types.h>
24 #include <linux/compiler.h>
26 #if BITS_PER_LONG == 64
28 # define do_div(n,base) ({ \
29 u32 __base = (base); \
31 __rem = ((u64)(n)) % __base; \
32 (n) = ((u64)(n)) / __base; \
36 #elif BITS_PER_LONG == 32
38 #include <linux/log2.h>
41 * If the divisor happens to be constant, we determine the appropriate
42 * inverse at compile time to turn the division into a few inline
43 * multiplications which ought to be much faster. And yet only if compiling
44 * with a sufficiently recent gcc version to perform proper 64-bit constant
47 * (It is unfortunate that gcc doesn't perform all this internally.)
50 #ifndef __div64_const32_is_OK
51 #define __div64_const32_is_OK (__GNUC__ >= 4)
54 #define __div64_const32(n, ___b) \
57 * Multiplication by reciprocal of b: n / b = n * (p / b) / p \
59 * We rely on the fact that most of this code gets optimized \
60 * away at compile time due to constant propagation and only \
61 * a few multiplication instructions should remain. \
62 * Hence this monstrous macro (static inline doesn't always \
63 * do the trick here). \
65 u64 ___res, ___x, ___t, ___m, ___n = (n); \
68 /* determine MSB of b */ \
69 ___p = 1 << ilog2(___b); \
71 /* compute m = ((p << 64) + b - 1) / b */ \
72 ___m = (~0ULL / ___b) * ___p; \
73 ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
75 /* one less than the dividend with highest result */ \
76 ___x = ~0ULL / ___b * ___b - 1; \
78 /* test our ___m with res = m * x / (p << 64) */ \
79 ___res = ((___m & 0xffffffff) * (___x & 0xffffffff)) >> 32; \
80 ___t = ___res += (___m & 0xffffffff) * (___x >> 32); \
81 ___res += (___x & 0xffffffff) * (___m >> 32); \
82 ___t = (___res < ___t) ? (1ULL << 32) : 0; \
83 ___res = (___res >> 32) + ___t; \
84 ___res += (___m >> 32) * (___x >> 32); \
87 /* Now sanitize and optimize what we've got. */ \
88 if (~0ULL % (___b / (___b & -___b)) == 0) { \
89 /* special case, can be simplified to ... */ \
90 ___n /= (___b & -___b); \
91 ___m = ~0ULL / (___b / (___b & -___b)); \
94 } else if (___res != ___x / ___b) { \
96 * We can't get away without a bias to compensate \
97 * for bit truncation errors. To avoid it we'd need an \
98 * additional bit to represent m which would overflow \
99 * a 64-bit variable. \
101 * Instead we do m = p / b and n / b = (n * m + m) / p. \
104 /* Compute m = (p << 64) / b */ \
105 ___m = (~0ULL / ___b) * ___p; \
106 ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
109 * Reduce m / p, and try to clear bit 31 of m when \
110 * possible, otherwise that'll need extra overflow \
113 u32 ___bits = -(___m & -___m); \
114 ___bits |= ___m >> 32; \
115 ___bits = (~___bits) << 1; \
117 * If ___bits == 0 then setting bit 31 is unavoidable. \
118 * Simply apply the maximum possible reduction in that \
119 * case. Otherwise the MSB of ___bits indicates the \
120 * best reduction we should apply. \
123 ___p /= (___m & -___m); \
124 ___m /= (___m & -___m); \
126 ___p >>= ilog2(___bits); \
127 ___m >>= ilog2(___bits); \
129 /* No bias needed. */ \
134 * Now we have a combination of 2 conditions: \
136 * 1) whether or not we need to apply a bias, and \
138 * 2) whether or not there might be an overflow in the cross \
139 * product determined by (___m & ((1 << 63) | (1 << 31))). \
141 * Select the best way to do (m_bias + m * n) / (1 << 64). \
142 * From now on there will be actual runtime code generated. \
144 ___res = __arch_xprod_64(___m, ___n, ___bias); \
149 #ifndef __arch_xprod_64
151 * Default C implementation for __arch_xprod_64()
153 * Prototype: u64 __arch_xprod_64(const u64 m, u64 n, bool bias)
154 * Semantic: retval = ((bias ? m : 0) + m * n) >> 64
156 * The product is a 128-bit value, scaled down to 64 bits.
157 * Assuming constant propagation to optimize away unused conditional code.
158 * Architectures may provide their own optimized assembly implementation.
160 static inline u64 __arch_xprod_64(const u64 m, u64 n, bool bias)
169 res = ((u64)m_lo * n_lo) >> 32;
170 } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
171 /* there can't be any overflow here */
172 res = (m + (u64)m_lo * n_lo) >> 32;
174 res = m + (u64)m_lo * n_lo;
175 tmp = (res < m) ? (1ULL << 32) : 0;
176 res = (res >> 32) + tmp;
179 if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
180 /* there can't be any overflow here */
181 res += (u64)m_lo * n_hi;
182 res += (u64)m_hi * n_lo;
185 tmp = res += (u64)m_lo * n_hi;
186 res += (u64)m_hi * n_lo;
187 tmp = (res < tmp) ? (1ULL << 32) : 0;
188 res = (res >> 32) + tmp;
191 res += (u64)m_hi * n_hi;
198 extern u32 __div64_32(u64 *dividend, u32 divisor);
201 /* The unnecessary pointer compare is there
202 * to check for type safety (n must be 64bit)
204 # define do_div(n,base) ({ \
205 u32 __base = (base); \
207 (void)(((typeof((n)) *)0) == ((u64 *)0)); \
208 if (__builtin_constant_p(__base) && \
209 is_power_of_2(__base)) { \
210 __rem = (n) & (__base - 1); \
211 (n) >>= ilog2(__base); \
212 } else if (__div64_const32_is_OK && \
213 __builtin_constant_p(__base) && \
215 u32 __res_lo, __n_lo = (n); \
216 (n) = __div64_const32(n, __base); \
217 /* the remainder can be computed with 32-bit regs */ \
219 __rem = __n_lo - __res_lo * __base; \
220 } else if (likely(((n) >> 32) == 0)) { \
221 __rem = (u32)(n) % __base; \
222 (n) = (u32)(n) / __base; \
224 __rem = __div64_32(&(n), __base); \
228 #else /* BITS_PER_LONG == ?? */
230 # error do_div() does not yet support the C64
232 #endif /* BITS_PER_LONG */
234 /* Wrapper for do_div(). Doesn't modify dividend and returns
235 * the result, not remainder.
237 static inline u64 lldiv(u64 dividend, u32 divisor)
239 u64 __res = dividend;
240 do_div(__res, divisor);
244 #endif /* _ASM_GENERIC_DIV64_H */