From 81e3c46d065c1f2a8df20159f5c70f2b883926ea Mon Sep 17 00:00:00 2001 From: Samuel Pitoiset Date: Thu, 4 Apr 2024 13:19:37 +0200 Subject: [PATCH] util: add util_is_aligned() Signed-off-by: Samuel Pitoiset Part-of: --- src/util/u_math.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/util/u_math.h b/src/util/u_math.h index c51352fcdc4..7afe136278f 100644 --- a/src/util/u_math.h +++ b/src/util/u_math.h @@ -808,6 +808,17 @@ util_clamped_uadd(unsigned a, unsigned b) return res; } +/** + * Checks the value 'n' is aligned to 'a'. + * The alignment must be a power of two. + */ +static inline bool +util_is_aligned(uintmax_t n, uintmax_t a) +{ + assert(a == (a & -a)); + return (n & (a - 1)) == 0; +} + #ifdef __cplusplus } #endif