blob: eaa9fd5b9685faee28a240c032ead689f8e4e1cc [file] [log] [blame]
Stefan Roese2fc10f62009-03-19 15:35:05 +01001#ifndef _LINUX_MATH64_H
2#define _LINUX_MATH64_H
3
Peng Fand2183202017-04-10 13:39:48 +08004#include <div64.h>
5#include <linux/bitops.h>
Stefan Roese2fc10f62009-03-19 15:35:05 +01006#include <linux/types.h>
7
8#if BITS_PER_LONG == 64
9
Peng Fand2183202017-04-10 13:39:48 +080010#define div64_long(x, y) div64_s64((x), (y))
11#define div64_ul(x, y) div64_u64((x), (y))
12
Stefan Roese2fc10f62009-03-19 15:35:05 +010013/**
14 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
15 *
16 * This is commonly provided by 32bit archs to provide an optimized 64bit
17 * divide.
18 */
19static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
20{
21 *remainder = dividend % divisor;
22 return dividend / divisor;
23}
24
25/**
26 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
27 */
28static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
29{
30 *remainder = dividend % divisor;
31 return dividend / divisor;
32}
33
34/**
Peng Fand2183202017-04-10 13:39:48 +080035 * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
36 */
37static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
38{
39 *remainder = dividend % divisor;
40 return dividend / divisor;
41}
42
43/**
Stefan Roese2fc10f62009-03-19 15:35:05 +010044 * div64_u64 - unsigned 64bit divide with 64bit divisor
45 */
46static inline u64 div64_u64(u64 dividend, u64 divisor)
47{
48 return dividend / divisor;
49}
50
Ashok Reddy Soma982686c2022-02-23 15:36:04 +010051#define DIV64_U64_ROUND_UP(ll, d) \
52 ({ u64 _tmp = (d); div64_u64((ll) + _tmp - 1, _tmp); })
53
Peng Fand2183202017-04-10 13:39:48 +080054/**
55 * div64_s64 - signed 64bit divide with 64bit divisor
56 */
57static inline s64 div64_s64(s64 dividend, s64 divisor)
58{
59 return dividend / divisor;
60}
61
Stefan Roese2fc10f62009-03-19 15:35:05 +010062#elif BITS_PER_LONG == 32
63
Peng Fand2183202017-04-10 13:39:48 +080064#define div64_long(x, y) div_s64((x), (y))
65#define div64_ul(x, y) div_u64((x), (y))
66
Stefan Roese2fc10f62009-03-19 15:35:05 +010067#ifndef div_u64_rem
68static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
69{
70 *remainder = do_div(dividend, divisor);
71 return dividend;
72}
73#endif
74
75#ifndef div_s64_rem
76extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
77#endif
78
Peng Fand2183202017-04-10 13:39:48 +080079#ifndef div64_u64_rem
80extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
81#endif
82
Stefan Roese2fc10f62009-03-19 15:35:05 +010083#ifndef div64_u64
84extern u64 div64_u64(u64 dividend, u64 divisor);
85#endif
86
Peng Fand2183202017-04-10 13:39:48 +080087#ifndef div64_s64
88extern s64 div64_s64(s64 dividend, s64 divisor);
89#endif
90
Stefan Roese2fc10f62009-03-19 15:35:05 +010091#endif /* BITS_PER_LONG */
92
93/**
94 * div_u64 - unsigned 64bit divide with 32bit divisor
95 *
96 * This is the most common 64bit divide and should be used if possible,
97 * as many 32bit archs can optimize this variant better than a full 64bit
98 * divide.
99 */
100#ifndef div_u64
101static inline u64 div_u64(u64 dividend, u32 divisor)
102{
103 u32 remainder;
104 return div_u64_rem(dividend, divisor, &remainder);
105}
106#endif
107
108/**
109 * div_s64 - signed 64bit divide with 32bit divisor
110 */
111#ifndef div_s64
112static inline s64 div_s64(s64 dividend, s32 divisor)
113{
114 s32 remainder;
115 return div_s64_rem(dividend, divisor, &remainder);
116}
117#endif
118
119u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
120
Peng Fand2183202017-04-10 13:39:48 +0800121static __always_inline u32
122__iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
123{
124 u32 ret = 0;
125
126 while (dividend >= divisor) {
127 /* The following asm() prevents the compiler from
128 optimising this loop into a modulo operation. */
129 asm("" : "+rm"(dividend));
130
131 dividend -= divisor;
132 ret++;
133 }
134
135 *remainder = dividend;
136
137 return ret;
138}
139
140#ifndef mul_u32_u32
141/*
142 * Many a GCC version messes this up and generates a 64x64 mult :-(
143 */
144static inline u64 mul_u32_u32(u32 a, u32 b)
145{
146 return (u64)a * b;
147}
148#endif
149
150#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
151
152#ifndef mul_u64_u32_shr
153static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
154{
155 return (u64)(((unsigned __int128)a * mul) >> shift);
156}
157#endif /* mul_u64_u32_shr */
158
159#ifndef mul_u64_u64_shr
160static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
161{
162 return (u64)(((unsigned __int128)a * mul) >> shift);
163}
164#endif /* mul_u64_u64_shr */
165
166#else
167
168#ifndef mul_u64_u32_shr
169static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
170{
171 u32 ah, al;
172 u64 ret;
173
174 al = a;
175 ah = a >> 32;
176
177 ret = mul_u32_u32(al, mul) >> shift;
178 if (ah)
179 ret += mul_u32_u32(ah, mul) << (32 - shift);
180
181 return ret;
182}
183#endif /* mul_u64_u32_shr */
184
185#ifndef mul_u64_u64_shr
186static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
187{
188 union {
189 u64 ll;
190 struct {
191#ifdef __BIG_ENDIAN
192 u32 high, low;
193#else
194 u32 low, high;
195#endif
196 } l;
197 } rl, rm, rn, rh, a0, b0;
198 u64 c;
199
200 a0.ll = a;
201 b0.ll = b;
202
203 rl.ll = mul_u32_u32(a0.l.low, b0.l.low);
204 rm.ll = mul_u32_u32(a0.l.low, b0.l.high);
205 rn.ll = mul_u32_u32(a0.l.high, b0.l.low);
206 rh.ll = mul_u32_u32(a0.l.high, b0.l.high);
207
208 /*
209 * Each of these lines computes a 64-bit intermediate result into "c",
210 * starting at bits 32-95. The low 32-bits go into the result of the
211 * multiplication, the high 32-bits are carried into the next step.
212 */
213 rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low;
214 rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
215 rh.l.high = (c >> 32) + rh.l.high;
216
217 /*
218 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
219 * shift it right and throw away the high part of the result.
220 */
221 if (shift == 0)
222 return rl.ll;
223 if (shift < 64)
224 return (rl.ll >> shift) | (rh.ll << (64 - shift));
225 return rh.ll >> (shift & 63);
226}
227#endif /* mul_u64_u64_shr */
228
229#endif
230
231#ifndef mul_u64_u32_div
232static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
233{
234 union {
235 u64 ll;
236 struct {
237#ifdef __BIG_ENDIAN
238 u32 high, low;
239#else
240 u32 low, high;
241#endif
242 } l;
243 } u, rl, rh;
244
245 u.ll = a;
246 rl.ll = mul_u32_u32(u.l.low, mul);
247 rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high;
248
249 /* Bits 32-63 of the result will be in rh.l.low. */
250 rl.l.high = do_div(rh.ll, divisor);
251
252 /* Bits 0-31 of the result will be in rl.l.low. */
253 do_div(rl.ll, divisor);
254
255 rl.l.high = rh.l.low;
256 return rl.ll;
257}
258#endif /* mul_u64_u32_div */
259
Stefan Roese2fc10f62009-03-19 15:35:05 +0100260#endif /* _LINUX_MATH64_H */