]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - include/linux/math64.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_MATH64_H
3 #define _LINUX_MATH64_H
5 #include <linux/types.h>
6 #include <vdso/math64.h>
9 #if BITS_PER_LONG == 64
11 #define div64_long(x, y) div64_s64((x), (y))
12 #define div64_ul(x, y) div64_u64((x), (y))
15 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
16 * @dividend: unsigned 64bit dividend
17 * @divisor: unsigned 32bit divisor
18 * @remainder: pointer to unsigned 32bit remainder
20 * Return: sets ``*remainder``, then returns dividend / divisor
22 * This is commonly provided by 32bit archs to provide an optimized 64bit
25 static inline u64
div_u64_rem(u64 dividend
, u32 divisor
, u32
*remainder
)
27 *remainder
= dividend
% divisor
;
28 return dividend
/ divisor
;
32 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
33 * @dividend: signed 64bit dividend
34 * @divisor: signed 32bit divisor
35 * @remainder: pointer to signed 32bit remainder
37 * Return: sets ``*remainder``, then returns dividend / divisor
39 static inline s64
div_s64_rem(s64 dividend
, s32 divisor
, s32
*remainder
)
41 *remainder
= dividend
% divisor
;
42 return dividend
/ divisor
;
46 * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
47 * @dividend: unsigned 64bit dividend
48 * @divisor: unsigned 64bit divisor
49 * @remainder: pointer to unsigned 64bit remainder
51 * Return: sets ``*remainder``, then returns dividend / divisor
53 static inline u64
div64_u64_rem(u64 dividend
, u64 divisor
, u64
*remainder
)
55 *remainder
= dividend
% divisor
;
56 return dividend
/ divisor
;
60 * div64_u64 - unsigned 64bit divide with 64bit divisor
61 * @dividend: unsigned 64bit dividend
62 * @divisor: unsigned 64bit divisor
64 * Return: dividend / divisor
66 static inline u64
div64_u64(u64 dividend
, u64 divisor
)
68 return dividend
/ divisor
;
72 * div64_s64 - signed 64bit divide with 64bit divisor
73 * @dividend: signed 64bit dividend
74 * @divisor: signed 64bit divisor
76 * Return: dividend / divisor
78 static inline s64
div64_s64(s64 dividend
, s64 divisor
)
80 return dividend
/ divisor
;
83 #elif BITS_PER_LONG == 32
85 #define div64_long(x, y) div_s64((x), (y))
86 #define div64_ul(x, y) div_u64((x), (y))
89 static inline u64
div_u64_rem(u64 dividend
, u32 divisor
, u32
*remainder
)
91 *remainder
= do_div(dividend
, divisor
);
97 extern s64
div_s64_rem(s64 dividend
, s32 divisor
, s32
*remainder
);
100 #ifndef div64_u64_rem
101 extern u64
div64_u64_rem(u64 dividend
, u64 divisor
, u64
*remainder
);
105 extern u64
div64_u64(u64 dividend
, u64 divisor
);
109 extern s64
div64_s64(s64 dividend
, s64 divisor
);
112 #endif /* BITS_PER_LONG */
115 * div_u64 - unsigned 64bit divide with 32bit divisor
116 * @dividend: unsigned 64bit dividend
117 * @divisor: unsigned 32bit divisor
119 * This is the most common 64bit divide and should be used if possible,
120 * as many 32bit archs can optimize this variant better than a full 64bit
124 static inline u64
div_u64(u64 dividend
, u32 divisor
)
127 return div_u64_rem(dividend
, divisor
, &remainder
);
132 * div_s64 - signed 64bit divide with 32bit divisor
133 * @dividend: signed 64bit dividend
134 * @divisor: signed 32bit divisor
137 static inline s64
div_s64(s64 dividend
, s32 divisor
)
140 return div_s64_rem(dividend
, divisor
, &remainder
);
144 u32
iter_div_u64_rem(u64 dividend
, u32 divisor
, u64
*remainder
);
148 * Many a GCC version messes this up and generates a 64x64 mult :-(
150 static inline u64
mul_u32_u32(u32 a
, u32 b
)
156 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
158 #ifndef mul_u64_u32_shr
159 static inline u64
mul_u64_u32_shr(u64 a
, u32 mul
, unsigned int shift
)
161 return (u64
)(((unsigned __int128
)a
* mul
) >> shift
);
163 #endif /* mul_u64_u32_shr */
165 #ifndef mul_u64_u64_shr
166 static inline u64
mul_u64_u64_shr(u64 a
, u64 mul
, unsigned int shift
)
168 return (u64
)(((unsigned __int128
)a
* mul
) >> shift
);
170 #endif /* mul_u64_u64_shr */
174 #ifndef mul_u64_u32_shr
175 static inline u64
mul_u64_u32_shr(u64 a
, u32 mul
, unsigned int shift
)
183 ret
= mul_u32_u32(al
, mul
) >> shift
;
185 ret
+= mul_u32_u32(ah
, mul
) << (32 - shift
);
189 #endif /* mul_u64_u32_shr */
191 #ifndef mul_u64_u64_shr
192 static inline u64
mul_u64_u64_shr(u64 a
, u64 b
, unsigned int shift
)
203 } rl
, rm
, rn
, rh
, a0
, b0
;
209 rl
.ll
= mul_u32_u32(a0
.l
.low
, b0
.l
.low
);
210 rm
.ll
= mul_u32_u32(a0
.l
.low
, b0
.l
.high
);
211 rn
.ll
= mul_u32_u32(a0
.l
.high
, b0
.l
.low
);
212 rh
.ll
= mul_u32_u32(a0
.l
.high
, b0
.l
.high
);
215 * Each of these lines computes a 64-bit intermediate result into "c",
216 * starting at bits 32-95. The low 32-bits go into the result of the
217 * multiplication, the high 32-bits are carried into the next step.
219 rl
.l
.high
= c
= (u64
)rl
.l
.high
+ rm
.l
.low
+ rn
.l
.low
;
220 rh
.l
.low
= c
= (c
>> 32) + rm
.l
.high
+ rn
.l
.high
+ rh
.l
.low
;
221 rh
.l
.high
= (c
>> 32) + rh
.l
.high
;
224 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
225 * shift it right and throw away the high part of the result.
230 return (rl
.ll
>> shift
) | (rh
.ll
<< (64 - shift
));
231 return rh
.ll
>> (shift
& 63);
233 #endif /* mul_u64_u64_shr */
237 #ifndef mul_u64_u32_div
238 static inline u64
mul_u64_u32_div(u64 a
, u32 mul
, u32 divisor
)
252 rl
.ll
= mul_u32_u32(u
.l
.low
, mul
);
253 rh
.ll
= mul_u32_u32(u
.l
.high
, mul
) + rl
.l
.high
;
255 /* Bits 32-63 of the result will be in rh.l.low. */
256 rl
.l
.high
= do_div(rh
.ll
, divisor
);
258 /* Bits 0-31 of the result will be in rl.l.low. */
259 do_div(rl
.ll
, divisor
);
261 rl
.l
.high
= rh
.l
.low
;
264 #endif /* mul_u64_u32_div */
266 u64
mul_u64_u64_div_u64(u64 a
, u64 mul
, u64 div
);
268 #define DIV64_U64_ROUND_UP(ll, d) \
269 ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); })
272 * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer
273 * @dividend: unsigned 64bit dividend
274 * @divisor: unsigned 64bit divisor
276 * Divide unsigned 64bit dividend by unsigned 64bit divisor
277 * and round to closest integer.
279 * Return: dividend / divisor rounded to nearest integer
281 #define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \
282 ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); })
285 * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer
286 * @dividend: signed 64bit dividend
287 * @divisor: signed 32bit divisor
289 * Divide signed 64bit dividend by signed 32bit divisor
290 * and round to closest integer.
292 * Return: dividend / divisor rounded to nearest integer
294 #define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \
296 s64 __x = (dividend); \
297 s32 __d = (divisor); \
298 ((__x > 0) == (__d > 0)) ? \
299 div_s64((__x + (__d / 2)), __d) : \
300 div_s64((__x - (__d / 2)), __d); \
303 #endif /* _LINUX_MATH64_H */