xref: /openbmc/u-boot/include/linux/math64.h (revision be059e88)
1 #ifndef _LINUX_MATH64_H
2 #define _LINUX_MATH64_H
3 
4 #include <linux/types.h>
5 
6 #if BITS_PER_LONG == 64
7 
8 /**
9  * div_u64_rem - unsigned 64bit divide with 32bit divisor with remainder
10  *
11  * This is commonly provided by 32bit archs to provide an optimized 64bit
12  * divide.
13  */
14 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
15 {
16 	*remainder = dividend % divisor;
17 	return dividend / divisor;
18 }
19 
20 /**
21  * div_s64_rem - signed 64bit divide with 32bit divisor with remainder
22  */
23 static inline s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder)
24 {
25 	*remainder = dividend % divisor;
26 	return dividend / divisor;
27 }
28 
29 /**
30  * div64_u64 - unsigned 64bit divide with 64bit divisor
31  */
32 static inline u64 div64_u64(u64 dividend, u64 divisor)
33 {
34 	return dividend / divisor;
35 }
36 
37 #elif BITS_PER_LONG == 32
38 
39 #ifndef div_u64_rem
40 static inline u64 div_u64_rem(u64 dividend, u32 divisor, u32 *remainder)
41 {
42 	*remainder = do_div(dividend, divisor);
43 	return dividend;
44 }
45 #endif
46 
47 #ifndef div_s64_rem
48 extern s64 div_s64_rem(s64 dividend, s32 divisor, s32 *remainder);
49 #endif
50 
51 #ifndef div64_u64
52 extern u64 div64_u64(u64 dividend, u64 divisor);
53 #endif
54 
55 #endif /* BITS_PER_LONG */
56 
57 /**
58  * div_u64 - unsigned 64bit divide with 32bit divisor
59  *
60  * This is the most common 64bit divide and should be used if possible,
61  * as many 32bit archs can optimize this variant better than a full 64bit
62  * divide.
63  */
64 #ifndef div_u64
65 static inline u64 div_u64(u64 dividend, u32 divisor)
66 {
67 	u32 remainder;
68 	return div_u64_rem(dividend, divisor, &remainder);
69 }
70 #endif
71 
72 /**
73  * div_s64 - signed 64bit divide with 32bit divisor
74  */
75 #ifndef div_s64
76 static inline s64 div_s64(s64 dividend, s32 divisor)
77 {
78 	s32 remainder;
79 	return div_s64_rem(dividend, divisor, &remainder);
80 }
81 #endif
82 
83 u32 iter_div_u64_rem(u64 dividend, u32 divisor, u64 *remainder);
84 
85 #endif /* _LINUX_MATH64_H */
86