1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MATH64_H
3 #define _LINUX_MATH64_H
5 #include <linux/types.h>
6 #include <linux/math.h>
7 #include <vdso/math64.h>
10 #if BITS_PER_LONG == 64
12 #define div64_long(x, y) div64_s64((x), (y))
13 #define div64_ul(x, y) div64_u64((x), (y))
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
21 * Return: sets ``*remainder``, then returns dividend / divisor
23 * This is commonly provided by 32bit archs to provide an optimized 64bit
26 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
28 *remainder = dividend % divisor;
29 return dividend / divisor;
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
38 * Return: sets ``*remainder``, then returns dividend / divisor
40 static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
42 *remainder = dividend % divisor;
43 return dividend / divisor;
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
52 * Return: sets ``*remainder``, then returns dividend / divisor
54 static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
56 *remainder = dividend % divisor;
57 return dividend / divisor;
61 * div64_u64 - unsigned 64bit divide with 64bit divisor
62 * @dividend: unsigned 64bit dividend
63 * @divisor: unsigned 64bit divisor
65 * Return: dividend / divisor
67 static inline u64 div64_u64(u64 dividend, u64 divisor)
69 return dividend / divisor;
73 * div64_s64 - signed 64bit divide with 64bit divisor
74 * @dividend: signed 64bit dividend
75 * @divisor: signed 64bit divisor
77 * Return: dividend / divisor
79 static inline s64 div64_s64(s64 dividend, s64 divisor)
81 return dividend / divisor;
84 #elif BITS_PER_LONG == 32
86 #define div64_long(x, y) div_s64((x), (y))
87 #define div64_ul(x, y) div_u64((x), (y))
90 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
92 *remainder = do_div(dividend, divisor);
98 extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
101 #ifndef div64_u64_rem
102 extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
106 extern u64 div64_u64(u64 dividend, u64 divisor);
110 extern s64 div64_s64(s64 dividend, s64 divisor);
113 #endif /* BITS_PER_LONG */
116 * div_u64 - unsigned 64bit divide with 32bit divisor
117 * @dividend: unsigned 64bit dividend
118 * @divisor: unsigned 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
125 static inline u64 div_u64(u64 dividend, u32 divisor)
128 return div_u64_rem(dividend, divisor, &remainder);
133 * div_s64 - signed 64bit divide with 32bit divisor
134 * @dividend: signed 64bit dividend
135 * @divisor: signed 32bit divisor
138 static inline s64 div_s64(s64 dividend, s32 divisor)
141 return div_s64_rem(dividend, divisor, &remainder);
145 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
149 * Many a GCC version messes this up and generates a 64x64 mult :-(
151 static inline u64 mul_u32_u32(u32 a, u32 b)
157 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
159 #ifndef mul_u64_u32_shr
160 static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
162 return (u64)(((unsigned __int128)a * mul) >> shift);
164 #endif /* mul_u64_u32_shr */
166 #ifndef mul_u64_u64_shr
167 static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
169 return (u64)(((unsigned __int128)a * mul) >> shift);
171 #endif /* mul_u64_u64_shr */
175 #ifndef mul_u64_u32_shr
176 static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
184 ret = mul_u32_u32(al, mul) >> shift;
186 ret += mul_u32_u32(ah, mul) << (32 - shift);
190 #endif /* mul_u64_u32_shr */
192 #ifndef mul_u64_u64_shr
193 static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
204 } rl, rm, rn, rh, a0, b0;
210 rl.ll = mul_u32_u32(a0.l.low, b0.l.low);
211 rm.ll = mul_u32_u32(a0.l.low, b0.l.high);
212 rn.ll = mul_u32_u32(a0.l.high, b0.l.low);
213 rh.ll = mul_u32_u32(a0.l.high, b0.l.high);
216 * Each of these lines computes a 64-bit intermediate result into "c",
217 * starting at bits 32-95. The low 32-bits go into the result of the
218 * multiplication, the high 32-bits are carried into the next step.
220 rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low;
221 rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
222 rh.l.high = (c >> 32) + rh.l.high;
225 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
226 * shift it right and throw away the high part of the result.
231 return (rl.ll >> shift) | (rh.ll << (64 - shift));
232 return rh.ll >> (shift & 63);
234 #endif /* mul_u64_u64_shr */
238 #ifndef mul_s64_u64_shr
239 static inline u64 mul_s64_u64_shr(s64 a, u64 b, unsigned int shift)
244 * Extract the sign before the multiplication and put it back
245 * afterwards if needed.
247 ret = mul_u64_u64_shr(abs(a), b, shift);
254 #endif /* mul_s64_u64_shr */
256 #ifndef mul_u64_u32_div
257 static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
271 rl.ll = mul_u32_u32(u.l.low, mul);
272 rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high;
274 /* Bits 32-63 of the result will be in rh.l.low. */
275 rl.l.high = do_div(rh.ll, divisor);
277 /* Bits 0-31 of the result will be in rl.l.low. */
278 do_div(rl.ll, divisor);
280 rl.l.high = rh.l.low;
283 #endif /* mul_u64_u32_div */
285 u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div);
287 #define DIV64_U64_ROUND_UP(ll, d) \
288 ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); })
291 * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer
292 * @dividend: unsigned 64bit dividend
293 * @divisor: unsigned 64bit divisor
295 * Divide unsigned 64bit dividend by unsigned 64bit divisor
296 * and round to closest integer.
298 * Return: dividend / divisor rounded to nearest integer
300 #define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \
301 ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); })
304 * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer
305 * @dividend: signed 64bit dividend
306 * @divisor: signed 32bit divisor
308 * Divide signed 64bit dividend by signed 32bit divisor
309 * and round to closest integer.
311 * Return: dividend / divisor rounded to nearest integer
313 #define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \
315 s64 __x = (dividend); \
316 s32 __d = (divisor); \
317 ((__x > 0) == (__d > 0)) ? \
318 div_s64((__x + (__d / 2)), __d) : \
319 div_s64((__x - (__d / 2)), __d); \
322 #endif /* _LINUX_MATH64_H */