Linux 4.6-rc6
[cris-mirror.git] / include / linux / math64.h
blob6e8b5b270ffeada870b3cdcd638530f2479b84ee
1 #ifndef _LINUX_MATH64_H
2 #define _LINUX_MATH64_H
4 #include <linux/types.h>
5 #include <asm/div64.h>
7 #if BITS_PER_LONG == 64
9 #define div64_long(x, y) div64_s64((x), (y))
10 #define div64_ul(x, y) div64_u64((x), (y))
12 /**
13 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
15 * This is commonly provided by 32bit archs to provide an optimized 64bit
16 * divide.
18 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
20 *remainder = dividend % divisor;
21 return dividend / divisor;
24 /**
25 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
27 static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
29 *remainder = dividend % divisor;
30 return dividend / divisor;
33 /**
34 * div64_u64_rem - unsigned 64bit divide with 64bit divisor and remainder
36 static inline u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder)
38 *remainder = dividend % divisor;
39 return dividend / divisor;
42 /**
43 * div64_u64 - unsigned 64bit divide with 64bit divisor
45 static inline u64 div64_u64(u64 dividend, u64 divisor)
47 return dividend / divisor;
50 /**
51 * div64_s64 - signed 64bit divide with 64bit divisor
53 static inline s64 div64_s64(s64 dividend, s64 divisor)
55 return dividend / divisor;
58 #elif BITS_PER_LONG == 32
60 #define div64_long(x, y) div_s64((x), (y))
61 #define div64_ul(x, y) div_u64((x), (y))
63 #ifndef div_u64_rem
64 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
66 *remainder = do_div(dividend, divisor);
67 return dividend;
69 #endif
71 #ifndef div_s64_rem
72 extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
73 #endif
75 #ifndef div64_u64_rem
76 extern u64 div64_u64_rem(u64 dividend, u64 divisor, u64 *remainder);
77 #endif
79 #ifndef div64_u64
80 extern u64 div64_u64(u64 dividend, u64 divisor);
81 #endif
83 #ifndef div64_s64
84 extern s64 div64_s64(s64 dividend, s64 divisor);
85 #endif
87 #endif /* BITS_PER_LONG */
89 /**
90 * div_u64 - unsigned 64bit divide with 32bit divisor
92 * This is the most common 64bit divide and should be used if possible,
93 * as many 32bit archs can optimize this variant better than a full 64bit
94 * divide.
96 #ifndef div_u64
97 static inline u64 div_u64(u64 dividend, u32 divisor)
99 u32 remainder;
100 return div_u64_rem(dividend, divisor, &remainder);
102 #endif
105 * div_s64 - signed 64bit divide with 32bit divisor
107 #ifndef div_s64
108 static inline s64 div_s64(s64 dividend, s32 divisor)
110 s32 remainder;
111 return div_s64_rem(dividend, divisor, &remainder);
113 #endif
115 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
117 static __always_inline u32
118 __iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
120 u32 ret = 0;
122 while (dividend >= divisor) {
123 /* The following asm() prevents the compiler from
124 optimising this loop into a modulo operation. */
125 asm("" : "+rm"(dividend));
127 dividend -= divisor;
128 ret++;
131 *remainder = dividend;
133 return ret;
136 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
138 #ifndef mul_u64_u32_shr
139 static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
141 return (u64)(((unsigned __int128)a * mul) >> shift);
143 #endif /* mul_u64_u32_shr */
145 #ifndef mul_u64_u64_shr
146 static inline u64 mul_u64_u64_shr(u64 a, u64 mul, unsigned int shift)
148 return (u64)(((unsigned __int128)a * mul) >> shift);
150 #endif /* mul_u64_u64_shr */
152 #else
154 #ifndef mul_u64_u32_shr
155 static inline u64 mul_u64_u32_shr(u64 a, u32 mul, unsigned int shift)
157 u32 ah, al;
158 u64 ret;
160 al = a;
161 ah = a >> 32;
163 ret = ((u64)al * mul) >> shift;
164 if (ah)
165 ret += ((u64)ah * mul) << (32 - shift);
167 return ret;
169 #endif /* mul_u64_u32_shr */
171 #ifndef mul_u64_u64_shr
172 static inline u64 mul_u64_u64_shr(u64 a, u64 b, unsigned int shift)
174 union {
175 u64 ll;
176 struct {
177 #ifdef __BIG_ENDIAN
178 u32 high, low;
179 #else
180 u32 low, high;
181 #endif
182 } l;
183 } rl, rm, rn, rh, a0, b0;
184 u64 c;
186 a0.ll = a;
187 b0.ll = b;
189 rl.ll = (u64)a0.l.low * b0.l.low;
190 rm.ll = (u64)a0.l.low * b0.l.high;
191 rn.ll = (u64)a0.l.high * b0.l.low;
192 rh.ll = (u64)a0.l.high * b0.l.high;
195 * Each of these lines computes a 64-bit intermediate result into "c",
196 * starting at bits 32-95. The low 32-bits go into the result of the
197 * multiplication, the high 32-bits are carried into the next step.
199 rl.l.high = c = (u64)rl.l.high + rm.l.low + rn.l.low;
200 rh.l.low = c = (c >> 32) + rm.l.high + rn.l.high + rh.l.low;
201 rh.l.high = (c >> 32) + rh.l.high;
204 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
205 * shift it right and throw away the high part of the result.
207 if (shift == 0)
208 return rl.ll;
209 if (shift < 64)
210 return (rl.ll >> shift) | (rh.ll << (64 - shift));
211 return rh.ll >> (shift & 63);
213 #endif /* mul_u64_u64_shr */
215 #endif
217 #ifndef mul_u64_u32_div
218 static inline u64 mul_u64_u32_div(u64 a, u32 mul, u32 divisor)
220 union {
221 u64 ll;
222 struct {
223 #ifdef __BIG_ENDIAN
224 u32 high, low;
225 #else
226 u32 low, high;
227 #endif
228 } l;
229 } u, rl, rh;
231 u.ll = a;
232 rl.ll = (u64)u.l.low * mul;
233 rh.ll = (u64)u.l.high * mul + rl.l.high;
235 /* Bits 32-63 of the result will be in rh.l.low. */
236 rl.l.high = do_div(rh.ll, divisor);
238 /* Bits 0-31 of the result will be in rl.l.low. */
239 do_div(rl.ll, divisor);
241 rl.l.high = rh.l.low;
242 return rl.ll;
244 #endif /* mul_u64_u32_div */
246 #endif /* _LINUX_MATH64_H */