From 95bdff7d534d10f54713864312ff01048794e12f Mon Sep 17 00:00:00 2001 From: Igor Fedotov Date: Mon, 29 Jan 2024 19:21:51 +0300 Subject: [PATCH] include/intarith: introduce p2aligned() Signed-off-by: Igor Fedotov --- src/include/intarith.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/include/intarith.h b/src/include/intarith.h index 92f827d88e5..42bafcf88b4 100644 --- a/src/include/intarith.h +++ b/src/include/intarith.h @@ -57,6 +57,18 @@ constexpr inline T p2align(T x, T align) { return x & -align; } +/* + * return whether x is aligned with (align) + * eg, p2aligned(1200, 1024) ==> false + * eg, p2aligned(1024, 1024) ==> true + * eg, p2aligned(0x1234, 0x100) ==> false + * eg, p2aligned(0x5600, 0x100) ==> true + */ +template +constexpr inline bool p2aligned(T x, T align) { + return p2align(x, align) == x; +} + /* * return x % (mod) align * eg, p2phase(0x1234, 0x100) == 0x34 (x-0x12*align) -- 2.39.5