xref: /aosp_15_r20/external/gsc-utils/include/compile_time_macros.h (revision 4f2df630800bdcf1d4f0decf95d8a1cb87344f5f)
1 /* Copyright 2013 The ChromiumOS Authors
2  * Use of this source code is governed by a BSD-style license that can be
3  * found in the LICENSE file.
4  */
5 
6 /* Handy clever tricks */
7 
8 #ifndef __CROS_EC_COMPILE_TIME_MACROS_H
9 #define __CROS_EC_COMPILE_TIME_MACROS_H
10 
11 /* Test an important condition at compile time, not run time */
12 #define _BA1_(cond, line)                                    \
13 	extern int __build_assertion_##line[1 - 2 * !(cond)] \
14 		__attribute__((unused))
15 #define _BA0_(c, x)	   _BA1_(c, x)
16 #define BUILD_ASSERT(cond) _BA0_(cond, __LINE__)
17 
18 /*
19  * Test an important condition inside code path at run time, taking advantage of
20  * -Werror=div-by-zero.
21  */
22 #define BUILD_CHECK_INLINE(value, cond_true) ((value) / (!!(cond_true)))
23 
24 /* Number of elements in an array */
25 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
26 
27 /* Make for loops that iterate over pointers to array entries more readable */
28 #define ARRAY_BEGIN(array) (array)
29 #define ARRAY_END(array)   ((array) + ARRAY_SIZE(array))
30 
31 /* Just in case - http://gcc.gnu.org/onlinedocs/gcc/Offsetof.html */
32 #ifndef offsetof
33 #define offsetof(type, member) __builtin_offsetof(type, member)
34 #endif
35 
36 #define member_size(type, member) sizeof(((type *)0)->member)
37 
38 /*
39  * Bit operation macros.
40  */
41 #define BIT(nr)	    (1U << (nr))
42 #define BIT_ULL(nr) (1ULL << (nr))
43 
44 #endif /* __CROS_EC_COMPILE_TIME_MACROS_H */
45