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 #ifndef RTE_INIT_PRIO /* Allow to override from EAL */
110 #define RTE_INIT_PRIO(func, prio) \
111 static void __attribute__((constructor(RTE_PRIO(prio)), used)) func(void)
115 * Run function before main() with low priority.
117 * The constructor will be run after prioritized constructors.
120 * Constructor function.
122 #define RTE_INIT(func) \
123 RTE_INIT_PRIO(func, LAST)
126 * Run after main() with low priority.
129 * Destructor function name.
131 * Priority number must be above 100.
132 * Lowest number is the last to run.
134 #ifndef RTE_FINI_PRIO /* Allow to override from EAL */
135 #define RTE_FINI_PRIO(func, prio) \
136 static void __attribute__((destructor(RTE_PRIO(prio)), used)) func(void)
140 * Run after main() with high priority.
142 * The destructor will be run *before* prioritized destructors.
145 * Destructor function name.
147 #define RTE_FINI(func) \
148 RTE_FINI_PRIO(func, LAST)
151 * Force a function to be inlined
153 #define __rte_always_inline inline __attribute__((always_inline))
156 * Force a function to be noinlined
158 #define __rte_noinline __attribute__((noinline))
160 /*********** Macros for pointer arithmetic ********/
163 * add a byte-value offset to a pointer
165 #define RTE_PTR_ADD(ptr, x) ((void*)((uintptr_t)(ptr) + (x)))
168 * subtract a byte-value offset from a pointer
170 #define RTE_PTR_SUB(ptr, x) ((void*)((uintptr_t)ptr - (x)))
173 * get the difference between two pointer values, i.e. how far apart
174 * in bytes are the locations they point two. It is assumed that
175 * ptr1 is greater than ptr2.
177 #define RTE_PTR_DIFF(ptr1, ptr2) ((uintptr_t)(ptr1) - (uintptr_t)(ptr2))
180 * Workaround to cast a const field of a structure to non-const type.
182 #define RTE_CAST_FIELD(var, field, type) \
183 (*(type *)((uintptr_t)(var) + offsetof(typeof(*(var)), field)))
185 /*********** Macros/static functions for doing alignment ********/
189 * Macro to align a pointer to a given power-of-two. The resultant
190 * pointer will be a pointer of the same type as the first parameter, and
191 * point to an address no higher than the first parameter. Second parameter
192 * must be a power-of-two value.
194 #define RTE_PTR_ALIGN_FLOOR(ptr, align) \
195 ((typeof(ptr))RTE_ALIGN_FLOOR((uintptr_t)ptr, align))
198 * Macro to align a value to a given power-of-two. The resultant value
199 * will be of the same type as the first parameter, and will be no
200 * bigger than the first parameter. Second parameter must be a
201 * power-of-two value.
203 #define RTE_ALIGN_FLOOR(val, align) \
204 (typeof(val))((val) & (~((typeof(val))((align) - 1))))
207 * Macro to align a pointer to a given power-of-two. The resultant
208 * pointer will be a pointer of the same type as the first parameter, and
209 * point to an address no lower than the first parameter. Second parameter
210 * must be a power-of-two value.
212 #define RTE_PTR_ALIGN_CEIL(ptr, align) \
213 RTE_PTR_ALIGN_FLOOR((typeof(ptr))RTE_PTR_ADD(ptr, (align) - 1), align)
216 * Macro to align a value to a given power-of-two. The resultant value
217 * will be of the same type as the first parameter, and will be no lower
218 * than the first parameter. Second parameter must be a power-of-two
221 #define RTE_ALIGN_CEIL(val, align) \
222 RTE_ALIGN_FLOOR(((val) + ((typeof(val)) (align) - 1)), align)
225 * Macro to align a pointer to a given power-of-two. The resultant
226 * pointer will be a pointer of the same type as the first parameter, and
227 * point to an address no lower than the first parameter. Second parameter
228 * must be a power-of-two value.
229 * This function is the same as RTE_PTR_ALIGN_CEIL
231 #define RTE_PTR_ALIGN(ptr, align) RTE_PTR_ALIGN_CEIL(ptr, align)
234 * Macro to align a value to a given power-of-two. The resultant
235 * value will be of the same type as the first parameter, and
236 * will be no lower than the first parameter. Second parameter
237 * must be a power-of-two value.
238 * This function is the same as RTE_ALIGN_CEIL
240 #define RTE_ALIGN(val, align) RTE_ALIGN_CEIL(val, align)
243 * Macro to align a value to the multiple of given value. The resultant
244 * value will be of the same type as the first parameter and will be no lower
245 * than the first parameter.
247 #define RTE_ALIGN_MUL_CEIL(v, mul) \
248 (((v + (typeof(v))(mul) - 1) / ((typeof(v))(mul))) * (typeof(v))(mul))
251 * Macro to align a value to the multiple of given value. The resultant
252 * value will be of the same type as the first parameter and will be no higher
253 * than the first parameter.
255 #define RTE_ALIGN_MUL_FLOOR(v, mul) \
256 ((v / ((typeof(v))(mul))) * (typeof(v))(mul))
259 * Macro to align value to the nearest multiple of the given value.
260 * The resultant value might be greater than or less than the first parameter
261 * whichever difference is the lowest.
263 #define RTE_ALIGN_MUL_NEAR(v, mul) \
265 typeof(v) ceil = RTE_ALIGN_MUL_CEIL(v, mul); \
266 typeof(v) floor = RTE_ALIGN_MUL_FLOOR(v, mul); \
267 (ceil - v) > (v - floor) ? floor : ceil; \
271 * Checks if a pointer is aligned to a given power-of-two value
274 * The pointer whose alignment is to be checked
276 * The power-of-two value to which the ptr should be aligned
279 * True(1) where the pointer is correctly aligned, false(0) otherwise
282 rte_is_aligned(void *ptr, unsigned align)
284 return RTE_PTR_ALIGN(ptr, align) == ptr;
287 /*********** Macros for compile type checks ********/
290 * Triggers an error at compilation time if the condition is true.
292 #define RTE_BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
295 * Combines 32b inputs most significant set bits into the least
296 * significant bits to construct a value with the same MSBs as x
297 * but all 1's under it.
300 * The integer whose MSBs need to be combined with its LSBs
302 * The combined value.
304 static inline uint32_t
305 rte_combine32ms1b(register uint32_t x)
317 * Combines 64b inputs most significant set bits into the least
318 * significant bits to construct a value with the same MSBs as x
319 * but all 1's under it.
322 * The integer whose MSBs need to be combined with its LSBs
324 * The combined value.
326 static inline uint64_t
327 rte_combine64ms1b(register uint64_t v)
339 /*********** Macros to work with powers of 2 ********/
342 * Macro to return 1 if n is a power of 2, 0 otherwise
344 #define RTE_IS_POWER_OF_2(n) ((n) && !(((n) - 1) & (n)))
347 * Returns true if n is a power of 2
350 * @return 1 if true, 0 otherwise
353 rte_is_power_of_2(uint32_t n)
355 return n && !(n & (n - 1));
359 * Aligns input parameter to the next power of 2
362 * The integer value to align
365 * Input parameter aligned to the next power of 2
367 static inline uint32_t
368 rte_align32pow2(uint32_t x)
371 x = rte_combine32ms1b(x);
377 * Aligns input parameter to the previous power of 2
380 * The integer value to align
383 * Input parameter aligned to the previous power of 2
385 static inline uint32_t
386 rte_align32prevpow2(uint32_t x)
388 x = rte_combine32ms1b(x);
394 * Aligns 64b input parameter to the next power of 2
397 * The 64b value to align
400 * Input parameter aligned to the next power of 2
402 static inline uint64_t
403 rte_align64pow2(uint64_t v)
406 v = rte_combine64ms1b(v);
412 * Aligns 64b input parameter to the previous power of 2
415 * The 64b value to align
418 * Input parameter aligned to the previous power of 2
420 static inline uint64_t
421 rte_align64prevpow2(uint64_t v)
423 v = rte_combine64ms1b(v);
428 /*********** Macros for calculating min and max **********/
431 * Macro to return the minimum of two numbers
433 #define RTE_MIN(a, b) \
435 typeof (a) _a = (a); \
436 typeof (b) _b = (b); \
441 * Macro to return the maximum of two numbers
443 #define RTE_MAX(a, b) \
445 typeof (a) _a = (a); \
446 typeof (b) _b = (b); \
450 /*********** Other general functions / macros ********/
453 * Searches the input parameter for the least significant set bit
454 * (starting from zero).
455 * If a least significant 1 bit is found, its bit index is returned.
456 * If the content of the input parameter is zero, then the content of the return
457 * value is undefined.
459 * input parameter, should not be zero.
461 * least significant set bit in the input parameter.
463 static inline uint32_t
464 rte_bsf32(uint32_t v)
466 return (uint32_t)__builtin_ctz(v);
470 * Searches the input parameter for the least significant set bit
471 * (starting from zero). Safe version (checks for input parameter being zero).
473 * @warning ``pos`` must be a valid pointer. It is not checked!
476 * The input parameter.
478 * If ``v`` was not 0, this value will contain position of least significant
479 * bit within the input parameter.
481 * Returns 0 if ``v`` was 0, otherwise returns 1.
484 rte_bsf32_safe(uint64_t v, uint32_t *pos)
494 * Return the rounded-up log2 of a integer.
497 * The input parameter.
499 * The rounded-up log2 of the input, or 0 if the input is 0.
501 static inline uint32_t
502 rte_log2_u32(uint32_t v)
506 v = rte_align32pow2(v);
512 * Return the last (most-significant) bit set.
514 * @note The last (most significant) bit is at position 32.
515 * @note rte_fls_u32(0) = 0, rte_fls_u32(1) = 1, rte_fls_u32(0x80000000) = 32
518 * The input parameter.
520 * The last (most-significant) bit set, or 0 if the input is 0.
523 rte_fls_u32(uint32_t x)
525 return (x == 0) ? 0 : 32 - __builtin_clz(x);
529 * Searches the input parameter for the least significant set bit
530 * (starting from zero).
531 * If a least significant 1 bit is found, its bit index is returned.
532 * If the content of the input parameter is zero, then the content of the return
533 * value is undefined.
535 * input parameter, should not be zero.
537 * least significant set bit in the input parameter.
540 rte_bsf64(uint64_t v)
542 return (uint32_t)__builtin_ctzll(v);
546 * Searches the input parameter for the least significant set bit
547 * (starting from zero). Safe version (checks for input parameter being zero).
549 * @warning ``pos`` must be a valid pointer. It is not checked!
552 * The input parameter.
554 * If ``v`` was not 0, this value will contain position of least significant
555 * bit within the input parameter.
557 * Returns 0 if ``v`` was 0, otherwise returns 1.
560 rte_bsf64_safe(uint64_t v, uint32_t *pos)
570 * Return the last (most-significant) bit set.
572 * @note The last (most significant) bit is at position 64.
573 * @note rte_fls_u64(0) = 0, rte_fls_u64(1) = 1,
574 * rte_fls_u64(0x8000000000000000) = 64
577 * The input parameter.
579 * The last (most-significant) bit set, or 0 if the input is 0.
582 rte_fls_u64(uint64_t x)
584 return (x == 0) ? 0 : 64 - __builtin_clzll(x);
588 * Return the rounded-up log2 of a 64-bit integer.
591 * The input parameter.
593 * The rounded-up log2 of the input, or 0 if the input is 0.
595 static inline uint32_t
596 rte_log2_u64(uint64_t v)
600 v = rte_align64pow2(v);
601 /* we checked for v being 0 already, so no undefined behavior */
606 /** Return the offset of a field in a structure. */
607 #define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)
611 * Return pointer to the wrapping struct instance.
621 * struct child *x = obtain(...);
622 * struct wrapper *w = container_of(x, struct wrapper, c);
625 #define container_of(ptr, type, member) __extension__ ({ \
626 const typeof(((type *)0)->member) *_ptr = (ptr); \
627 __attribute__((unused)) type *_target_ptr = \
629 (type *)(((uintptr_t)_ptr) - offsetof(type, member)); \
633 #define _RTE_STR(x) #x
634 /** Take a macro value and get a string version of it */
635 #define RTE_STR(x) _RTE_STR(x)
638 * ISO C helpers to modify format strings using variadic macros.
639 * This is a replacement for the ", ## __VA_ARGS__" GNU extension.
640 * An empty %s argument is appended to avoid a dangling comma.
642 #define RTE_FMT(fmt, ...) fmt "%.0s", __VA_ARGS__ ""
643 #define RTE_FMT_HEAD(fmt, ...) fmt
644 #define RTE_FMT_TAIL(fmt, ...) __VA_ARGS__
646 /** Mask value of type "tp" for the first "ln" bit set. */
647 #define RTE_LEN2MASK(ln, tp) \
648 ((tp)((uint64_t)-1 >> (sizeof(uint64_t) * CHAR_BIT - (ln))))
650 /** Number of elements in the array. */
651 #define RTE_DIM(a) (sizeof (a) / sizeof ((a)[0]))
654 * Converts a numeric string to the equivalent uint64_t value.
655 * As well as straight number conversion, also recognises the suffixes
656 * k, m and g for kilobytes, megabytes and gigabytes respectively.
658 * If a negative number is passed in i.e. a string with the first non-black
659 * character being "-", zero is returned. Zero is also returned in the case of
660 * an error with the strtoull call in the function.
663 * String containing number to convert.
667 static inline uint64_t
668 rte_str_to_size(const char *str)
671 unsigned long long size;
673 while (isspace((int)*str))
679 size = strtoull(str, &endptr, 0);
684 endptr++; /* allow 1 space gap */
687 case 'G': case 'g': size *= 1024; /* fall-through */
688 case 'M': case 'm': size *= 1024; /* fall-through */
689 case 'K': case 'k': size *= 1024; /* fall-through */
697 * Function to terminate the application immediately, printing an error
698 * message and returning the exit_code back to the shell.
700 * This function never returns
703 * The exit code to be returned by the application
705 * The format string to be used for printing the message. This can include
706 * printf format characters which will be expanded using any further parameters
710 rte_exit(int exit_code, const char *format, ...)
711 __attribute__((noreturn))
712 __attribute__((format(printf, 2, 3)));