1
2#ifndef _LINUX_MATH_H
3#define _LINUX_MATH_H
4
5#include <asm/div64.h>
6#include <uapi/linux/kernel.h>
7
8
9
10
11
12
13
14#define __round_mask(x, y) ((__typeof__(x))((y)-1))
15
16
17
18
19
20
21
22
23
24#define round_up(x, y) ((((x)-1) | __round_mask(x, y))+1)
25
26
27
28
29
30
31
32
33
34#define round_down(x, y) ((x) & ~__round_mask(x, y))
35
36#define DIV_ROUND_UP __KERNEL_DIV_ROUND_UP
37
38#define DIV_ROUND_DOWN_ULL(ll, d) \
39 ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
40
41#define DIV_ROUND_UP_ULL(ll, d) \
42 DIV_ROUND_DOWN_ULL((unsigned long long)(ll) + (d) - 1, (d))
43
44#if BITS_PER_LONG == 32
45# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP_ULL(ll, d)
46#else
47# define DIV_ROUND_UP_SECTOR_T(ll,d) DIV_ROUND_UP(ll,d)
48#endif
49
50
51
52
53
54
55
56
57
58#define roundup(x, y) ( \
59{ \
60 typeof(y) __y = y; \
61 (((x) + (__y - 1)) / __y) * __y; \
62} \
63)
64
65
66
67
68
69
70
71
72#define rounddown(x, y) ( \
73{ \
74 typeof(x) __x = (x); \
75 __x - (__x % (y)); \
76} \
77)
78
79
80
81
82
83
84
85#define DIV_ROUND_CLOSEST(x, divisor)( \
86{ \
87 typeof(x) __x = x; \
88 typeof(divisor) __d = divisor; \
89 (((typeof(x))-1) > 0 || \
90 ((typeof(divisor))-1) > 0 || \
91 (((__x) > 0) == ((__d) > 0))) ? \
92 (((__x) + ((__d) / 2)) / (__d)) : \
93 (((__x) - ((__d) / 2)) / (__d)); \
94} \
95)
96
97
98
99
100#define DIV_ROUND_CLOSEST_ULL(x, divisor)( \
101{ \
102 typeof(divisor) __d = divisor; \
103 unsigned long long _tmp = (x) + (__d) / 2; \
104 do_div(_tmp, __d); \
105 _tmp; \
106} \
107)
108
109
110
111
112
113#define mult_frac(x, numer, denom)( \
114{ \
115 typeof(x) quot = (x) / (denom); \
116 typeof(x) rem = (x) % (denom); \
117 (quot * (numer)) + ((rem * (numer)) / (denom)); \
118} \
119)
120
121#define sector_div(a, b) do_div(a, b)
122
123
124
125
126
127
128
129
130
131#define abs(x) __abs_choose_expr(x, long long, \
132 __abs_choose_expr(x, long, \
133 __abs_choose_expr(x, int, \
134 __abs_choose_expr(x, short, \
135 __abs_choose_expr(x, char, \
136 __builtin_choose_expr( \
137 __builtin_types_compatible_p(typeof(x), char), \
138 (char)({ signed char __x = (x); __x<0?-__x:__x; }), \
139 ((void)0)))))))
140
141#define __abs_choose_expr(x, type, other) __builtin_choose_expr( \
142 __builtin_types_compatible_p(typeof(x), signed type) || \
143 __builtin_types_compatible_p(typeof(x), unsigned type), \
144 ({ signed type __x = (x); __x < 0 ? -__x : __x; }), other)
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160static inline u32 reciprocal_scale(u32 val, u32 ep_ro)
161{
162 return (u32)(((u64) val * ep_ro) >> 32);
163}
164
165u64 int_pow(u64 base, unsigned int exp);
166unsigned long int_sqrt(unsigned long);
167
168#if BITS_PER_LONG < 64
169u32 int_sqrt64(u64 x);
170#else
171static inline u32 int_sqrt64(u64 x)
172{
173 return (u32)int_sqrt(x);
174}
175#endif
176
177#endif
178