1/*-
2 * Copyright (c) 2007 Cisco Systems, Inc.  All rights reserved.
3 * Copyright (c) 2014 Mellanox Technologies, Ltd. All rights reserved.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice unmodified, this list of conditions, and the following
11 *    disclaimer.
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.
15 *
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.
26 */
27
28#ifndef _LINUX_MATH64_H
29#define _LINUX_MATH64_H
30
31#include <linux/types.h>
32#include <linux/bitops.h>
33
34#if BITS_PER_LONG == 64
35
36# define do_div(n, base) ({                                    \
37	uint32_t __base = (base);                               \
38	uint32_t __rem;                                         \
39	__rem = ((uint64_t)(n)) % __base;                       \
40	(n) = ((uint64_t)(n)) / __base;                         \
41	__rem;                                                  \
42})
43
44/**
45* div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
46*
47* This is commonly provided by 32bit archs to provide an optimized 64bit
48* divide.
49*/
50static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
51{
52        *remainder = dividend % divisor;
53        return dividend / divisor;
54}
55
56
57#elif BITS_PER_LONG == 32
58
59static uint32_t __div64_32(uint64_t *n, uint32_t base)
60{
61	uint64_t rem = *n;
62	uint64_t b = base;
63	uint64_t res, d = 1;
64	uint32_t high = rem >> 32;
65
66	/* Reduce the thing a bit first */
67	res = 0;
68	if (high >= base) {
69		high /= base;
70		res = (uint64_t) high << 32;
71		rem -= (uint64_t) (high*base) << 32;
72	}
73
74	while ((int64_t)b > 0 && b < rem) {
75		b = b+b;
76		d = d+d;
77	}
78
79	do {
80		if (rem >= b) {
81			rem -= b;
82			res += d;
83		}
84		b >>= 1;
85		d >>= 1;
86	} while (d);
87
88	*n = res;
89	return rem;
90}
91
92# define do_div(n, base) ({                            \
93	uint32_t __base = (base);                       \
94	uint32_t __rem;                                 \
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;           \
99	} else                                          \
100		__rem = __div64_32(&(n), __base);       \
101	__rem;                                          \
102})
103
104#ifndef div_u64_rem
105static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
106{
107        *remainder = do_div(dividend, divisor);
108        return dividend;
109}
110#endif
111
112
113#endif /* BITS_PER_LONG */
114
115
116
117/**
118 ** div_u64 - unsigned 64bit divide with 32bit divisor
119 **
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
122 ** divide.
123 *  */
124#ifndef div_u64
125
126static inline u64 div_u64(u64 dividend, u32 divisor)
127{
128        u32 remainder;
129        return div_u64_rem(dividend, divisor, &remainder);
130}
131#endif
132
133#endif	/* _LINUX_MATH64_H */
134