Zephyr API Documentation 4.5.99
A Scalable Open Source RTOS
Loading...
Searching...
No Matches
util_macro.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
16
17#ifndef ZEPHYR_INCLUDE_SYS_UTIL_MACRO_H_
18#define ZEPHYR_INCLUDE_SYS_UTIL_MACRO_H_
19
20#ifdef __cplusplus
21extern "C" {
22#endif
23
28
29/*
30 * Most of the eldritch implementation details for all the macrobatics
31 * below (APIs like IS_ENABLED(), COND_CODE_1(), etc.) are hidden away
32 * in this file.
33 */
35
36#ifndef BIT
37#if defined(_ASMLANGUAGE)
38#define BIT(n) (1 << (n))
39#else
44#define BIT(n) (1UL << (n))
45#endif
46#endif
47
49#define BIT64(_n) (1ULL << (_n))
50
60#define WRITE_BIT(var, bit, set) \
61 do { \
62 __typeof__(var) __mask = ((__typeof__(var))1U << (bit)); \
63 if (set) { \
64 (var) |= __mask; \
65 } else { \
66 (var) &= ~__mask; \
67 } \
68 } while (0)
69
74#define BIT_MASK(n) (BIT(n) - 1UL)
75
80#define BIT64_MASK(n) (BIT64(n) - 1ULL)
81
86#define GENMASK(h, l) (((1UL << (h)) << 1U) - (1UL << (l)))
87
92#define GENMASK64(h, l) (((1ULL << (h)) << 1U) - (1ULL << (l)))
93
95#define IS_POWER_OF_TWO(x) (((x) != 0U) && (((x) & ((x) - 1U)) == 0U))
96
105#define IS_SHIFTED_BIT_MASK(m, s) (!(((m) >> (s)) & (((m) >> (s)) + 1U)))
106
112#define IS_BIT_MASK(m) IS_SHIFTED_BIT_MASK(m, 0)
113
120#define IS_BIT_SET(value, bit) ((((value) >> (bit)) & (0x1)) != 0)
121
123#define LSB_GET(value) ((value) & -(value))
124
129#define FIELD_GET(mask, value) (((value) & (mask)) / LSB_GET(mask))
130
136#define FIELD_PREP(mask, value) (((value) * LSB_GET(mask)) & (mask))
137
166#define IS_ENABLED(config_macro) Z_IS_ENABLED1(config_macro)
167/* INTERNAL: the first pass above is just to expand any existing
168 * macros, we need the macro value to be e.g. a literal "1" at
169 * expansion time in the next macro, not "(1)", etc... Standard
170 * recursive expansion does not work.
171 */
172
221#define COND_CODE_1(_flag, _if_1_code, _else_code) \
222 Z_COND_CODE_1(_flag, _if_1_code, _else_code)
223
237#define COND_CODE_0(_flag, _if_0_code, _else_code) \
238 Z_COND_CODE_0(_flag, _if_0_code, _else_code)
239
262#define COND_CASE_1(...) \
263 Z_COND_CASE_1(__VA_ARGS__)
264
290#define IF_ENABLED(_flag, _code) \
291 COND_CODE_1(_flag, _code, ())
292
314#define IF_DISABLED(_flag, _code) \
315 COND_CODE_1(_flag, (), _code)
316
344#define IS_EMPTY(...) Z_IS_EMPTY_(__VA_ARGS__)
345
365#define IS_EQ(a, b) Z_IS_EQ(a, b)
366
394#define LIST_DROP_EMPTY(...) \
395 Z_LIST_DROP_FIRST(FOR_EACH(Z_LIST_NO_EMPTIES, (), __VA_ARGS__))
396
414#define EMPTY
415
424#define IDENTITY(V) V
425
434#define GET_ARG_N(N, ...) UTIL_CAT(Z_GET_ARG_, N)(__VA_ARGS__)
435
444#define GET_ARGS_LESS_N(N, ...) UTIL_CAT(Z_GET_ARGS_LESS_, N)(__VA_ARGS__)
445
454#define GET_ARGS_FIRST_N(N, ...) UTIL_CAT(Z_GET_ARGS_FIRST_, N)(__VA_ARGS__)
455
467#define UTIL_OR(a, b) COND_CODE_1(UTIL_BOOL(a), (a), (b))
468
480#define UTIL_AND(a, b) COND_CODE_1(UTIL_BOOL(a), (b), (0))
481
488#define UTIL_INC(x) UTIL_PRIMITIVE_CAT(Z_UTIL_INC_, x)
489
496#define UTIL_DEC(x) UTIL_PRIMITIVE_CAT(Z_UTIL_DEC_, x)
497
502#define UTIL_X2(y) UTIL_PRIMITIVE_CAT(Z_UTIL_X2_, y)
503
504
531#define LISTIFY(LEN, F, sep, ...) UTIL_CAT(Z_UTIL_LISTIFY_, LEN)(F, sep, __VA_ARGS__)
532
554#define FOR_EACH(F, sep, ...) \
555 Z_FOR_EACH(F, sep, REVERSE_ARGS(__VA_ARGS__))
556
609#define FOR_EACH_NONEMPTY_TERM(F, term, ...) \
610 COND_CODE_0( \
611 /* are there zero non-empty arguments ? */ \
612 NUM_VA_ARGS_LESS_1(LIST_DROP_EMPTY(__VA_ARGS__, _)), \
613 /* if so, expand to nothing */ \
614 (), \
615 /* otherwise, expand to: */ \
616 (/* FOR_EACH() on nonempty elements, */ \
617 FOR_EACH(F, term, LIST_DROP_EMPTY(__VA_ARGS__)) \
618 /* plus a final terminator */ \
619 __DEBRACKET term \
620 ))
621
646#define FOR_EACH_IDX(F, sep, ...) \
647 Z_FOR_EACH_IDX(F, sep, REVERSE_ARGS(__VA_ARGS__))
648
674#define FOR_EACH_FIXED_ARG(F, sep, fixed_arg, ...) \
675 Z_FOR_EACH_FIXED_ARG(F, sep, fixed_arg, REVERSE_ARGS(__VA_ARGS__))
676
702#define FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, ...) \
703 Z_FOR_EACH_IDX_FIXED_ARG(F, sep, fixed_arg, REVERSE_ARGS(__VA_ARGS__))
704
709#define REVERSE_ARGS(...) \
710 Z_FOR_EACH_ENGINE(Z_FOR_EACH_EXEC, (,), Z_BYPASS, _, __VA_ARGS__)
711
729#define ARGS_UNUSED(...) FOR_EACH(ARG_UNUSED, (;), __VA_ARGS__)
730
739#define NUM_VA_ARGS_LESS_1(...) \
740 NUM_VA_ARGS_LESS_1_IMPL(__VA_ARGS__, 63, 62, 61, \
741 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, \
742 50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
743 40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
744 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
745 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
746 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, ~)
747
756#define NUM_VA_ARGS(...) \
757 COND_CODE_1(IS_EMPTY(__VA_ARGS__), (0), (UTIL_INC(NUM_VA_ARGS_LESS_1(__VA_ARGS__))))
758
779#define MACRO_MAP_CAT(...) Z_MACRO_MAP_CAT_(__VA_ARGS__)
780
794#define MACRO_MAP_CAT_N(N, ...) Z_MACRO_MAP_CAT_N_(N, __VA_ARGS__)
795
799
800#ifdef __cplusplus
801}
802#endif
803
804#endif /* ZEPHYR_INCLUDE_SYS_UTIL_MACRO_H_ */
Misc utilities.