[Mesa-dev] [PATCH v3 11/18] mesa/macros: add power-of-two assertions for alignment macros

Brian Paul brianp at vmware.com
Mon Jun 22 16:11:23 PDT 2015


On 06/22/2015 05:02 PM, Nanley Chery wrote:
> 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.
>
> v2: use static inline functions instead of gcc-specific statement expressions.
>
> Signed-off-by: Nanley Chery <nanley.g.chery at intel.com>
> ---
>   src/mesa/drivers/dri/i965/brw_fs_nir.cpp |  2 +-
>   src/mesa/main/macros.h                   | 16 +++++++++++++---
>   2 files changed, 14 insertions(+), 4 deletions(-)
>
> diff --git a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp
> index 59081ea..1a57784 100644
> --- a/src/mesa/drivers/dri/i965/brw_fs_nir.cpp
> +++ b/src/mesa/drivers/dri/i965/brw_fs_nir.cpp
> @@ -134,7 +134,7 @@ fs_visitor::nir_setup_outputs(nir_shader *shader)
>                                  : var->type->vector_elements;
>
>         if (stage == MESA_SHADER_VERTEX) {
> -         for (int i = 0; i < ALIGN(type_size(var->type), 4) / 4; i++) {
> +         for (unsigned int i = 0; i < ALIGN(type_size(var->type), 4) / 4; i++) {
>               int output = var->data.location + i;
>               this->outputs[output] = offset(reg, 4 * i);
>               this->output_components[output] = vector_elements;
> diff --git a/src/mesa/main/macros.h b/src/mesa/main/macros.h
> index 0608650..4a640ad 100644
> --- a/src/mesa/main/macros.h
> +++ b/src/mesa/main/macros.h
> @@ -684,7 +684,7 @@ minify(unsigned value, unsigned levels)
>    * Note that this considers 0 a power of two.
>    */
>   static inline bool
> -is_power_of_two(unsigned value)
> +is_power_of_two(uintptr_t value)
>   {
>      return (value & (value - 1)) == 0;
>   }
> @@ -700,7 +700,12 @@ is_power_of_two(unsigned value)
>    *
>    * \sa ROUND_DOWN_TO()
>    */
> -#define ALIGN(value, alignment)  (((value) + (alignment) - 1) & ~((alignment) - 1))
> +static inline uintptr_t
> +ALIGN(uintptr_t value, uintptr_t alignment)
> +{
> +      assert(is_power_of_two(alignment));
> +      return (((value) + (alignment) - 1) & ~((alignment) - 1));

Looks like more than 3-space indentation here and below.

-Brian

> +}
>
>   /**
>    * Align a value down to an alignment value
> @@ -713,7 +718,12 @@ is_power_of_two(unsigned value)
>    *
>    * \sa ALIGN()
>    */
> -#define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
> +static inline uintptr_t
> +ROUND_DOWN_TO(uintptr_t value, uintptr_t alignment)
> +{
> +      assert(is_power_of_two(alignment));
> +      return ((value) & ~(alignment - 1));
> +}
>
>
>   /** Cross product of two 3-element vectors */
>



More information about the mesa-dev mailing list