1 /* Copyright (C) 1991-2016, the Linux Kernel authors
3 * This source code is licensed under the GNU General Public License
4 * Version 2. See the file COPYING for more details.
10 #include <arch/div64.h>
12 #if BITS_PER_LONG == 64
14 #define div64_long(x, y) div64_s64((x), (y))
15 #define div64_ul(x, y) div64_u64((x), (y))
18 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
20 * This is commonly provided by 32bit archs to provide an optimized 64bit
23 static inline uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor,
26 *remainder = dividend % divisor;
27 return dividend / divisor;
31 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
33 static inline int64_t div_s64_rem(int64_t dividend, int32_t divisor,
36 *remainder = dividend % divisor;
37 return dividend / divisor;
41 * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
43 static inline uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor,
46 *remainder = dividend % divisor;
47 return dividend / divisor;
51 * div64_u64 - unsigned 64bit divide with 64bit divisor
53 static inline uint64_t div64_u64(uint64_t dividend, uint64_t divisor)
55 return dividend / divisor;
59 * div64_s64 - signed 64bit divide with 64bit divisor
61 static inline int64_t div64_s64(int64_t dividend, int64_t divisor)
63 return dividend / divisor;
66 #elif BITS_PER_LONG == 32
68 #define div64_long(x, y) div_s64((x), (y))
69 #define div64_ul(x, y) div_u64((x), (y))
72 static inline uint64_t div_u64_rem(uint64_t dividend, uint32_t divisor,
75 *remainder = do_div(dividend, divisor);
81 extern int64_t div_s64_rem(int64_t dividend, int32_t divisor,
86 extern uint64_t div64_u64_rem(uint64_t dividend, uint64_t divisor,
91 extern uint64_t div64_u64(uint64_t dividend, uint64_t divisor);
95 extern int64_t div64_s64(int64_t dividend, int64_t divisor);
98 #endif /* BITS_PER_LONG */
101 * div_u64 - unsigned 64bit divide with 32bit divisor
103 * This is the most common 64bit divide and should be used if possible,
104 * as many 32bit archs can optimize this variant better than a full 64bit
108 static inline uint64_t div_u64(uint64_t dividend, uint32_t divisor)
111 return div_u64_rem(dividend, divisor, &remainder);
116 * div_s64 - signed 64bit divide with 32bit divisor
119 static inline int64_t div_s64(int64_t dividend, int32_t divisor)
122 return div_s64_rem(dividend, divisor, &remainder);
126 uint32_t iter_div_u64_rem(uint64_t dividend, uint32_t divisor,
127 uint64_t *remainder);
129 static __always_inline uint32_t
130 __iter_div_u64_rem(uint64_t dividend, uint32_t divisor, uint64_t *remainder)
134 while (dividend >= divisor) {
135 /* The following asm() prevents the compiler from
136 optimising this loop into a modulo operation. */
137 asm("" : "+rm"(dividend));
143 *remainder = dividend;
150 * Many a GCC version messes this up and generates a 64x64 mult :-(
152 static inline uint64_t mul_u32_u32(uint32_t a, uint32_t b)
154 return (uint64_t)a * b;
158 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
160 #ifndef mul_u64_u32_shr
161 static inline uint64_t mul_u64_u32_shr(uint64_t a, uint32_t mul,
164 return (uint64_t)(((unsigned __int128)a * mul) >> shift);
166 #endif /* mul_u64_u32_shr */
168 #ifndef mul_u64_u64_shr
169 static inline uint64_t mul_u64_u64_shr(uint64_t a, uint64_t mul,
172 return (uint64_t)(((unsigned __int128)a * mul) >> shift);
174 #endif /* mul_u64_u64_shr */
178 #ifndef mul_u64_u32_shr
179 static inline uint64_t mul_u64_u32_shr(uint64_t a, uint32_t mul,
188 ret = mul_u32_u32(al, mul) >> shift;
190 ret += mul_u32_u32(ah, mul) << (32 - shift);
194 #endif /* mul_u64_u32_shr */
196 #ifndef mul_u64_u64_shr
197 static inline uint64_t mul_u64_u64_shr(uint64_t a, uint64_t b,
209 } rl, rm, rn, rh, a0, b0;
215 rl.ll = mul_u32_u32(a0.l.low, b0.l.low);
216 rm.ll = mul_u32_u32(a0.l.low, b0.l.high);
217 rn.ll = mul_u32_u32(a0.l.high, b0.l.low);
218 rh.ll = mul_u32_u32(a0.l.high, b0.l.high);
221 * Each of these lines computes a 64-bit intermediate result into "c",
222 * starting at bits 32-95. The low 32-bits go into the result of the
223 * multiplication, the high 32-bits are carried into the next step.
225 rl.l.high = c = (uint64_t)rl.l.high + rm.l.low + rn.l.low;
226 rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
227 rh.l.high = (c >> 32) + rh.l.high;
230 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
231 * shift it right and throw away the high part of the result.
236 return (rl.ll >> shift) | (rh.ll << (64 - shift));
237 return rh.ll >> (shift & 63);
239 #endif /* mul_u64_u64_shr */
243 #ifndef mul_u64_u32_div
244 static inline uint64_t mul_u64_u32_div(uint64_t a, uint32_t mul,
259 rl.ll = mul_u32_u32(u.l.low, mul);
260 rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high;
262 /* Bits 32-63 of the result will be in rh.l.low. */
263 rl.l.high = do_div(rh.ll, divisor);
265 /* Bits 0-31 of the result will be in rl.l.low. */
266 do_div(rl.ll, divisor);
268 rl.l.high = rh.l.low;
271 #endif /* mul_u64_u32_div */