blob: ba2938687b7dcd73a92a61efb768990d5943a218 [file] [log] [blame]
Tamas Banf70ef8c2017-12-19 15:35:09 +00001/*
2 * Copyright (c) 2011-2014, Wind River Systems, Inc.
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7/**
8 * @file
9 * @brief Misc utilities
10 *
11 * Misc utilities usable by the kernel and application code.
12 */
13
14#ifndef _UTIL__H_
15#define _UTIL__H_
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#ifndef _ASMLANGUAGE
22
23#include <zephyr/types.h>
24
25/* Helper to pass a int as a pointer or vice-versa.
26 * Those are available for 32 bits architectures:
27 */
28#define POINTER_TO_UINT(x) ((u32_t) (x))
29#define UINT_TO_POINTER(x) ((void *) (x))
30#define POINTER_TO_INT(x) ((s32_t) (x))
31#define INT_TO_POINTER(x) ((void *) (x))
32
33/* Evaluates to 0 if cond is true-ish; compile error otherwise */
34#define ZERO_OR_COMPILE_ERROR(cond) ((int) sizeof(char[1 - 2 * !(cond)]) - 1)
35
36/* Evaluates to 0 if array is an array; compile error if not array (e.g.
37 * pointer)
38 */
39#define IS_ARRAY(array) \
40 ZERO_OR_COMPILE_ERROR( \
41 !__builtin_types_compatible_p(__typeof__(array), \
42 __typeof__(&(array)[0])))
43
44/* Evaluates to number of elements in an array; compile error if not
45 * an array (e.g. pointer)
46 */
47#define ARRAY_SIZE(array) \
48 ((unsigned long) (IS_ARRAY(array) + \
49 (sizeof(array) / sizeof((array)[0]))))
50
51/* Evaluates to 1 if ptr is part of array, 0 otherwise; compile error if
52 * "array" argument is not an array (e.g. "ptr" and "array" mixed up)
53 */
54#define PART_OF_ARRAY(array, ptr) \
55 ((ptr) && ((ptr) >= &array[0] && (ptr) < &array[ARRAY_SIZE(array)]))
56
57#define CONTAINER_OF(ptr, type, field) \
58 ((type *)(((char *)(ptr)) - offsetof(type, field)))
59
60/* round "x" up/down to next multiple of "align" (which must be a power of 2) */
61#define ROUND_UP(x, align) \
62 (((unsigned long)(x) + ((unsigned long)align - 1)) & \
63 ~((unsigned long)align - 1))
64#define ROUND_DOWN(x, align) ((unsigned long)(x) & ~((unsigned long)align - 1))
65
66#define ceiling_fraction(numerator, divider) \
67 (((numerator) + ((divider) - 1)) / (divider))
68
69#ifdef INLINED
70#define INLINE inline
71#else
72#define INLINE
73#endif
74
75#ifndef max
76#define max(a, b) (((a) > (b)) ? (a) : (b))
77#endif
78
79#ifndef min
80#define min(a, b) (((a) < (b)) ? (a) : (b))
81#endif
82
83static inline int is_power_of_two(unsigned int x)
84{
85 return (x != 0) && !(x & (x - 1));
86}
87
88static inline s64_t arithmetic_shift_right(s64_t value, u8_t shift)
89{
90 s64_t sign_ext;
91
92 if (shift == 0) {
93 return value;
94 }
95
96 /* extract sign bit */
97 sign_ext = (value >> 63) & 1;
98
99 /* make all bits of sign_ext be the same as the value's sign bit */
100 sign_ext = -sign_ext;
101
102 /* shift value and fill opened bit positions with sign bit */
103 return (value >> shift) | (sign_ext << (64 - shift));
104}
105
106#endif /* !_ASMLANGUAGE */
107
108/* KB, MB, GB */
109#define KB(x) ((x) << 10)
110#define MB(x) (KB(x) << 10)
111#define GB(x) (MB(x) << 10)
112
113/* KHZ, MHZ */
114#define KHZ(x) ((x) * 1000)
115#define MHZ(x) (KHZ(x) * 1000)
116
117#ifndef BIT
118#define BIT(n) (1UL << (n))
119#endif
120
121#define BIT_MASK(n) (BIT(n) - 1)
122
123/**
124 * @brief Check for macro definition in compiler-visible expressions
125 *
126 * This trick was pioneered in Linux as the config_enabled() macro.
127 * The madness has the effect of taking a macro value that may be
128 * defined to "1" (e.g. CONFIG_MYFEATURE), or may not be defined at
129 * all and turning it into a literal expression that can be used at
130 * "runtime". That is, it works similarly to
131 * "defined(CONFIG_MYFEATURE)" does except that it is an expansion
132 * that can exist in a standard expression and be seen by the compiler
133 * and optimizer. Thus much ifdef usage can be replaced with cleaner
134 * expressions like:
135 *
136 * if (IS_ENABLED(CONFIG_MYFEATURE))
137 * myfeature_enable();
138 *
139 * INTERNAL
140 * First pass just to expand any existing macros, we need the macro
141 * value to be e.g. a literal "1" at expansion time in the next macro,
142 * not "(1)", etc... Standard recursive expansion does not work.
143 */
144#define IS_ENABLED(config_macro) _IS_ENABLED1(config_macro)
145
146/* Now stick on a "_XXXX" prefix, it will now be "_XXXX1" if config_macro
147 * is "1", or just "_XXXX" if it's undefined.
148 * ENABLED: _IS_ENABLED2(_XXXX1)
149 * DISABLED _IS_ENABLED2(_XXXX)
150 */
151#define _IS_ENABLED1(config_macro) _IS_ENABLED2(_XXXX##config_macro)
152
153/* Here's the core trick, we map "_XXXX1" to "_YYYY," (i.e. a string
154 * with a trailing comma), so it has the effect of making this a
155 * two-argument tuple to the preprocessor only in the case where the
156 * value is defined to "1"
157 * ENABLED: _YYYY, <--- note comma!
158 * DISABLED: _XXXX
159 */
160#define _XXXX1 _YYYY,
161
162/* Then we append an extra argument to fool the gcc preprocessor into
163 * accepting it as a varargs macro.
164 * arg1 arg2 arg3
165 * ENABLED: _IS_ENABLED3(_YYYY, 1, 0)
166 * DISABLED _IS_ENABLED3(_XXXX 1, 0)
167 */
168#define _IS_ENABLED2(one_or_two_args) _IS_ENABLED3(one_or_two_args 1, 0)
169
170/* And our second argument is thus now cooked to be 1 in the case
171 * where the value is defined to 1, and 0 if not:
172 */
173#define _IS_ENABLED3(ignore_this, val, ...) val
174
175/**
176 * Macros for doing code-generation with the preprocessor.
177 *
178 * Generally it is better to generate code with the preprocessor than
179 * to copy-paste code or to generate code with the build system /
180 * python script's etc.
181 *
182 * http://stackoverflow.com/a/12540675
183 */
184#define UTIL_EMPTY(...)
185#define UTIL_DEFER(...) __VA_ARGS__ UTIL_EMPTY()
186#define UTIL_OBSTRUCT(...) __VA_ARGS__ UTIL_DEFER(UTIL_EMPTY)()
187#define UTIL_EXPAND(...) __VA_ARGS__
188
189#define UTIL_EVAL(...) UTIL_EVAL1(UTIL_EVAL1(UTIL_EVAL1(__VA_ARGS__)))
190#define UTIL_EVAL1(...) UTIL_EVAL2(UTIL_EVAL2(UTIL_EVAL2(__VA_ARGS__)))
191#define UTIL_EVAL2(...) UTIL_EVAL3(UTIL_EVAL3(UTIL_EVAL3(__VA_ARGS__)))
192#define UTIL_EVAL3(...) UTIL_EVAL4(UTIL_EVAL4(UTIL_EVAL4(__VA_ARGS__)))
193#define UTIL_EVAL4(...) UTIL_EVAL5(UTIL_EVAL5(UTIL_EVAL5(__VA_ARGS__)))
194#define UTIL_EVAL5(...) __VA_ARGS__
195
196#define UTIL_CAT(a, ...) UTIL_PRIMITIVE_CAT(a, __VA_ARGS__)
197#define UTIL_PRIMITIVE_CAT(a, ...) a##__VA_ARGS__
198
199#define UTIL_INC(x) UTIL_PRIMITIVE_CAT(UTIL_INC_, x)
200#define UTIL_INC_0 1
201#define UTIL_INC_1 2
202#define UTIL_INC_2 3
203#define UTIL_INC_3 4
204#define UTIL_INC_4 5
205#define UTIL_INC_5 6
206#define UTIL_INC_6 7
207#define UTIL_INC_7 8
208#define UTIL_INC_8 9
209#define UTIL_INC_9 10
210#define UTIL_INC_10 11
211#define UTIL_INC_11 12
212#define UTIL_INC_12 13
213#define UTIL_INC_13 14
214#define UTIL_INC_14 15
215#define UTIL_INC_15 16
216#define UTIL_INC_16 17
217#define UTIL_INC_17 18
218#define UTIL_INC_18 19
219#define UTIL_INC_19 19
220
221#define UTIL_DEC(x) UTIL_PRIMITIVE_CAT(UTIL_DEC_, x)
222#define UTIL_DEC_0 0
223#define UTIL_DEC_1 0
224#define UTIL_DEC_2 1
225#define UTIL_DEC_3 2
226#define UTIL_DEC_4 3
227#define UTIL_DEC_5 4
228#define UTIL_DEC_6 5
229#define UTIL_DEC_7 6
230#define UTIL_DEC_8 7
231#define UTIL_DEC_9 8
232#define UTIL_DEC_10 9
233#define UTIL_DEC_11 10
234#define UTIL_DEC_12 11
235#define UTIL_DEC_13 12
236#define UTIL_DEC_14 13
237#define UTIL_DEC_15 14
238#define UTIL_DEC_16 15
239#define UTIL_DEC_17 16
240#define UTIL_DEC_18 17
241#define UTIL_DEC_19 18
242
243#define UTIL_CHECK_N(x, n, ...) n
244#define UTIL_CHECK(...) UTIL_CHECK_N(__VA_ARGS__, 0,)
245
246#define UTIL_NOT(x) UTIL_CHECK(UTIL_PRIMITIVE_CAT(UTIL_NOT_, x))
247#define UTIL_NOT_0 ~, 1,
248
249#define UTIL_COMPL(b) UTIL_PRIMITIVE_CAT(UTIL_COMPL_, b)
250#define UTIL_COMPL_0 1
251#define UTIL_COMPL_1 0
252
253#define UTIL_BOOL(x) UTIL_COMPL(UTIL_NOT(x))
254
255#define UTIL_IIF(c) UTIL_PRIMITIVE_CAT(UTIL_IIF_, c)
256#define UTIL_IIF_0(t, ...) __VA_ARGS__
257#define UTIL_IIF_1(t, ...) t
258
259#define UTIL_IF(c) UTIL_IIF(UTIL_BOOL(c))
260
261#define UTIL_EAT(...)
262#define UTIL_EXPAND(...) __VA_ARGS__
263#define UTIL_WHEN(c) UTIL_IF(c)(UTIL_EXPAND, UTIL_EAT)
264
265#define UTIL_REPEAT(count, macro, ...) \
266 UTIL_WHEN(count) \
267 ( \
268 UTIL_OBSTRUCT(UTIL_REPEAT_INDIRECT) () \
269 ( \
270 UTIL_DEC(count), macro, __VA_ARGS__ \
271 ) \
272 UTIL_OBSTRUCT(macro) \
273 ( \
274 UTIL_DEC(count), __VA_ARGS__ \
275 ) \
276 )
277#define UTIL_REPEAT_INDIRECT() UTIL_REPEAT
278
279/**
280 * Generates a sequence of code.
281 * Useful for generating code like;
282 *
283 * NRF_PWM0, NRF_PWM1, NRF_PWM2,
284 *
285 * @arg LEN: The length of the sequence. Must be defined and less than
286 * 20.
287 *
288 * @arg F(i, F_ARG): A macro function that accepts two arguments.
289 * F is called repeatedly, the first argument
290 * is the index in the sequence, and the second argument is the third
291 * argument given to UTIL_LISTIFY.
292 *
293 * Example:
294 *
295 * \#define FOO(i, _) NRF_PWM ## i ,
296 * { UTIL_LISTIFY(PWM_COUNT, FOO) }
297 * // The above two lines will generate the below:
298 * { NRF_PWM0 , NRF_PWM1 , }
299 *
300 * @note Calling UTIL_LISTIFY with undefined arguments has undefined
301 * behaviour.
302 */
303#define UTIL_LISTIFY(LEN, F, F_ARG) UTIL_EVAL(UTIL_REPEAT(LEN, F, F_ARG))
304
305#ifdef __cplusplus
306}
307#endif
308
309#endif /* _UTIL__H_ */