175 lines
6.6 KiB
C++
175 lines
6.6 KiB
C++
/* Copyright 2016-2021 Michele Santullo
|
|
* This file is part of "duckhandy".
|
|
*
|
|
* "duckhandy" is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* "duckhandy" is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef id895441FC389F481EB824A809A0BF5004
|
|
#define id895441FC389F481EB824A809A0BF5004
|
|
|
|
#include <cstddef>
|
|
#include <stdexcept>
|
|
#include <climits>
|
|
|
|
namespace dhandy {
|
|
namespace implem {
|
|
template <typename T, std::size_t Index>
|
|
class BitfieldData;
|
|
|
|
template <typename T, std::size_t Index>
|
|
class BitfieldData : public BitfieldData<T, Index - 1> {
|
|
protected:
|
|
BitfieldData (T init) : BitfieldData<T, Index - 1>(init), m_value(init) {}
|
|
public:
|
|
T& value() { return m_value; }
|
|
T value() const { return m_value; }
|
|
private:
|
|
T m_value;
|
|
};
|
|
|
|
template <typename T>
|
|
class BitfieldData<T, 1> {
|
|
protected:
|
|
BitfieldData (T init) : m_value(init) {}
|
|
public:
|
|
T& value() { return m_value; }
|
|
T value() const { return m_value; }
|
|
private:
|
|
T m_value;
|
|
};
|
|
|
|
template <typename T>
|
|
class BitfieldData<T, 0>;
|
|
|
|
struct SizeAndOffs {
|
|
std::size_t size, offset;
|
|
};
|
|
struct IndexAndSize {
|
|
std::size_t index, size;
|
|
};
|
|
|
|
template <std::size_t Sum>
|
|
constexpr inline SizeAndOffs size_and_offset_impl (std::size_t) {
|
|
throw std::out_of_range("Requested pack index out of range");
|
|
}
|
|
template <std::size_t Sum, std::size_t Sz, std::size_t... Sizes>
|
|
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});
|
|
}
|
|
template <std::size_t... Sizes>
|
|
constexpr inline SizeAndOffs size_and_offset (std::size_t 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>
|
|
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 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>
|
|
constexpr inline T make_mask (std::size_t len) {
|
|
return (1 << len) - 1;
|
|
}
|
|
} //namespace implem
|
|
|
|
template <typename T, std::size_t... Sizes>
|
|
class BitfieldPack;
|
|
|
|
template <std::size_t Idx, typename T, std::size_t... Sizes>
|
|
T get (const BitfieldPack<T, Sizes...>&);
|
|
template <std::size_t Idx, typename T, typename V, std::size_t... Sizes>
|
|
void set (BitfieldPack<T, Sizes...>&, V);
|
|
|
|
template <typename T, std::size_t... Sizes>
|
|
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, typename V, std::size_t... S> friend void set (BitfieldPack<U, S...>&, V);
|
|
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:
|
|
BitfieldPack (T init=T{}) : base_type(init) {}
|
|
};
|
|
|
|
template <std::size_t Idx, typename T, std::size_t... Sizes>
|
|
T get (const BitfieldPack<T, Sizes...>& pack) {
|
|
using implem::size_and_offset;
|
|
using implem::BitfieldData;
|
|
using implem::make_mask;
|
|
|
|
static_assert(Idx < sizeof...(Sizes), "Index out of bounds");
|
|
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 reloffs = so.offset % (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;
|
|
if (bleeds_into_next) {
|
|
const std::size_t safe_index = index + 2 - not bleeds_into_next;
|
|
const T val = static_cast<const BitfieldData<T, safe_index>&>(pack).value();
|
|
const T top = val << (sizeof(T) * CHAR_BIT - reloffs);
|
|
return static_cast<T>(top | 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>
|
|
void set (BitfieldPack<T, Sizes...>& pack, V val) {
|
|
using implem::size_and_offset;
|
|
using implem::BitfieldData;
|
|
using implem::make_mask;
|
|
|
|
static_assert(Idx < sizeof...(Sizes), "Index out of bounds");
|
|
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 reloffs = so.offset % (sizeof(T) * CHAR_BIT);
|
|
const constexpr bool bleeds_into_next = reloffs + so.size > sizeof(T) * CHAR_BIT;
|
|
|
|
T& bottom = static_cast<BitfieldData<T, index + 1>&>(pack).value();
|
|
bottom &= ~(make_mask<T>(so.size) << reloffs);
|
|
bottom |= static_cast<T>((val & make_mask<T>(so.size)) << reloffs);
|
|
|
|
if (bleeds_into_next) {
|
|
const std::size_t safe_index = index + 2 - not bleeds_into_next;
|
|
T& top = static_cast<BitfieldData<T, safe_index>&>(pack).value();
|
|
const constexpr std::size_t shift = sizeof(T) * CHAR_BIT - reloffs;
|
|
top &= ~(make_mask<T>(so.size) >> shift);
|
|
top |= val >> shift;
|
|
}
|
|
}
|
|
} //namespace dhandy
|
|
|
|
#endif
|