lib: work around braced-groups within expressions
[dpdk.git] / lib / librte_eal / common / include / rte_common.h
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
36
37 /**
38  * @file
39  *
40  * Generic, commonly-used macro and inline function definitions
41  * for DPDK.
42  */
43
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47
48 #include <stdint.h>
49 #include <stdlib.h>
50 #include <ctype.h>
51 #include <errno.h>
52 #include <limits.h>
53
54 #ifndef typeof
55 #define typeof __typeof__
56 #endif
57
58 #ifndef asm
59 #define asm __asm__
60 #endif
61
62 #ifdef RTE_ARCH_STRICT_ALIGN
63 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
64 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
65 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
66 #else
67 typedef uint64_t unaligned_uint64_t;
68 typedef uint32_t unaligned_uint32_t;
69 typedef uint16_t unaligned_uint16_t;
70 #endif
71
72 /**
73  * Force alignment
74  */
75 #define __rte_aligned(a) __attribute__((__aligned__(a)))
76
77 /**
78  * Force a structure to be packed
79  */
80 #define __rte_packed __attribute__((__packed__))
81
82 /******* Macro to mark functions and fields scheduled for removal *****/
83 #define __rte_deprecated        __attribute__((__deprecated__))
84
85 /*********** Macros to eliminate unused variable warnings ********/
86
87 /**
88  * short definition to mark a function parameter unused
89  */
90 #define __rte_unused __attribute__((__unused__))
91
92 /**
93  * definition to mark a variable or function parameter as used so
94  * as to avoid a compiler warning
95  */
96 #define RTE_SET_USED(x) (void)(x)
97
98 /*********** Macros for pointer arithmetic ********/
99
100 /**
101  * add a byte-value offset from a pointer
102  */
103 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
104
105 /**
106  * subtract a byte-value offset from a pointer
107  */
108 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
109
110 /**
111  * get the difference between two pointer values, i.e. how far apart
112  * in bytes are the locations they point two. It is assumed that
113  * ptr1 is greater than ptr2.
114  */
115 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
116
117 /*********** Macros/static functions for doing alignment ********/
118
119
120 /**
121  * Macro to align a pointer to a given power-of-two. The resultant
122  * pointer will be a pointer of the same type as the first parameter, and
123  * point to an address no higher than the first parameter. Second parameter
124  * must be a power-of-two value.
125  */
126 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
127         ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
128
129 /**
130  * Macro to align a value to a given power-of-two. The resultant value
131  * will be of the same type as the first parameter, and will be no
132  * bigger than the first parameter. Second parameter must be a
133  * power-of-two value.
134  */
135 #define RTE_ALIGN_FLOOR(val, align) \
136         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
137
138 /**
139  * Macro to align a pointer to a given power-of-two. The resultant
140  * pointer will be a pointer of the same type as the first parameter, and
141  * point to an address no lower than the first parameter. Second parameter
142  * must be a power-of-two value.
143  */
144 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
145         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
146
147 /**
148  * Macro to align a value to a given power-of-two. The resultant value
149  * will be of the same type as the first parameter, and will be no lower
150  * than the first parameter. Second parameter must be a power-of-two
151  * value.
152  */
153 #define RTE_ALIGN_CEIL(val, align) \
154         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
155
156 /**
157  * Macro to align a pointer to a given power-of-two. The resultant
158  * pointer will be a pointer of the same type as the first parameter, and
159  * point to an address no lower than the first parameter. Second parameter
160  * must be a power-of-two value.
161  * This function is the same as RTE_PTR_ALIGN_CEIL
162  */
163 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
164
165 /**
166  * Macro to align a value to a given power-of-two. The resultant
167  * value will be of the same type as the first parameter, and
168  * will be no lower than the first parameter. Second parameter
169  * must be a power-of-two value.
170  * This function is the same as RTE_ALIGN_CEIL
171  */
172 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
173
174 /**
175  * Checks if a pointer is aligned to a given power-of-two value
176  *
177  * @param ptr
178  *   The pointer whose alignment is to be checked
179  * @param align
180  *   The power-of-two value to which the ptr should be aligned
181  *
182  * @return
183  *   True(1) where the pointer is correctly aligned, false(0) otherwise
184  */
185 static inline int
186 rte_is_aligned(void *ptr, unsigned align)
187 {
188         return RTE_PTR_ALIGN(ptr, align) == ptr;
189 }
190
191 /*********** Macros for compile type checks ********/
192
193 /**
194  * Triggers an error at compilation time if the condition is true.
195  */
196 #ifndef __OPTIMIZE__
197 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
198 #else
199 extern int RTE_BUILD_BUG_ON_detected_error;
200 #define RTE_BUILD_BUG_ON(condition) do {             \
201         ((void)sizeof(char[1 - 2*!!(condition)]));   \
202         if (condition)                               \
203                 RTE_BUILD_BUG_ON_detected_error = 1; \
204 } while(0)
205 #endif
206
207 /*********** Macros to work with powers of 2 ********/
208
209 /**
210  * Returns true if n is a power of 2
211  * @param n
212  *     Number to check
213  * @return 1 if true, 0 otherwise
214  */
215 static inline int
216 rte_is_power_of_2(uint32_t n)
217 {
218         return n && !(n & (n - 1));
219 }
220
221 /**
222  * Aligns input parameter to the next power of 2
223  *
224  * @param x
225  *   The integer value to algin
226  *
227  * @return
228  *   Input parameter aligned to the next power of 2
229  */
230 static inline uint32_t
231 rte_align32pow2(uint32_t x)
232 {
233         x--;
234         x |= x >> 1;
235         x |= x >> 2;
236         x |= x >> 4;
237         x |= x >> 8;
238         x |= x >> 16;
239
240         return x + 1;
241 }
242
243 /**
244  * Aligns 64b input parameter to the next power of 2
245  *
246  * @param v
247  *   The 64b value to align
248  *
249  * @return
250  *   Input parameter aligned to the next power of 2
251  */
252 static inline uint64_t
253 rte_align64pow2(uint64_t v)
254 {
255         v--;
256         v |= v >> 1;
257         v |= v >> 2;
258         v |= v >> 4;
259         v |= v >> 8;
260         v |= v >> 16;
261         v |= v >> 32;
262
263         return v + 1;
264 }
265
266 /*********** Macros for calculating min and max **********/
267
268 /**
269  * Macro to return the minimum of two numbers
270  */
271 #define RTE_MIN(a, b) \
272         __extension__ ({ \
273                 typeof (a) _a = (a); \
274                 typeof (b) _b = (b); \
275                 _a < _b ? _a : _b; \
276         })
277
278 /**
279  * Macro to return the maximum of two numbers
280  */
281 #define RTE_MAX(a, b) \
282         __extension__ ({ \
283                 typeof (a) _a = (a); \
284                 typeof (b) _b = (b); \
285                 _a > _b ? _a : _b; \
286         })
287
288 /*********** Other general functions / macros ********/
289
290 #ifdef __SSE2__
291 #include <emmintrin.h>
292 /**
293  * PAUSE instruction for tight loops (avoid busy waiting)
294  */
295 static inline void
296 rte_pause (void)
297 {
298         _mm_pause();
299 }
300 #else
301 static inline void
302 rte_pause(void) {}
303 #endif
304
305 /**
306  * Searches the input parameter for the least significant set bit
307  * (starting from zero).
308  * If a least significant 1 bit is found, its bit index is returned.
309  * If the content of the input parameter is zero, then the content of the return
310  * value is undefined.
311  * @param v
312  *     input parameter, should not be zero.
313  * @return
314  *     least significant set bit in the input parameter.
315  */
316 static inline uint32_t
317 rte_bsf32(uint32_t v)
318 {
319         return __builtin_ctz(v);
320 }
321
322 #ifndef offsetof
323 /** Return the offset of a field in a structure. */
324 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
325 #endif
326
327 #define _RTE_STR(x) #x
328 /** Take a macro value and get a string version of it */
329 #define RTE_STR(x) _RTE_STR(x)
330
331 /** Mask value of type "tp" for the first "ln" bit set. */
332 #define RTE_LEN2MASK(ln, tp)    \
333         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
334
335 /** Number of elements in the array. */
336 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
337
338 /**
339  * Converts a numeric string to the equivalent uint64_t value.
340  * As well as straight number conversion, also recognises the suffixes
341  * k, m and g for kilobytes, megabytes and gigabytes respectively.
342  *
343  * If a negative number is passed in  i.e. a string with the first non-black
344  * character being "-", zero is returned. Zero is also returned in the case of
345  * an error with the strtoull call in the function.
346  *
347  * @param str
348  *     String containing number to convert.
349  * @return
350  *     Number.
351  */
352 static inline uint64_t
353 rte_str_to_size(const char *str)
354 {
355         char *endptr;
356         unsigned long long size;
357
358         while (isspace((int)*str))
359                 str++;
360         if (*str == '-')
361                 return 0;
362
363         errno = 0;
364         size = strtoull(str, &endptr, 0);
365         if (errno)
366                 return 0;
367
368         if (*endptr == ' ')
369                 endptr++; /* allow 1 space gap */
370
371         switch (*endptr){
372         case 'G': case 'g': size *= 1024; /* fall-through */
373         case 'M': case 'm': size *= 1024; /* fall-through */
374         case 'K': case 'k': size *= 1024; /* fall-through */
375         default:
376                 break;
377         }
378         return size;
379 }
380
381 /**
382  * Function to terminate the application immediately, printing an error
383  * message and returning the exit_code back to the shell.
384  *
385  * This function never returns
386  *
387  * @param exit_code
388  *     The exit code to be returned by the application
389  * @param format
390  *     The format string to be used for printing the message. This can include
391  *     printf format characters which will be expanded using any further parameters
392  *     to the function.
393  */
394 void
395 rte_exit(int exit_code, const char *format, ...)
396         __attribute__((noreturn))
397         __attribute__((format(printf, 2, 3)));
398
399 #ifdef __cplusplus
400 }
401 #endif
402
403 #endif