2 * Copyright (c) 2007 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2014 Mellanox Technologies, Ltd. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice unmodified, this list of conditions, and the following
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #ifndef _LINUX_MATH64_H
29 #define _LINUX_MATH64_H
31 #include <linux/types.h>
32 #include <linux/bitops.h>
34 #if BITS_PER_LONG == 64
36 # define do_div(n, base) ({ \
37 uint32_t __base = (base); \
39 __rem = ((uint64_t)(n)) % __base; \
40 (n) = ((uint64_t)(n)) / __base; \
45 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
47 * This is commonly provided by 32bit archs to provide an optimized 64bit
50 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
52 *remainder = dividend % divisor;
53 return dividend / divisor;
57 #elif BITS_PER_LONG == 32
59 static uint32_t __div64_32(uint64_t *n, uint32_t base)
64 uint32_t high = rem >> 32;
66 /* Reduce the thing a bit first */
70 res = (uint64_t) high << 32;
71 rem -= (uint64_t) (high*base) << 32;
74 while ((int64_t)b > 0 && b < rem) {
92 # define do_div(n, base) ({ \
93 uint32_t __base = (base); \
95 (void)(((typeof((n)) *)0) == ((uint64_t *)0)); \
96 if (likely(((n) >> 32) == 0)) { \
97 __rem = (uint32_t)(n) % __base; \
98 (n) = (uint32_t)(n) / __base; \
100 __rem = __div64_32(&(n), __base); \
105 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
107 *remainder = do_div(dividend, divisor);
113 #endif /* BITS_PER_LONG */
118 ** div_u64 - unsigned 64bit divide with 32bit divisor
120 ** This is the most common 64bit divide and should be used if possible,
121 ** as many 32bit archs can optimize this variant better than a full 64bit
126 static inline u64 div_u64(u64 dividend, u32 divisor)
129 return div_u64_rem(dividend, divisor, &remainder);
133 #endif /* _LINUX_MATH64_H */