4 * Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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.
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.
34 #ifndef _RTE_COMMON_H_
35 #define _RTE_COMMON_H_
40 * Generic, commonly-used macro and inline function definitions
55 #define typeof __typeof__
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)));
67 typedef uint64_t unaligned_uint64_t;
68 typedef uint32_t unaligned_uint32_t;
69 typedef uint16_t unaligned_uint16_t;
72 /*********** Macros to eliminate unused variable warnings ********/
75 * short definition to mark a function parameter unused
77 #define __rte_unused __attribute__((__unused__))
80 * definition to mark a variable or function parameter as used so
81 * as to avoid a compiler warning
83 #define RTE_SET_USED(x) (void)(x)
85 /*********** Macros for pointer arithmetic ********/
88 * add a byte-value offset from a pointer
90 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
93 * subtract a byte-value offset from a pointer
95 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
98 * get the difference between two pointer values, i.e. how far apart
99 * in bytes are the locations they point two. It is assumed that
100 * ptr1 is greater than ptr2.
102 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
104 /*********** Macros/static functions for doing alignment ********/
108 * Macro to align a pointer to a given power-of-two. The resultant
109 * pointer will be a pointer of the same type as the first parameter, and
110 * point to an address no higher than the first parameter. Second parameter
111 * must be a power-of-two value.
113 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
114 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
117 * Macro to align a value to a given power-of-two. The resultant value
118 * will be of the same type as the first parameter, and will be no
119 * bigger than the first parameter. Second parameter must be a
120 * power-of-two value.
122 #define RTE_ALIGN_FLOOR(val, align) \
123 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
126 * Macro to align a pointer to a given power-of-two. The resultant
127 * pointer will be a pointer of the same type as the first parameter, and
128 * point to an address no lower than the first parameter. Second parameter
129 * must be a power-of-two value.
131 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
132 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
135 * Macro to align a value to a given power-of-two. The resultant value
136 * will be of the same type as the first parameter, and will be no lower
137 * than the first parameter. Second parameter must be a power-of-two
140 #define RTE_ALIGN_CEIL(val, align) \
141 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
144 * Macro to align a pointer to a given power-of-two. The resultant
145 * pointer will be a pointer of the same type as the first parameter, and
146 * point to an address no lower than the first parameter. Second parameter
147 * must be a power-of-two value.
148 * This function is the same as RTE_PTR_ALIGN_CEIL
150 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
153 * Macro to align a value to a given power-of-two. The resultant
154 * value will be of the same type as the first parameter, and
155 * will be no lower than the first parameter. Second parameter
156 * must be a power-of-two value.
157 * This function is the same as RTE_ALIGN_CEIL
159 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
162 * Checks if a pointer is aligned to a given power-of-two value
165 * The pointer whose alignment is to be checked
167 * The power-of-two value to which the ptr should be aligned
170 * True(1) where the pointer is correctly aligned, false(0) otherwise
173 rte_is_aligned(void *ptr, unsigned align)
175 return RTE_PTR_ALIGN(ptr, align) == ptr;
178 /*********** Macros for compile type checks ********/
181 * Triggers an error at compilation time if the condition is true.
184 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
186 extern int RTE_BUILD_BUG_ON_detected_error;
187 #define RTE_BUILD_BUG_ON(condition) do { \
188 ((void)sizeof(char[1 - 2*!!(condition)])); \
190 RTE_BUILD_BUG_ON_detected_error = 1; \
194 /*********** Macros to work with powers of 2 ********/
197 * Returns true if n is a power of 2
200 * @return 1 if true, 0 otherwise
203 rte_is_power_of_2(uint32_t n)
205 return n && !(n & (n - 1));
209 * Aligns input parameter to the next power of 2
212 * The integer value to algin
215 * Input parameter aligned to the next power of 2
217 static inline uint32_t
218 rte_align32pow2(uint32_t x)
231 * Aligns 64b input parameter to the next power of 2
234 * The 64b value to align
237 * Input parameter aligned to the next power of 2
239 static inline uint64_t
240 rte_align64pow2(uint64_t v)
253 /*********** Macros for calculating min and max **********/
256 * Macro to return the minimum of two numbers
258 #define RTE_MIN(a, b) ({ \
259 typeof (a) _a = (a); \
260 typeof (b) _b = (b); \
265 * Macro to return the maximum of two numbers
267 #define RTE_MAX(a, b) ({ \
268 typeof (a) _a = (a); \
269 typeof (b) _b = (b); \
273 /*********** Other general functions / macros ********/
276 #include <emmintrin.h>
278 * PAUSE instruction for tight loops (avoid busy waiting)
291 * Searches the input parameter for the least significant set bit
292 * (starting from zero).
293 * If a least significant 1 bit is found, its bit index is returned.
294 * If the content of the input parameter is zero, then the content of the return
295 * value is undefined.
297 * input parameter, should not be zero.
299 * least significant set bit in the input parameter.
301 static inline uint32_t
302 rte_bsf32(uint32_t v)
304 return __builtin_ctz(v);
308 /** Return the offset of a field in a structure. */
309 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
312 #define _RTE_STR(x) #x
313 /** Take a macro value and get a string version of it */
314 #define RTE_STR(x) _RTE_STR(x)
316 /** Mask value of type "tp" for the first "ln" bit set. */
317 #define RTE_LEN2MASK(ln, tp) \
318 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
320 /** Number of elements in the array. */
321 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
324 * Converts a numeric string to the equivalent uint64_t value.
325 * As well as straight number conversion, also recognises the suffixes
326 * k, m and g for kilobytes, megabytes and gigabytes respectively.
328 * If a negative number is passed in i.e. a string with the first non-black
329 * character being "-", zero is returned. Zero is also returned in the case of
330 * an error with the strtoull call in the function.
333 * String containing number to convert.
337 static inline uint64_t
338 rte_str_to_size(const char *str)
341 unsigned long long size;
343 while (isspace((int)*str))
349 size = strtoull(str, &endptr, 0);
354 endptr++; /* allow 1 space gap */
357 case 'G': case 'g': size *= 1024; /* fall-through */
358 case 'M': case 'm': size *= 1024; /* fall-through */
359 case 'K': case 'k': size *= 1024; /* fall-through */
367 * Function to terminate the application immediately, printing an error
368 * message and returning the exit_code back to the shell.
370 * This function never returns
373 * The exit code to be returned by the application
375 * The format string to be used for printing the message. This can include
376 * printf format characters which will be expanded using any further parameters
380 rte_exit(int exit_code, const char *format, ...)
381 __attribute__((noreturn))
382 __attribute__((format(printf, 2, 3)));