gallium/util: add u_bit_scan_consecutive_range
authorMarek Olšák <marek.olsak@amd.com>
Fri, 28 Aug 2015 18:53:08 +0000 (20:53 +0200)
committerMarek Olšák <marek.olsak@amd.com>
Tue, 1 Sep 2015 19:51:13 +0000 (21:51 +0200)
Reviewed-by: Alex Deucher <alexander.deucher@amd.com>
Acked-by: Christian König <christian.koenig@amd.com>
src/gallium/auxiliary/util/u_math.h

index 56bd185f52790600596a94bcca38a7ddf3646926..7175d1d4ee8c08ba63a89c0f13c193719b7b516e 100644 (file)
@@ -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.
  */