OpenOCD
align.h
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 
3 /*
4  * The content of this file is mainly copied/inspired from Linux kernel
5  * code in include/linux/align.h and include/uapi/linux/const.h
6  *
7  * Macro name 'ALIGN' conflicts with macOS/BSD file param.h
8  */
9 
10 #ifndef OPENOCD_HELPER_ALIGN_H
11 #define OPENOCD_HELPER_ALIGN_H
12 
13 #define ALIGN_MASK(x, mask) \
14 ({ \
15  typeof(mask) _mask = (mask); \
16  ((x) + _mask) & ~_mask; \
17 })
18 
19 /* @a is a power of 2 value */
20 #define ALIGN_UP(x, a) ALIGN_MASK(x, (typeof(x))(a) - 1)
21 #define ALIGN_DOWN(x, a) ((x) & ~((typeof(x))(a) - 1))
22 #define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
23 
24 #define IS_PWR_OF_2(x) \
25 ({ \
26  typeof(x) _x = (x); \
27  _x == 0 || (_x & (_x - 1)) == 0; \
28 })
29 
30 #endif /* OPENOCD_HELPER_ALIGN_H */