Mesa (main): util: add IS_POT macro

GitLab Mirror gitlab-mirror at kemper.freedesktop.org
Fri Jun 3 07:49:47 UTC 2022


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

Author: Erik Faye-Lund <erik.faye-lund at collabora.com>
Date:   Tue May 31 12:31:41 2022 +0200

util: add IS_POT macro

This macro kinda complements util_is_power_of_two_*, but is implemented
as a macro. This means that it can expand to a constant integral
expression, and thus be used in static_assert.

Because we don't really need the added complexity, this doesn't handle
zero correctly. But that's OK, because the call-sites will.

Reviewed-by: Ian Romanick <ian.d.romanick at intel.com>
Reviewed-by: Jesse Natalie <jenatali at microsoft.com>
Reviewed-by: Yonggang Luo <luoyonggang at gmail.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/16670>

---

 src/util/macros.h | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/src/util/macros.h b/src/util/macros.h
index dd8af86bcf8..fafe59e04cc 100644
--- a/src/util/macros.h
+++ b/src/util/macros.h
@@ -371,6 +371,9 @@ do {                       \
 /** Align a value to a power of two */
 #define ALIGN_POT(x, pot_align) (((x) + (pot_align) - 1) & ~((pot_align) - 1))
 
+/** Checks is a value is a power of two. Does not handle zero. */
+#define IS_POT(v) (((v) & ((v) - 1)) == 0)
+
 /**
  * Macro for declaring an explicit conversion operator.  Defaults to an
  * implicit conversion if C++11 is not supported.



More information about the mesa-commit mailing list