Add linux-next specific files for 20110831
[linux-2.6/next.git] / include / linux / math64.h
blob23fcdfcba81b33ef0d5e2957c591f8baeb1f1836
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 /**
10 * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
12 * This is commonly provided by 32bit archs to provide an optimized 64bit
13 * divide.
15 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
17 *remainder = dividend % divisor;
18 return dividend / divisor;
21 /**
22 * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
24 static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
26 *remainder = dividend % divisor;
27 return dividend / divisor;
30 /**
31 * div64_u64 - unsigned 64bit divide with 64bit divisor
33 static inline u64 div64_u64(u64 dividend, u64 divisor)
35 return dividend / divisor;
38 /**
39 * div64_s64 - signed 64bit divide with 64bit divisor
41 static inline s64 div64_s64(s64 dividend, s64 divisor)
43 return dividend / divisor;
46 #elif BITS_PER_LONG == 32
48 #ifndef div_u64_rem
49 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
51 *remainder = do_div(dividend, divisor);
52 return dividend;
54 #endif
56 #ifndef div_s64_rem
57 extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
58 #endif
60 #ifndef div64_u64
61 extern u64 div64_u64(u64 dividend, u64 divisor);
62 #endif
64 #ifndef div64_s64
65 extern s64 div64_s64(s64 dividend, s64 divisor);
66 #endif
68 #endif /* BITS_PER_LONG */
70 /**
71 * div_u64 - unsigned 64bit divide with 32bit divisor
73 * This is the most common 64bit divide and should be used if possible,
74 * as many 32bit archs can optimize this variant better than a full 64bit
75 * divide.
77 #ifndef div_u64
78 static inline u64 div_u64(u64 dividend, u32 divisor)
80 u32 remainder;
81 return div_u64_rem(dividend, divisor, &remainder);
83 #endif
85 /**
86 * div_s64 - signed 64bit divide with 32bit divisor
88 #ifndef div_s64
89 static inline s64 div_s64(s64 dividend, s32 divisor)
91 s32 remainder;
92 return div_s64_rem(dividend, divisor, &remainder);
94 #endif
96 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
98 static __always_inline u32
99 __iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder)
101 u32 ret = 0;
103 while (dividend >= divisor) {
104 /* The following asm() prevents the compiler from
105 optimising this loop into a modulo operation. */
106 asm("" : "+rm"(dividend));
108 dividend -= divisor;
109 ret++;
112 *remainder = dividend;
114 return ret;
117 #endif /* _LINUX_MATH64_H */