util/drirc: turn on force_glsl_extensions_warn for No Mans Sky
[mesa.git] / src / util / bitset.h
index febcddefd9714c37c35281d0da89fb53a5700fd7..adafc72a5f74d46e118a1333cc84900f0e468932 100644 (file)
@@ -31,7 +31,8 @@
 #ifndef BITSET_H
 #define BITSET_H
 
-#include "util/u_math.h"
+#include "util/bitscan.h"
+#include "util/macros.h"
 
 /****************************************************************************
  * generic bitset implementation
@@ -96,4 +97,157 @@ __bitset_ffs(const BITSET_WORD *x, int n)
 
 #define BITSET_FFS(x) __bitset_ffs(x, ARRAY_SIZE(x))
 
+static inline unsigned
+__bitset_next_set(unsigned i, BITSET_WORD *tmp,
+                  const BITSET_WORD *set, unsigned size)
+{
+   unsigned bit, word;
+
+   /* NOTE: The initial conditions for this function are very specific.  At
+    * the start of the loop, the tmp variable must be set to *set and the
+    * initial i value set to 0.  This way, if there is a bit set in the first
+    * word, we ignore the i-value and just grab that bit (so 0 is ok, even
+    * though 0 may be returned).  If the first word is 0, then the value of
+    * `word` will be 0 and we will go on to look at the second word.
+    */
+   word = BITSET_BITWORD(i);
+   while (*tmp == 0) {
+      word++;
+
+      if (word >= BITSET_WORDS(size))
+         return size;
+
+      *tmp = set[word];
+   }
+
+   /* Find the next set bit in the non-zero word */
+   bit = ffs(*tmp) - 1;
+
+   /* Unset the bit */
+   *tmp &= ~(1ull << bit);
+
+   return word * BITSET_WORDBITS + bit;
+}
+
+#define BITSET_FOREACH_SET(__i, __tmp, __set, __size) \
+   for (__tmp = *(__set), __i = 0; \
+        (__i = __bitset_next_set(__i, &__tmp, __set, __size)) < __size;)
+
+#ifdef __cplusplus
+
+/**
+ * Simple C++ wrapper of a bitset type of static size, with value semantics
+ * and basic bitwise arithmetic operators.  The operators defined below are
+ * expected to have the same semantics as the same operator applied to other
+ * fundamental integer types.  T is the name of the struct to instantiate
+ * it as, and N is the number of bits in the bitset.
+ */
+#define DECLARE_BITSET_T(T, N) struct T {                       \
+      EXPLICIT_CONVERSION                                       \
+      operator bool() const                                     \
+      {                                                         \
+         for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
+            if (words[i])                                       \
+               return true;                                     \
+         return false;                                          \
+      }                                                         \
+                                                                \
+      T &                                                       \
+      operator=(int x)                                          \
+      {                                                         \
+         const T c = {{ (BITSET_WORD)x }};                      \
+         return *this = c;                                      \
+      }                                                         \
+                                                                \
+      friend bool                                               \
+      operator==(const T &b, const T &c)                        \
+      {                                                         \
+         return BITSET_EQUAL(b.words, c.words);                 \
+      }                                                         \
+                                                                \
+      friend bool                                               \
+      operator!=(const T &b, const T &c)                        \
+      {                                                         \
+         return !(b == c);                                      \
+      }                                                         \
+                                                                \
+      friend bool                                               \
+      operator==(const T &b, int x)                             \
+      {                                                         \
+         const T c = {{ (BITSET_WORD)x }};                      \
+         return b == c;                                         \
+      }                                                         \
+                                                                \
+      friend bool                                               \
+      operator!=(const T &b, int x)                             \
+      {                                                         \
+         return !(b == x);                                      \
+      }                                                         \
+                                                                \
+      friend T                                                  \
+      operator~(const T &b)                                     \
+      {                                                         \
+         T c;                                                   \
+         for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
+            c.words[i] = ~b.words[i];                           \
+         return c;                                              \
+      }                                                         \
+                                                                \
+      T &                                                       \
+      operator|=(const T &b)                                    \
+      {                                                         \
+         for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
+            words[i] |= b.words[i];                             \
+         return *this;                                          \
+      }                                                         \
+                                                                \
+      friend T                                                  \
+      operator|(const T &b, const T &c)                         \
+      {                                                         \
+         T d = b;                                               \
+         d |= c;                                                \
+         return d;                                              \
+      }                                                         \
+                                                                \
+      T &                                                       \
+      operator&=(const T &b)                                    \
+      {                                                         \
+         for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
+            words[i] &= b.words[i];                             \
+         return *this;                                          \
+      }                                                         \
+                                                                \
+      friend T                                                  \
+      operator&(const T &b, const T &c)                         \
+      {                                                         \
+         T d = b;                                               \
+         d &= c;                                                \
+         return d;                                              \
+      }                                                         \
+                                                                \
+      bool                                                      \
+      test(unsigned i) const                                    \
+      {                                                         \
+         return BITSET_TEST(words, i);                          \
+      }                                                         \
+                                                                \
+      T &                                                       \
+      set(unsigned i)                                           \
+      {                                                         \
+         BITSET_SET(words, i);                                  \
+         return *this;                                          \
+      }                                                         \
+                                                                \
+      T &                                                       \
+      clear(unsigned i)                                         \
+      {                                                         \
+         BITSET_CLEAR(words, i);                                \
+         return *this;                                          \
+      }                                                         \
+                                                                \
+      BITSET_WORD words[BITSET_WORDS(N)];                       \
+   }
+
+#endif
+
 #endif