remove trailing whitespaces
[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 Intel 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 /*********** Macros to eliminate unused variable warnings ********/
55
56 /**
57  * short definition to mark a function parameter unused
58  */
59 #define __rte_unused __attribute__((__unused__))
60
61 /**
62  * definition to mark a variable or function parameter as used so
63  * as to avoid a compiler warning
64  */
65 #define RTE_SET_USED(x) (void)(x)
66
67 /*********** Macros for pointer arithmetic ********/
68
69 /**
70  * add a byte-value offset from a pointer
71  */
72 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
73
74 /**
75  * subtract a byte-value offset from a pointer
76  */
77 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
78
79 /**
80  * get the difference between two pointer values, i.e. how far apart
81  * in bytes are the locations they point two. It is assumed that
82  * ptr1 is greater than ptr2.
83  */
84 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
85
86 /*********** Macros/static functions for doing alignment ********/
87
88 /**
89  * Function which rounds an unsigned int down to a given power-of-two value.
90  * Takes uintptr_t types as parameters, as this type of operation is most
91  * commonly done for pointer alignment. (See also RTE_ALIGN_FLOOR,
92  * RTE_ALIGN_CEIL, RTE_ALIGN, RTE_PTR_ALIGN_FLOOR, RTE_PTR_ALIGN_CEL,
93  * RTE_PTR_ALIGN macros)
94  * @param ptr
95  *   The value to be rounded down
96  * @param align
97  *   The power-of-two of which the result must be a multiple.
98  * @return
99  *   Function returns a properly aligned value where align is a power-of-two.
100  *   If align is not a power-of-two, result will be incorrect.
101  */
102 static inline uintptr_t
103 rte_align_floor_int(uintptr_t ptr, uintptr_t align)
104 {
105         return (ptr & ~(align - 1));
106 }
107
108 /**
109  * Macro to align a pointer to a given power-of-two. The resultant
110  * pointer will be a pointer of the same type as the first parameter, and
111  * point to an address no higher than the first parameter. Second parameter
112  * must be a power-of-two value.
113  */
114 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
115         (typeof(ptr))rte_align_floor_int((uintptr_t)ptr, align)
116
117 /**
118  * Macro to align a value to a given power-of-two. The resultant value
119  * will be of the same type as the first parameter, and will be no
120  * bigger than the first parameter. Second parameter must be a
121  * power-of-two value.
122  */
123 #define RTE_ALIGN_FLOOR(val, align) \
124         (typeof(val))((val) & (~((typeof(val))((align) - 1))))
125
126 /**
127  * Macro to align a pointer to a given power-of-two. The resultant
128  * pointer will be a pointer of the same type as the first parameter, and
129  * point to an address no lower than the first parameter. Second parameter
130  * must be a power-of-two value.
131  */
132 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
133         RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
134
135 /**
136  * Macro to align a value to a given power-of-two. The resultant value
137  * will be of the same type as the first parameter, and will be no lower
138  * than the first parameter. Second parameter must be a power-of-two
139  * value.
140  */
141 #define RTE_ALIGN_CEIL(val, align) \
142         RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
143
144 /**
145  * Macro to align a pointer to a given power-of-two. The resultant
146  * pointer will be a pointer of the same type as the first parameter, and
147  * point to an address no lower than the first parameter. Second parameter
148  * must be a power-of-two value.
149  * This function is the same as RTE_PTR_ALIGN_CEIL
150  */
151 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
152
153 /**
154  * Macro to align a value to a given power-of-two. The resultant
155  * value will be of the same type as the first parameter, and
156  * will be no lower than the first parameter. Second parameter
157  * must be a power-of-two value.
158  * This function is the same as RTE_ALIGN_CEIL
159  */
160 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
161
162 /**
163  * Checks if a pointer is aligned to a given power-of-two value
164  *
165  * @param ptr
166  *   The pointer whose alignment is to be checked
167  * @param align
168  *   The power-of-two value to which the ptr should be aligned
169  *
170  * @return
171  *   True(1) where the pointer is correctly aligned, false(0) otherwise
172  */
173 static inline int
174 rte_is_aligned(void *ptr, unsigned align)
175 {
176         return RTE_PTR_ALIGN(ptr, align) == ptr;
177 }
178
179 /*********** Macros for compile type checks ********/
180
181 /**
182  * Triggers an error at compilation time if the condition is true.
183  */
184 #ifndef __OPTIMIZE__
185 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
186 #else
187 extern int RTE_BUILD_BUG_ON_detected_error;
188 #define RTE_BUILD_BUG_ON(condition) do {             \
189         ((void)sizeof(char[1 - 2*!!(condition)]));   \
190         if (condition)                               \
191                 RTE_BUILD_BUG_ON_detected_error = 1; \
192 } while(0)
193 #endif
194
195 /*********** Macros to work with powers of 2 ********/
196
197 /**
198  * Returns true if n is a power of 2
199  * @param n
200  *     Number to check
201  * @return 1 if true, 0 otherwise
202  */
203 static inline int
204 rte_is_power_of_2(uint32_t n)
205 {
206         return ((n-1) & n) == 0;
207 }
208
209 /**
210  * Aligns input parameter to the next power of 2
211  *
212  * @param x
213  *   The integer value to algin
214  *
215  * @return
216  *   Input parameter aligned to the next power of 2
217  */
218 static inline uint32_t
219 rte_align32pow2(uint32_t x)
220 {
221         x--;
222         x |= x >> 1;
223         x |= x >> 2;
224         x |= x >> 4;
225         x |= x >> 8;
226         x |= x >> 16;
227
228         return x + 1;
229 }
230
231 /**
232  * Aligns 64b input parameter to the next power of 2
233  *
234  * @param x
235  *   The 64b value to algin
236  *
237  * @return
238  *   Input parameter aligned to the next power of 2
239  */
240 static inline uint64_t
241 rte_align64pow2(uint64_t v)
242 {
243         v--;
244         v |= v >> 1;
245         v |= v >> 2;
246         v |= v >> 4;
247         v |= v >> 8;
248         v |= v >> 16;
249         v |= v >> 32;
250
251         return v + 1;
252 }
253
254 /*********** Macros for calculating min and max **********/
255
256 /**
257  * Macro to return the minimum of two numbers
258  */
259 #define RTE_MIN(a, b) ({ \
260                 typeof (a) _a = (a); \
261                 typeof (b) _b = (b); \
262                 _a < _b ? _a : _b; \
263         })
264
265 /**
266  * Macro to return the maximum of two numbers
267  */
268 #define RTE_MAX(a, b) ({ \
269                 typeof (a) _a = (a); \
270                 typeof (b) _b = (b); \
271                 _a > _b ? _a : _b; \
272         })
273
274 /*********** Other general functions / macros ********/
275
276 #ifdef __SSE2__
277 #include <emmintrin.h>
278 /**
279  * PAUSE instruction for tight loops (avoid busy waiting)
280  */
281 static inline void
282 rte_pause (void)
283 {
284         _mm_pause();
285 }
286 #else
287 static inline void
288 rte_pause(void) {}
289 #endif
290
291 /**
292  * Searches the input parameter for the least significant set bit
293  * (starting from zero).
294  * If a least significant 1 bit is found, its bit index is returned.
295  * If the content of the input parameter is zero, then the content of the return
296  * value is undefined.
297  * @param v
298  *     input parameter, should not be zero.
299  * @return
300  *     least significant set bit in the input parameter.
301  */
302 static inline uint32_t
303 rte_bsf32(uint32_t v)
304 {
305         return (__builtin_ctz(v));
306 }
307
308 #ifndef offsetof
309 /** Return the offset of a field in a structure. */
310 #define offsetof(TYPE, MEMBER)  __builtin_offsetof (TYPE, MEMBER)
311 #endif
312
313 #define _RTE_STR(x) #x
314 /** Take a macro value and get a string version of it */
315 #define RTE_STR(x) _RTE_STR(x)
316
317 /** Mask value of type <tp> for the first <ln> bit set. */
318 #define RTE_LEN2MASK(ln, tp)    \
319         ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
320
321 /** Number of elements in the array. */
322 #define RTE_DIM(a)      (sizeof (a) / sizeof ((a)[0]))
323
324 /**
325  * Converts a numeric string to the equivalent uint64_t value.
326  * As well as straight number conversion, also recognises the suffixes
327  * k, m and g for kilobytes, megabytes and gigabytes respectively.
328  *
329  * If a negative number is passed in  i.e. a string with the first non-black
330  * character being "-", zero is returned. Zero is also returned in the case of
331  * an error with the strtoull call in the function.
332  *
333  * @param str
334  *     String containing number to convert.
335  * @return
336  *     Number.
337  */
338 static inline uint64_t
339 rte_str_to_size(const char *str)
340 {
341         char *endptr;
342         unsigned long long size;
343
344         while (isspace((int)*str))
345                 str++;
346         if (*str == '-')
347                 return 0;
348
349         errno = 0;
350         size = strtoull(str, &endptr, 0);
351         if (errno)
352                 return 0;
353
354         if (*endptr == ' ')
355                 endptr++; /* allow 1 space gap */
356
357         switch (*endptr){
358         case 'G': case 'g': size *= 1024; /* fall-through */
359         case 'M': case 'm': size *= 1024; /* fall-through */
360         case 'K': case 'k': size *= 1024; /* fall-through */
361         default:
362                 break;
363         }
364         return size;
365 }
366
367 /**
368  * Function to terminate the application immediately, printing an error
369  * message and returning the exit_code back to the shell.
370  *
371  * This function never returns
372  *
373  * @param exit_code
374  *     The exit code to be returned by the application
375  * @param format
376  *     The format string to be used for printing the message. This can include
377  *     printf format characters which will be expanded using any further parameters
378  *     to the function.
379  */
380 void
381 rte_exit(int exit_code, const char *format, ...)
382         __attribute__((noreturn))
383         __attribute__((format(printf, 2, 3)));
384
385 #ifdef __cplusplus
386 }
387 #endif
388
389 #endif