From 5ef9d11cb5b85a9e4b3c36634a4ccf5ee2a9cbbb Mon Sep 17 00:00:00 2001 From: Adam Kupczyk Date: Thu, 9 Nov 2023 01:06:38 +0000 Subject: [PATCH] include/intarith: add p2remain p2remain gives remaining data in a block. It simialar to p2nphase, but for 0 offset returns full size. Signed-off-by: Adam Kupczyk --- src/include/intarith.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/include/intarith.h b/src/include/intarith.h index 92f827d88e580..260083edeb630 100644 --- a/src/include/intarith.h +++ b/src/include/intarith.h @@ -78,6 +78,17 @@ constexpr inline T p2nphase(T x, T align) { return -x & (align - 1); } +/* + * return how much space is left in this block, + * when perfectly aligned, return whole block + * eg, p2remain(0x1234, 0x100) == 0xcc + * eg, p2remain(0x5600, 0x100) == 0x100 + */ +template +constexpr inline T p2remain(T x, T align) { + return align - p2phase(x, align); +} + /* * return x rounded up to an align boundary * eg, p2roundup(0x1234, 0x100) == 0x1300 (0x13*align) -- 2.39.5