Add BitfieldPack implementation.
This commit is contained in:
parent
4eb4209409
commit
a7d0cefbb9
3 changed files with 189 additions and 0 deletions
153
include/duckhandy/bitfield_pack.hpp
Normal file
153
include/duckhandy/bitfield_pack.hpp
Normal file
|
@ -0,0 +1,153 @@
|
||||||
|
/* Copyright 2016-2018 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;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <std::size_t Sum>
|
||||||
|
[[gnu::pure]]
|
||||||
|
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>
|
||||||
|
[[gnu::pure]]
|
||||||
|
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>
|
||||||
|
[[gnu::pure]]
|
||||||
|
constexpr inline SizeAndOffs size_and_offset (std::size_t index) {
|
||||||
|
return size_and_offset_impl<0, Sizes...>(index);
|
||||||
|
}
|
||||||
|
|
||||||
|
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));
|
||||||
|
};
|
||||||
|
|
||||||
|
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> {
|
||||||
|
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, implem::BitfieldFacts<T, Sizes...>::ElementCount> base_type;
|
||||||
|
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;
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
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 << reloffs) & make_mask<T>(so.size));
|
||||||
|
|
||||||
|
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
|
|
@ -6,6 +6,7 @@ add_executable(${PROJECT_NAME}
|
||||||
endianness_test.cpp
|
endianness_test.cpp
|
||||||
int_conv_test.cpp
|
int_conv_test.cpp
|
||||||
reversed_sized_array_test.cpp
|
reversed_sized_array_test.cpp
|
||||||
|
bitfield_pack_test.cpp
|
||||||
)
|
)
|
||||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
|
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
|
||||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||||
|
|
35
test/unit/bitfield_pack_test.cpp
Normal file
35
test/unit/bitfield_pack_test.cpp
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
/* Copyright 2016-2018 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "catch2/catch.hpp"
|
||||||
|
#include "duckhandy/bitfield_pack.hpp"
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
TEST_CASE ("Check correctness in BitfieldPack", "[BitfieldPack][containers]") {
|
||||||
|
dhandy::BitfieldPack<uint8_t, 4, 7, 1> pack(0xAA);
|
||||||
|
CHECK(sizeof(pack) == sizeof(uint8_t) * 2);
|
||||||
|
|
||||||
|
CHECK(dhandy::get<0>(pack) == 0b1010U);
|
||||||
|
CHECK(dhandy::get<1>(pack) == 0b0101010U);
|
||||||
|
CHECK(dhandy::get<2>(pack) == 0b1U);
|
||||||
|
|
||||||
|
dhandy::set<0>(pack, 2);
|
||||||
|
CHECK(dhandy::get<0>(pack) == 2);
|
||||||
|
dhandy::set<1>(pack, 50);
|
||||||
|
CHECK(dhandy::get<1>(pack) == 50);
|
||||||
|
CHECK(dhandy::get<2>(pack) == 0b1U);
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue