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