diff options
author | Ian Romanick <[email protected]> | 2017-11-13 11:17:41 -0800 |
---|---|---|
committer | Ian Romanick <[email protected]> | 2018-03-29 14:09:23 -0700 |
commit | d76c204d0564701b4b8b6a2bdda50e2939683e66 (patch) | |
tree | edcf51df711640ffd89326079f10b1dcf65f311b /src/util | |
parent | a3a16d4aa7e5a22816226d8e7417138164b10525 (diff) |
util: Move util_is_power_of_two to bitscan.h and rename to util_is_power_of_two_or_zero
The new name make the zero-input behavior more obvious. The next
patch adds a new function with different zero-input behavior.
Signed-off-by: Ian Romanick <[email protected]>
Suggested-by: Matt Turner <[email protected]>
Reviewed-by: Alejandro PiƱeiro <[email protected]>
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/bitscan.h | 12 | ||||
-rw-r--r-- | src/util/u_vector.c | 4 |
2 files changed, 14 insertions, 2 deletions
diff --git a/src/util/bitscan.h b/src/util/bitscan.h index 611e8120596..2d4e46ec0f1 100644 --- a/src/util/bitscan.h +++ b/src/util/bitscan.h @@ -31,6 +31,7 @@ #include <assert.h> #include <stdint.h> +#include <stdbool.h> #include <string.h> #if defined(_MSC_VER) @@ -107,6 +108,17 @@ u_bit_scan64(uint64_t *mask) return i; } +/* Determine if an unsigned value is a power of two. + * + * \note + * Zero is treated as a power of two. + */ +static inline bool +util_is_power_of_two_or_zero(unsigned v) +{ + return (v & (v - 1)) == 0; +} + /* For looping over a bitmask when you want to loop over consecutive bits * manually, for example: * diff --git a/src/util/u_vector.c b/src/util/u_vector.c index 0de492ccf9a..bec6e5bbc30 100644 --- a/src/util/u_vector.c +++ b/src/util/u_vector.c @@ -37,8 +37,8 @@ int u_vector_init(struct u_vector *vector, uint32_t element_size, uint32_t size) { - assert(util_is_power_of_two(size)); - assert(element_size < size && util_is_power_of_two(element_size)); + assert(util_is_power_of_two_or_zero(size)); + assert(element_size < size && util_is_power_of_two_or_zero(element_size)); vector->head = 0; vector->tail = 0; |