Module: Mesa
Branch: main
Commit: 9bed04c5a65416352097033ef1649e6ae6463059
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=9bed04c5a65416352097033ef1649e6ae6463059

Author: Yonggang Luo <luoyongg...@gmail.com>
Date:   Thu Nov  2 03:27:10 2023 +0800

util: Add function util_is_power_of_two_nonzero_uintptr and macro IS_POT_NONZERO

The name suffix 'NONZERO' matched suffix of util_is_power_of_two_nonzero
IS_POT_NONZERO added for reduce duplicated code and compatible for different 
size
uintptr_t,uint32_t,uint64_t

Signed-off-by: Yonggang Luo <luoyongg...@gmail.com>
Reviewed-by: Marek Olšák <marek.ol...@amd.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26909>

---

 src/util/bitscan.h | 15 +++++++++++++--
 src/util/macros.h  |  3 +++
 src/util/u_math.h  |  6 +-----
 3 files changed, 17 insertions(+), 7 deletions(-)

diff --git a/src/util/bitscan.h b/src/util/bitscan.h
index 3da2e9639e4..ae0e721e61c 100644
--- a/src/util/bitscan.h
+++ b/src/util/bitscan.h
@@ -164,7 +164,7 @@ util_is_power_of_two_nonzero(uint32_t v)
 #ifdef __POPCNT__
    return _mm_popcnt_u32(v) == 1;
 #else
-   return v != 0 && IS_POT(v);
+   return IS_POT_NONZERO(v);
 #endif
 }
 
@@ -176,7 +176,18 @@ util_is_power_of_two_nonzero(uint32_t v)
 static inline bool
 util_is_power_of_two_nonzero64(uint64_t v)
 {
-   return v != 0 && IS_POT(v);
+   return IS_POT_NONZERO(v);
+}
+
+/* Determine if an size_t/uintptr_t/intptr_t value is a power of two.
+ *
+ * \note
+ * Zero is \b not treated as a power of two.
+ */
+static inline bool
+util_is_power_of_two_nonzero_uintptr(uintptr_t v)
+{
+   return IS_POT_NONZERO(v);
 }
 
 /* For looping over a bitmask when you want to loop over consecutive bits
diff --git a/src/util/macros.h b/src/util/macros.h
index cfcda53ac0f..dac3872e781 100644
--- a/src/util/macros.h
+++ b/src/util/macros.h
@@ -384,6 +384,9 @@ do {                       \
 /** Checks is a value is a power of two. Does not handle zero. */
 #define IS_POT(v) (((v) & ((v) - 1)) == 0)
 
+/** Checks is a value is a power of two. Zero handled. */
+#define IS_POT_NONZERO(v) ((v) != 0 && IS_POT(v))
+
 /** Set a single bit */
 #define BITFIELD_BIT(b)      (1u << (b))
 /** Set all bits up to excluding bit b */
diff --git a/src/util/u_math.h b/src/util/u_math.h
index 773ad31be46..208183d4069 100644
--- a/src/util/u_math.h
+++ b/src/util/u_math.h
@@ -691,11 +691,7 @@ align64(uint64_t value, uint64_t alignment)
 static inline uintptr_t
 align_uintptr(uintptr_t value, uintptr_t alignment)
 {
-#if UINTPTR_MAX == UINT64_MAX
-   assert(util_is_power_of_two_nonzero64(alignment));
-#else
-   assert(util_is_power_of_two_nonzero(alignment));
-#endif
+   assert(util_is_power_of_two_nonzero_uintptr(alignment));
    return ALIGN_POT(value, alignment);
 }
 

Reply via email to