Merge tag 'nfsd-6.12' of git://git.kernel.org/pub/scm/linux/kernel/git/cel/linux
[linux-2.6-block.git] / include / linux / math64.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
2418f4f2
RZ
2#ifndef _LINUX_MATH64_H
3#define _LINUX_MATH64_H
4
5#include <linux/types.h>
605a140a 6#include <linux/math.h>
2418f4f2 7#include <asm/div64.h>
1beb35ec 8#include <vdso/math64.h>
2418f4f2
RZ
9
10#if BITS_PER_LONG == 64
11
c2853c8d
AS
12#define div64_long(x, y) div64_s64((x), (y))
13#define div64_ul(x, y) div64_u64((x), (y))
f910381a 14
2418f4f2
RZ
15/**
16 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
078843f7
RD
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
2418f4f2
RZ
22 *
23 * This is commonly provided by 32bit archs to provide an optimized 64bit
24 * divide.
25 */
26static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
27{
28 *remainder = dividend % divisor;
29 return dividend / divisor;
30}
31
d28a1de5 32/**
2418f4f2 33 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
078843f7
RD
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
2418f4f2
RZ
39 */
40static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
41{
42 *remainder = dividend % divisor;
43 return dividend / divisor;
44}
45
d28a1de5 46/**
eb18cba7 47 * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
078843f7
RD
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
eb18cba7
MS
53 */
54static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
55{
56 *remainder = dividend % divisor;
57 return dividend / divisor;
58}
59
d28a1de5 60/**
6f6d6a1a 61 * div64_u64 - unsigned 64bit divide with 64bit divisor
078843f7
RD
62 * @dividend: unsigned 64bit dividend
63 * @divisor: unsigned 64bit divisor
64 *
65 * Return: dividend / divisor
6f6d6a1a
RZ
66 */
67static inline u64 div64_u64(u64 dividend, u64 divisor)
68{
69 return dividend / divisor;
70}
71
d28a1de5 72/**
658716d1 73 * div64_s64 - signed 64bit divide with 64bit divisor
078843f7
RD
74 * @dividend: signed 64bit dividend
75 * @divisor: signed 64bit divisor
76 *
77 * Return: dividend / divisor
658716d1
BB
78 */
79static inline s64 div64_s64(s64 dividend, s64 divisor)
80{
81 return dividend / divisor;
82}
83
2418f4f2
RZ
84#elif BITS_PER_LONG == 32
85
c2853c8d
AS
86#define div64_long(x, y) div_s64((x), (y))
87#define div64_ul(x, y) div_u64((x), (y))
f910381a 88
2418f4f2
RZ
89#ifndef div_u64_rem
90static 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
98extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
99#endif
100
eb18cba7
MS
101#ifndef div64_u64_rem
102extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
103#endif
104
6f6d6a1a 105#ifndef div64_u64
f3002134 106extern u64 div64_u64(u64 dividend, u64 divisor);
6f6d6a1a
RZ
107#endif
108
658716d1
BB
109#ifndef div64_s64
110extern s64 div64_s64(s64 dividend, s64 divisor);
111#endif
112
2418f4f2
RZ
113#endif /* BITS_PER_LONG */
114
115/**
116 * div_u64 - unsigned 64bit divide with 32bit divisor
078843f7
RD
117 * @dividend: unsigned 64bit dividend
118 * @divisor: unsigned 32bit divisor
2418f4f2
RZ
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.
a898db21
LB
123 *
124 * Return: dividend / divisor
2418f4f2
RZ
125 */
126#ifndef div_u64
127static 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
078843f7
RD
136 * @dividend: signed 64bit dividend
137 * @divisor: signed 32bit divisor
a898db21
LB
138 *
139 * Return: dividend / divisor
2418f4f2
RZ
140 */
141#ifndef div_s64
142static inline s64 div_s64(s64 dividend, s32 divisor)
143{
144 s32 remainder;
145 return div_s64_rem(dividend, divisor, &remainder);
146}
147#endif
148
f595ec96
JF
149u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
150
9e3d6223
PZ
151#ifndef mul_u32_u32
152/*
153 * Many a GCC version messes this up and generates a 64x64 mult :-(
154 */
155static inline u64 mul_u32_u32(u32 a, u32 b)
156{
157 return (u64)a * b;
158}
159#endif
160
be5e610c
PZ
161#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
162
163#ifndef mul_u64_u32_shr
8739c681 164static __always_inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
be5e610c
PZ
165{
166 return (u64)(((unsigned __int128)a * mul) >> shift);
167}
168#endif /* mul_u64_u32_shr */
169
35181e86 170#ifndef mul_u64_u64_shr
fc4a0db4 171static __always_inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
35181e86
HZ
172{
173 return (u64)(((unsigned __int128)a * mul) >> shift);
174}
175#endif /* mul_u64_u64_shr */
176
be5e610c
PZ
177#else
178
179#ifndef mul_u64_u32_shr
8739c681 180static __always_inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
be5e610c 181{
5e5e5142 182 u32 ah = a >> 32, al = a;
be5e610c
PZ
183 u64 ret;
184
9e3d6223 185 ret = mul_u32_u32(al, mul) >> shift;
be5e610c 186 if (ah)
9e3d6223 187 ret += mul_u32_u32(ah, mul) << (32 - shift);
be5e610c
PZ
188 return ret;
189}
190#endif /* mul_u64_u32_shr */
191
35181e86
HZ
192#ifndef mul_u64_u64_shr
193static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
194{
195 union {
196 u64 ll;
197 struct {
198#ifdef __BIG_ENDIAN
199 u32 high, low;
200#else
201 u32 low, high;
202#endif
203 } l;
204 } rl, rm, rn, rh, a0, b0;
205 u64 c;
206
207 a0.ll = a;
208 b0.ll = b;
209
9e3d6223
PZ
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);
35181e86
HZ
214
215 /*
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.
219 */
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;
223
224 /*
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.
227 */
228 if (shift == 0)
229 return rl.ll;
230 if (shift < 64)
231 return (rl.ll >> shift) | (rh.ll << (64 - shift));
232 return rh.ll >> (shift & 63);
233}
234#endif /* mul_u64_u64_shr */
235
be5e610c
PZ
236#endif
237
605a140a
IS
238#ifndef mul_s64_u64_shr
239static inline u64 mul_s64_u64_shr(s64 a, u64 b, unsigned int shift)
240{
241 u64 ret;
242
243 /*
244 * Extract the sign before the multiplication and put it back
245 * afterwards if needed.
246 */
247 ret = mul_u64_u64_shr(abs(a), b, shift);
248
249 if (a < 0)
250 ret = -((s64) ret);
251
252 return ret;
253}
254#endif /* mul_s64_u64_shr */
255
381d585c
HZ
256#ifndef mul_u64_u32_div
257static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
258{
259 union {
260 u64 ll;
261 struct {
262#ifdef __BIG_ENDIAN
263 u32 high, low;
264#else
265 u32 low, high;
266#endif
267 } l;
268 } u, rl, rh;
269
270 u.ll = a;
9e3d6223
PZ
271 rl.ll = mul_u32_u32(u.l.low, mul);
272 rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high;
381d585c
HZ
273
274 /* Bits 32-63 of the result will be in rh.l.low. */
275 rl.l.high = do_div(rh.ll, divisor);
276
277 /* Bits 0-31 of the result will be in rl.l.low. */
278 do_div(rl.ll, divisor);
279
280 rl.l.high = rh.l.low;
281 return rl.ll;
282}
283#endif /* mul_u64_u32_div */
284
3dc167ba
ON
285u64 mul_u64_u64_div_u64(u64 a, u64 mul, u64 div);
286
090f13ca
LB
287/**
288 * DIV64_U64_ROUND_UP - unsigned 64bit divide with 64bit divisor rounded up
289 * @ll: unsigned 64bit dividend
290 * @d: unsigned 64bit divisor
291 *
292 * Divide unsigned 64bit dividend by unsigned 64bit divisor
293 * and round up.
294 *
295 * Return: dividend / divisor rounded up
296 */
68600f62
RG
297#define DIV64_U64_ROUND_UP(ll, d) \
298 ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); })
299
1d4ce389
JK
300/**
301 * DIV_U64_ROUND_UP - unsigned 64bit divide with 32bit divisor rounded up
302 * @ll: unsigned 64bit dividend
303 * @d: unsigned 32bit divisor
304 *
305 * Divide unsigned 64bit dividend by unsigned 32bit divisor
306 * and round up.
307 *
308 * Return: dividend / divisor rounded up
309 */
310#define DIV_U64_ROUND_UP(ll, d) \
311 ({ u32 _tmp = (d); div_u64((ll) + _tmp - 1, _tmp); })
312
cb8be119
SH
313/**
314 * DIV64_U64_ROUND_CLOSEST - unsigned 64bit divide with 64bit divisor rounded to nearest integer
315 * @dividend: unsigned 64bit dividend
316 * @divisor: unsigned 64bit divisor
317 *
318 * Divide unsigned 64bit dividend by unsigned 64bit divisor
319 * and round to closest integer.
320 *
321 * Return: dividend / divisor rounded to nearest integer
322 */
323#define DIV64_U64_ROUND_CLOSEST(dividend, divisor) \
324 ({ u64 _tmp = (divisor); div64_u64((dividend) + _tmp / 2, _tmp); })
325
d28a1de5 326/**
2c861b73
PR
327 * DIV_U64_ROUND_CLOSEST - unsigned 64bit divide with 32bit divisor rounded to nearest integer
328 * @dividend: unsigned 64bit dividend
329 * @divisor: unsigned 32bit divisor
330 *
331 * Divide unsigned 64bit dividend by unsigned 32bit divisor
332 * and round to closest integer.
333 *
334 * Return: dividend / divisor rounded to nearest integer
335 */
336#define DIV_U64_ROUND_CLOSEST(dividend, divisor) \
337 ({ u32 _tmp = (divisor); div_u64((u64)(dividend) + _tmp / 2, _tmp); })
338
d28a1de5 339/**
af60459a
CZ
340 * DIV_S64_ROUND_CLOSEST - signed 64bit divide with 32bit divisor rounded to nearest integer
341 * @dividend: signed 64bit dividend
342 * @divisor: signed 32bit divisor
343 *
344 * Divide signed 64bit dividend by signed 32bit divisor
345 * and round to closest integer.
346 *
347 * Return: dividend / divisor rounded to nearest integer
348 */
349#define DIV_S64_ROUND_CLOSEST(dividend, divisor)( \
350{ \
351 s64 __x = (dividend); \
352 s32 __d = (divisor); \
353 ((__x > 0) == (__d > 0)) ? \
354 div_s64((__x + (__d / 2)), __d) : \
355 div_s64((__x - (__d / 2)), __d); \
356} \
357)
1d4ce389
JK
358
359/**
360 * roundup_u64 - Round up a 64bit value to the next specified 32bit multiple
361 * @x: the value to up
362 * @y: 32bit multiple to round up to
363 *
364 * Rounds @x to the next multiple of @y. For 32bit @x values, see roundup and
365 * the faster round_up() for powers of 2.
366 *
367 * Return: rounded up value.
368 */
369static inline u64 roundup_u64(u64 x, u32 y)
370{
371 return DIV_U64_ROUND_UP(x, y) * y;
372}
2418f4f2 373#endif /* _LINUX_MATH64_H */