2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
23 #define NOOP do {} while (0)
25 #define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
26 #define ARRAYEND(x) (&(x)[ARRAYLEN(x)])
28 #define CONST_CAST(type, value) ((type)(value))
30 #define CONCAT_HELPER(x,y) x ## y
31 #define CONCAT(x,y) CONCAT_HELPER(x, y)
32 #define CONCAT2(_1,_2) CONCAT(_1, _2)
33 #define CONCAT3(_1,_2,_3) CONCAT(CONCAT(_1, _2), _3)
34 #define CONCAT4(_1,_2,_3,_4) CONCAT(CONCAT3(_1, _2, _3), _4)
36 #define STR_HELPER(x) #x
37 #define STR(x) STR_HELPER(x)
40 #define EXPAND(x) EXPAND_I(x)
42 // expand to t if bit is 1, f when bit is 0. Other bit values are not supported
43 #define PP_IIF(bit, t, f) PP_IIF_I(bit, t, f)
44 #define PP_IIF_I(bit, t, f) PP_IIF_ ## bit(t, f)
45 #define PP_IIF_0(t, f) f
46 #define PP_IIF_1(t, f) t
48 // Expand all argumens and call macro with them. When expansion of some argument contains ',', it will be passed as multiple arguments
49 // #define TAKE3(_1,_2,_3) CONCAT3(_1,_2,_3)
51 // PP_CALL(TAKE3, MULTI2, C) expands to ABC
52 #define PP_CALL(macro, ...) macro(__VA_ARGS__)
55 #define UNUSED(x) (void)(x)
57 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
58 #define STATIC_ASSERT(condition, name) \
59 typedef char assert_failed_ ## name [(condition) ? 1 : -1 ] __attribute__((unused))
62 #define BIT(x) (1 << (x))
65 http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
67 #define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
68 #define BX_(x) ((x) - (((x)>>1)&0x77777777) - (((x)>>2)&0x33333333) - (((x)>>3)&0x11111111))
72 * https://groups.google.com/forum/?hl=en#!msg/comp.lang.c/attFnqwhvGk/sGBKXvIkY3AJ
73 * Return (v ? floor(log2(v)) : 0) when 0 <= v < 1<<[8, 16, 32, 64].
74 * Inefficient algorithm, intended for compile-time constants.
76 #define LOG2_8BIT(v) (8 - 90/(((v)/4+14)|1) - 2/((v)/2+1))
77 #define LOG2_16BIT(v) (8*((v)>255) + LOG2_8BIT((v) >>8*((v)>255)))
78 #define LOG2_32BIT(v) (16*((v)>65535L) + LOG2_16BIT((v)*1L >>16*((v)>65535L)))
79 #define LOG2_64BIT(v) \
80 (32*((v)/2L>>31 > 0) \
81 + LOG2_32BIT((v)*1L >>16*((v)/2L>>31 > 0) \
82 >>16*((v)/2L>>31 > 0)))
83 #define LOG2(v) LOG2_64BIT(v)
86 // ISO C version, but no type checking
87 #define container_of(ptr, type, member) \
88 ((type *) ((char *)(ptr) - offsetof(type, member)))
90 // non ISO variant from linux kernel; checks ptr type, but triggers 'ISO C forbids braced-groups within expressions [-Wpedantic]'
91 // __extension__ is here to disable this warning
92 #define container_of(ptr, type, member) ( __extension__ ({ \
93 const typeof( ((type *)0)->member ) *__mptr = (ptr); \
94 (type *)( (char *)__mptr - offsetof(type,member) );}))
96 static inline int16_t cmp16(uint16_t a
, uint16_t b
) { return (int16_t)(a
-b
); }
97 static inline int32_t cmp32(uint32_t a
, uint32_t b
) { return (int32_t)(a
-b
); }
99 // using memcpy_fn will force memcpy function call, instead of inlining it. In most cases function call takes fewer instructions
100 // than inlined version (inlining is cheaper for very small moves < 8 bytes / 2 store instructions)
101 #if defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
102 // Call memcpy when building unittest - this is easier that asm symbol name mangling (symbols start with _underscore on win32)
104 static inline void memcpy_fn ( void * destination
, const void * source
, size_t num
) { memcpy(destination
, source
, num
); }
106 void * memcpy_fn ( void * destination
, const void * source
, size_t num
) asm("memcpy");
113 #define FALLTHROUGH __attribute__ ((fallthrough))
115 #define FALLTHROUGH do {} while(0)