Files
sw_openocd/src/helper/align.h
Jan Matyas edf2c82cf6 helper/align.h: Fix macro IS_PWR_OF_2
Zero is not a power of two.

All functions that use IS_PWR_OF_2 were checked and the edge case
of IS_PWR_OF_2(0) does not occur anywhere at the moment. Therefore
the fix is safe.

Change-Id: I84d9f9c64c9a7df452ca6e99c2ee4169ccb2b0be
Signed-off-by: Jan Matyas <jan.matyas@codasip.com>
Fixes: 9544cd653d ("helper: add align.h")
Reviewed-on: https://review.openocd.org/c/openocd/+/8511
Reviewed-by: Antonio Borneo <borneo.antonio@gmail.com>
Tested-by: jenkins
2024-10-05 15:53:43 +00:00

31 lines
915 B
C

/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* The content of this file is mainly copied/inspired from Linux kernel
* code in include/linux/align.h and include/uapi/linux/const.h
*
* Macro name 'ALIGN' conflicts with macOS/BSD file param.h
*/
#ifndef OPENOCD_HELPER_ALIGN_H
#define OPENOCD_HELPER_ALIGN_H
#define ALIGN_MASK(x, mask) \
({ \
typeof(mask) _mask = (mask); \
((x) + _mask) & ~_mask; \
})
/* @a is a power of 2 value */
#define ALIGN_UP(x, a) ALIGN_MASK(x, (typeof(x))(a) - 1)
#define ALIGN_DOWN(x, a) ((x) & ~((typeof(x))(a) - 1))
#define IS_ALIGNED(x, a) (((x) & ((typeof(x))(a) - 1)) == 0)
#define IS_PWR_OF_2(x) \
({ \
typeof(x) _x = (x); \
_x != 0 && (_x & (_x - 1)) == 0; \
})
#endif /* OPENOCD_HELPER_ALIGN_H */