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