[util] Remove asserts from util::bitset

When used in kernel vs. non-kernel code the assert macros were not
working as expected. Other util code does not use assert like this, so
I'm just dropping it from bitset.
This commit is contained in:
Justin C. Miller
2023-03-16 19:49:43 -07:00
parent 069bc63d1f
commit 3ab1a6b170

View File

@@ -2,12 +2,6 @@
/// \file bitset.h /// \file bitset.h
/// Definition of the `bitset` template class /// Definition of the `bitset` template class
#if __has_include(<assert.h>)
#include <assert.h>
#else
#define assert(...)
#endif
#include <stdint.h> #include <stdint.h>
namespace util { namespace util {
@@ -20,26 +14,27 @@ class bitset
public: public:
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bool get(T i) const { inline bool get(T i) const {
assert(static_cast<unsigned>(i) < N);
return bits(i) & bit(i); return bits(i) & bit(i);
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & set(T i) { inline bitset & set(T i) {
assert(static_cast<unsigned>(i) < N);
bits(i) |= bit(i); bits(i) |= bit(i);
return *this; return *this;
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & clear(T i) { inline bitset & clear(T i) {
assert(static_cast<unsigned>(i) < N);
bits(i) &= ~bit(i); bits(i) &= ~bit(i);
return *this; return *this;
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bool operator[](T i) const { return get(i); } inline bool operator[](T i) const { return get(i); }
inline bool empty() const { inline bool empty() const {
@@ -50,12 +45,15 @@ public:
private: private:
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline uint64_t bit(T i) const { return (1ull << (static_cast<uint64_t>(i) & 63)); } inline uint64_t bit(T i) const { return (1ull << (static_cast<uint64_t>(i) & 63)); }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline uint64_t &bits(T i) { return m_bits[static_cast<uint64_t>(i) >> 6]; } inline uint64_t &bits(T i) { return m_bits[static_cast<uint64_t>(i) >> 6]; }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline uint64_t bits(T i) const { return m_bits[static_cast<uint64_t>(i) >> 6]; } inline uint64_t bits(T i) const { return m_bits[static_cast<uint64_t>(i) >> 6]; }
uint64_t m_bits[num_elems] = {0}; uint64_t m_bits[num_elems] = {0};
@@ -85,30 +83,33 @@ public:
constexpr explicit bitset(Args... args) : m_bits(bit_or(args...)) {} constexpr explicit bitset(Args... args) : m_bits(bit_or(args...)) {}
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & operator=(T v) { m_bits = static_cast<uint64_t>(v); return *this; } inline bitset & operator=(T v) { m_bits = static_cast<uint64_t>(v); return *this; }
inline constexpr operator const uint64_t () const { return m_bits; } inline constexpr operator const uint64_t () const { return m_bits; }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline constexpr bool get(T i) const { inline constexpr bool get(T i) const {
return m_bits & bit(i); return m_bits & bit(i);
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & set(T i) { inline bitset & set(T i) {
assert(static_cast<unsigned>(i) < 64);
m_bits |= bit(i); m_bits |= bit(i);
return *this; return *this;
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & clear(T i) { inline bitset & clear(T i) {
assert(static_cast<unsigned>(i) < 64);
m_bits &= ~bit(i); m_bits &= ~bit(i);
return *this; return *this;
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline constexpr bool operator[](T i) const { return get(i); } inline constexpr bool operator[](T i) const { return get(i); }
inline constexpr bool empty() const { return m_bits == 0; } inline constexpr bool empty() const { return m_bits == 0; }
@@ -148,25 +149,27 @@ public:
inline constexpr operator uint32_t () const { return m_bits; } inline constexpr operator uint32_t () const { return m_bits; }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline constexpr bool get(T i) const { inline constexpr bool get(T i) const {
return m_bits & bit(i); return m_bits & bit(i);
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & set(T i) { inline bitset & set(T i) {
assert(static_cast<unsigned>(i) < 32);
m_bits |= bit(i); m_bits |= bit(i);
return *this; return *this;
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bitset & clear(T i) { inline bitset & clear(T i) {
assert(static_cast<unsigned>(i) < 32);
m_bits &= ~bit(i); m_bits &= ~bit(i);
return *this; return *this;
} }
template <typename T> template <typename T>
__attribute__ ((force_inline))
inline bool operator[](T i) const { return get(i); } inline bool operator[](T i) const { return get(i); }
inline bool empty() const { return m_bits == 0; } inline bool empty() const { return m_bits == 0; }