1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MATH64_H
3 #define _LINUX_MATH64_H
4
5 #include <linux/types.h>
6 #include <linux/math.h>
7 #include <asm/div64.h>
8 #include <vdso/math64.h>
9
10 #if BITS_PER_LONG == 64
11
12 #define div64_long(x, y) div64_s64((x), (y))
13 #define div64_ul(x, y) div64_u64((x), (y))
14
15 /**
16 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
17 * @dividend: unsigned 64bit dividend
18 * @divisor: unsigned 32bit divisor
19 * @remainder: pointer to unsigned 32bit remainder
20 *
21 * Return: sets ``*remainder``, then returns dividend / divisor
22 *
23 * This is commonly provided by 32bit archs to provide an optimized 64bit
24 * divide.
25 */
div_u64_rem(u64 dividend,u32 divisor,u32 * remainder)26 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
27 {
28 *remainder = dividend % divisor;
29 return dividend / divisor;
30 }
31
32 /**
33 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
34 * @dividend: signed 64bit dividend
35 * @divisor: signed 32bit divisor
36 * @remainder: pointer to signed 32bit remainder
37 *
38 * Return: sets ``*remainder``, then returns dividend / divisor
39 */
div_s64_rem(s64 dividend,s32 divisor,s32 * remainder)40 static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
41 {
42 *remainder = dividend % divisor;
43 return dividend / divisor;
44 }
45
46 /**
47 * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
48 * @dividend: unsigned 64bit dividend
49 * @divisor: unsigned 64bit divisor
50 * @remainder: pointer to unsigned 64bit remainder
51 *
52 * Return: sets ``*remainder``, then returns dividend / divisor
53 */
div64_u64_rem(u64 dividend,u64 divisor,u64 * remainder)54 static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
55 {
56 *remainder = dividend % divisor;
57 return dividend / divisor;
58 }
59
60 /**
61 * div64_u64 - unsigned 64bit divide with 64bit divisor
62 * @dividend: unsigned 64bit dividend
63 * @divisor: unsigned 64bit divisor
64 *
65 * Return: dividend / divisor
66 */
div64_u64(u64 dividend,u64 divisor)67 static inline u64 div64_u64(u64 dividend, u64 divisor)
68 {
69 return dividend / divisor;
70 }
71
72 /**
73 * div64_s64 - signed 64bit divide with 64bit divisor
74 * @dividend: signed 64bit dividend
75 * @divisor: signed 64bit divisor
76 *
77 * Return: dividend / divisor
78 */
div64_s64(s64 dividend,s64 divisor)79 static inline s64 div64_s64(s64 dividend, s64 divisor)
80 {
81 return dividend / divisor;
82 }
83
84 #elif BITS_PER_LONG == 32
85
86 #define div64_long(x, y) div_s64((x), (y))
87 #define div64_ul(x, y) div_u64((x), (y))
88
89 #ifndef div_u64_rem
div_u64_rem(u64 dividend,u32 divisor,u32 * remainder)90 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
91 {
92 *remainder = do_div(dividend, divisor);
93 return dividend;
94 }
95 #endif
96
97 #ifndef div_s64_rem
98 extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
99 #endif
100
101 #ifndef div64_u64_rem
102 extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
103 #endif
104
105 #ifndef div64_u64
106 extern u64 div64_u64(u64 dividend, u64 divisor);
107 #endif
108
109 #ifndef div64_s64
110 extern s64 div64_s64(s64 dividend, s64 divisor);
111 #endif
112
113 #endif /* BITS_PER_LONG */
114
115 /**
116 * div_u64 - unsigned 64bit divide with 32bit divisor
117 * @dividend: unsigned 64bit dividend
118 * @divisor: unsigned 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 * Return: dividend / divisor
125 */
126 #ifndef div_u64
div_u64(u64 dividend,u32 divisor)127 static inline u64 div_u64(u64 dividend, u32 divisor)
128 {
129 u32 remainder;
130 return div_u64_rem(dividend, divisor, &remainder);
131 }
132 #endif
133
134 /**
135 * div_s64 - signed 64bit divide with 32bit divisor
136 * @dividend: signed 64bit dividend
137 * @divisor: signed 32bit divisor
138 *
139 * Return: dividend / divisor
140 */
141 #ifndef div_s64
div_s64(s64 dividend,s32 divisor)142 static inline s64 div_s64(s64 dividend, s32 divisor)
143 {
144 s32 remainder;
145 return div_s64_rem(dividend, divisor, &remainder);
146 }
147 #endif
148
149 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
150
151 #ifndef mul_u32_u32
152 /*
153 * Many a GCC version messes this up and generates a 64x64 mult :-(
154 */
mul_u32_u32(u32 a,u32 b)155 static inline u64 mul_u32_u32(u32 a, u32 b)
156 {
157 return (u64)a * b;
158 }
159 #endif
160
161 #ifndef add_u64_u32
162 /*
163 * Many a GCC version also messes this up.
164 * Zero extending b and then spilling everything to stack.
165 */
add_u64_u32(u64 a,u32 b)166 static inline u64 add_u64_u32(u64 a, u32 b)
167 {
168 return a + b;
169 }
170 #endif
171
172 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
173
174 #ifndef mul_u64_u32_shr
mul_u64_u32_shr(u64 a,u32 mul,unsigned int shift)175 static __always_inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
176 {
177 return (u64)(((unsigned __int128)a * mul) >> shift);
178 }
179 #endif /* mul_u64_u32_shr */
180
181 #ifndef mul_u64_u64_shr
mul_u64_u64_shr(u64 a,u64 mul,unsigned int shift)182 static __always_inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
183 {
184 return (u64)(((unsigned __int128)a * mul) >> shift);
185 }
186 #endif /* mul_u64_u64_shr */
187
188 #else
189
190 #ifndef mul_u64_u32_shr
mul_u64_u32_shr(u64 a,u32 mul,unsigned int shift)191 static __always_inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
192 {
193 u32 ah = a >> 32, al = a;
194 u64 ret;
195
196 ret = mul_u32_u32(al, mul) >> shift;
197 if (ah)
198 ret += mul_u32_u32(ah, mul) << (32 - shift);
199 return ret;
200 }
201 #endif /* mul_u64_u32_shr */
202
203 #ifndef mul_u64_u64_shr
mul_u64_u64_shr(u64 a,u64 b,unsigned int shift)204 static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
205 {
206 union {
207 u64 ll;
208 struct {
209 #ifdef __BIG_ENDIAN
210 u32 high, low;
211 #else
212 u32 low, high;
213 #endif
214 } l;
215 } rl, rm, rn, rh, a0, b0;
216 u64 c;
217
218 a0.ll = a;
219 b0.ll = b;
220
221 rl.ll = mul_u32_u32(a0.l.low, b0.l.low);
222 rm.ll = mul_u32_u32(a0.l.low, b0.l.high);
223 rn.ll = mul_u32_u32(a0.l.high, b0.l.low);
224 rh.ll = mul_u32_u32(a0.l.high, b0.l.high);
225
226 /*
227 * Each of these lines computes a 64-bit intermediate result into "c",
228 * starting at bits 32-95. The low 32-bits go into the result of the
229 * multiplication, the high 32-bits are carried into the next step.
230 */
231 rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low;
232 rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
233 rh.l.high = (c >> 32) + rh.l.high;
234
235 /*
236 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
237 * shift it right and throw away the high part of the result.
238 */
239 if (shift == 0)
240 return rl.ll;
241 if (shift < 64)
242 return (rl.ll >> shift) | (rh.ll << (64 - shift));
243 return rh.ll >> (shift & 63);
244 }
245 #endif /* mul_u64_u64_shr */
246
247 #endif
248
249 #ifndef mul_s64_u64_shr
mul_s64_u64_shr(s64 a,u64 b,unsigned int shift)250 static inline u64 mul_s64_u64_shr(s64 a, u64 b, unsigned int shift)
251 {
252 u64 ret;
253
254 /*
255 * Extract the sign before the multiplication and put it back
256 * afterwards if needed.
257 */
258 ret = mul_u64_u64_shr(abs(a), b, shift);
259
260 if (a < 0)
261 ret = -((s64) ret);
262
263 return ret;
264 }
265 #endif /* mul_s64_u64_shr */
266
267 #ifndef mul_u64_u32_div
mul_u64_u32_div(u64 a,u32 mul,u32 divisor)268 static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
269 {
270 union {
271 u64 ll;
272 struct {
273 #ifdef __BIG_ENDIAN
274 u32 high, low;
275 #else
276 u32 low, high;
277 #endif
278 } l;
279 } u, rl, rh;
280
281 u.ll = a;
282 rl.ll = mul_u32_u32(u.l.low, mul);
283 rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high;
284
285 /* Bits 32-63 of the result will be in rh.l.low. */
286 rl.l.high = do_div(rh.ll, divisor);
287
288 /* Bits 0-31 of the result will be in rl.l.low. */
289 do_div(rl.ll, divisor);
290
291 rl.l.high = rh.l.low;
292 return rl.ll;
293 }
294 #endif /* mul_u64_u32_div */
295
296 /**
297 * mul_u64_add_u64_div_u64 - unsigned 64bit multiply, add, and divide
298 * @a: first unsigned 64bit multiplicand
299 * @b: second unsigned 64bit multiplicand
300 * @c: unsigned 64bit addend
301 * @d: unsigned 64bit divisor
302 *
303 * Multiply two 64bit values together to generate a 128bit product
304 * add a third value and then divide by a fourth.
305 * The Generic code divides by 0 if @d is zero and returns ~0 on overflow.
306 * Architecture specific code may trap on zero or overflow.
307 *
308 * Return: (@a * @b + @c) / @d
309 */
310 u64 mul_u64_add_u64_div_u64(u64 a, u64 b, u64 c, u64 d);
311
312 /**
313 * mul_u64_u64_div_u64 - unsigned 64bit multiply and divide
314 * @a: first unsigned 64bit multiplicand
315 * @b: second unsigned 64bit multiplicand
316 * @d: unsigned 64bit divisor
317 *
318 * Multiply two 64bit values together to generate a 128bit product
319 * and then divide by a third value.
320 * The Generic code divides by 0 if @d is zero and returns ~0 on overflow.
321 * Architecture specific code may trap on zero or overflow.
322 *
323 * Return: @a * @b / @d
324 */
325 #define mul_u64_u64_div_u64(a, b, d) mul_u64_add_u64_div_u64(a, b, 0, d)
326
327 /**
328 * mul_u64_u64_div_u64_roundup - unsigned 64bit multiply and divide rounded up
329 * @a: first unsigned 64bit multiplicand
330 * @b: second unsigned 64bit multiplicand
331 * @d: unsigned 64bit divisor
332 *
333 * Multiply two 64bit values together to generate a 128bit product
334 * and then divide and round up.
335 * The Generic code divides by 0 if @d is zero and returns ~0 on overflow.
336 * Architecture specific code may trap on zero or overflow.
337 *
338 * Return: (@a * @b + @d - 1) / @d
339 */
340 #define mul_u64_u64_div_u64_roundup(a, b, d) \
341 ({ u64 _tmp = (d); mul_u64_add_u64_div_u64(a, b, _tmp - 1, _tmp); })
342
343
344 /**
345 * DIV64_U64_ROUND_UP - unsigned 64bit divide with 64bit divisor rounded up
346 * @ll: unsigned 64bit dividend
347 * @d: unsigned 64bit divisor
348 *
349 * Divide unsigned 64bit dividend by unsigned 64bit divisor
350 * and round up.
351 *
352 * Return: dividend / divisor rounded up
353 */
354 #define DIV64_U64_ROUND_UP(ll, d) \
355 ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); })
356
357 /**
358 * DIV_U64_ROUND_UP - unsigned 64bit divide with 32bit divisor rounded up
359 * @ll: unsigned 64bit dividend
360 * @d: unsigned 32bit divisor
361 *
362 * Divide unsigned 64bit dividend by unsigned 32bit divisor
363 * and round up.
364 *
365 * Return: dividend / divisor rounded up
366 */
367 #define DIV_U64_ROUND_UP(ll, d) \
368 ({ u32 _tmp = (d); div_u64((ll) + _tmp - 1, _tmp); })
369
370 /**
371 * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer
372 * @dividend: unsigned 64bit dividend
373 * @divisor: unsigned 64bit divisor
374 *
375 * Divide unsigned 64bit dividend by unsigned 64bit divisor
376 * and round to closest integer.
377 *
378 * Return: dividend / divisor rounded to nearest integer
379 */
380 #define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \
381 ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); })
382
383 /**
384 * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer
385 * @dividend: unsigned 64bit dividend
386 * @divisor: unsigned 32bit divisor
387 *
388 * Divide unsigned 64bit dividend by unsigned 32bit divisor
389 * and round to closest integer.
390 *
391 * Return: dividend / divisor rounded to nearest integer
392 */
393 #define DIV_U64_ROUND_CLOSEST(dividend, divisor) \
394 ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); })
395
396 /**
397 * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer
398 * @dividend: signed 64bit dividend
399 * @divisor: signed 32bit divisor
400 *
401 * Divide signed 64bit dividend by signed 32bit divisor
402 * and round to closest integer.
403 *
404 * Return: dividend / divisor rounded to nearest integer
405 */
406 #define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \
407 { \
408 s64 __x = (dividend); \
409 s32 __d = (divisor); \
410 ((__x > 0) == (__d > 0)) ? \
411 div_s64((__x + (__d / 2)), __d) : \
412 div_s64((__x - (__d / 2)), __d); \
413 } \
414 )
415
416 /**
417 * roundup_u64 - Round up a 64bit value to the next specified 32bit multiple
418 * @x: the value to up
419 * @y: 32bit multiple to round up to
420 *
421 * Rounds @x to the next multiple of @y. For 32bit @x values, see roundup and
422 * the faster round_up() for powers of 2.
423 *
424 * Return: rounded up value.
425 */
roundup_u64(u64 x,u32 y)426 static inline u64 roundup_u64(u64 x, u32 y)
427 {
428 return DIV_U64_ROUND_UP(x, y) * y;
429 }
430 #endif /* _LINUX_MATH64_H */
431