Fixes and more tests

This commit is contained in:
King_DuckZ 2018-11-20 09:26:17 +00:00
parent a7d0cefbb9
commit fb774671ad
2 changed files with 86 additions and 19 deletions

View file

@ -55,29 +55,47 @@ namespace dhandy {
struct SizeAndOffs { struct SizeAndOffs {
std::size_t size, offset; std::size_t size, offset;
}; };
struct IndexAndSize {
std::size_t index, size;
};
template <std::size_t Sum> template <std::size_t Sum>
[[gnu::pure]]
constexpr inline SizeAndOffs size_and_offset_impl (std::size_t) { constexpr inline SizeAndOffs size_and_offset_impl (std::size_t) {
throw std::out_of_range("Requested pack index out of range"); throw std::out_of_range("Requested pack index out of range");
} }
template <std::size_t Sum, std::size_t Sz, std::size_t... Sizes> template <std::size_t Sum, std::size_t Sz, std::size_t... Sizes>
[[gnu::pure]]
constexpr inline SizeAndOffs size_and_offset_impl (std::size_t index) { constexpr inline SizeAndOffs size_and_offset_impl (std::size_t index) {
return (index ? size_and_offset_impl<Sum + Sz, Sizes...>(index - 1) : SizeAndOffs{Sz, Sum}); return (index ? size_and_offset_impl<Sum + Sz, Sizes...>(index - 1) : SizeAndOffs{Sz, Sum});
} }
template <std::size_t... Sizes> template <std::size_t... Sizes>
[[gnu::pure]]
constexpr inline SizeAndOffs size_and_offset (std::size_t index) { constexpr inline SizeAndOffs size_and_offset (std::size_t index) {
return size_and_offset_impl<0, Sizes...>(index); return size_and_offset_impl<0, Sizes...>(index);
} }
template <std::size_t CurrMax, std::size_t MaxIdx, std::size_t Idx>
constexpr inline IndexAndSize index_and_size_impl() {
throw std::runtime_error("Should not be reached");
}
template <std::size_t CurrMax, std::size_t MaxIdx, std::size_t Idx, std::size_t Sz, std::size_t... Sizes>
constexpr inline IndexAndSize index_and_size_impl() {
return (Idx ? index_and_size_impl<
(CurrMax > Sz ? CurrMax : Sz),
(CurrMax > Sz ? MaxIdx : Idx),
Idx - 1,
Sizes...
>() : IndexAndSize{(CurrMax > Sz ? MaxIdx : Idx) - 1, CurrMax > Sz ? CurrMax : Sz});
}
template <std::size_t Sz, std::size_t... Sizes>
constexpr inline IndexAndSize index_and_size() {
return index_and_size_impl<Sz, sizeof...(Sizes), sizeof...(Sizes), Sz, Sizes...>();
}
template <typename T, std::size_t... Sizes> template <typename T, std::size_t... Sizes>
struct BitfieldFacts { struct BitfieldFacts {
static const constexpr std::size_t Sum = size_and_offset<Sizes...>(sizeof...(Sizes) - 1).size + size_and_offset<Sizes...>(sizeof...(Sizes) - 1).offset; static const constexpr std::size_t Sum = size_and_offset<Sizes...>(sizeof...(Sizes) - 1).size + size_and_offset<Sizes...>(sizeof...(Sizes) - 1).offset;
static const constexpr std::size_t ElementCount = (Sum + CHAR_BIT * sizeof(T) - 1) / (CHAR_BIT *sizeof(T)); static const constexpr std::size_t ElementCount = (Sum + CHAR_BIT * sizeof(T) - 1) / (CHAR_BIT *sizeof(T));
static const constexpr std::size_t LargestIndex = index_and_size<Sizes...>().index;
static const constexpr std::size_t LargestSize = index_and_size<Sizes...>().size;
}; };
template <typename T> template <typename T>
@ -96,9 +114,11 @@ namespace dhandy {
template <typename T, std::size_t... Sizes> template <typename T, std::size_t... Sizes>
class BitfieldPack : private implem::BitfieldData<T, implem::BitfieldFacts<T, Sizes...>::ElementCount> { class BitfieldPack : private implem::BitfieldData<T, implem::BitfieldFacts<T, Sizes...>::ElementCount> {
typedef implem::BitfieldFacts<T, Sizes...> Facts;
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, implem::BitfieldFacts<T, Sizes...>::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");
public: public:
BitfieldPack (T init=T{}) : base_type(init) {} BitfieldPack (T init=T{}) : base_type(init) {}
}; };
@ -109,6 +129,7 @@ namespace dhandy {
using implem::BitfieldData; using implem::BitfieldData;
using implem::make_mask; using implem::make_mask;
static_assert(Idx < sizeof...(Sizes), "Index out of bounds");
const constexpr auto so = size_and_offset<Sizes...>(Idx); const constexpr auto so = size_and_offset<Sizes...>(Idx);
const constexpr std::size_t index = so.offset / (sizeof(T) * CHAR_BIT); const constexpr std::size_t index = so.offset / (sizeof(T) * CHAR_BIT);
const constexpr std::size_t reloffs = so.offset % (sizeof(T) * CHAR_BIT); const constexpr std::size_t reloffs = so.offset % (sizeof(T) * CHAR_BIT);
@ -131,6 +152,7 @@ namespace dhandy {
using implem::BitfieldData; using implem::BitfieldData;
using implem::make_mask; using implem::make_mask;
static_assert(Idx < sizeof...(Sizes), "Index out of bounds");
const constexpr auto so = size_and_offset<Sizes...>(Idx); const constexpr auto so = size_and_offset<Sizes...>(Idx);
const constexpr std::size_t index = so.offset / (sizeof(T) * CHAR_BIT); const constexpr std::size_t index = so.offset / (sizeof(T) * CHAR_BIT);
const constexpr std::size_t reloffs = so.offset % (sizeof(T) * CHAR_BIT); const constexpr std::size_t reloffs = so.offset % (sizeof(T) * CHAR_BIT);
@ -138,7 +160,7 @@ namespace dhandy {
T& bottom = static_cast<BitfieldData<T, index + 1>&>(pack).value(); T& bottom = static_cast<BitfieldData<T, index + 1>&>(pack).value();
bottom &= ~(make_mask<T>(so.size) << reloffs); bottom &= ~(make_mask<T>(so.size) << reloffs);
bottom |= static_cast<T>((val << reloffs) & make_mask<T>(so.size)); bottom |= static_cast<T>((val & make_mask<T>(so.size)) << reloffs);
if (bleeds_into_next) { if (bleeds_into_next) {
const std::size_t safe_index = index + 2 - not bleeds_into_next; const std::size_t safe_index = index + 2 - not bleeds_into_next;

View file

@ -20,16 +20,61 @@
#include <cstdint> #include <cstdint>
TEST_CASE ("Check correctness in BitfieldPack", "[BitfieldPack][containers]") { TEST_CASE ("Check correctness in BitfieldPack", "[BitfieldPack][containers]") {
using dhandy::set;
using dhandy::get;
{
dhandy::BitfieldPack<uint8_t, 4, 7, 1> pack(0xAA); dhandy::BitfieldPack<uint8_t, 4, 7, 1> pack(0xAA);
CHECK(sizeof(pack) == sizeof(uint8_t) * 2); CHECK(sizeof(pack) == sizeof(uint8_t) * 2);
CHECK(dhandy::get<0>(pack) == 0b1010U); CHECK(get<0>(pack) == 0b1010U);
CHECK(dhandy::get<1>(pack) == 0b0101010U); CHECK(get<1>(pack) == 0b0101010U);
CHECK(dhandy::get<2>(pack) == 0b1U); CHECK(get<2>(pack) == 0b1U);
set<0>(pack, 2);
CHECK(get<0>(pack) == 2);
set<1>(pack, 50);
CHECK(get<1>(pack) == 50);
CHECK(get<2>(pack) == 0b1U);
CHECK(get<0>(pack) == 2);
}
{
dhandy::BitfieldPack<uint16_t, 16, 5, 8, 11, 3, 1, 1> pack;
set<6>(pack, 1);
CHECK(get<0>(pack) == 0);
CHECK(get<1>(pack) == 0);
CHECK(get<2>(pack) == 0);
CHECK(get<3>(pack) == 0);
CHECK(get<4>(pack) == 0);
CHECK(get<5>(pack) == 0);
CHECK(get<6>(pack) == 1);
set<6>(pack, 0);
CHECK(get<6>(pack) == 0);
set<6>(pack, 1);
set<1>(pack, 5);
CHECK(get<0>(pack) == 0);
CHECK(get<1>(pack) == 5);
CHECK(get<2>(pack) == 0);
CHECK(get<3>(pack) == 0);
CHECK(get<4>(pack) == 0);
CHECK(get<5>(pack) == 0);
CHECK(get<6>(pack) == 1);
set<1>(pack, 0);
CHECK(get<1>(pack) == 0);
dhandy::set<0>(pack, 2); set<6>(pack, 1);
CHECK(dhandy::get<0>(pack) == 2); set<1>(pack, 5);
dhandy::set<1>(pack, 50); set<3>(pack, 0b10011001101);
CHECK(dhandy::get<1>(pack) == 50); CHECK(get<0>(pack) == 0);
CHECK(dhandy::get<2>(pack) == 0b1U); CHECK(get<1>(pack) == 5);
CHECK(get<2>(pack) == 0);
CHECK(get<3>(pack) == 0b10011001101);
CHECK(get<4>(pack) == 0);
CHECK(get<5>(pack) == 0);
CHECK(get<6>(pack) == 1);
}
} }