aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChris Robinson <[email protected]>2020-03-22 21:34:50 -0700
committerChris Robinson <[email protected]>2020-03-22 21:34:50 -0700
commit132a69c03da18aab88d10179888fd595943100e5 (patch)
tree09b8eb7ca5f2cbe2e713ec0e825c7dfce90dc081
parent46234171d134b4cbca15e276c4fdb1dfbbc70fbc (diff)
Use more appropriate types for bitfields
-rw-r--r--common/albyte.h14
1 files changed, 10 insertions, 4 deletions
diff --git a/common/albyte.h b/common/albyte.h
index 40a10d04..b74659ba 100644
--- a/common/albyte.h
+++ b/common/albyte.h
@@ -59,29 +59,35 @@ inline constexpr al::byte operator~(al::byte b) noexcept
template<size_t N>
class bitfield {
+ template<size_t> struct ElemT { };
+ template<> struct ElemT<1> { using type = uint8_t; };
+ template<> struct ElemT<2> { using type = uint16_t; };
+ template<> struct ElemT<3> { using type = uint32_t; };
+ template<> struct ElemT<4> { using type = uint32_t; };
+
static constexpr size_t bits_per_byte{std::numeric_limits<unsigned char>::digits};
static constexpr size_t NumElems{(N+bits_per_byte-1) / bits_per_byte};
- byte vals[NumElems]{};
+ typename ElemT<NumElems>::type vals{};
public:
template<size_t b>
inline void set() noexcept
{
static_assert(b < N, "Bit index out of range");
- vals[b/bits_per_byte] |= 1 << (b%bits_per_byte);
+ vals |= 1 << b;
}
template<size_t b>
inline void unset() noexcept
{
static_assert(b < N, "Bit index out of range");
- vals[b/bits_per_byte] &= ~(1 << (b%bits_per_byte));
+ vals &= ~(1 << b);
}
template<size_t b>
inline bool get() const noexcept
{
static_assert(b < N, "Bit index out of range");
- return (vals[b/bits_per_byte] & (1 << (b%bits_per_byte))) != byte{};
+ return (vals & (1 << b)) != 0;
}
template<size_t b, size_t ...args, REQUIRES(sizeof...(args) > 0)>