blob: 082de345b73ce0c02d5a3466336bb97bd408894f [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Roman Zippel2418f4f2008-05-01 04:34:25 -07002#ifndef _LINUX_MATH64_H
3#define _LINUX_MATH64_H
4
5#include <linux/types.h>
6#include <asm/div64.h>
7
8#if BITS_PER_LONG == 64
9
Alex Shic2853c82013-06-12 14:05:10 -070010#define div64_long(x, y) div64_s64((x), (y))
11#define div64_ul(x, y) div64_u64((x), (y))
Sasha Levinf9103812012-03-15 12:36:13 -040012
Roman Zippel2418f4f2008-05-01 04:34:25 -070013/**
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
Roman Zippel6f6d6a12008-05-01 04:34:28 -070034/**
Mike Snitzereb18cba2013-08-20 15:05:17 -040035 * 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/**
Roman Zippel6f6d6a12008-05-01 04:34:28 -070044 * 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
Brian Behlendorf658716d2010-10-26 14:23:10 -070051/**
52 * div64_s64 - signed 64bit divide with 64bit divisor
53 */
54static inline s64 div64_s64(s64 dividend, s64 divisor)
55{
56 return dividend / divisor;
57}
58
Roman Zippel2418f4f2008-05-01 04:34:25 -070059#elif BITS_PER_LONG == 32
60
Alex Shic2853c82013-06-12 14:05:10 -070061#define div64_long(x, y) div_s64((x), (y))
62#define div64_ul(x, y) div_u64((x), (y))
Sasha Levinf9103812012-03-15 12:36:13 -040063
Roman Zippel2418f4f2008-05-01 04:34:25 -070064#ifndef div_u64_rem
65static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
66{
67 *remainder = do_div(dividend, divisor);
68 return dividend;
69}
70#endif
71
72#ifndef div_s64_rem
73extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
74#endif
75
Mike Snitzereb18cba2013-08-20 15:05:17 -040076#ifndef div64_u64_rem
77extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
78#endif
79
Roman Zippel6f6d6a12008-05-01 04:34:28 -070080#ifndef div64_u64
Stanislaw Gruszkaf3002132013-04-30 11:35:07 +020081extern u64 div64_u64(u64 dividend, u64 divisor);
Roman Zippel6f6d6a12008-05-01 04:34:28 -070082#endif
83
Brian Behlendorf658716d2010-10-26 14:23:10 -070084#ifndef div64_s64
85extern s64 div64_s64(s64 dividend, s64 divisor);
86#endif
87
Roman Zippel2418f4f2008-05-01 04:34:25 -070088#endif /* BITS_PER_LONG */
89
90/**
91 * div_u64 - unsigned 64bit divide with 32bit divisor
92 *
93 * This is the most common 64bit divide and should be used if possible,
94 * as many 32bit archs can optimize this variant better than a full 64bit
95 * divide.
96 */
97#ifndef div_u64
98static inline u64 div_u64(u64 dividend, u32 divisor)
99{
100 u32 remainder;
101 return div_u64_rem(dividend, divisor, &remainder);
102}
103#endif
104
105/**
106 * div_s64 - signed 64bit divide with 32bit divisor
107 */
108#ifndef div_s64
109static inline s64 div_s64(s64 dividend, s32 divisor)
110{
111 s32 remainder;
112 return div_s64_rem(dividend, divisor, &remainder);
113}
114#endif
115
Jeremy Fitzhardingef595ec92008-06-12 10:47:56 +0200116u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
117
Jeremy Fitzhardinged5e181f2008-06-12 10:47:58 +0200118static __always_inline u32
119__iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
120{
121 u32 ret = 0;
122
123 while (dividend >= divisor) {
124 /* The following asm() prevents the compiler from
125 optimising this loop into a modulo operation. */
126 asm("" : "+rm"(dividend));
127
128 dividend -= divisor;
129 ret++;
130 }
131
132 *remainder = dividend;
133
134 return ret;
135}
136
Peter Zijlstra9e3d6222016-12-09 09:30:11 +0100137#ifndef mul_u32_u32
138/*
139 * Many a GCC version messes this up and generates a 64x64 mult :-(
140 */
141static inline u64 mul_u32_u32(u32 a, u32 b)
142{
143 return (u64)a * b;
144}
145#endif
146
Peter Zijlstrabe5e6102013-11-18 18:27:06 +0100147#if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
148
149#ifndef mul_u64_u32_shr
150static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
151{
152 return (u64)(((unsigned __int128)a * mul) >> shift);
153}
154#endif /* mul_u64_u32_shr */
155
Haozhong Zhang35181e82015-10-20 15:39:03 +0800156#ifndef mul_u64_u64_shr
157static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
158{
159 return (u64)(((unsigned __int128)a * mul) >> shift);
160}
161#endif /* mul_u64_u64_shr */
162
Peter Zijlstrabe5e6102013-11-18 18:27:06 +0100163#else
164
165#ifndef mul_u64_u32_shr
166static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
167{
168 u32 ah, al;
169 u64 ret;
170
171 al = a;
172 ah = a >> 32;
173
Peter Zijlstra9e3d6222016-12-09 09:30:11 +0100174 ret = mul_u32_u32(al, mul) >> shift;
Peter Zijlstrabe5e6102013-11-18 18:27:06 +0100175 if (ah)
Peter Zijlstra9e3d6222016-12-09 09:30:11 +0100176 ret += mul_u32_u32(ah, mul) << (32 - shift);
Peter Zijlstrabe5e6102013-11-18 18:27:06 +0100177
178 return ret;
179}
180#endif /* mul_u64_u32_shr */
181
Haozhong Zhang35181e82015-10-20 15:39:03 +0800182#ifndef mul_u64_u64_shr
183static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
184{
185 union {
186 u64 ll;
187 struct {
188#ifdef __BIG_ENDIAN
189 u32 high, low;
190#else
191 u32 low, high;
192#endif
193 } l;
194 } rl, rm, rn, rh, a0, b0;
195 u64 c;
196
197 a0.ll = a;
198 b0.ll = b;
199
Peter Zijlstra9e3d6222016-12-09 09:30:11 +0100200 rl.ll = mul_u32_u32(a0.l.low, b0.l.low);
201 rm.ll = mul_u32_u32(a0.l.low, b0.l.high);
202 rn.ll = mul_u32_u32(a0.l.high, b0.l.low);
203 rh.ll = mul_u32_u32(a0.l.high, b0.l.high);
Haozhong Zhang35181e82015-10-20 15:39:03 +0800204
205 /*
206 * Each of these lines computes a 64-bit intermediate result into "c",
207 * starting at bits 32-95. The low 32-bits go into the result of the
208 * multiplication, the high 32-bits are carried into the next step.
209 */
210 rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low;
211 rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
212 rh.l.high = (c >> 32) + rh.l.high;
213
214 /*
215 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
216 * shift it right and throw away the high part of the result.
217 */
218 if (shift == 0)
219 return rl.ll;
220 if (shift < 64)
221 return (rl.ll >> shift) | (rh.ll << (64 - shift));
222 return rh.ll >> (shift & 63);
223}
224#endif /* mul_u64_u64_shr */
225
Peter Zijlstrabe5e6102013-11-18 18:27:06 +0100226#endif
227
Haozhong Zhang381d5852015-10-20 15:39:04 +0800228#ifndef mul_u64_u32_div
229static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
230{
231 union {
232 u64 ll;
233 struct {
234#ifdef __BIG_ENDIAN
235 u32 high, low;
236#else
237 u32 low, high;
238#endif
239 } l;
240 } u, rl, rh;
241
242 u.ll = a;
Peter Zijlstra9e3d6222016-12-09 09:30:11 +0100243 rl.ll = mul_u32_u32(u.l.low, mul);
244 rh.ll = mul_u32_u32(u.l.high, mul) + rl.l.high;
Haozhong Zhang381d5852015-10-20 15:39:04 +0800245
246 /* Bits 32-63 of the result will be in rh.l.low. */
247 rl.l.high = do_div(rh.ll, divisor);
248
249 /* Bits 0-31 of the result will be in rl.l.low. */
250 do_div(rl.ll, divisor);
251
252 rl.l.high = rh.l.low;
253 return rl.ll;
254}
255#endif /* mul_u64_u32_div */
256
Roman Zippel2418f4f2008-05-01 04:34:25 -0700257#endif /* _LINUX_MATH64_H */