[Mesa-dev] [RFC v2 13/15] mesa/macros: add power-of-two assertions for ALIGN and ROUND_DOWN_TO

Nanley Chery nanleychery at gmail.com
Mon Jun 1 10:13:51 PDT 2015


From: Nanley Chery <nanley.g.chery at intel.com>

ALIGN and ROUND_DOWN_TO both require that the alignment value passed into the
macro be a power of two in the comments. Using software assertions verifies
this to be the case.

Signed-off-by: Nanley Chery <nanley.g.chery at intel.com>
---
 src/mesa/main/macros.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/mesa/main/macros.h b/src/mesa/main/macros.h
index c794da3..1348b08 100644
--- a/src/mesa/main/macros.h
+++ b/src/mesa/main/macros.h
@@ -700,7 +700,10 @@ is_power_of_two(unsigned value)
  *
  * \sa ROUND_DOWN_TO()
  */
-#define ALIGN(value, alignment)  (((value) + (alignment) - 1) & ~((alignment) - 1))
+#define ALIGN(value, alignment) ({                        \
+      assert(is_power_of_two(alignment));                 \
+      (((value) + (alignment) - 1) & ~((alignment) - 1)); \
+   })
 
 /**
  * Align a value down to an alignment value
@@ -713,7 +716,10 @@ is_power_of_two(unsigned value)
  *
  * \sa ALIGN()
  */
-#define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
+#define ROUND_DOWN_TO(value, alignment) ({                \
+      assert(is_power_of_two(alignment));                 \
+      ((value) & ~(alignment - 1));                       \
+   })
 
 /**
  * Align a value up to an alignment value
-- 
2.4.1



More information about the mesa-dev mailing list