From: Marek Olšák Date: Fri, 28 Aug 2015 18:53:08 +0000 (+0200) Subject: gallium/util: add u_bit_scan_consecutive_range X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=fc292b5821ca2d21cf5ebc83994138b87085d878;p=mesa.git gallium/util: add u_bit_scan_consecutive_range Reviewed-by: Alex Deucher Acked-by: Christian König --- diff --git a/src/gallium/auxiliary/util/u_math.h b/src/gallium/auxiliary/util/u_math.h index 56bd185f527..7175d1d4ee8 100644 --- a/src/gallium/auxiliary/util/u_math.h +++ b/src/gallium/auxiliary/util/u_math.h @@ -483,6 +483,26 @@ u_bit_scan64(uint64_t *mask) } #endif +/* For looping over a bitmask when you want to loop over consecutive bits + * manually, for example: + * + * while (mask) { + * int start, count, i; + * + * u_bit_scan_consecutive_range(&mask, &start, &count); + * + * for (i = 0; i < count; i++) + * ... process element (start+i) + * } + */ +static inline void +u_bit_scan_consecutive_range(unsigned *mask, int *start, int *count) +{ + *start = ffs(*mask) - 1; + *count = ffs(~(*mask >> *start)) - 1; + *mask &= ~(((1 << *count) - 1) << *start); +} + /** * Return float bits. */