diff --git a/include/ucore/utils.h b/include/ucore/utils.h index 1b3737e..c251ef5 100644 --- a/include/ucore/utils.h +++ b/include/ucore/utils.h @@ -50,13 +50,60 @@ typeof( ((type *)0)->member ) *__mptr = (ptr); \ (type *)( (void *)__mptr - offsetof(type, member) ); }) +/** Align a value. + * @param val value to align + * @param align alignment, must be power of 2 + * @return the aligned val + */ #define UC_ALIGN(val,align) (((val)+(align)-1UL)&~((align)-1UL)) +/** + * Test if x is a power of 2 + * + * @param x value + * @return 1 if x is a power of 20, 0 if it is not + */ +#define UC_IS_POW2(x) ((x) && !((x) & (x) - 1)) + /** * * Expands @x and turns it into a string. * */ #define UC_STRINGIFY(x) UC_STRINGIFY_HLP(x) #define UC_STRINGIFY_HLP(x) #x +/** + * Macro for asserting code that should not be reached, + * terminates the program if executed + */ +#define UC_NOT_REACED() abort() + +/** + * Round up X / y + * + * @param x numerator, must be positive + * @param y denominator + * @return x/y rounded up + */ +#define UC_DIV_ROUND_UP(x, y) (((x) + ((y) - 1))/(y)) + +/** + * Round x down to nearest multiple of y + * + * @param x numerator, must be positive + * @param y denominator + * @return x rounded to nearest multiple of y + */ +#define UC_ROUND_UP(x, y) (((x) / (y)) * (y)) + +/** + * Round x up to nearest multiple of y + * + * @param x numerator, must be positive + * @param y denominator + * @return x rounded to nearest multiple of y + */ +#define UC_ROUND_UP(x, y) (UC_DIV_ROUND_UP((x),(y)) * (y)) + + #endif