From d2b1cd8033c7fa9472e2f9f7264d1d3b9f73169b Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Fri, 3 May 2024 19:05:23 +0200 Subject: serialize: remove packbits code It's actually not very useful. --- serialize/packbits-impl.cpp | 16 ------ serialize/packbits-impl.hpp | 13 ----- serialize/packbits-read.cpp | 127 ----------------------------------------- serialize/packbits-read.hpp | 132 ------------------------------------------- serialize/packbits-write.cpp | 53 ----------------- serialize/packbits-write.hpp | 90 ----------------------------- 6 files changed, 431 deletions(-) delete mode 100644 serialize/packbits-impl.cpp delete mode 100644 serialize/packbits-impl.hpp delete mode 100644 serialize/packbits-read.cpp delete mode 100644 serialize/packbits-read.hpp delete mode 100644 serialize/packbits-write.cpp delete mode 100644 serialize/packbits-write.hpp diff --git a/serialize/packbits-impl.cpp b/serialize/packbits-impl.cpp deleted file mode 100644 index b03b31b6..00000000 --- a/serialize/packbits-impl.cpp +++ /dev/null @@ -1,16 +0,0 @@ -#include "packbits-impl.hpp" -#include "compat/exception.hpp" - -namespace floormat::Pack_impl { - -void throw_on_read_nonzero() noexcept(false) -{ - throw std::runtime_error{"extra bits in pack_read()"}; -} - -void throw_on_write_input_bit_overflow() noexcept(false) -{ - throw std::runtime_error{"extra bits in pack_write()"}; -} - -} // namespace floormat::Pack_impl diff --git a/serialize/packbits-impl.hpp b/serialize/packbits-impl.hpp deleted file mode 100644 index 3e57ea75..00000000 --- a/serialize/packbits-impl.hpp +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -namespace floormat::Pack_impl { - -[[noreturn]] void throw_on_read_nonzero() noexcept(false); -[[noreturn]] void throw_on_write_input_bit_overflow() noexcept(false); - -template struct expand_sum; -template struct expand_sum { static constexpr size_t value = N + expand_sum::value; }; -template<> struct expand_sum<> { static constexpr size_t value = 0; }; -template constexpr inline size_t sum = expand_sum::value; - -} // namespace floormat::Pack_impl diff --git a/serialize/packbits-read.cpp b/serialize/packbits-read.cpp deleted file mode 100644 index d8b9451c..00000000 --- a/serialize/packbits-read.cpp +++ /dev/null @@ -1,127 +0,0 @@ -#include "packbits-read.hpp" -#include "compat/assert.hpp" -#include "compat/exception.hpp" - -namespace floormat { - -using namespace floormat::Pack_impl; - -namespace { - -static_assert(sum<1, 2, 3, 4, 5> == 6*(6-1)/2); -static_assert(sum<5, 10, 15> == 30); - -using u32 = uint32_t; -using u16 = uint16_t; -using u8 = uint8_t; - -template constexpr inline T lowbits = N == sizeof(T)*8 ? (T)-1 : (T{1} << N)-T{1}; - -static_assert(!input{65535}.check_zero()); -static_assert(input{65535}.advance<16>() == 0); - -static_assert(input::next<16>{ input{65535}.advance<16>() }.check_zero()); -static_assert(input::next<16>{}.Left == 14); - -constexpr bool test1() -{ - constexpr size_t bits[] = { 5, 2, 1 }; - constexpr size_t vals[] = { 8, 3, 1, 0 }; - - constexpr auto S0 = input{0b10111011}; - constexpr auto S1 = input{0b00000101}; - constexpr auto S2 = input{0b00000001}; - constexpr auto S3 = input{0b00000000}; - - using P0 = std::decay_t; - using P1 = P0::next; - using P2 = P1::next; - using P3 = P2::next; - - static_assert(std::is_same_v>); - static_assert(std::is_same_v>); - static_assert(std::is_same_v>); - static_assert(std::is_same_v>); - - static_assert(S0.advance< 0>() == S0.value); - static_assert(S0.advance() == S1.value); - static_assert(S1.advance() == S2.value); - static_assert(S2.advance() == S3.value); - - static_assert(S0.get() == (S0.value & (1<() == (S1.value & (1<() == (S2.value & (1<{0b1110100110001011}; - constexpr auto S1 = input{0b1110}; - constexpr auto S2 = input{0b1}; - - static_assert(S0.get<12>() == 0b100110001011); - static_assert(S0.advance<12>() == S1.value); - - static_assert(S1.get<3>() == 0b110); - static_assert(S1.advance<3>() == S2.value); - - static_assert(S2.get<1>() == 0b1); - static_assert(S2.advance<1>() == 0); - - return true; -} -static_assert(test3()); - -template using f32 = input_field; -template using f8 = input_field; - -constexpr bool test4() -{ - - { - static_assert(lowbits == 0x1ffffU); - //auto tuple = std::tuple&, f32<14>&, f32<1>&>{a, b, c}; - f32<17> a; f32<14> b; f32< 1> c; - auto tuple = std::tie(a, b, c); - read_(tuple, input{(uint32_t)-1}, std::make_index_sequence<3>{}); - fm_assert(a == lowbits); - fm_assert(b == lowbits); - fm_assert(c & 1); - } - { - f8<1> a; - f8<3> b; - f8<2> c; - read_(std::tie(a, b, c), input{0b101011}, std::make_index_sequence<3>{}); - fm_assert(a == 0b1); - fm_assert(b == 0b101); - fm_assert(c == 0b10); - } - { - read_(std::tuple<>{}, input{0}, std::index_sequence<>{}); - //f32<2> a, b, c; - //read_(std::tuple<>{}, input{1}, std::index_sequence<>{}); - //read_(std::tie(a, b, c), input{0b11111}, std::make_index_sequence<3>{}); - //(void)input{}; - //read_(std::tie(a), input{3}, std::index_sequence<0>{}); fm_assert(a == 3); - //f8<1> d; read_(std::tie(d), input{1}, std::index_sequence<>{}); - } - { - f8<1> a; f8<3> b; f8<2> c; - pack_read(std::tie(a, b, c), uint8_t{0b101011}); - fm_assert(a == 0b1); - fm_assert(b == 0b101); - fm_assert(c == 0b10); - } - - return true; -} -static_assert(test4()); - -} // namespace - -} // namespace floormat diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp deleted file mode 100644 index 4ad3aa74..00000000 --- a/serialize/packbits-read.hpp +++ /dev/null @@ -1,132 +0,0 @@ -#pragma once -#include "packbits-impl.hpp" -#include "compat/assert.hpp" -#include -#include -#include - -namespace floormat::Pack_impl { - -template -struct input_field final -{ - static_assert(LENGTH > 0); - static_assert(LENGTH <= sizeof(T)*8); - static constexpr size_t Length = LENGTH; - T value; - constexpr T operator*() const { return value; } - constexpr operator T() const { return value; } -}; - -template -struct input -{ - static_assert(LEFT > 0); - static_assert(LEFT <= sizeof(T)*8); - static constexpr size_t Left = LEFT; - - T value; - - template - struct next_ - { - static_assert(N <= LEFT); - using type = input; - }; - template using next = typename next_::type; - - template - constexpr CORRADE_ALWAYS_INLINE T get() const - { - static_assert(N > 0); - static_assert(N <= sizeof(T)*8); - static_assert(N <= LEFT); - return T(value & (T{1} << N) - T{1}); - } - - template - constexpr T advance() const - { - static_assert(N <= sizeof(T)*8); - static_assert(N <= LEFT); - return T(value >> N); - } - - constexpr bool operator==(const input&) const noexcept = default; - [[nodiscard]] constexpr inline bool check_zero() const { return value == T{0}; } -}; - -template -struct input -{ - static constexpr size_t Left = 0; - using Type = T; - T value; - - template [[maybe_unused]] constexpr T get() const = delete; - template [[maybe_unused]] constexpr T advance() const = delete; - constexpr bool operator==(const input&) const noexcept = default; - [[nodiscard]] constexpr inline bool check_zero() const { return true; } - - template struct next - { - static_assert(!N, "reading past the end"); - static_assert( N, "reading past the end"); - }; -}; - -template struct is_input_field : std::bool_constant {}; -template struct is_input_field> : std::bool_constant { static_assert(N > 0); }; - -template -requires requires (Field& x) -{ - { size_t{Field::Length} > 0 }; - sizeof(std::decay_t); - requires std::unsigned_integral>; -} -struct is_input_field : std::bool_constant {}; - -template -constexpr CORRADE_ALWAYS_INLINE void read_(Tuple&& tuple, input st, std::index_sequence) -{ - using U = std::decay_t; - static_assert(Left <= sizeof(T)*8, "bits to read count too large"); - static_assert(Left > 0, "too many bits to write"); - static_assert(std::tuple_size_v >= sizeof...(Is)+1, "index count larger than tuple element count"); - static_assert(I < std::tuple_size_v, "too few tuple elements"); - using Field = std::decay_t>; - static_assert(is_input_field{}, "tuple element must be input_field"); - constexpr size_t Size = Field::Length; - static_assert(Size <= Left, "data type too small"); - using next_type = typename input::template next; - std::get(tuple).value = st.template get(); - T next_value = st.template advance(); - read_(floormat::forward(tuple), next_type{ next_value }, std::index_sequence{}); -} - -template -constexpr CORRADE_ALWAYS_INLINE void read_(Tuple&&, input st, std::index_sequence<>) -{ - if (!st.check_zero()) [[unlikely]] - throw_on_read_nonzero(); -} - -} // namespace floormat::Pack_impl - -namespace floormat { - -template constexpr void pack_read(Tuple&& tuple, T value) -requires requires (const Tuple& tuple) { - std::tuple_size_v > 0uz; - Pack_impl::is_input_field(tuple))>>::value; -} -{ - constexpr size_t nbits = sizeof(T)*8, - tuple_size = std::tuple_size_v>; - Pack_impl::read_(floormat::forward(tuple), - Pack_impl::input{value}, - std::make_index_sequence{}); -} - -} // namespace floormat diff --git a/serialize/packbits-write.cpp b/serialize/packbits-write.cpp deleted file mode 100644 index 7dab800a..00000000 --- a/serialize/packbits-write.cpp +++ /dev/null @@ -1,53 +0,0 @@ -#include "packbits-write.hpp" - -namespace floormat { - -namespace { - -using namespace floormat::Pack_impl; - -using u32 = uint32_t; -using u16 = uint16_t; -using u8 = uint8_t; -template using f32 = output_field; -template using f16 = output_field; -template using f8 = output_field; - -static_assert(write_(std::tuple>{4242}, output{0}, std::index_sequence<0>{}) == 4242u); - -static_assert(write_( - std::tuple{f8<3>{7}, f8<2>{3}, f8<1>{1}}, - output{0}, - std::make_index_sequence<3>{} -) == (1 << 6) - 1); - -static_assert(write_( - std::tuple{f32<2>{0b10}, f32<3>{0b011}, f32<3>{0b001}}, - output{0}, - make_reverse_index_sequence<3>{}) == 0b000101110); - -static_assert(pack_write(std::tuple{f32<2>{0b10}, f32<3>{0b011}, f32<3>{0b01}}) == 0b00101110); -//static_assert(pack_write(std::tuple{f32<2>{0b10}, f32<3>{0b1011}, f32<3>{0b001}}) == 0b000101110); -static_assert(pack_write(std::tuple{f8<2>{0b10}, f8<3>{0b011}, f8<3>{0b01}}) == 0b00101110); -//static_assert(pack_write(std::tuple{f8<2>{0b10}, f8<3>{0b011}, f8<4>{0b01}}) == 0b00101110); -//static_assert(pack_write(std::tuple{}) == 0); -static_assert(pack_write(std::tuple{f8<1>{0b1}, f8<3>{0b101}, f8<2>{0b10}}) == 0b101011); - -#if 0 // check disassembly -u32 foo1(u32 a, u32 b, u32 c); -u32 foo1(u32 a, u32 b, u32 c) -{ - return pack_write(std::tuple{f32<2>{a}, f32<3>{b}, f32<3>{c}}); -} -#endif - -} // namespace - -#if 0 -[[maybe_unused]] uint8_t test1(uint8_t a, uint8_t b, uint8_t c) -{ - return pack_write(std::tuple{f8<2>{a}, f8<3>{b}, f8<3>{c}}); -} -#endif - -} // namespace floormat diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp deleted file mode 100644 index 4689419a..00000000 --- a/serialize/packbits-write.hpp +++ /dev/null @@ -1,90 +0,0 @@ -#pragma once -#include "packbits-impl.hpp" -#include "compat/reverse-index-sequence.hpp" -#include -#include -#include -#include - -namespace floormat::Pack_impl { - -template -struct output -{ - static_assert(LEFT >= 0); - static_assert(LEFT <= CAPACITY); - static_assert(CAPACITY <= sizeof(T)*8); - static constexpr size_t Capacity = CAPACITY, Left = LEFT; - T value{0}; -}; - -template -struct output_field -{ - static_assert(LENGTH > 0); - static_assert(LENGTH <= sizeof(T)*8); - static constexpr size_t Length = LENGTH; - T value; -}; - -template struct is_output_field : std::bool_constant {}; -template struct is_output_field> : std::bool_constant { static_assert(N > 0); }; - -template -requires requires (const Field& x) { - { size_t{Field::Length} > 0 }; - sizeof(std::decay_t); - requires std::unsigned_integral>; -} -struct is_output_field : std::bool_constant {}; - -template -constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple& tuple, output st, std::index_sequence) -{ - static_assert(Capacity > 0); - static_assert(Capacity <= sizeof(T)*8); - static_assert(Left > 0, "too many bits to write"); - static_assert(Left <= Capacity, "too many bits to write"); - static_assert(I < std::tuple_size_v, "too few tuple elements"); - static_assert(is_output_field>>{}, - "tuple element must be output_field"); - constexpr size_t N = std::tuple_element_t::Length; - static_assert(N <= Left, "too many bits to write"); - - T x = std::get(tuple).value; - - if (!((size_t)std::bit_width(x) <= N)) [[unlikely]] - throw_on_write_input_bit_overflow(); - T value = T(T(st.value << N) | x); - return write_(tuple, output{value}, std::index_sequence{}); -} - -template -constexpr CORRADE_ALWAYS_INLINE T write_(const Tuple&, output st, std::index_sequence<>) -{ - return st.value; -} - -} // namespace floormat::Pack_impl - -namespace floormat { - -template -requires requires (const Tuple& tuple) { - std::tuple_size_v > 0uz; - Pack_impl::is_output_field(tuple))>>::value; -} -[[nodiscard]] constexpr auto pack_write(const Tuple& tuple) -{ - using Field = std::decay_t>; - static_assert(Pack_impl::is_output_field{}); - using T = std::decay_t().value)>; - constexpr size_t nbits = sizeof(T)*8, tuple_size = std::tuple_size_v; - return Pack_impl::write_(tuple, - Pack_impl::output{T{0}}, - make_reverse_index_sequence{}); -} - -constexpr uint8_t pack_write(const std::tuple<>&) = delete; - -} // namespace floormat -- cgit v1.2.3