Some small code improvements

This commit is contained in:
King_DuckZ 2021-05-28 02:41:41 +02:00
parent 562eba5f68
commit 9539b5430f

View file

@ -99,7 +99,8 @@ namespace dhandy {
}; };
template <typename T> template <typename T>
constexpr inline T make_mask (std::size_t len) { [[gnu::const]]
constexpr inline T make_mask (std::size_t len) noexcept {
return (1 << len) - 1; return (1 << len) - 1;
} }
} //namespace implem } //namespace implem
@ -118,7 +119,7 @@ namespace dhandy {
template <std::size_t Idx, typename U, std::size_t... S> friend U get (const BitfieldPack<U, S...>&); template <std::size_t Idx, typename U, std::size_t... S> friend U get (const BitfieldPack<U, S...>&);
template <std::size_t Idx, typename U, typename V, std::size_t... S> friend void set (BitfieldPack<U, S...>&, V); template <std::size_t Idx, typename U, typename V, std::size_t... S> friend void set (BitfieldPack<U, S...>&, V);
typedef implem::BitfieldData<T, Facts::ElementCount> base_type; typedef implem::BitfieldData<T, Facts::ElementCount> base_type;
static_assert(Facts::LargestSize <= sizeof(T) * CHAR_BIT, "Bitfield size can't be larget than the data type itself"); static_assert(Facts::LargestSize <= sizeof(T) * CHAR_BIT, "Bitfield size can't be larger than the data type itself");
public: public:
BitfieldPack (T init=T{}) : base_type(init) {} BitfieldPack (T init=T{}) : base_type(init) {}
}; };
@ -136,15 +137,16 @@ namespace dhandy {
const constexpr bool bleeds_into_next = reloffs + so.size > sizeof(T) * CHAR_BIT; const constexpr bool bleeds_into_next = reloffs + so.size > sizeof(T) * CHAR_BIT;
const T bottom = static_cast<const BitfieldData<T, index + 1>&>(pack).value() >> reloffs; const T bottom = static_cast<const BitfieldData<T, index + 1>&>(pack).value() >> reloffs;
if (bleeds_into_next) { if constexpr (bleeds_into_next) {
const std::size_t safe_index = index + 2 - not bleeds_into_next; const std::size_t safe_index = index + 2;
const T val = static_cast<const BitfieldData<T, safe_index>&>(pack).value(); const T val = static_cast<const BitfieldData<T, safe_index>&>(pack).value();
const T top = val << (sizeof(T) * CHAR_BIT - reloffs); const T top = val << (sizeof(T) * CHAR_BIT - reloffs);
return static_cast<T>(top | bottom) & make_mask<T>(so.size); return static_cast<T>(top | bottom) & make_mask<T>(so.size);
} }
else {
return bottom & make_mask<T>(so.size); return bottom & make_mask<T>(so.size);
} }
}
template <std::size_t Idx, typename T, typename V, std::size_t... Sizes> template <std::size_t Idx, typename T, typename V, std::size_t... Sizes>
void set (BitfieldPack<T, Sizes...>& pack, V val) { void set (BitfieldPack<T, Sizes...>& pack, V val) {