NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
utils.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight 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.
8  *
9  * Ninjaflight 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <stddef.h>
21 
22 #define ARRAYLEN(x) (sizeof(x) / sizeof((x)[0]))
23 #define ARRAYEND(x) (&(x)[ARRAYLEN(x)])
24 
25 #define CONCAT_HELPER(x,y) x ## y
26 #define CONCAT(x,y) CONCAT_HELPER(x, y)
27 
28 #define STR_HELPER(x) #x
29 #define STR(x) STR_HELPER(x)
30 
31 /*
32 http://resnet.uoregon.edu/~gurney_j/jmpc/bitwise.html
33 */
34 #define BITCOUNT(x) (((BX_(x)+(BX_(x)>>4)) & 0x0F0F0F0F) % 255)
35 #define BX_(x) ((x) - (((x)>>1)&0x77777777) - (((x)>>2)&0x33333333) - (((x)>>3)&0x11111111))
36 
37 #define UNUSED(x) (void)(x)
38 
39 #if 0
40 // ISO C version, but no type checking
41 #define container_of(ptr, type, member) \
42  ((type *) ((char *)(ptr) - offsetof(type, member)))
43 #else
44 // non ISO variant from linux kernel; checks ptr type, but triggers 'ISO C forbids braced-groups within expressions [-Wpedantic]'
45 // __extension__ is here to disable this warning
46 #define container_of(ptr, type, member) (\
47  (type *)( (char *)((const typeof( ((type *)0)->member )*)ptr) - offsetof(type,member) ))
48 
49 static inline int32_t cmp16(uint16_t a, uint16_t b) { return a-b; }
50 static inline int64_t cmp32(uint32_t a, uint32_t b) { return a-b; }
51 
52 #endif