Whamcloud - gitweb
7b785a7fa231ae90cb818136f9a10e15106a0cda
[fs/lustre-release.git] / lustre / utils / lstddef.h
1 #ifndef _LSTDDEF_H
2 #define _LSTDDEF_H
3
4 #include <linux/types.h>
5 #include <sys/param.h>
6
7 #define __ALIGN_LSTDDEF_MASK(x, mask) (((x) + (mask)) & ~(mask))
8 #define __ALIGN_LSTDDEF(x, a) __ALIGN_LSTDDEF_MASK(x, (typeof(x))(a) - 1)
9 #define __LSTDDEF_DIV_ROUND_UP(n, d) (((n) + (d) - 1) / (d))
10
11 #define ALIGN(x, a)             __ALIGN_LSTDDEF((x), (a))
12 #define ALIGN_DOWN(x, a)        __ALIGN_LSTDDEF((x) - ((a) - 1), (a))
13 #define __ALIGN_MASK(x, mask)   __ALIGN_LSTDDEF_MASK((x), (mask))
14 #define PTR_ALIGN(p, a)         ((typeof(p))ALIGN((unsigned long)(p), (a)))
15 #define IS_ALIGNED(x, a)                (((x) & ((typeof(x))(a) - 1)) == 0)
16
17 #ifndef __must_be_array
18 # define __must_be_array(arr) 0
19 #endif
20
21 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]) + __must_be_array(arr))
22
23 /*
24  * This looks more complex than it should be. But we need to
25  * get the type for the ~ right in round_down (it needs to be
26  * as wide as the result!), and we want to evaluate the macro
27  * arguments just once each.
28  */
29 #define __round_mask(x, y) ((__typeof__(x))((y) - 1))
30 #define round_up(x, y) ((((x) - 1) | __round_mask(x, y)) + 1)
31 #define round_down(x, y) ((x) & ~__round_mask(x, y))
32
33 #define FIELD_SIZEOF(t, f) (sizeof(((t *)0)->f))
34 #define DIV_ROUND_UP __USER_DIV_ROUND_UP
35
36 #define DIV_ROUND_DOWN_ULL(ll, d) \
37         ({ unsigned long long _tmp = (ll); do_div(_tmp, d); _tmp; })
38
39 #define DIV_ROUND_UP_ULL(ll, d) DIV_ROUND_DOWN_ULL((ll) + (d) - 1, (d))
40
41 #if BITS_PER_LONG == 32
42 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP_ULL(ll, d)
43 #else
44 # define DIV_ROUND_UP_SECTOR_T(ll, d) DIV_ROUND_UP(ll, d)
45 #endif
46
47 #define rounddown(x, y) ({                              \
48         typeof(x) __x = (x);                            \
49         __x - (__x % (y));                              \
50 })
51
52 /*
53  * Divide positive or negative dividend by positive divisor and round
54  * to closest integer. Result is undefined for negative divisors and
55  * for negative dividends if the divisor variable type is unsigned.
56  */
57 #define DIV_ROUND_CLOSEST(x, divisor) ({                \
58         typeof(x) __x = x;                              \
59         typeof(divisor) __d = divisor;                  \
60         (((typeof(x))-1) > 0 ||                         \
61          ((typeof(divisor))-1) > 0 || (__x) > 0) ?      \
62                 (((__x) + ((__d) / 2)) / (__d)) :       \
63                 (((__x) - ((__d) / 2)) / (__d));        \
64 })
65
66 /*
67  * Same as above but for u64 dividends. divisor must be a 32-bit
68  * number.
69  */
70 #define DIV_ROUND_CLOSEST_ULL(x, divisor) ({            \
71         typeof(divisor) __d = divisor;                  \
72         unsigned long long _tmp = (x) + (__d) / 2;      \
73         do_div(_tmp, __d);                              \
74         _tmp;                                           \
75 })
76
77 /*
78  * Multiplies an integer by a fraction, while avoiding unnecessary
79  * overflow or loss of precision.
80  */
81 #define mult_frac(x, numer, denom) ({                   \
82         typeof(x) quot = (x) / (denom);                 \
83         typeof(x) rem  = (x) % (denom);                 \
84         (quot * (numer)) + ((rem * (numer)) / (denom)); \
85 })
86
87 /**
88  * upper_32_bits - return bits 32-63 of a number
89  * @n: the number we're accessing
90  *
91  * A basic shift-right of a 64- or 32-bit quantity.  Use this to suppress
92  * the "right shift count >= width of type" warning when that quantity is
93  * 32-bits.
94  */
95 #define upper_32_bits(n) ((__u32)(((n) >> 16) >> 16))
96
97 /**
98  * lower_32_bits - return bits 0-31 of a number
99  * @n: the number we're accessing
100  */
101 #define lower_32_bits(n) ((__u32)(n))
102
103 /**
104  * abs - return absolute value of an argument
105  * @x: the value.  If it is unsigned type, it is converted to signed type first
106  *   (s64, long or int depending on its size).
107  *
108  * Return: an absolute value of x.  If x is 64-bit, macro's return type is s64,
109  *   otherwise it is signed long.
110  */
111 #define abs(x) __builtin_choose_expr(sizeof(x) == sizeof(__s64), ({     \
112                 __s64 __x = (x);                                        \
113                 (__x < 0) ? -__x : __x;                                 \
114         }), ({                                                          \
115                 long ret;                                               \
116                 if (sizeof(x) == sizeof(long)) {                        \
117                         long __x = (x);                                 \
118                         ret = (__x < 0) ? -__x : __x;                   \
119                 } else {                                                \
120                         int __x = (x);                                  \
121                         ret = (__x < 0) ? -__x : __x;                   \
122                 }                                                       \
123                 ret;                                                    \
124         }))
125
126 /**
127  * reciprocal_scale - "scale" a value into range [0, ep_ro)
128  * @val: value
129  * @ep_ro: right open interval endpoint
130  *
131  * Perform a "reciprocal multiplication" in order to "scale" a value into
132  * range [0, ep_ro), where the upper interval endpoint is right-open.
133  * This is useful, e.g. for accessing a index of an array containing
134  * ep_ro elements, for example. Think of it as sort of modulus, only that
135  * the result isn't that of modulo. ;) Note that if initial input is a
136  * small value, then result will return 0.
137  *
138  * Return: a result based on val in interval [0, ep_ro).
139  */
140 static inline __u32 reciprocal_scale(__u32 val, __u32 ep_ro)
141 {
142         return (__u32)(((__u64) val * ep_ro) >> 32);
143 }
144
145 /*
146  * min()/max()/clamp() macros that also do
147  * strict type-checking.. See the
148  * "unnecessary" pointer comparison.
149  */
150 #define min(x, y) ({                            \
151         typeof(x) _min1 = (x);                  \
152         typeof(y) _min2 = (y);                  \
153         (void) (&_min1 == &_min2);              \
154         _min1 < _min2 ? _min1 : _min2;          \
155 })
156
157 #define max(x, y) ({                            \
158         typeof(x) _max1 = (x);                  \
159         typeof(y) _max2 = (y);                  \
160         (void) (&_max1 == &_max2);              \
161         _max1 > _max2 ? _max1 : _max2;          \
162 })
163
164 #define min3(x, y, z) ({                        \
165         typeof(x) _min1 = (x);                  \
166         typeof(y) _min2 = (y);                  \
167         typeof(z) _min3 = (z);                  \
168         (void) (&_min1 == &_min2);              \
169         (void) (&_min1 == &_min3);              \
170         _min1 < _min2 ? (_min1 < _min3 ? _min1 : _min3) : \
171                 (_min2 < _min3 ? _min2 : _min3); \
172 })
173
174 #define max3(x, y, z) ({                        \
175         typeof(x) _max1 = (x);                  \
176         typeof(y) _max2 = (y);                  \
177         typeof(z) _max3 = (z);                  \
178         (void) (&_max1 == &_max2);              \
179         (void) (&_max1 == &_max3);              \
180         _max1 > _max2 ? (_max1 > _max3 ? _max1 : _max3) : \
181                 (_max2 > _max3 ? _max2 : _max3); \
182 })
183
184 /**
185  * min_not_zero - return the minimum that is _not_ zero, unless both are zero
186  * @x: value1
187  * @y: value2
188  */
189 #define min_not_zero(x, y) ({                   \
190         typeof(x) __x = (x);                    \
191         typeof(y) __y = (y);                    \
192         __x == 0 ? __y : ((__y == 0) ? __x : min(__x, __y)); \
193 })
194
195 /**
196  * clamp - return a value clamped to a given range with strict typechecking
197  * @val: current value
198  * @min: minimum allowable value
199  * @max: maximum allowable value
200  *
201  * This macro does strict typechecking of min/max to make sure they are of the
202  * same type as val.  See the unnecessary pointer comparisons.
203  */
204 #define clamp(val, min, max) ({                 \
205         typeof(val) __val = (val);              \
206         typeof(min) __min = (min);              \
207         typeof(max) __max = (max);              \
208         (void) (&__val == &__min);              \
209         (void) (&__val == &__max);              \
210         __val = __val < __min ? __min : __val;  \
211         __val > __max ? __max : __val;          \
212 })
213
214 /*
215  * ..and if you can't take the strict
216  * types, you can specify one yourself.
217  *
218  * Or not use min/max/clamp at all, of course.
219  */
220 #define min_t(type, x, y) ({                    \
221         type __min1 = (x);                      \
222         type __min2 = (y);                      \
223         __min1 < __min2 ? __min1 : __min2;      \
224 })
225
226 #define max_t(type, x, y) ({                    \
227         type __max1 = (x);                      \
228         type __max2 = (y);                      \
229         __max1 > __max2 ? __max1 : __max2;      \
230 })
231
232 /**
233  * clamp_t - return a value clamped to a given range using a given type
234  * @type: the type of variable to use
235  * @val: current value
236  * @min: minimum allowable value
237  * @max: maximum allowable value
238  *
239  * This macro does no typechecking and uses temporary variables of type
240  * 'type' to make all the comparisons.
241  */
242 #define clamp_t(type, val, min, max) ({         \
243         type __val = (val);                     \
244         type __min = (min);                     \
245         type __max = (max);                     \
246         __val = __val < __min ? __min : __val;  \
247         __val > __max ? __max : __val;          \
248 })
249
250 /**
251  * clamp_val - return a value clamped to a given range using val's type
252  * @val: current value
253  * @min: minimum allowable value
254  * @max: maximum allowable value
255  *
256  * This macro does no typechecking and uses temporary variables of whatever
257  * type the input argument 'val' is.  This is useful when val is an unsigned
258  * type and min and max are literals that will otherwise be assigned a signed
259  * integer type.
260  */
261 #define clamp_val(val, min, max) ({             \
262         typeof(val) __val = (val);              \
263         typeof(val) __min = (min);              \
264         typeof(val) __max = (max);              \
265         __val = __val < __min ? __min : __val;  \
266         __val > __max ? __max : __val;          \
267 })
268
269 /*
270  * swap - swap value of @a and @b
271  */
272 #define swap(a, b) do {                         \
273         typeof(a) __tmp = (a);                  \
274         (a) = (b);                              \
275         (b) = __tmp;                            \
276 } while (0)
277
278 /**
279  * container_of - cast a member of a structure out to the containing structure
280  * @ptr:        the pointer to the member.
281  * @type:       the type of the container struct this is embedded in.
282  * @member:     the name of the member within the struct.
283  *
284  */
285 #define container_of(ptr, type, member) ({                      \
286         const typeof(((type *)0)->member) *__mptr = (ptr);      \
287         (type *)((char *)__mptr - offsetof(type, member));      \
288 })
289
290 #ifndef HAVE_COPY_FILE_RANGE
291
292 #ifndef __NR_copy_file_range
293
294 #if defined(_ASM_X86_UNISTD_64_H)
295 #define __NR_copy_file_range 326
296 #elif defined(_ASM_X86_UNISTD_32_H)
297 #define __NR_copy_file_range 285
298 #else
299 #define __NR_copy_file_range 285
300 #endif
301
302 #endif
303
304 static inline loff_t copy_file_range(int fd_in, loff_t *off_in, int fd_out,
305                                      loff_t *off_out, size_t len,
306                                      unsigned int flags)
307 {
308         return syscall(__NR_copy_file_range, fd_in, off_in, fd_out,
309                        off_out, len, flags);
310 }
311 #endif /* !HAVE_COPY_FILE_RANGE */
312
313 #endif /* !_LSTDDEF_H */