1#ifndef _ASM_GENERIC_DIV64_H
2#define _ASM_GENERIC_DIV64_H
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23#include <linux/types.h>
24#include <linux/compiler.h>
25
26#if BITS_PER_LONG == 64
27
28# define do_div(n,base) ({ \
29 uint32_t __base = (base); \
30 uint32_t __rem; \
31 __rem = ((uint64_t)(n)) % __base; \
32 (n) = ((uint64_t)(n)) / __base; \
33 __rem; \
34 })
35
36#elif BITS_PER_LONG == 32
37
38#include <linux/log2.h>
39
40
41
42
43
44
45
46
47
48
49
50#ifndef __div64_const32_is_OK
51#define __div64_const32_is_OK (__GNUC__ >= 4)
52#endif
53
54#define __div64_const32(n, ___b) \
55({ \
56
57
58
59
60
61
62
63
64 \
65 uint64_t ___res, ___x, ___t, ___m, ___n = (n); \
66 uint32_t ___p, ___bias; \
67 \
68 \
69 ___p = 1 << ilog2(___b); \
70 \
71 \
72 ___m = (~0ULL / ___b) * ___p; \
73 ___m += (((~0ULL % ___b + 1) * ___p) + ___b - 1) / ___b; \
74 \
75 \
76 ___x = ~0ULL / ___b * ___b - 1; \
77 \
78 \
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); \
85 ___res /= ___p; \
86 \
87 \
88 if (~0ULL % (___b / (___b & -___b)) == 0) { \
89 \
90 ___n /= (___b & -___b); \
91 ___m = ~0ULL / (___b / (___b & -___b)); \
92 ___p = 1; \
93 ___bias = 1; \
94 } else if (___res != ___x / ___b) { \
95
96
97
98
99
100
101
102 \
103 ___bias = 1; \
104 \
105 ___m = (~0ULL / ___b) * ___p; \
106 ___m += ((~0ULL % ___b + 1) * ___p) / ___b; \
107 } else { \
108
109
110
111
112 \
113 uint32_t ___bits = -(___m & -___m); \
114 ___bits |= ___m >> 32; \
115 ___bits = (~___bits) << 1; \
116
117
118
119
120
121 \
122 if (!___bits) { \
123 ___p /= (___m & -___m); \
124 ___m /= (___m & -___m); \
125 } else { \
126 ___p >>= ilog2(___bits); \
127 ___m >>= ilog2(___bits); \
128 } \
129 \
130 ___bias = 0; \
131 } \
132 \
133
134
135
136
137
138
139
140
141
142
143 \
144 ___res = __arch_xprod_64(___m, ___n, ___bias); \
145 \
146 ___res /= ___p; \
147})
148
149#ifndef __arch_xprod_64
150
151
152
153
154
155
156
157
158
159
160static inline uint64_t __arch_xprod_64(const uint64_t m, uint64_t n, bool bias)
161{
162 uint32_t m_lo = m;
163 uint32_t m_hi = m >> 32;
164 uint32_t n_lo = n;
165 uint32_t n_hi = n >> 32;
166 uint64_t res, tmp;
167
168 if (!bias) {
169 res = ((uint64_t)m_lo * n_lo) >> 32;
170 } else if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
171
172 res = (m + (uint64_t)m_lo * n_lo) >> 32;
173 } else {
174 res = m + (uint64_t)m_lo * n_lo;
175 tmp = (res < m) ? (1ULL << 32) : 0;
176 res = (res >> 32) + tmp;
177 }
178
179 if (!(m & ((1ULL << 63) | (1ULL << 31)))) {
180
181 res += (uint64_t)m_lo * n_hi;
182 res += (uint64_t)m_hi * n_lo;
183 res >>= 32;
184 } else {
185 tmp = res += (uint64_t)m_lo * n_hi;
186 res += (uint64_t)m_hi * n_lo;
187 tmp = (res < tmp) ? (1ULL << 32) : 0;
188 res = (res >> 32) + tmp;
189 }
190
191 res += (uint64_t)m_hi * n_hi;
192
193 return res;
194}
195#endif
196
197#ifndef __div64_32
198extern uint32_t __div64_32(uint64_t *dividend, uint32_t divisor);
199#endif
200
201
202
203
204# define do_div(n,base) ({ \
205 uint32_t __base = (base); \
206 uint32_t __rem; \
207 (void)(((typeof((n)) *)0) == ((uint64_t *)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) && \
214 __base != 0) { \
215 uint32_t __res_lo, __n_lo = (n); \
216 (n) = __div64_const32(n, __base); \
217 \
218 __res_lo = (n); \
219 __rem = __n_lo - __res_lo * __base; \
220 } else if (likely(((n) >> 32) == 0)) { \
221 __rem = (uint32_t)(n) % __base; \
222 (n) = (uint32_t)(n) / __base; \
223 } else \
224 __rem = __div64_32(&(n), __base); \
225 __rem; \
226 })
227
228#else
229
230# error do_div() does not yet support the C64
231
232#endif
233
234
235
236
237static inline uint64_t lldiv(uint64_t dividend, uint32_t divisor)
238{
239 uint64_t __res = dividend;
240 do_div(__res, divisor);
241 return(__res);
242}
243
244#endif
245