mesa/macros: add power-of-two assertions for alignment macros

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 (Brian).
v3: fix indendation (Brian).
v4: add greater than zero requirement (Anuj).

Reviewed-by: Anuj Phogat <anuj.phogat@gmail.com>
Signed-off-by: Nanley Chery <nanley.g.chery@intel.com>
This commit is contained in:
Nanley Chery 2015-05-27 13:25:30 -07:00
parent 8b1f008e9a
commit 97f4efd573
2 changed files with 13 additions and 3 deletions

View file

@ -131,7 +131,7 @@ fs_visitor::nir_setup_outputs(nir_shader *shader)
switch (stage) {
case MESA_SHADER_VERTEX:
for (int i = 0; i < ALIGN(type_size_scalar(var->type), 4) / 4; i++) {
for (unsigned int i = 0; i < ALIGN(type_size_scalar(var->type), 4) / 4; i++) {
int output = var->data.location + i;
this->outputs[output] = offset(reg, bld, 4 * i);
this->output_components[output] = vector_elements;

View file

@ -690,7 +690,12 @@ minify(unsigned value, unsigned levels)
*
* \sa ROUND_DOWN_TO()
*/
#define ALIGN(value, alignment) (((value) + (alignment) - 1) & ~((alignment) - 1))
static inline uintptr_t
ALIGN(uintptr_t value, int32_t alignment)
{
assert((alignment > 0) && _mesa_is_pow_two(alignment));
return (((value) + (alignment) - 1) & ~((alignment) - 1));
}
/**
* Align a value down to an alignment value
@ -703,7 +708,12 @@ minify(unsigned value, unsigned levels)
*
* \sa ALIGN()
*/
#define ROUND_DOWN_TO(value, alignment) ((value) & ~(alignment - 1))
static inline uintptr_t
ROUND_DOWN_TO(uintptr_t value, int32_t alignment)
{
assert((alignment > 0) && _mesa_is_pow_two(alignment));
return ((value) & ~(alignment - 1));
}
/** Cross product of two 3-element vectors */