From 152f9ef5feff7ea41c647e004f6e98082abf4b1c Mon Sep 17 00:00:00 2001 From: Stanislaw Halik Date: Mon, 15 Jan 2024 07:24:08 +0100 Subject: w --- serialize/packbits-read.hpp | 42 ++++++++++++++++++++++++++++++++++-------- serialize/packbits-write.hpp | 34 +++++++++++++++++----------------- serialize/packbits.cpp | 2 +- serialize/packbits.hpp | 39 --------------------------------------- serialize/world-writer.cpp | 4 ++-- 5 files changed, 54 insertions(+), 67 deletions(-) delete mode 100644 serialize/packbits.hpp diff --git a/serialize/packbits-read.hpp b/serialize/packbits-read.hpp index 362ec604..67b2efcb 100644 --- a/serialize/packbits-read.hpp +++ b/serialize/packbits-read.hpp @@ -1,11 +1,37 @@ #pragma once -#include "packbits.hpp" #include #include #include +#include #include "compat/assert.hpp" -namespace floormat::detail_Pack { +namespace floormat::detail_Pack_input { + +template +struct input_bits final +{ + static_assert(std::is_fundamental_v); + static_assert(N > 0); + static_assert(N < sizeof(T)*8); + + using type = T; +}; + +template +struct make_tuple_type_ +{ + template using index_to_type = T; + template struct aux; + template struct aux> + { + static_assert(sizeof...(Is) > 0); + using type = std::tuple...>; + }; + using Seq = typename aux>::type; +}; +template using make_tuple_type = typename make_tuple_type_::Seq; + +template struct empty_pack_tuple {}; template struct input @@ -57,16 +83,16 @@ struct input template struct next { - static_assert(N == 0, "reading past the end"); - static_assert(N != 0, "reading past the end"); + static_assert(N == (size_t)-1, "reading past the end"); + static_assert(N != (size_t)-1); }; }; template -constexpr void read(Place& p, input st, std::index_sequence, empty_pack_tuple, Sizes...>) +constexpr void read(Place& p, input st, std::index_sequence, empty_pack_tuple, Sizes...>) { static_assert(sizeof...(Is) == sizeof...(Sizes)); - static_assert(Size <= Left, "too many bits requested"); + static_assert(Size <= Left, "data type too small"); static_assert(I < std::tuple_size_v, "too few tuple members"); using S = input; using next_type = typename S::template next; @@ -85,9 +111,9 @@ template, std::index_sequence, empty_pack_tuple) = delete; -template using make_pack = empty_pack_tuple...>; +template using make_pack = empty_pack_tuple...>; -} // namespace floormat::detail_Pack +} // namespace floormat::detail_Pack_input namespace floormat::pack { diff --git a/serialize/packbits-write.hpp b/serialize/packbits-write.hpp index 7b69069a..34fc140f 100644 --- a/serialize/packbits-write.hpp +++ b/serialize/packbits-write.hpp @@ -1,28 +1,28 @@ #pragma once -#include "packbits.hpp" +#include -namespace floormat::detail_Pack { +namespace floormat::detail_Pack_output { -template +template struct output { - static_assert(CAPACITY <= sizeof(T)*8); - static constexpr size_t Capacity = CAPACITY; - T value; + T value = 0; + uint8_t capacity = sizeof(T)*8; - template - constexpr void set(T x) const + constexpr inline output next(T x, uint8_t bits) const { - static_assert(N > 0); - static_assert(N <= sizeof(T)*8); - static_assert(N <= Capacity); - if constexpr(CAPACITY < sizeof(T)*8) - value <<= CAPACITY; - T x_ = T(x & (1 << N)-1); + fm_assert(bits > 0 && bits <= capacity); + auto val = value; + val <<= bits; + T x_ = T(x & (T{1} << bits)- T{1}); fm_assert(x_ == x); - value |= x; + val |= x_; + return { val | x_, capacity - bits }; } - template using next = output; }; -} // namespace floormat::detail_Pack + + + + +} // namespace floormat::detail_Pack_output diff --git a/serialize/packbits.cpp b/serialize/packbits.cpp index 1a7f5de8..e9ce54bc 100644 --- a/serialize/packbits.cpp +++ b/serialize/packbits.cpp @@ -4,7 +4,7 @@ namespace floormat { -using namespace floormat::detail_Pack; +using namespace floormat::detail_Pack_input; namespace { diff --git a/serialize/packbits.hpp b/serialize/packbits.hpp deleted file mode 100644 index 7d612942..00000000 --- a/serialize/packbits.hpp +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -#include -#include - -namespace floormat::detail_Pack { - -template -struct bits final -{ - static_assert(std::is_fundamental_v); - static_assert(N > 0); - static_assert(N < sizeof(T)*8); - - using type = T; -}; - -template -struct make_tuple_type_ -{ - template using index_to_type = T; - template struct aux; - template struct aux> - { - static_assert(sizeof...(Is) > 0); - using type = std::tuple...>; - }; - using Seq = typename aux>::type; -}; -template using make_tuple_type = typename make_tuple_type_::Seq; - -template struct empty_pack_tuple {}; - -} // namespace floormat::detail_Pack - -namespace floormat::pack { - - - -} // namespace floormat::pack diff --git a/serialize/world-writer.cpp b/serialize/world-writer.cpp index 2e1c7daa..fd533cee 100644 --- a/serialize/world-writer.cpp +++ b/serialize/world-writer.cpp @@ -417,8 +417,8 @@ void writer_state::serialize_scenery(const chunk& c, writer_t& s) fm_assert(L.falloff < light_falloff_COUNT); uint8_t flags = 0; flags |= (uint8_t)exact; // 1 bit - flags |= ((uint8_t)L.r & lowbits) << 1; // 3 bits - flags |= ((uint8_t)L.falloff & lowbits) << 4; // 2 bits + flags |= ((uint8_t)L.r & lowbits) << 1; // 3 input_bits + flags |= ((uint8_t)L.falloff & lowbits) << 4; // 2 input_bits flags |= (uint8_t)!!L.enabled << 7; // 1 bit s << flags; } -- cgit v1.2.3