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