1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
11 * Generic, commonly-used macro and inline function definitions
25 #include <rte_config.h>
28 #define typeof __typeof__
35 /** C extension macro for environments lacking C11 features. */
36 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
37 #define RTE_STD_C11 __extension__
42 /** Define GCC_VERSION **/
43 #ifdef RTE_TOOLCHAIN_GCC
44 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
48 #ifdef RTE_ARCH_STRICT_ALIGN
49 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
50 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
51 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
53 typedef uint64_t unaligned_uint64_t;
54 typedef uint32_t unaligned_uint32_t;
55 typedef uint16_t unaligned_uint16_t;
61 #define __rte_aligned(a) __attribute__((__aligned__(a)))
64 * Force a structure to be packed
66 #define __rte_packed __attribute__((__packed__))
68 /******* Macro to mark functions and fields scheduled for removal *****/
69 #define __rte_deprecated __attribute__((__deprecated__))
72 * Mark a function or variable to a weak reference.
74 #define __rte_weak __attribute__((__weak__))
76 /*********** Macros to eliminate unused variable warnings ********/
79 * short definition to mark a function parameter unused
81 #define __rte_unused __attribute__((__unused__))
84 * definition to mark a variable or function parameter as used so
85 * as to avoid a compiler warning
87 #define RTE_SET_USED(x) (void)(x)
89 #define RTE_PRIORITY_LOG 101
90 #define RTE_PRIORITY_BUS 110
91 #define RTE_PRIORITY_CLASS 120
92 #define RTE_PRIORITY_LAST 65535
94 #define RTE_PRIO(prio) \
98 * Run function before main() with high priority.
101 * Constructor function.
103 * Priority number must be above 100.
104 * Lowest number is the first to run.
106 #define RTE_INIT_PRIO(func, prio) \
107 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
110 * Run function before main() with low priority.
112 * The constructor will be run after prioritized constructors.
115 * Constructor function.
117 #define RTE_INIT(func) \
118 RTE_INIT_PRIO(func, LAST)
121 * Run after main() with low priority.
124 * Destructor function name.
126 * Priority number must be above 100.
127 * Lowest number is the last to run.
129 #define RTE_FINI_PRIO(func, prio) \
130 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
133 * Run after main() with high priority.
135 * The destructor will be run *before* prioritized destructors.
138 * Destructor function name.
140 #define RTE_FINI(func) \
141 RTE_FINI_PRIO(func, LAST)
144 * Force a function to be inlined
146 #define __rte_always_inline inline __attribute__((always_inline))
149 * Force a function to be noinlined
151 #define __rte_noinline __attribute__((noinline))
153 /*********** Macros for pointer arithmetic ********/
156 * add a byte-value offset to a pointer
158 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
161 * subtract a byte-value offset from a pointer
163 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
166 * get the difference between two pointer values, i.e. how far apart
167 * in bytes are the locations they point two. It is assumed that
168 * ptr1 is greater than ptr2.
170 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
173 * Workaround to cast a const field of a structure to non-const type.
175 #define RTE_CAST_FIELD(var, field, type) \
176 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
178 /*********** Macros/static functions for doing alignment ********/
182 * Macro to align a pointer to a given power-of-two. The resultant
183 * pointer will be a pointer of the same type as the first parameter, and
184 * point to an address no higher than the first parameter. Second parameter
185 * must be a power-of-two value.
187 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
188 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
191 * Macro to align a value to a given power-of-two. The resultant value
192 * will be of the same type as the first parameter, and will be no
193 * bigger than the first parameter. Second parameter must be a
194 * power-of-two value.
196 #define RTE_ALIGN_FLOOR(val, align) \
197 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
200 * Macro to align a pointer to a given power-of-two. The resultant
201 * pointer will be a pointer of the same type as the first parameter, and
202 * point to an address no lower than the first parameter. Second parameter
203 * must be a power-of-two value.
205 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
206 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
209 * Macro to align a value to a given power-of-two. The resultant value
210 * will be of the same type as the first parameter, and will be no lower
211 * than the first parameter. Second parameter must be a power-of-two
214 #define RTE_ALIGN_CEIL(val, align) \
215 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
218 * Macro to align a pointer to a given power-of-two. The resultant
219 * pointer will be a pointer of the same type as the first parameter, and
220 * point to an address no lower than the first parameter. Second parameter
221 * must be a power-of-two value.
222 * This function is the same as RTE_PTR_ALIGN_CEIL
224 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
227 * Macro to align a value to a given power-of-two. The resultant
228 * value will be of the same type as the first parameter, and
229 * will be no lower than the first parameter. Second parameter
230 * must be a power-of-two value.
231 * This function is the same as RTE_ALIGN_CEIL
233 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
236 * Macro to align a value to the multiple of given value. The resultant
237 * value will be of the same type as the first parameter and will be no lower
238 * than the first parameter.
240 #define RTE_ALIGN_MUL_CEIL(v, mul) \
241 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
244 * Macro to align a value to the multiple of given value. The resultant
245 * value will be of the same type as the first parameter and will be no higher
246 * than the first parameter.
248 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
249 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
252 * Checks if a pointer is aligned to a given power-of-two value
255 * The pointer whose alignment is to be checked
257 * The power-of-two value to which the ptr should be aligned
260 * True(1) where the pointer is correctly aligned, false(0) otherwise
263 rte_is_aligned(void *ptr, unsigned align)
265 return RTE_PTR_ALIGN(ptr, align) == ptr;
268 /*********** Macros for compile type checks ********/
271 * Triggers an error at compilation time if the condition is true.
273 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
276 * Combines 32b inputs most significant set bits into the least
277 * significant bits to construct a value with the same MSBs as x
278 * but all 1's under it.
281 * The integer whose MSBs need to be combined with its LSBs
283 * The combined value.
285 static inline uint32_t
286 rte_combine32ms1b(register uint32_t x)
298 * Combines 64b inputs most significant set bits into the least
299 * significant bits to construct a value with the same MSBs as x
300 * but all 1's under it.
303 * The integer whose MSBs need to be combined with its LSBs
305 * The combined value.
307 static inline uint64_t
308 rte_combine64ms1b(register uint64_t v)
320 /*********** Macros to work with powers of 2 ********/
323 * Macro to return 1 if n is a power of 2, 0 otherwise
325 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
328 * Returns true if n is a power of 2
331 * @return 1 if true, 0 otherwise
334 rte_is_power_of_2(uint32_t n)
336 return n && !(n & (n - 1));
340 * Aligns input parameter to the next power of 2
343 * The integer value to algin
346 * Input parameter aligned to the next power of 2
348 static inline uint32_t
349 rte_align32pow2(uint32_t x)
352 x = rte_combine32ms1b(x);
358 * Aligns input parameter to the previous power of 2
361 * The integer value to algin
364 * Input parameter aligned to the previous power of 2
366 static inline uint32_t
367 rte_align32prevpow2(uint32_t x)
369 x = rte_combine32ms1b(x);
375 * Aligns 64b input parameter to the next power of 2
378 * The 64b value to align
381 * Input parameter aligned to the next power of 2
383 static inline uint64_t
384 rte_align64pow2(uint64_t v)
387 v = rte_combine64ms1b(v);
393 * Aligns 64b input parameter to the previous power of 2
396 * The 64b value to align
399 * Input parameter aligned to the previous power of 2
401 static inline uint64_t
402 rte_align64prevpow2(uint64_t v)
404 v = rte_combine64ms1b(v);
409 /*********** Macros for calculating min and max **********/
412 * Macro to return the minimum of two numbers
414 #define RTE_MIN(a, b) \
416 typeof (a) _a = (a); \
417 typeof (b) _b = (b); \
422 * Macro to return the maximum of two numbers
424 #define RTE_MAX(a, b) \
426 typeof (a) _a = (a); \
427 typeof (b) _b = (b); \
431 /*********** Other general functions / macros ********/
434 * Searches the input parameter for the least significant set bit
435 * (starting from zero).
436 * If a least significant 1 bit is found, its bit index is returned.
437 * If the content of the input parameter is zero, then the content of the return
438 * value is undefined.
440 * input parameter, should not be zero.
442 * least significant set bit in the input parameter.
444 static inline uint32_t
445 rte_bsf32(uint32_t v)
447 return (uint32_t)__builtin_ctz(v);
451 * Searches the input parameter for the least significant set bit
452 * (starting from zero). Safe version (checks for input parameter being zero).
454 * @warning ``pos`` must be a valid pointer. It is not checked!
457 * The input parameter.
459 * If ``v`` was not 0, this value will contain position of least significant
460 * bit within the input parameter.
462 * Returns 0 if ``v`` was 0, otherwise returns 1.
465 rte_bsf32_safe(uint64_t v, uint32_t *pos)
475 * Return the rounded-up log2 of a integer.
478 * The input parameter.
480 * The rounded-up log2 of the input, or 0 if the input is 0.
482 static inline uint32_t
483 rte_log2_u32(uint32_t v)
487 v = rte_align32pow2(v);
493 * Return the last (most-significant) bit set.
495 * @note The last (most significant) bit is at position 32.
496 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
499 * The input parameter.
501 * The last (most-significant) bit set, or 0 if the input is 0.
504 rte_fls_u32(uint32_t x)
506 return (x == 0) ? 0 : 32 - __builtin_clz(x);
510 * Searches the input parameter for the least significant set bit
511 * (starting from zero).
512 * If a least significant 1 bit is found, its bit index is returned.
513 * If the content of the input parameter is zero, then the content of the return
514 * value is undefined.
516 * input parameter, should not be zero.
518 * least significant set bit in the input parameter.
521 rte_bsf64(uint64_t v)
523 return (uint32_t)__builtin_ctzll(v);
527 * Searches the input parameter for the least significant set bit
528 * (starting from zero). Safe version (checks for input parameter being zero).
530 * @warning ``pos`` must be a valid pointer. It is not checked!
533 * The input parameter.
535 * If ``v`` was not 0, this value will contain position of least significant
536 * bit within the input parameter.
538 * Returns 0 if ``v`` was 0, otherwise returns 1.
541 rte_bsf64_safe(uint64_t v, uint32_t *pos)
551 * Return the last (most-significant) bit set.
553 * @note The last (most significant) bit is at position 64.
554 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
555 * rte_fls_u64(0x8000000000000000) = 64
558 * The input parameter.
560 * The last (most-significant) bit set, or 0 if the input is 0.
563 rte_fls_u64(uint64_t x)
565 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
569 * Return the rounded-up log2 of a 64-bit integer.
572 * The input parameter.
574 * The rounded-up log2 of the input, or 0 if the input is 0.
576 static inline uint32_t
577 rte_log2_u64(uint64_t v)
581 v = rte_align64pow2(v);
582 /* we checked for v being 0 already, so no undefined behavior */
587 /** Return the offset of a field in a structure. */
588 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
592 * Return pointer to the wrapping struct instance.
602 * struct child *x = obtain(...);
603 * struct wrapper *w = container_of(x, struct wrapper, c);
606 #define container_of(ptr, type, member) __extension__ ({ \
607 const typeof(((type *)0)->member) *_ptr = (ptr); \
608 __attribute__((unused)) type *_target_ptr = \
610 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
614 #define _RTE_STR(x) #x
615 /** Take a macro value and get a string version of it */
616 #define RTE_STR(x) _RTE_STR(x)
619 * ISO C helpers to modify format strings using variadic macros.
620 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
621 * An empty %s argument is appended to avoid a dangling comma.
623 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
624 #define RTE_FMT_HEAD(fmt, ...) fmt
625 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
627 /** Mask value of type "tp" for the first "ln" bit set. */
628 #define RTE_LEN2MASK(ln, tp) \
629 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
631 /** Number of elements in the array. */
632 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
635 * Converts a numeric string to the equivalent uint64_t value.
636 * As well as straight number conversion, also recognises the suffixes
637 * k, m and g for kilobytes, megabytes and gigabytes respectively.
639 * If a negative number is passed in i.e. a string with the first non-black
640 * character being "-", zero is returned. Zero is also returned in the case of
641 * an error with the strtoull call in the function.
644 * String containing number to convert.
648 static inline uint64_t
649 rte_str_to_size(const char *str)
652 unsigned long long size;
654 while (isspace((int)*str))
660 size = strtoull(str, &endptr, 0);
665 endptr++; /* allow 1 space gap */
668 case 'G': case 'g': size *= 1024; /* fall-through */
669 case 'M': case 'm': size *= 1024; /* fall-through */
670 case 'K': case 'k': size *= 1024; /* fall-through */
678 * Function to terminate the application immediately, printing an error
679 * message and returning the exit_code back to the shell.
681 * This function never returns
684 * The exit code to be returned by the application
686 * The format string to be used for printing the message. This can include
687 * printf format characters which will be expanded using any further parameters
691 rte_exit(int exit_code, const char *format, ...)
692 __attribute__((noreturn))
693 __attribute__((format(printf, 2, 3)));