From b5176ded585bfc183cd517b0eec1acce7c13a3da Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Wed, 9 Aug 2017 17:15:37 -0700 Subject: [PATCH] util: add spdk_u32_is_pow2() function Change-Id: I438053d85360f1c3ecc2b7661dbd573e0217ac46 Signed-off-by: Daniel Verkamp Reviewed-on: https://review.gerrithub.io/373672 Tested-by: SPDK Automated Test System Reviewed-by: Ben Walker Reviewed-by: Changpeng Liu Reviewed-by: Jim Harris --- include/spdk/util.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/include/spdk/util.h b/include/spdk/util.h index 0ab4843bc..f7a36a611 100644 --- a/include/spdk/util.h +++ b/include/spdk/util.h @@ -67,6 +67,19 @@ spdk_align32pow2(uint32_t x) return 1u << (1 + spdk_u32log2(x - 1)); } +/** + * Check if a uint32_t is a power of 2. + */ +static inline bool +spdk_u32_is_pow2(uint32_t x) +{ + if (x == 0) { + return false; + } + + return (x & (x - 1)) == 0; +} + #ifdef __cplusplus } #endif