1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2019 Intel Corporation
11 * Generic, commonly-used macro and inline function definitions
25 #include <rte_config.h>
27 /* OS specific include */
31 #define typeof __typeof__
38 /** C extension macro for environments lacking C11 features. */
39 #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 201112L
40 #define RTE_STD_C11 __extension__
45 /** Define GCC_VERSION **/
46 #ifdef RTE_TOOLCHAIN_GCC
47 #define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + \
51 #ifdef RTE_ARCH_STRICT_ALIGN
52 typedef uint64_t unaligned_uint64_t __attribute__ ((aligned(1)));
53 typedef uint32_t unaligned_uint32_t __attribute__ ((aligned(1)));
54 typedef uint16_t unaligned_uint16_t __attribute__ ((aligned(1)));
56 typedef uint64_t unaligned_uint64_t;
57 typedef uint32_t unaligned_uint32_t;
58 typedef uint16_t unaligned_uint16_t;
64 #define __rte_aligned(a) __attribute__((__aligned__(a)))
67 * Force a structure to be packed
69 #define __rte_packed __attribute__((__packed__))
71 /******* Macro to mark functions and fields scheduled for removal *****/
72 #define __rte_deprecated __attribute__((__deprecated__))
75 * Mark a function or variable to a weak reference.
77 #define __rte_weak __attribute__((__weak__))
79 /*********** Macros to eliminate unused variable warnings ********/
82 * short definition to mark a function parameter unused
84 #define __rte_unused __attribute__((__unused__))
87 * definition to mark a variable or function parameter as used so
88 * as to avoid a compiler warning
90 #define RTE_SET_USED(x) (void)(x)
92 #define RTE_PRIORITY_LOG 101
93 #define RTE_PRIORITY_BUS 110
94 #define RTE_PRIORITY_CLASS 120
95 #define RTE_PRIORITY_LAST 65535
97 #define RTE_PRIO(prio) \
101 * Run function before main() with high priority.
104 * Constructor function.
106 * Priority number must be above 100.
107 * Lowest number is the first to run.
109 #define RTE_INIT_PRIO(func, prio) \
110 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
113 * Run function before main() with low priority.
115 * The constructor will be run after prioritized constructors.
118 * Constructor function.
120 #define RTE_INIT(func) \
121 RTE_INIT_PRIO(func, LAST)
124 * Run after main() with low priority.
127 * Destructor function name.
129 * Priority number must be above 100.
130 * Lowest number is the last to run.
132 #define RTE_FINI_PRIO(func, prio) \
133 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
136 * Run after main() with high priority.
138 * The destructor will be run *before* prioritized destructors.
141 * Destructor function name.
143 #define RTE_FINI(func) \
144 RTE_FINI_PRIO(func, LAST)
147 * Force a function to be inlined
149 #define __rte_always_inline inline __attribute__((always_inline))
152 * Force a function to be noinlined
154 #define __rte_noinline __attribute__((noinline))
156 /*********** Macros for pointer arithmetic ********/
159 * add a byte-value offset to a pointer
161 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
164 * subtract a byte-value offset from a pointer
166 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
169 * get the difference between two pointer values, i.e. how far apart
170 * in bytes are the locations they point two. It is assumed that
171 * ptr1 is greater than ptr2.
173 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
176 * Workaround to cast a const field of a structure to non-const type.
178 #define RTE_CAST_FIELD(var, field, type) \
179 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
181 /*********** Macros/static functions for doing alignment ********/
185 * Macro to align a pointer to a given power-of-two. The resultant
186 * pointer will be a pointer of the same type as the first parameter, and
187 * point to an address no higher than the first parameter. Second parameter
188 * must be a power-of-two value.
190 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
191 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
194 * Macro to align a value to a given power-of-two. The resultant value
195 * will be of the same type as the first parameter, and will be no
196 * bigger than the first parameter. Second parameter must be a
197 * power-of-two value.
199 #define RTE_ALIGN_FLOOR(val, align) \
200 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
203 * Macro to align a pointer to a given power-of-two. The resultant
204 * pointer will be a pointer of the same type as the first parameter, and
205 * point to an address no lower than the first parameter. Second parameter
206 * must be a power-of-two value.
208 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
209 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
212 * Macro to align a value to a given power-of-two. The resultant value
213 * will be of the same type as the first parameter, and will be no lower
214 * than the first parameter. Second parameter must be a power-of-two
217 #define RTE_ALIGN_CEIL(val, align) \
218 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
221 * Macro to align a pointer to a given power-of-two. The resultant
222 * pointer will be a pointer of the same type as the first parameter, and
223 * point to an address no lower than the first parameter. Second parameter
224 * must be a power-of-two value.
225 * This function is the same as RTE_PTR_ALIGN_CEIL
227 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
230 * Macro to align a value to a given power-of-two. The resultant
231 * value will be of the same type as the first parameter, and
232 * will be no lower than the first parameter. Second parameter
233 * must be a power-of-two value.
234 * This function is the same as RTE_ALIGN_CEIL
236 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
239 * Macro to align a value to the multiple of given value. The resultant
240 * value will be of the same type as the first parameter and will be no lower
241 * than the first parameter.
243 #define RTE_ALIGN_MUL_CEIL(v, mul) \
244 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
247 * Macro to align a value to the multiple of given value. The resultant
248 * value will be of the same type as the first parameter and will be no higher
249 * than the first parameter.
251 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
252 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
255 * Macro to align value to the nearest multiple of the given value.
256 * The resultant value might be greater than or less than the first parameter
257 * whichever difference is the lowest.
259 #define RTE_ALIGN_MUL_NEAR(v, mul) \
261 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
262 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
263 (ceil - v) > (v - floor) ? floor : ceil; \
267 * Checks if a pointer is aligned to a given power-of-two value
270 * The pointer whose alignment is to be checked
272 * The power-of-two value to which the ptr should be aligned
275 * True(1) where the pointer is correctly aligned, false(0) otherwise
278 rte_is_aligned(void *ptr, unsigned align)
280 return RTE_PTR_ALIGN(ptr, align) == ptr;
283 /*********** Macros for compile type checks ********/
286 * Triggers an error at compilation time if the condition is true.
288 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
291 * Combines 32b inputs most significant set bits into the least
292 * significant bits to construct a value with the same MSBs as x
293 * but all 1's under it.
296 * The integer whose MSBs need to be combined with its LSBs
298 * The combined value.
300 static inline uint32_t
301 rte_combine32ms1b(register uint32_t x)
313 * Combines 64b inputs most significant set bits into the least
314 * significant bits to construct a value with the same MSBs as x
315 * but all 1's under it.
318 * The integer whose MSBs need to be combined with its LSBs
320 * The combined value.
322 static inline uint64_t
323 rte_combine64ms1b(register uint64_t v)
335 /*********** Macros to work with powers of 2 ********/
338 * Macro to return 1 if n is a power of 2, 0 otherwise
340 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
343 * Returns true if n is a power of 2
346 * @return 1 if true, 0 otherwise
349 rte_is_power_of_2(uint32_t n)
351 return n && !(n & (n - 1));
355 * Aligns input parameter to the next power of 2
358 * The integer value to algin
361 * Input parameter aligned to the next power of 2
363 static inline uint32_t
364 rte_align32pow2(uint32_t x)
367 x = rte_combine32ms1b(x);
373 * Aligns input parameter to the previous power of 2
376 * The integer value to algin
379 * Input parameter aligned to the previous power of 2
381 static inline uint32_t
382 rte_align32prevpow2(uint32_t x)
384 x = rte_combine32ms1b(x);
390 * Aligns 64b input parameter to the next power of 2
393 * The 64b value to align
396 * Input parameter aligned to the next power of 2
398 static inline uint64_t
399 rte_align64pow2(uint64_t v)
402 v = rte_combine64ms1b(v);
408 * Aligns 64b input parameter to the previous power of 2
411 * The 64b value to align
414 * Input parameter aligned to the previous power of 2
416 static inline uint64_t
417 rte_align64prevpow2(uint64_t v)
419 v = rte_combine64ms1b(v);
424 /*********** Macros for calculating min and max **********/
427 * Macro to return the minimum of two numbers
429 #define RTE_MIN(a, b) \
431 typeof (a) _a = (a); \
432 typeof (b) _b = (b); \
437 * Macro to return the maximum of two numbers
439 #define RTE_MAX(a, b) \
441 typeof (a) _a = (a); \
442 typeof (b) _b = (b); \
446 /*********** Other general functions / macros ********/
449 * Searches the input parameter for the least significant set bit
450 * (starting from zero).
451 * If a least significant 1 bit is found, its bit index is returned.
452 * If the content of the input parameter is zero, then the content of the return
453 * value is undefined.
455 * input parameter, should not be zero.
457 * least significant set bit in the input parameter.
459 static inline uint32_t
460 rte_bsf32(uint32_t v)
462 return (uint32_t)__builtin_ctz(v);
466 * Searches the input parameter for the least significant set bit
467 * (starting from zero). Safe version (checks for input parameter being zero).
469 * @warning ``pos`` must be a valid pointer. It is not checked!
472 * The input parameter.
474 * If ``v`` was not 0, this value will contain position of least significant
475 * bit within the input parameter.
477 * Returns 0 if ``v`` was 0, otherwise returns 1.
480 rte_bsf32_safe(uint64_t v, uint32_t *pos)
490 * Return the rounded-up log2 of a integer.
493 * The input parameter.
495 * The rounded-up log2 of the input, or 0 if the input is 0.
497 static inline uint32_t
498 rte_log2_u32(uint32_t v)
502 v = rte_align32pow2(v);
508 * Return the last (most-significant) bit set.
510 * @note The last (most significant) bit is at position 32.
511 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
514 * The input parameter.
516 * The last (most-significant) bit set, or 0 if the input is 0.
519 rte_fls_u32(uint32_t x)
521 return (x == 0) ? 0 : 32 - __builtin_clz(x);
525 * Searches the input parameter for the least significant set bit
526 * (starting from zero).
527 * If a least significant 1 bit is found, its bit index is returned.
528 * If the content of the input parameter is zero, then the content of the return
529 * value is undefined.
531 * input parameter, should not be zero.
533 * least significant set bit in the input parameter.
536 rte_bsf64(uint64_t v)
538 return (uint32_t)__builtin_ctzll(v);
542 * Searches the input parameter for the least significant set bit
543 * (starting from zero). Safe version (checks for input parameter being zero).
545 * @warning ``pos`` must be a valid pointer. It is not checked!
548 * The input parameter.
550 * If ``v`` was not 0, this value will contain position of least significant
551 * bit within the input parameter.
553 * Returns 0 if ``v`` was 0, otherwise returns 1.
556 rte_bsf64_safe(uint64_t v, uint32_t *pos)
566 * Return the last (most-significant) bit set.
568 * @note The last (most significant) bit is at position 64.
569 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
570 * rte_fls_u64(0x8000000000000000) = 64
573 * The input parameter.
575 * The last (most-significant) bit set, or 0 if the input is 0.
578 rte_fls_u64(uint64_t x)
580 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
584 * Return the rounded-up log2 of a 64-bit integer.
587 * The input parameter.
589 * The rounded-up log2 of the input, or 0 if the input is 0.
591 static inline uint32_t
592 rte_log2_u64(uint64_t v)
596 v = rte_align64pow2(v);
597 /* we checked for v being 0 already, so no undefined behavior */
602 /** Return the offset of a field in a structure. */
603 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
607 * Return pointer to the wrapping struct instance.
617 * struct child *x = obtain(...);
618 * struct wrapper *w = container_of(x, struct wrapper, c);
621 #define container_of(ptr, type, member) __extension__ ({ \
622 const typeof(((type *)0)->member) *_ptr = (ptr); \
623 __attribute__((unused)) type *_target_ptr = \
625 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
629 #define _RTE_STR(x) #x
630 /** Take a macro value and get a string version of it */
631 #define RTE_STR(x) _RTE_STR(x)
634 * ISO C helpers to modify format strings using variadic macros.
635 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
636 * An empty %s argument is appended to avoid a dangling comma.
638 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
639 #define RTE_FMT_HEAD(fmt, ...) fmt
640 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
642 /** Mask value of type "tp" for the first "ln" bit set. */
643 #define RTE_LEN2MASK(ln, tp) \
644 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
646 /** Number of elements in the array. */
647 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
650 * Converts a numeric string to the equivalent uint64_t value.
651 * As well as straight number conversion, also recognises the suffixes
652 * k, m and g for kilobytes, megabytes and gigabytes respectively.
654 * If a negative number is passed in i.e. a string with the first non-black
655 * character being "-", zero is returned. Zero is also returned in the case of
656 * an error with the strtoull call in the function.
659 * String containing number to convert.
663 static inline uint64_t
664 rte_str_to_size(const char *str)
667 unsigned long long size;
669 while (isspace((int)*str))
675 size = strtoull(str, &endptr, 0);
680 endptr++; /* allow 1 space gap */
683 case 'G': case 'g': size *= 1024; /* fall-through */
684 case 'M': case 'm': size *= 1024; /* fall-through */
685 case 'K': case 'k': size *= 1024; /* fall-through */
693 * Function to terminate the application immediately, printing an error
694 * message and returning the exit_code back to the shell.
696 * This function never returns
699 * The exit code to be returned by the application
701 * The format string to be used for printing the message. This can include
702 * printf format characters which will be expanded using any further parameters
706 rte_exit(int exit_code, const char *format, ...)
707 __attribute__((noreturn))
708 __attribute__((format(printf, 2, 3)));