]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - include/linux/math64.h
1 #ifndef _LINUX_MATH64_H
2 #define _LINUX_MATH64_H
4 #include <linux/types.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))
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
18 static inline u64
div_u64_rem(u64 dividend
, u32 divisor
, u32
*remainder
)
20 *remainder
= dividend
% divisor
;
21 return dividend
/ divisor
;
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
;
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
;
43 * div64_u64 - unsigned 64bit divide with 64bit divisor
45 static inline u64
div64_u64(u64 dividend
, u64 divisor
)
47 return dividend
/ divisor
;
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))
64 static inline u64
div_u64_rem(u64 dividend
, u32 divisor
, u32
*remainder
)
66 *remainder
= do_div(dividend
, divisor
);
72 extern s64
div_s64_rem(s64 dividend
, s32 divisor
, s32
*remainder
);
76 extern u64
div64_u64_rem(u64 dividend
, u64 divisor
, u64
*remainder
);
80 extern u64
div64_u64(u64 dividend
, u64 divisor
);
84 extern s64
div64_s64(s64 dividend
, s64 divisor
);
87 #endif /* BITS_PER_LONG */
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
97 static inline u64
div_u64(u64 dividend
, u32 divisor
)
100 return div_u64_rem(dividend
, divisor
, &remainder
);
105 * div_s64 - signed 64bit divide with 32bit divisor
108 static inline s64
div_s64(s64 dividend
, s32 divisor
)
111 return div_s64_rem(dividend
, divisor
, &remainder
);
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
)
122 while (dividend
>= divisor
) {
123 /* The following asm() prevents the compiler from
124 optimising this loop into a modulo operation. */
125 asm("" : "+rm"(dividend
));
131 *remainder
= dividend
;
138 * Many a GCC version messes this up and generates a 64x64 mult :-(
140 static inline u64
mul_u32_u32(u32 a
, u32 b
)
146 #if defined(CONFIG_ARCH_SUPPORTS_INT128) && defined(__SIZEOF_INT128__)
148 #ifndef mul_u64_u32_shr
149 static inline u64
mul_u64_u32_shr(u64 a
, u32 mul
, unsigned int shift
)
151 return (u64
)(((unsigned __int128
)a
* mul
) >> shift
);
153 #endif /* mul_u64_u32_shr */
155 #ifndef mul_u64_u64_shr
156 static inline u64
mul_u64_u64_shr(u64 a
, u64 mul
, unsigned int shift
)
158 return (u64
)(((unsigned __int128
)a
* mul
) >> shift
);
160 #endif /* mul_u64_u64_shr */
164 #ifndef mul_u64_u32_shr
165 static inline u64
mul_u64_u32_shr(u64 a
, u32 mul
, unsigned int shift
)
173 ret
= mul_u32_u32(al
, mul
) >> shift
;
175 ret
+= mul_u32_u32(ah
, mul
) << (32 - shift
);
179 #endif /* mul_u64_u32_shr */
181 #ifndef mul_u64_u64_shr
182 static inline u64
mul_u64_u64_shr(u64 a
, u64 b
, unsigned int shift
)
193 } rl
, rm
, rn
, rh
, a0
, b0
;
199 rl
.ll
= mul_u32_u32(a0
.l
.low
, b0
.l
.low
);
200 rm
.ll
= mul_u32_u32(a0
.l
.low
, b0
.l
.high
);
201 rn
.ll
= mul_u32_u32(a0
.l
.high
, b0
.l
.low
);
202 rh
.ll
= mul_u32_u32(a0
.l
.high
, b0
.l
.high
);
205 * Each of these lines computes a 64-bit intermediate result into "c",
206 * starting at bits 32-95. The low 32-bits go into the result of the
207 * multiplication, the high 32-bits are carried into the next step.
209 rl
.l
.high
= c
= (u64
)rl
.l
.high
+ rm
.l
.low
+ rn
.l
.low
;
210 rh
.l
.low
= c
= (c
>> 32) + rm
.l
.high
+ rn
.l
.high
+ rh
.l
.low
;
211 rh
.l
.high
= (c
>> 32) + rh
.l
.high
;
214 * The 128-bit result of the multiplication is in rl.ll and rh.ll,
215 * shift it right and throw away the high part of the result.
220 return (rl
.ll
>> shift
) | (rh
.ll
<< (64 - shift
));
221 return rh
.ll
>> (shift
& 63);
223 #endif /* mul_u64_u64_shr */
227 #ifndef mul_u64_u32_div
228 static inline u64
mul_u64_u32_div(u64 a
, u32 mul
, u32 divisor
)
242 rl
.ll
= mul_u32_u32(u
.l
.low
, mul
);
243 rh
.ll
= mul_u32_u32(u
.l
.high
, mul
) + rl
.l
.high
;
245 /* Bits 32-63 of the result will be in rh.l.low. */
246 rl
.l
.high
= do_div(rh
.ll
, divisor
);
248 /* Bits 0-31 of the result will be in rl.l.low. */
249 do_div(rl
.ll
, divisor
);
251 rl
.l
.high
= rh
.l
.low
;
254 #endif /* mul_u64_u32_div */
256 #endif /* _LINUX_MATH64_H */