[Mesa-dev] [PATCH 07/15] util/bitset: Add C++ wrapper for static-size bitsets.

Eric Engestrom eric.engestrom at imgtec.com
Thu Feb 15 10:51:12 UTC 2018


On Wednesday, 2018-02-14 13:18:29 -0800, Francisco Jerez wrote:
> ---
>  src/util/bitset.h | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  1 file changed, 114 insertions(+)
> 
> diff --git a/src/util/bitset.h b/src/util/bitset.h
> index 2404ce7f630..7bb5f3c83cf 100644
> --- a/src/util/bitset.h
> +++ b/src/util/bitset.h
> @@ -132,4 +132,118 @@ __bitset_next_set(unsigned i, BITSET_WORD *tmp,
>     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.
> + */

Any reason not to use std::bitset<N> for this?
http://en.cppreference.com/w/cpp/utility/bitset

> +#define DECLARE_BITSET_T(T, N) struct T {                       \
> +      /* XXX - Replace this with an implicitly-defined          \
> +       * constructor when support for C++11 defaulted           \
> +       * constructors can be assumed (available on GCC 4.4 and  \
> +       * later) in order to make the object trivially           \
> +       * constructible like a fundamental integer type for      \
> +       * convenience.                                           \
> +       */                                                       \
> +      T()                                                       \
> +      {                                                         \
> +      }                                                         \
> +                                                                \
> +      T(BITSET_WORD x)                                          \
> +      {                                                         \
> +         for (unsigned i = 0; i < BITSET_WORDS(N); i++, x = 0)  \
> +            words[i] = x;                                       \
> +      }                                                         \
> +                                                                \
> +      EXPLICIT_CONVERSION                                       \
> +      operator bool() const                                     \
> +      {                                                         \
> +         for (unsigned i = 0; i < BITSET_WORDS(N); i++)         \
> +            if (words[i])                                       \
> +               return true;                                     \
> +         return false;                                          \
> +      }                                                         \
> +                                                                \
> +      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 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
> -- 
> 2.16.1
> 
> _______________________________________________
> mesa-dev mailing list
> mesa-dev at lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/mesa-dev


More information about the mesa-dev mailing list