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